From 8b60ba93a731072a4b0d83b1af488c49aa08ded6 Mon Sep 17 00:00:00 2001 From: zaydanA <13521104@std.stei.itb.ac.id> Date: Fri, 5 Apr 2024 10:45:39 +0700 Subject: [PATCH] add service to manifest --- app/src/main/AndroidManifest.xml | 6 +----- .../java/com/example/pbd_jwr/MainActivity.kt | 10 ++++----- .../backgroundService/JWTValidationService.kt | 21 ++++++++++--------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2563d89..22bbc61 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -56,10 +56,6 @@ > </activity> - <activity android:name=".ScanActivity" - android:exported="false" - > - </activity> <meta-data android:name="preloaded_fonts" @@ -75,6 +71,6 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> - + <service android:name=".backgroundService.JWTValidationService"/> </application> </manifest> \ No newline at end of file diff --git a/app/src/main/java/com/example/pbd_jwr/MainActivity.kt b/app/src/main/java/com/example/pbd_jwr/MainActivity.kt index 0cad702..d0ad4d3 100644 --- a/app/src/main/java/com/example/pbd_jwr/MainActivity.kt +++ b/app/src/main/java/com/example/pbd_jwr/MainActivity.kt @@ -44,9 +44,6 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val serviceIntent = Intent(this, JWTValidationService::class.java) - startService(serviceIntent) - sharedPreferences = EncryptedSharedPref.create(applicationContext,"login") sharedPreferencesEditor = sharedPreferences.edit() binding = ActivityMainBinding.inflate(layoutInflater) @@ -128,6 +125,8 @@ class MainActivity : AppCompatActivity() { connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager networkCallback = NetworkCallbackImplementation(this) registerNetworkCallback() + val serviceIntent = Intent(this, JWTValidationService::class.java) + startService(serviceIntent) } private fun isLocationPermissionGranted(): Boolean { return ContextCompat.checkSelfPermission( @@ -180,12 +179,13 @@ class MainActivity : AppCompatActivity() { override fun onPause() { super.onPause() unregisterNetworkCallback() + val serviceIntent = Intent(this, JWTValidationService::class.java) + stopService(serviceIntent) } override fun onDestroy() { super.onDestroy() - val serviceIntent = Intent(this, JWTValidationService::class.java) - stopService(serviceIntent) + } private fun registerNetworkCallback() { diff --git a/app/src/main/java/com/example/pbd_jwr/backgroundService/JWTValidationService.kt b/app/src/main/java/com/example/pbd_jwr/backgroundService/JWTValidationService.kt index d323d3a..6c1fdaa 100644 --- a/app/src/main/java/com/example/pbd_jwr/backgroundService/JWTValidationService.kt +++ b/app/src/main/java/com/example/pbd_jwr/backgroundService/JWTValidationService.kt @@ -28,7 +28,7 @@ import kotlin.math.log class JWTValidationService : Service() { private lateinit var handler: Handler private lateinit var runnable: Runnable - private val INTERVAL: Long = 5 * 60 * 1000 // 5 minutes in milliseconds + private val INTERVAL: Long = 3 * 60 * 1000 // 2 minutes in milliseconds private var client = OkHttpClient() private val validateTokenURL = "https://pbd-backend-2024.vercel.app/api/auth/token" @@ -76,7 +76,7 @@ class JWTValidationService : Service() { override fun onFailure(call: Call, e: IOException) { handler.post{ - Toast.makeText(applicationContext, "Failed to re-login", Toast.LENGTH_SHORT).show() + Toast.makeText(applicationContext, "Connection error", Toast.LENGTH_SHORT).show() } @@ -97,21 +97,23 @@ class JWTValidationService : Service() { val password = sharedPreferences.getString("password","") var isRememberTemp = sharedPreferences.getString("isRemember","") var isRemember = false - if (isRememberTemp == "true"){ - isRemember = true - }else{ - isRemember = false - } + isRemember = isRememberTemp == "true" it.close() if (email != null && password != null) { post(email,password,isRemember) + handler.post{ + Toast.makeText(applicationContext,"Re-login success", Toast.LENGTH_SHORT).show() + } + } else { + handler.post{ + Toast.makeText(applicationContext, "Server Error", Toast.LENGTH_SHORT).show() + } } }else{ handler.post{ Toast.makeText(applicationContext,"Token is not exp", Toast.LENGTH_SHORT).show() } - return } } ?: run { @@ -119,7 +121,7 @@ class JWTValidationService : Service() { }else{ handler.post{ - Toast.makeText(applicationContext,"Failed to check token, server error", Toast.LENGTH_SHORT).show() + Toast.makeText(applicationContext,"Failed to check token, Server error", Toast.LENGTH_SHORT).show() } } @@ -128,7 +130,6 @@ class JWTValidationService : Service() { } }) - } fun isTokenExpired(expirationTime: Long): Boolean { val currentTimeSeconds = Instant.now().epochSecond -- GitLab