blob: d316c07e303b090e99e5849666994c64c99dd936 (
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
|
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.137.1"
/**
* Set initial configuration
*/
override fun onCreate(savedInstanceState: Bundle?) {
matchDressCode()
super.onCreate(savedInstanceState)
dressCodeName = getThemeName(this)
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
*/
@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 { // 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>()
}
}
}
|