diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ff933a28d6473dd6f02ffde9e552e4cdf217143c..52c12d200033c12512e1a762fb98a6cedc9d1236 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,9 @@ <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" + tools:ignore="ProtectedPermissions" /> + <application android:allowBackup="true" diff --git a/app/src/main/java/itb/bos/bondoman/services/JWTService.kt b/app/src/main/java/itb/bos/bondoman/services/JWTService.kt index 922646dc6e3f5770b5a66123033a25587faac279..e0fe824c630d2223b5d51c2b9af837621f0d4573 100644 --- a/app/src/main/java/itb/bos/bondoman/services/JWTService.kt +++ b/app/src/main/java/itb/bos/bondoman/services/JWTService.kt @@ -30,7 +30,7 @@ class JWTService : Service() { override fun onDestroy() { isServiceRunning = false - unregisterReceiver(broadcastReceiver) +// unregisterReceiver(broadcastReceiver) super.onDestroy() } @@ -43,13 +43,16 @@ class JWTService : Service() { while (isServiceRunning) { // Perform token expiration check checkTokenExpiry() - delay(TimeUnit.MINUTES.toMillis(5)) + // delay(TimeUnit.MINUTES.toMillis(5)) + delay(TimeUnit.SECONDS.toMillis(10)) } } } private suspend fun checkTokenExpiry() { try { + Log.d("TokenExpirationCheck", "Checking token expiry...") + val token = tokenHelper.getToken() if (token != null) { // Call the checkExpireToken function with the token @@ -62,6 +65,7 @@ class JWTService : Service() { val currentTime = System.currentTimeMillis() / 1000 // Convert to seconds if (currentTime > expireTime) { // Token has expired, send broadcast to notify the application to logout + Log.d("TokenExpirationCheck", "Token has expired, sending broadcast...") val intent = Intent(ACTION_TOKEN_EXPIRED) sendBroadcast(intent) } else { @@ -76,6 +80,7 @@ class JWTService : Service() { } } + companion object { const val ACTION_TOKEN_EXPIRED = "itb.bos.bondoman.ACTION_TOKEN_EXPIRED" } diff --git a/app/src/main/java/itb/bos/bondoman/viewModel/AuthViewModel.kt b/app/src/main/java/itb/bos/bondoman/viewModel/AuthViewModel.kt index 6f8a7d88e4510a05ed626ea91b9f3fd86f3629af..9bcd017812e149716426670ab3af1c22163a4f02 100644 --- a/app/src/main/java/itb/bos/bondoman/viewModel/AuthViewModel.kt +++ b/app/src/main/java/itb/bos/bondoman/viewModel/AuthViewModel.kt @@ -12,6 +12,7 @@ import itb.bos.bondoman.LoginActivity import itb.bos.bondoman.LogoutActivity import itb.bos.bondoman.helper.TokenHelper import itb.bos.bondoman.helper.performLogin +import itb.bos.bondoman.services.JWTService import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -44,8 +45,12 @@ class AuthViewModel : ViewModel() { CoroutineScope(Dispatchers.Main).launch { try { val token = performLogin(email, password) + // Store the token and start jwt service storeToken(token) - checkToken() + // checkToken() + startJWTService() + + // change to logout activity -> temporary val intent = Intent(context, LogoutActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK context.startActivity(intent) @@ -61,12 +66,28 @@ class AuthViewModel : ViewModel() { } fun logout() { + // Stop jwt service and remove token + stopJWTService() removeToken() + + // Change to login activity val intent = Intent(context, LoginActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK context.startActivity(intent) } + private fun stopJWTService() { + Log.d("JWTStop", "JWT service has stopped") + val intent = Intent(context, JWTService::class.java) + context.stopService(intent) + } + + private fun startJWTService() { + Log.d("JWTStart", "JWT service has started") + val intent = Intent(context, JWTService::class.java) + context.startService(intent) + } + private fun handleLoginFailure(e: Exception) { val errorMessage = e.message ?: "Unknown error occurred" Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()