From 59ac0c59c919c5133e6cd81eb49114bb7164cb0d Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Thu, 25 Jul 2024 23:41:43 +0530 Subject: [PATCH 1/7] Callback method loggedOut added in login listner and its implementations --- .../android/auth/oauth2/OAuth2LoginManager.kt | 2 ++ .../org/radarbase/android/MainActivity.kt | 32 ++++++++++++++++--- .../org/radarbase/android/RadarService.kt | 13 +++++++- .../radarbase/android/auth/AppAuthState.kt | 2 ++ .../org/radarbase/android/auth/AuthService.kt | 15 +++++++++ .../radarbase/android/auth/LoginActivity.kt | 2 ++ .../radarbase/android/auth/LoginListener.kt | 2 ++ .../portal/ManagementPortalLoginManager.kt | 2 +- .../android/source/SourceProviderRegistrar.kt | 2 ++ .../radarbase/android/source/SourceService.kt | 2 ++ .../android/splash/SplashActivity.kt | 2 ++ 11 files changed, 70 insertions(+), 6 deletions(-) diff --git a/plugins/radar-android-login-oauth2/src/main/java/org/radarbase/android/auth/oauth2/OAuth2LoginManager.kt b/plugins/radar-android-login-oauth2/src/main/java/org/radarbase/android/auth/oauth2/OAuth2LoginManager.kt index 9340ae320..d159d5b66 100644 --- a/plugins/radar-android-login-oauth2/src/main/java/org/radarbase/android/auth/oauth2/OAuth2LoginManager.kt +++ b/plugins/radar-android-login-oauth2/src/main/java/org/radarbase/android/auth/oauth2/OAuth2LoginManager.kt @@ -110,6 +110,8 @@ class OAuth2LoginManager( override fun loginFailed(manager: LoginManager?, ex: Exception?) = this.service.loginFailed(this, ex) + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) = Unit + companion object { private const val OAUTH2_SOURCE_TYPE = "org.radarcns.android.auth.oauth2.OAuth2LoginManager" private val OAUTH2_SOURCE_TYPES = listOf(OAUTH2_SOURCE_TYPE) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt index d6920f40e..a5e6225e1 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt @@ -33,15 +33,19 @@ import org.radarbase.android.RadarConfiguration.Companion.UI_REFRESH_RATE_KEY import org.radarbase.android.RadarConfiguration.Companion.USER_ID_KEY import org.radarbase.android.RadarService.Companion.ACTION_CHECK_PERMISSIONS import org.radarbase.android.RadarService.Companion.ACTION_PROVIDERS_UPDATED +import org.radarbase.android.RadarService.Companion.ACTION_STOP_FOREGROUND_SERVICE import org.radarbase.android.RadarService.Companion.EXTRA_PERMISSIONS +import org.radarbase.android.auth.AppAuthState import org.radarbase.android.auth.AuthService -import org.radarbase.android.config.CombinedRadarConfig +import org.radarbase.android.auth.AuthServiceConnection +import org.radarbase.android.auth.LoginListener +import org.radarbase.android.auth.LoginManager import org.radarbase.android.util.* import org.slf4j.LoggerFactory /** Base MainActivity class. It manages the services to collect the data and starts up a view. To * create an application, extend this class and override the abstract methods. */ -abstract class MainActivity : AppCompatActivity() { +abstract class MainActivity : AppCompatActivity(), LoginListener { /** Time between refreshes. */ private var uiRefreshRate: Long = 0 @@ -56,13 +60,14 @@ abstract class MainActivity : AppCompatActivity() { private var configurationBroadcastReceiver: BroadcastRegistration? = null private lateinit var permissionHandler: PermissionHandler - protected lateinit var authConnection: ManagedServiceConnection + protected lateinit var authConnection: AuthServiceConnection protected lateinit var radarConnection: ManagedServiceConnection private lateinit var bluetoothEnforcer: BluetoothEnforcer protected lateinit var configuration: RadarConfiguration private var connectionsUpdatedReceiver: BroadcastRegistration? = null + private lateinit var broadcaster: LocalBroadcastManager protected open val requestPermissionTimeoutMs: Long get() = REQUEST_PERMISSION_TIMEOUT_MS @@ -98,8 +103,9 @@ abstract class MainActivity : AppCompatActivity() { onUnboundListeners += IRadarBinder::stopScanning } + broadcaster = LocalBroadcastManager.getInstance(this) bluetoothEnforcer = BluetoothEnforcer(this, radarConnection) - authConnection = ManagedServiceConnection(this, radarApp.authService) + authConnection = AuthServiceConnection(this, this) create() } @@ -148,6 +154,14 @@ abstract class MainActivity : AppCompatActivity() { } } + override fun loginSucceeded(manager: LoginManager?, authState: AppAuthState) = Unit + + override fun loginFailed(manager: LoginManager?, ex: Exception?) = Unit + + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { + + } + override fun onPause() { super.onPause() uiUpdater?.let { @@ -227,10 +241,20 @@ abstract class MainActivity : AppCompatActivity() { */ protected fun logout(disableRefresh: Boolean) { authConnection.applyBinder { invalidate(null, disableRefresh) } + radarConfig.reset() + cacheDir.deleteRecursively() logger.debug("Disabling Firebase Analytics") FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false) + logger.info("Starting SplashActivity") + val intent = packageManager.getLaunchIntentForPackage(BuildConfig.LIBRARY_PACKAGE_NAME) ?: return + startActivity(intent.apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME or + Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP + }) + finish() } + companion object { private val logger = LoggerFactory.getLogger(MainActivity::class.java) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt index 4eac4080c..672c720fc 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt @@ -20,7 +20,6 @@ import android.Manifest.permission.* import android.app.Notification import android.app.PendingIntent import android.content.ComponentName -import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.pm.PackageManager.PERMISSION_GRANTED @@ -78,6 +77,7 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis private lateinit var authConnection: AuthServiceConnection private lateinit var permissionsBroadcastReceiver: BroadcastRegistration private lateinit var sourceFailedReceiver: BroadcastRegistration + private lateinit var stopForegroundReceiver: BroadcastRegistration private lateinit var serverStatusReceiver: BroadcastRegistration private var sourceRegistrar: SourceProviderRegistrar? = null private val configuredProviders = ChangeRunner>>() @@ -236,6 +236,7 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis } permissionsBroadcastReceiver.unregister() sourceFailedReceiver.unregister() + stopForegroundReceiver.unregister() serverStatusReceiver.unregister() mHandler.stop { @@ -528,6 +529,15 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis } } + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { + mHandler.execute { + updateProviders(authState, configuration.latestConfig) + removeProviders(mConnections.toSet()) + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + } + private fun removeProviders(sourceProviders: Set>) { if (sourceProviders.isEmpty()) { return @@ -680,6 +690,7 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis const val ACTION_PERMISSIONS_GRANTED = "$RADAR_PACKAGE.ACTION_PERMISSIONS_GRANTED" const val EXTRA_GRANT_RESULTS = "$RADAR_PACKAGE.EXTRA_GRANT_RESULTS" + const val ACTION_STOP_FOREGROUND_SERVICE = "$RADAR_PACKAGE.ACTION_STOP_FOREGROUND_SERVICE" private const val BLUETOOTH_NOTIFICATION = 521290 diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt index 937306c31..5f1ea3bef 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt @@ -99,6 +99,8 @@ class AppAuthState private constructor(builder: Builder) { }.apply(changes).build() } + fun reset(): AppAuthState = Builder().build() + class Builder { val lastUpdate = SystemClock.elapsedRealtime() diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt index 1a4467d7f..c7adafc28 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt @@ -75,6 +75,11 @@ abstract class AuthService : Service(), LoginListener { authSerialization.store(appAuth) config.updateWithAuthState(this@AuthService, appAuth) } + + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { + authSerialization.store(appAuth) + config.updateWithAuthState(this@AuthService, appAuth) + } }) } @@ -208,6 +213,16 @@ abstract class AuthService : Service(), LoginListener { } } + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { + handler.executeReentrant { + logger.info("Logout Succeed") + appAuth = authState + callListeners { + it.loginListener.loggedOut(manager, appAuth) + } + } + } + override fun onDestroy() { networkConnectedListener.unregister() configRegistration?.let { removeLoginListener(it) } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginActivity.kt index f1ea48ec6..96fab6c27 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginActivity.kt @@ -109,6 +109,8 @@ abstract class LoginActivity : AppCompatActivity(), LoginListener { finish() } + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) = Unit + companion object { private val logger = LoggerFactory.getLogger(LoginActivity::class.java) const val ACTION_LOGIN = "org.radarcns.auth.LoginActivity.login" diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginListener.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginListener.kt index 1107bd218..327247108 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginListener.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/LoginListener.kt @@ -41,4 +41,6 @@ interface LoginListener { * It my also be `null`. */ fun loginFailed(manager: LoginManager?, ex: Exception?) + + fun loggedOut(manager: LoginManager?, authState: AppAuthState) } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt index cfb829034..4d7326f03 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt @@ -118,7 +118,7 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App disableRefresh -> authState.alter { attributes -= MP_REFRESH_TOKEN_PROPERTY isPrivacyPolicyAccepted = false - } + }.apply { listener.loggedOut(this@ManagementPortalLoginManager, authState.reset()) } else -> authState } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProviderRegistrar.kt b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProviderRegistrar.kt index 9d278a32a..9ac9cd2e8 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProviderRegistrar.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProviderRegistrar.kt @@ -42,6 +42,8 @@ class SourceProviderRegistrar( } } + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) = Unit + private fun registerProvider(provider: SourceProvider<*>, authState: AppAuthState) { if (isClosed) { return diff --git a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceService.kt b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceService.kt index f5cf76385..58ceeb7a7 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceService.kt @@ -336,6 +336,8 @@ abstract class SourceService : LifecycleService(), SourceSt } } + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) = Unit + override fun loginFailed(manager: LoginManager?, ex: Exception?) { } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/splash/SplashActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/splash/SplashActivity.kt index f9ba65502..38061054c 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/splash/SplashActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/splash/SplashActivity.kt @@ -160,6 +160,8 @@ abstract class SplashActivity : AppCompatActivity() { startActivity(radarApp.loginActivity) } } + + override fun loggedOut(manager: LoginManager?, authState: AppAuthState) = Unit } } From 7bf9d39b63e53d0526cf80886fbfb91f061ab101 Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Sat, 27 Jul 2024 14:55:32 +0530 Subject: [PATCH 2/7] More work on logout --- .../org/radarbase/android/MainActivity.kt | 49 +++++++++++++++++-- .../radarbase/android/RadarConfiguration.kt | 2 + .../org/radarbase/android/RadarService.kt | 8 ++- .../portal/ManagementPortalLoginManager.kt | 4 +- .../android/config/CombinedRadarConfig.kt | 5 ++ .../config/FirebaseRemoteConfiguration.kt | 5 ++ .../android/source/SourceProvider.kt | 8 +++ 7 files changed, 75 insertions(+), 6 deletions(-) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt index a5e6225e1..b65058be4 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt @@ -42,6 +42,7 @@ import org.radarbase.android.auth.LoginListener import org.radarbase.android.auth.LoginManager import org.radarbase.android.util.* import org.slf4j.LoggerFactory +import java.io.File /** Base MainActivity class. It manages the services to collect the data and starts up a view. To * create an application, extend this class and override the abstract methods. */ @@ -240,13 +241,16 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { * still valid. */ protected fun logout(disableRefresh: Boolean) { + radarConnection.unbind() authConnection.applyBinder { invalidate(null, disableRefresh) } radarConfig.reset() - cacheDir.deleteRecursively() + clearConfigSharedPrefs() + clearAppData(this) logger.debug("Disabling Firebase Analytics") FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false) - logger.info("Starting SplashActivity") - val intent = packageManager.getLaunchIntentForPackage(BuildConfig.LIBRARY_PACKAGE_NAME) ?: return + logger.info("Starting SplashActivity: ${packageManager.getLaunchIntentForPackage(packageName)}") + val intent = packageManager.getLaunchIntentForPackage(packageName) ?: return + logger.info("Starting Launch Activity named: ${BuildConfig.LIBRARY_PACKAGE_NAME}") startActivity(intent.apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP @@ -254,6 +258,45 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { finish() } + private fun clearConfigSharedPrefs() { + val sharedPreferences = getSharedPreferences("org.radarbase.android.config.LocalConfiguration", Context.MODE_PRIVATE) + sharedPreferences.all.forEach { (key, value) -> + logger.info("Finalizing Source service: Shared Prefs: Deleting: $key -> $value") + } + sharedPreferences.edit().clear().apply() + } + + private fun clearAppData(context: Context) { + clearCache(context) + clearFilesDir(context) + } + + private fun clearFilesDir(context: Context) { + logger.info("Finalizing Source Service: clearing files") + val filesDir = context.filesDir + deleteFilesInDirectory(filesDir) + } + + private fun clearCache(context: Context) { + logger.info("Finalizing Source Service: clearing cache") + val cacheDir = context.cacheDir + deleteFilesInDirectory(cacheDir) + } + + private fun deleteFilesInDirectory(directory: File) { + if (directory.isDirectory) { + val children = directory.listFiles() + if (children != null) { + for (child in children) { + logger.info("Finalizing Source Service: deleting: $child") + deleteFilesInDirectory(child) + } + } + } + directory.delete() + } + + companion object { private val logger = LoggerFactory.getLogger(MainActivity::class.java) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt index 92309b63e..c7148965f 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt @@ -53,6 +53,8 @@ interface RadarConfiguration { */ fun reset(vararg keys: String) + fun resetFirebaseRemoteConfigs() + /** * Fetch the remote configuration from server if it is outdated. */ diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt index 672c720fc..9ccc05bec 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt @@ -77,7 +77,6 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis private lateinit var authConnection: AuthServiceConnection private lateinit var permissionsBroadcastReceiver: BroadcastRegistration private lateinit var sourceFailedReceiver: BroadcastRegistration - private lateinit var stopForegroundReceiver: BroadcastRegistration private lateinit var serverStatusReceiver: BroadcastRegistration private var sourceRegistrar: SourceProviderRegistrar? = null private val configuredProviders = ChangeRunner>>() @@ -236,7 +235,6 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis } permissionsBroadcastReceiver.unregister() sourceFailedReceiver.unregister() - stopForegroundReceiver.unregister() serverStatusReceiver.unregister() mHandler.stop { @@ -251,6 +249,7 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis .filter(SourceProvider<*>::isBound) .forEach(SourceProvider<*>::unbind) + logger.info("Destroying RADAR Service") super.onDestroy() } @@ -532,7 +531,12 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { mHandler.execute { updateProviders(authState, configuration.latestConfig) + val oldProviders = mConnections removeProviders(mConnections.toSet()) + oldProviders.forEach { + it.stopService() + } + logger.info("Finalizing Source Services") stopForeground(STOP_FOREGROUND_REMOVE) stopSelf() } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt index 4d7326f03..e483740d1 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt @@ -273,7 +273,7 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App logger.error("Cannot construct ManagementPortalClient without client credentials") null } - + logger.debug("Client Config: {} and new client config: {}", clientConfig, newClientConfig) if (newClientConfig == clientConfig) return client = newClientConfig?.let { @@ -284,6 +284,8 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App client = restClient, ).also { restClient = it.client } } + + logger.info("Ensured new client: id: {}, secret: {}", {config.getString(OAUTH2_CLIENT_ID)}, {config.getString(OAUTH2_CLIENT_SECRET, "")}) } private fun addSource(authState: AppAuthState, source: SourceMetadata): AppAuthState { diff --git a/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt b/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt index 50f9660f6..6312d636a 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt @@ -97,6 +97,11 @@ class CombinedRadarConfig( persistChanges() } + override fun resetFirebaseRemoteConfigs() { + (remoteConfigs.find { it is FirebaseRemoteConfiguration } as FirebaseRemoteConfiguration).resetConfigs() + status = INITIAL + } + override fun fetch() = remoteConfigs.forEach { it.fetch(latestConfig.getLong(FETCH_TIMEOUT_MS_KEY, FETCH_TIMEOUT_MS_DEFAULT)) } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt b/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt index ed1574b34..016fdb1bb 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt @@ -116,6 +116,11 @@ class FirebaseRemoteConfiguration(private val context: Context, inDevelopmentMod } } + fun resetConfigs() { + firebase.reset() + status = RadarConfiguration.RemoteConfigStatus.INITIAL + } + companion object { private val logger = LoggerFactory.getLogger(FirebaseRemoteConfiguration::class.java) private const val FIREBASE_FETCH_TIMEOUT_MS_DEFAULT = 12 * 60 * 60 * 1000L diff --git a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt index 65c6864dd..144254aca 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt @@ -149,6 +149,14 @@ abstract class SourceProvider(protected val radarService: R connection.onServiceDisconnected(null) } + fun stopService() { + logger.info("Start of Finalizing Source Service") + val intent = Intent(radarService, serviceClass) + val isStopped = radarService.stopService(intent) + logger.info("Mid of Finalizing Source Service: ${serviceClass.name} isStopped: ${isStopped}") + logger.info("End of Finalizing Source Service: ${serviceClass.name}") + } + /** * Update the configuration of the service based on the given RadarConfiguration. * @throws IllegalStateException if [.getConnection] has not been called From 61c88f7a3b1c31135b17a0e77f7182269dbfacee Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Sat, 27 Jul 2024 22:32:44 +0530 Subject: [PATCH 3/7] Finalizing logout functionality --- .../java/org/radarbase/android/MainActivity.kt | 14 ++++---------- .../org/radarbase/android/RadarConfiguration.kt | 2 +- .../java/org/radarbase/android/RadarService.kt | 3 +++ .../org/radarbase/android/auth/AppAuthState.kt | 2 +- .../java/org/radarbase/android/auth/AuthService.kt | 7 ++++--- .../auth/portal/ManagementPortalLoginManager.kt | 2 +- .../android/config/CombinedRadarConfig.kt | 4 ++-- .../android/config/FirebaseRemoteConfiguration.kt | 3 +-- 8 files changed, 17 insertions(+), 20 deletions(-) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt index b65058be4..addb4c20b 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt @@ -33,10 +33,8 @@ import org.radarbase.android.RadarConfiguration.Companion.UI_REFRESH_RATE_KEY import org.radarbase.android.RadarConfiguration.Companion.USER_ID_KEY import org.radarbase.android.RadarService.Companion.ACTION_CHECK_PERMISSIONS import org.radarbase.android.RadarService.Companion.ACTION_PROVIDERS_UPDATED -import org.radarbase.android.RadarService.Companion.ACTION_STOP_FOREGROUND_SERVICE import org.radarbase.android.RadarService.Companion.EXTRA_PERMISSIONS import org.radarbase.android.auth.AppAuthState -import org.radarbase.android.auth.AuthService import org.radarbase.android.auth.AuthServiceConnection import org.radarbase.android.auth.LoginListener import org.radarbase.android.auth.LoginManager @@ -249,13 +247,8 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { logger.debug("Disabling Firebase Analytics") FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false) logger.info("Starting SplashActivity: ${packageManager.getLaunchIntentForPackage(packageName)}") - val intent = packageManager.getLaunchIntentForPackage(packageName) ?: return - logger.info("Starting Launch Activity named: ${BuildConfig.LIBRARY_PACKAGE_NAME}") - startActivity(intent.apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME or - Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP - }) - finish() + radarConfig.resetStatus() + // Start Launcher Activity in overriding method } private fun clearConfigSharedPrefs() { @@ -288,7 +281,8 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { val children = directory.listFiles() if (children != null) { for (child in children) { - logger.info("Finalizing Source Service: deleting: $child") + if (child.absolutePath.toString().contains("firebase")) return + logger.info("Finalizing Source Service: deleting: ${child.absolutePath}") deleteFilesInDirectory(child) } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt index c7148965f..52c4810e2 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt @@ -53,7 +53,7 @@ interface RadarConfiguration { */ fun reset(vararg keys: String) - fun resetFirebaseRemoteConfigs() + fun resetStatus() /** * Fetch the remote configuration from server if it is outdated. diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt index 9ccc05bec..aadae99ff 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt @@ -530,9 +530,12 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { mHandler.execute { + logger.info("RADAR Service: Start: Finalizing Source Service") updateProviders(authState, configuration.latestConfig) val oldProviders = mConnections + logger.info("RADAR Service: Mid: Finalizing Source Service") removeProviders(mConnections.toSet()) + logger.info("RADAR Service: End: Finalizing Source Service") oldProviders.forEach { it.stopService() } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt index 5f1ea3bef..8b4d9f425 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/AppAuthState.kt @@ -99,7 +99,7 @@ class AppAuthState private constructor(builder: Builder) { }.apply(changes).build() } - fun reset(): AppAuthState = Builder().build() + fun clear(): AppAuthState = Builder().build() class Builder { val lastUpdate = SystemClock.elapsedRealtime() diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt index c7adafc28..723e65282 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt @@ -77,8 +77,8 @@ abstract class AuthService : Service(), LoginListener { } override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { - authSerialization.store(appAuth) - config.updateWithAuthState(this@AuthService, appAuth) +// authSerialization.store(appAuth) +// config.updateWithAuthState(this@AuthService, appAuth) } }) } @@ -188,6 +188,7 @@ abstract class AuthService : Service(), LoginListener { listeners.filter { it.lastUpdate < sinceUpdate } .forEach { listener -> listener.lastUpdate = sinceUpdate + logger.info("Sending Login Succeed to listener: {}", listener.loginListener) call(listener) } } @@ -206,7 +207,6 @@ abstract class AuthService : Service(), LoginListener { appAuth = authState broadcaster.send(ACTION_LOGIN_SUCCESS) - callListeners(sinceUpdate = appAuth.lastUpdate) { it.loginListener.loginSucceeded(manager, appAuth) } @@ -218,6 +218,7 @@ abstract class AuthService : Service(), LoginListener { logger.info("Logout Succeed") appAuth = authState callListeners { + logger.debug("Finalizing Source Service: LL #{}: {} (starting with {} listeners)", it.id, it.loginListener, listeners.size) it.loginListener.loggedOut(manager, appAuth) } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt index e483740d1..8915b990f 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt @@ -118,7 +118,7 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App disableRefresh -> authState.alter { attributes -= MP_REFRESH_TOKEN_PROPERTY isPrivacyPolicyAccepted = false - }.apply { listener.loggedOut(this@ManagementPortalLoginManager, authState.reset()) } + }.also { listener.loggedOut(this@ManagementPortalLoginManager, authState.clear()) } else -> authState } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt b/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt index 6312d636a..7e3ed50fd 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/config/CombinedRadarConfig.kt @@ -97,8 +97,8 @@ class CombinedRadarConfig( persistChanges() } - override fun resetFirebaseRemoteConfigs() { - (remoteConfigs.find { it is FirebaseRemoteConfiguration } as FirebaseRemoteConfiguration).resetConfigs() + override fun resetStatus() { + (remoteConfigs.find { it is FirebaseRemoteConfiguration } as FirebaseRemoteConfiguration).resetStatus() status = INITIAL } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt b/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt index 016fdb1bb..4a759df21 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/config/FirebaseRemoteConfiguration.kt @@ -116,8 +116,7 @@ class FirebaseRemoteConfiguration(private val context: Context, inDevelopmentMod } } - fun resetConfigs() { - firebase.reset() + fun resetStatus() { status = RadarConfiguration.RemoteConfigStatus.INITIAL } From 09602d70f70dc418aacf21d4be1fbc71ddf8cdd0 Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Sat, 27 Jul 2024 22:43:16 +0530 Subject: [PATCH 4/7] Removing test logs --- .../src/main/java/org/radarbase/android/MainActivity.kt | 5 ----- .../src/main/java/org/radarbase/android/RadarService.kt | 4 ---- .../src/main/java/org/radarbase/android/auth/AuthService.kt | 1 - .../android/auth/portal/ManagementPortalLoginManager.kt | 2 -- .../main/java/org/radarbase/android/source/SourceProvider.kt | 5 +---- 5 files changed, 1 insertion(+), 16 deletions(-) diff --git a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt index addb4c20b..85ccf9311 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt @@ -246,7 +246,6 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { clearAppData(this) logger.debug("Disabling Firebase Analytics") FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false) - logger.info("Starting SplashActivity: ${packageManager.getLaunchIntentForPackage(packageName)}") radarConfig.resetStatus() // Start Launcher Activity in overriding method } @@ -254,7 +253,6 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { private fun clearConfigSharedPrefs() { val sharedPreferences = getSharedPreferences("org.radarbase.android.config.LocalConfiguration", Context.MODE_PRIVATE) sharedPreferences.all.forEach { (key, value) -> - logger.info("Finalizing Source service: Shared Prefs: Deleting: $key -> $value") } sharedPreferences.edit().clear().apply() } @@ -265,13 +263,11 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { } private fun clearFilesDir(context: Context) { - logger.info("Finalizing Source Service: clearing files") val filesDir = context.filesDir deleteFilesInDirectory(filesDir) } private fun clearCache(context: Context) { - logger.info("Finalizing Source Service: clearing cache") val cacheDir = context.cacheDir deleteFilesInDirectory(cacheDir) } @@ -282,7 +278,6 @@ abstract class MainActivity : AppCompatActivity(), LoginListener { if (children != null) { for (child in children) { if (child.absolutePath.toString().contains("firebase")) return - logger.info("Finalizing Source Service: deleting: ${child.absolutePath}") deleteFilesInDirectory(child) } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt index aadae99ff..d0a39d5c9 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt @@ -530,16 +530,12 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis override fun loggedOut(manager: LoginManager?, authState: AppAuthState) { mHandler.execute { - logger.info("RADAR Service: Start: Finalizing Source Service") updateProviders(authState, configuration.latestConfig) val oldProviders = mConnections - logger.info("RADAR Service: Mid: Finalizing Source Service") removeProviders(mConnections.toSet()) - logger.info("RADAR Service: End: Finalizing Source Service") oldProviders.forEach { it.stopService() } - logger.info("Finalizing Source Services") stopForeground(STOP_FOREGROUND_REMOVE) stopSelf() } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt index 723e65282..2af416ea4 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt @@ -218,7 +218,6 @@ abstract class AuthService : Service(), LoginListener { logger.info("Logout Succeed") appAuth = authState callListeners { - logger.debug("Finalizing Source Service: LL #{}: {} (starting with {} listeners)", it.id, it.loginListener, listeners.size) it.loginListener.loggedOut(manager, appAuth) } } diff --git a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt index 8915b990f..67231ac7a 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt @@ -273,7 +273,6 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App logger.error("Cannot construct ManagementPortalClient without client credentials") null } - logger.debug("Client Config: {} and new client config: {}", clientConfig, newClientConfig) if (newClientConfig == clientConfig) return client = newClientConfig?.let { @@ -285,7 +284,6 @@ class ManagementPortalLoginManager(private val listener: AuthService, state: App ).also { restClient = it.client } } - logger.info("Ensured new client: id: {}, secret: {}", {config.getString(OAUTH2_CLIENT_ID)}, {config.getString(OAUTH2_CLIENT_SECRET, "")}) } private fun addSource(authState: AppAuthState, source: SourceMetadata): AppAuthState { diff --git a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt index 144254aca..6968d8096 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/source/SourceProvider.kt @@ -150,11 +150,8 @@ abstract class SourceProvider(protected val radarService: R } fun stopService() { - logger.info("Start of Finalizing Source Service") val intent = Intent(radarService, serviceClass) - val isStopped = radarService.stopService(intent) - logger.info("Mid of Finalizing Source Service: ${serviceClass.name} isStopped: ${isStopped}") - logger.info("End of Finalizing Source Service: ${serviceClass.name}") + radarService.stopService(intent) } /** From cef0740b4fb9bbe69c63471e14664fa4ed7d95b9 Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Thu, 15 Aug 2024 20:04:51 +0530 Subject: [PATCH 5/7] Prevent memory leaks --- .../radarbase/monitor/application/ApplicationStatusManager.kt | 1 + .../src/main/java/org/radarbase/android/RadarService.kt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/plugins/radar-android-application-status/src/main/java/org/radarbase/monitor/application/ApplicationStatusManager.kt b/plugins/radar-android-application-status/src/main/java/org/radarbase/monitor/application/ApplicationStatusManager.kt index dc5a676d8..fed0d7dcd 100755 --- a/plugins/radar-android-application-status/src/main/java/org/radarbase/monitor/application/ApplicationStatusManager.kt +++ b/plugins/radar-android-application-status/src/main/java/org/radarbase/monitor/application/ApplicationStatusManager.kt @@ -269,6 +269,7 @@ class ApplicationStatusManager( cacheReceiver?.unregister() serverRecordsReceiver?.unregister() serverStatusReceiver?.unregister() + tzProcessor?.close() } private fun processTimeZone() { diff --git a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt index d0a39d5c9..117fb8276 100644 --- a/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt +++ b/radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt @@ -242,6 +242,8 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis it.close() sourceRegistrar = null } + (dataHandler as? TableDataHandler)?.close() + dataHandler = null } authConnection.unbind() From 41644d01c27083b8f2c49aefa279b83dafd89bdf Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Thu, 15 Aug 2024 20:17:53 +0530 Subject: [PATCH 6/7] Using non snapshot version for CI passing --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 6ef1d0be9..494c6ea7a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -38,7 +38,7 @@ publish_plugin_version=2.0.0 versions_plugin_version=0.51.0 radar_commons_version=0.15.0 -radar_schemas_commons_version=0.8.10-SNAPSHOT +radar_schemas_commons_version=0.8.10 radar_faros_sdk_version=0.1.0 From 5890e16febd0a9330731f959873db40a71b25f6c Mon Sep 17 00:00:00 2001 From: this-Aditya Date: Tue, 27 Aug 2024 09:15:54 +0530 Subject: [PATCH 7/7] Skipping polar build for passing CI once --- plugins/radar-android-polar/gradle.skip | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 plugins/radar-android-polar/gradle.skip diff --git a/plugins/radar-android-polar/gradle.skip b/plugins/radar-android-polar/gradle.skip new file mode 100644 index 000000000..e69de29bb