95 lines
2.7 KiB
Kotlin
95 lines
2.7 KiB
Kotlin
|
package org.libre.agosto.p2play
|
||
|
|
||
|
import android.content.Intent
|
||
|
import android.content.SharedPreferences
|
||
|
import android.os.AsyncTask
|
||
|
import android.support.v7.app.AppCompatActivity
|
||
|
import android.os.Bundle
|
||
|
import android.os.Handler
|
||
|
import android.os.Looper
|
||
|
import android.preference.PreferenceManager
|
||
|
import org.libre.agosto.p2play.ajax.Auth
|
||
|
|
||
|
class SplashActivity : AppCompatActivity() {
|
||
|
lateinit var settings: SharedPreferences
|
||
|
lateinit var editor: SharedPreferences.Editor
|
||
|
val client: Auth = Auth()
|
||
|
val _db = Database(this)
|
||
|
|
||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
super.onCreate(savedInstanceState)
|
||
|
setContentView(R.layout.activity_splash)
|
||
|
|
||
|
settings = PreferenceManager.getDefaultSharedPreferences(this)
|
||
|
|
||
|
editor = settings.edit()
|
||
|
ManagerSingleton.context = this
|
||
|
|
||
|
val host = settings.getString("hostP2play","")
|
||
|
val lastHost = settings.getString("last_host","")
|
||
|
if(host!=""){
|
||
|
if(lastHost!=host){
|
||
|
_db.logout()
|
||
|
Handler().postDelayed({
|
||
|
startHostActivity()
|
||
|
}, 2000)
|
||
|
}else{
|
||
|
ManagerSingleton.url=host
|
||
|
checkUser()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Handler().postDelayed({
|
||
|
startHostActivity()
|
||
|
}, 2000)
|
||
|
}
|
||
|
|
||
|
private fun checkUser(){
|
||
|
val token = _db.getToken()
|
||
|
val user = _db.getUser()
|
||
|
AsyncTask.execute {
|
||
|
if (Looper.myLooper() == null)
|
||
|
Looper.prepare()
|
||
|
|
||
|
|
||
|
if (token.status == 1 && user.status == 1) {
|
||
|
val client_id = settings.getString("client_id", "")
|
||
|
val client_secret = settings.getString("client_secret", "")
|
||
|
|
||
|
val newToken = client.refreshToken(token, client_id, client_secret)
|
||
|
|
||
|
when (token.status.toString()) {
|
||
|
"1" -> {
|
||
|
_db.newToken(newToken)
|
||
|
ManagerSingleton.token = newToken
|
||
|
ManagerSingleton.user = user
|
||
|
}
|
||
|
else -> _db.logout()
|
||
|
}
|
||
|
} else {
|
||
|
_db.logout()
|
||
|
}
|
||
|
|
||
|
Handler().postDelayed({
|
||
|
startApp()
|
||
|
}, 2000)
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun startApp() {
|
||
|
runOnUiThread {
|
||
|
val intent = Intent(ManagerSingleton.context, MainActivity::class.java)
|
||
|
startActivity(intent)
|
||
|
this.finish()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun startHostActivity() {
|
||
|
runOnUiThread {
|
||
|
val intent = Intent(ManagerSingleton.context, HostActivity::class.java)
|
||
|
startActivity(intent)
|
||
|
this.finish()
|
||
|
}
|
||
|
}
|
||
|
}
|