blob: 168d07243d01e30fa5a8c4f1e1bbd0d4072c1ff0 (
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
|
package me.texx.Texx
import android.os.Bundle
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.ThemeUtil.getThemeName
import org.jetbrains.anko.*
import java.io.IOException
/**
* 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.0.102"
override fun onCreate(savedInstanceState: Bundle?) {
matchDressCode()
super.onCreate(savedInstanceState)
dressCodeName = getThemeName(this)
FuelManager.instance.basePath = "http://$serverAddress"
alert("Logging you in.", "Loading...") {
isCancelable = false
}.show()
verifyLogin()
}
@Throws(InterruptedException::class, IOException::class)
fun isConnected(): Boolean {
val command = "ping -c 1 google.com"
return Runtime.getRuntime().exec(command).waitFor() == 0
}
private fun verifyLogin() {
val accessToken: String? = SecureStorage(this@RoutingActivity).get("access_token")
// 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
.header("Authorization" to "Bearer $accessToken")
.responseJson { _, response, result ->
val (_, serverError) = result
when {
response.httpStatusCode == 200 -> startActivity<MainActivity>()
response.httpStatusCode == 401 -> startActivity<LoginActivity>()
!isConnected() -> startActivity<MainActivity>("notConnected" to true)
serverError != null -> startActivity(intentFor<MainActivity>("serverDown" to true).newTask().clearTask().noAnimation().excludeFromRecents())
else -> startActivity<LoginActivity>()
}
}
} else {
startActivity<LoginActivity>()
}
}
}
|