p2play-app-android/app/src/main/java/org/libre/agosto/p2play/SplashActivity.kt

102 lines
3.0 KiB
Kotlin
Raw Normal View History

2019-01-28 21:54:08 +01:00
package org.libre.agosto.p2play
import android.content.Intent
import android.content.SharedPreferences
import android.os.AsyncTask
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
2024-04-06 22:38:04 +02:00
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
2019-01-28 21:54:08 +01:00
import org.libre.agosto.p2play.ajax.Auth
import java.lang.Exception
2019-01-28 21:54:08 +01:00
class SplashActivity : AppCompatActivity() {
lateinit var settings: SharedPreferences
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)
2024-04-01 00:59:57 +02:00
ManagerSingleton.settings = settings
ManagerSingleton.db = _db
2019-01-28 21:54:08 +01:00
2024-04-01 00:59:57 +02:00
ManagerSingleton.reloadSettings()
2019-01-28 21:54:08 +01:00
2024-04-06 22:38:04 +02:00
val host = settings.getString("hostP2play", "")
val lastHost = settings.getString("last_host", "")
if (host != "") {
if (lastHost != host) {
2019-01-28 21:54:08 +01:00
Handler().postDelayed({
startHostActivity()
}, 2000)
2024-04-06 22:38:04 +02:00
} else {
ManagerSingleton.url = host
2019-01-28 21:54:08 +01:00
checkUser()
}
2024-04-01 00:59:57 +02:00
} else {
Handler().postDelayed({
startHostActivity()
}, 2000)
}
2019-01-28 21:54:08 +01:00
}
2024-04-06 22:38:04 +02:00
private fun checkUser() {
2024-04-01 00:59:57 +02:00
Log.d("was", "Checked")
try {
val token = _db.getToken()
val user = _db.getUser()
AsyncTask.execute {
2024-04-06 22:38:04 +02:00
if (Looper.myLooper() == null) {
Looper.prepare()
2024-04-06 22:38:04 +02:00
}
if (token.status == 1 && user.status == 1) {
2024-04-01 00:59:57 +02:00
val clientId = settings.getString("client_id", "")!!
val clientSecret = settings.getString("client_secret", "")!!
2024-04-01 00:59:57 +02:00
val newToken = client.refreshToken(token, clientId, clientSecret)
when (token.status.toString()) {
"1" -> {
_db.newToken(newToken)
ManagerSingleton.token = newToken
ManagerSingleton.user = user
}
2024-04-01 00:59:57 +02:00
else -> ManagerSingleton.logout()
}
} else {
2024-04-01 00:59:57 +02:00
ManagerSingleton.logout()
}
2019-01-28 21:54:08 +01:00
startApp()
2019-01-28 21:54:08 +01:00
}
2024-04-06 22:38:04 +02:00
} catch (err: Exception) {
err.printStackTrace()
2019-01-28 21:54:08 +01:00
Handler().postDelayed({
startApp()
}, 2000)
}
}
private fun startApp() {
runOnUiThread {
2019-02-10 18:41:56 +01:00
val intent = Intent(this, MainActivity::class.java)
2019-01-28 21:54:08 +01:00
startActivity(intent)
this.finish()
}
}
private fun startHostActivity() {
runOnUiThread {
2019-02-10 18:41:56 +01:00
val intent = Intent(this, HostActivity::class.java)
2019-01-28 21:54:08 +01:00
startActivity(intent)
this.finish()
}
}
2024-04-06 22:38:04 +02:00
}