blob: 4e11c60bf450528816a0ceaae67012e6e0869830 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package me.texx.Texx
import android.os.Bundle
import android.os.StrictMode
import android.support.v7.app.AppCompatActivity
import com.github.kittinunf.fuel.android.extension.responseJson
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.httpGet
import com.madapps.prefrences.EasyPrefrences
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import me.texx.Texx.Util.SecureStorage
import me.texx.Texx.util.ThemeUtil.getThemeName
import org.jetbrains.anko.*
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
/**
* Activity which will be run before any other to verify user and choose which activity
* should be started next
*/
class RoutingActivity : AppCompatActivity() {
private val serverAddress = "192.168.137.1"
/**
* Set initial configuration
*/
override fun onCreate(savedInstanceState: Bundle?) {
matchDressCode()
super.onCreate(savedInstanceState)
dressCodeName = getThemeName(this)
// Fuel configuration
val accessToken: String? = SecureStorage(this@RoutingActivity).get("access_token")
if (accessToken != null)
FuelManager.instance.baseHeaders = mapOf("Authorization" to "Bearer $accessToken")
FuelManager.instance.basePath = "http://$serverAddress"
if (EasyPrefrences(this@RoutingActivity).getBoolean("previously_started")) {
alert("Logging you in.", "Loading...") {
isCancelable = false
}.show()
verifyLogin()
} else {
EasyPrefrences(this@RoutingActivity).putBoolean("previously_started", true)
startActivity<IntroActivity>()
}
}
/**
* Checks if client is connected to the internet by pinging google
*/
private fun isConnected(): Boolean {
return try {
val policy = StrictMode.ThreadPolicy.Builder()
.permitAll().build()
StrictMode.setThreadPolicy(policy)
val timeoutMs = 1500
val socket = Socket()
val socketAddress = InetSocketAddress("8.8.8.8", 53)
socket.connect(socketAddress, timeoutMs)
socket.close()
true
} catch (e: IOException) {
false
}
}
private fun verifyLogin() {
val accessToken: String? = SecureStorage(this@RoutingActivity).get("access_token") // because it may be the first start
// synced function of fuel doesn't work here (#331) -> ugly workaround
if (accessToken == null) {
val userID = EasyPrefrences(this@RoutingActivity).getString("user_id")
"/users/$userID".httpGet() // verify by making request to user api // TODO: More secure way of verifying
.responseJson { _, response, result ->
val (_, serverError) = result
when { // TODO: Cleaner task solution
response.httpStatusCode == 200 -> startActivity(intentFor<MainActivity>().newTask().clearTask().noAnimation().excludeFromRecents())
response.httpStatusCode == 401 -> startActivity(intentFor<LoginActivity>().newTask().clearTask().noAnimation().excludeFromRecents())
!isConnected() -> startActivity(intentFor<MainActivity>("notConnected" to true).newTask().clearTask().noAnimation().excludeFromRecents())
serverError != null -> startActivity(intentFor<MainActivity>("serverDown" to true).newTask().clearTask().noAnimation().excludeFromRecents())
else -> startActivity(intentFor<LoginActivity>().newTask().clearTask().noAnimation().excludeFromRecents())
}
}
} else {
startActivity<LoginActivity>()
}
}
}
|