diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2563d894d8155def992d4597a7e543de86a5e9b8..22bbc618a84d17e0e52b0e4a2362051654e9107e 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 0cad702ff6caac03d70db790e045d5f344858072..d0ad4d36eb8d764583a7af604ddd4c4a52841d57 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 d323d3a5a9a30cf690f8948e65e0572903437f5d..6c1fdaa6bca4b8920705489d59e8aef933aab64a 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