Skip to content

Commit cb9cfcc

Browse files
committed
Added feature to do logout
1 parent 43167a1 commit cb9cfcc

File tree

19 files changed

+197
-34
lines changed

19 files changed

+197
-34
lines changed

plugins/radar-android-application-status/src/main/java/org/radarbase/monitor/application/ApplicationStatusManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class ApplicationStatusManager(
307307
override fun onClose() {
308308
applicationStatusExecutor.stop {
309309
this.processor.stop()
310+
tzProcessor?.stop()
310311
}
311312
}
312313

radar-commons-android/src/main/java/org/radarbase/android/MainActivity.kt

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.google.firebase.analytics.FirebaseAnalytics
3030
import kotlinx.coroutines.CoroutineStart
3131
import kotlinx.coroutines.Dispatchers
3232
import kotlinx.coroutines.Job
33+
import kotlinx.coroutines.delay
3334
import kotlinx.coroutines.flow.MutableSharedFlow
3435
import kotlinx.coroutines.flow.SharedFlow
3536
import kotlinx.coroutines.flow.StateFlow
@@ -48,6 +49,7 @@ import org.radarbase.android.auth.AuthService
4849
import org.radarbase.android.auth.AuthServiceStateReactor
4950
import org.radarbase.android.auth.LoginListener
5051
import org.radarbase.android.auth.LoginManager
52+
import org.radarbase.android.splash.SplashActivity
5153
import org.radarbase.android.util.BindState
5254
import org.radarbase.android.util.BluetoothEnforcer
5355
import org.radarbase.android.util.CoroutineTaskExecutor
@@ -57,6 +59,7 @@ import org.radarbase.android.util.PermissionBroadcast
5759
import org.radarbase.android.util.PermissionHandler
5860
import org.radarbase.kotlin.coroutines.launchJoin
5961
import org.slf4j.LoggerFactory
62+
import java.io.File
6063
import kotlin.time.Duration
6164
import kotlin.time.Duration.Companion.milliseconds
6265

@@ -373,22 +376,67 @@ abstract class MainActivity : AppCompatActivity(), LoginListener {
373376
* still valid.
374377
*/
375378
protected suspend fun logout(disableRefresh: Boolean) {
379+
logger.trace("NewBroadcastTrace: now performing logout from superclass")
376380
authConnection.applyBinder { invalidate(null, disableRefresh) }
381+
radarConfig.resetConfig()
382+
clearAppData(this)
377383
logger.debug("Disabling Firebase Analytics")
378384
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false)
379385
}
380386

381387
override fun loginSucceeded(manager: LoginManager?, authState: AppAuthState) = Unit
382388

383-
override fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) {
389+
override suspend fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) {
390+
logger.trace("NewBroadcastTrace: Starting splash 1")
391+
delay(1000)
392+
logger.trace("NewBroadcastTrace: Starting splash 2")
384393
logger.info("Starting SplashActivity")
385-
val intent = packageManager.getLaunchIntentForPackage(BuildConfig.LIBRARY_PACKAGE_NAME) ?: return
386-
startActivity(intent.apply {
387-
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
388-
})
389-
finish()
394+
try {
395+
val intent = packageManager.getLaunchIntentForPackage(packageName)
396+
intent ?: return
397+
startActivity(intent.apply {
398+
flags =
399+
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
400+
})
401+
logger.trace("NewBroadcastTrace: Started splash and now finishing")
402+
finish()
403+
logger.trace("NewBroadcastTrace: Started splash and now finished")
404+
} catch (ex: Exception) {
405+
logger.trace("NewBroadcastTrace: Exception when starting splash: $ex")
406+
} finally {
407+
logger.info("NewBroadcastTrace: Logout succeeded method completed")
408+
}
409+
}
410+
411+
private fun clearAppData(context: Context) {
412+
clearCache(context)
413+
clearFilesDir(context)
414+
}
415+
416+
private fun clearFilesDir(context: Context) {
417+
val filesDir = context.filesDir
418+
deleteFilesInDirectory(filesDir)
419+
}
420+
421+
private fun clearCache(context: Context) {
422+
val cacheDir = context.cacheDir
423+
deleteFilesInDirectory(cacheDir)
390424
}
391425

426+
private fun deleteFilesInDirectory(directory: File) {
427+
if (directory.isDirectory) {
428+
val children = directory.listFiles()
429+
if (children != null) {
430+
for (child in children) {
431+
if (child.absolutePath.toString().contains("firebase")) return
432+
deleteFilesInDirectory(child)
433+
}
434+
}
435+
}
436+
directory.delete()
437+
}
438+
439+
392440
companion object {
393441
private val logger = LoggerFactory.getLogger(MainActivity::class.java)
394442

radar-commons-android/src/main/java/org/radarbase/android/RadarConfiguration.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ interface RadarConfiguration {
6363
*/
6464
suspend fun forceFetch()
6565

66+
suspend fun resetConfig()
67+
6668
/**
6769
* Adds base URL from auth state to configuration.
6870
*/
69-
suspend fun updateWithAuthState(context: Context, appAuthState: AppAuthState?)
71+
suspend fun updateWithAuthState(context: Context, appAuthState: AppAuthState?, isLogoutCall: Boolean = false)
7072

7173
companion object {
7274
const val RADAR_CONFIGURATION_CHANGED = "org.radarcns.android.RadarConfiguration.CHANGED"

radar-commons-android/src/main/java/org/radarbase/android/RadarService.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import org.radarbase.android.kafka.ServerStatusListener
5858
import org.radarbase.android.kafka.TopicSendReceipt
5959
import org.radarbase.android.source.*
6060
import org.radarbase.android.util.*
61-
import org.radarbase.android.util.BluetoothEnforcer.Companion
6261
import org.radarbase.android.util.ManagedServiceConnection.Companion.serviceConnection
6362
import org.radarbase.android.util.NotificationHandler.Companion.NOTIFICATION_CHANNEL_INFO
6463
import org.radarbase.android.util.PermissionHandler.Companion.isPermissionGranted
@@ -238,9 +237,6 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
238237
launch {
239238
configuration.config.collect(::configure)
240239
}
241-
launch {
242-
243-
}
244240
}
245241

246242
bluetoothReceiver = BluetoothStateReceiver(this) { enabled ->
@@ -306,6 +302,8 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
306302
it.close()
307303
sourceRegistrar = null
308304
}
305+
(dataHandler as TableDataHandler).stop()
306+
dataHandler = null
309307
}
310308
recordTrackerJob?.cancel()
311309
statusTrackerJob?.cancel()
@@ -596,7 +594,18 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
596594
}
597595
}
598596

599-
override fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) = Unit
597+
override suspend fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) {
598+
radarExecutor.execute {
599+
updateProviders(authState, configuration.latestConfig)
600+
val oldProviders = mConnections
601+
removeProviders(mConnections.toSet())
602+
oldProviders.forEach {
603+
it.stopService()
604+
}
605+
stopForeground(STOP_FOREGROUND_REMOVE)
606+
stopSelf()
607+
}
608+
}
600609

601610
private fun removeProviders(sourceProviders: Set<SourceProvider<*>>) {
602611
if (sourceProviders.isEmpty()) {
@@ -770,8 +779,6 @@ abstract class RadarService : LifecycleService(), ServerStatusListener, LoginLis
770779
private val logger = LoggerFactory.getLogger(RadarService::class.java)
771780
private const val RADAR_PACKAGE = "org.radarbase.android"
772781

773-
const val ACTION_PROVIDERS_UPDATED = "$RADAR_PACKAGE.ACTION_PROVIDERS_UPDATED"
774-
775782
const val ACTION_CHECK_PERMISSIONS = "$RADAR_PACKAGE.ACTION_CHECK_PERMISSIONS"
776783
const val EXTRA_PERMISSIONS = "$RADAR_PACKAGE.EXTRA_PERMISSIONS"
777784

radar-commons-android/src/main/java/org/radarbase/android/auth/AuthService.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ abstract class AuthService : LifecycleService(), LoginListener {
5959
private val serviceMutex: Mutex = Mutex(false)
6060
private val registryTweakMutex: Mutex = Mutex(false)
6161
private val authUpdateMutex: Mutex = Mutex(false)
62+
private var shouldUpdateAuth: AtomicBoolean = AtomicBoolean(true)
6263

6364
private var needLoadedState: AtomicBoolean = AtomicBoolean(true)
6465

@@ -99,6 +100,7 @@ abstract class AuthService : LifecycleService(), LoginListener {
99100
override fun onCreate() {
100101
super.onCreate()
101102

103+
shouldUpdateAuth = AtomicBoolean(true)
102104
networkConnectedListener = NetworkConnectedReceiver(this)
103105

104106
executor.start()
@@ -135,8 +137,10 @@ abstract class AuthService : LifecycleService(), LoginListener {
135137
authState
136138
.collectLatest { state ->
137139
logger.trace("Collected AppAuth: {}", state)
138-
radarConfig.updateWithAuthState(this@AuthService, state)
139-
}
140+
if (shouldUpdateAuth.get()) {
141+
radarConfig.updateWithAuthState(this@AuthService, state)
142+
}
143+
}
140144
}
141145
launch(Dispatchers.Default) {
142146
networkConnectedListener.monitor()
@@ -185,6 +189,20 @@ abstract class AuthService : LifecycleService(), LoginListener {
185189
}
186190
}
187191
}
192+
launch(Dispatchers.Default) {
193+
authStateLogout
194+
.collectLatest {
195+
logger.info("Logout succeeded")
196+
shouldUpdateAuth = AtomicBoolean(false)
197+
val clearedState = latestAppAuth.reset()
198+
_authState.value = clearedState
199+
authSerialization.remove()
200+
callListeners {
201+
it.listener.logoutSucceeded(null, clearedState)
202+
}
203+
radarConfig.updateWithAuthState(this@AuthService, clearedState, true)
204+
}
205+
}
188206
}
189207
}
190208
}
@@ -235,6 +253,10 @@ abstract class AuthService : LifecycleService(), LoginListener {
235253
suspend fun refreshIfOnline() {
236254
executor.execute {
237255
logger.debug("Refreshing if online")
256+
if (!shouldUpdateAuth.get()) {
257+
logger.debug("Performing logout aborting refresh")
258+
return@execute
259+
}
238260
for (i in 1..3) {
239261
if (!isNetworkStatusReceived) {
240262
delay(100)
@@ -345,7 +367,7 @@ abstract class AuthService : LifecycleService(), LoginListener {
345367
_authState.value = authState
346368
}
347369

348-
override fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) {
370+
override suspend fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) {
349371
lifecycleScope.launch {
350372
_authStateLogout.emit(AuthLoginListener.AuthStateLogout(manager, authState))
351373
}
@@ -405,6 +427,7 @@ abstract class AuthService : LifecycleService(), LoginListener {
405427

406428
suspend fun invalidate(token: String?, disableRefresh: Boolean) {
407429
executor.execute {
430+
logger.trace("NewBroadcastTrace: Invalidating auth state")
408431
updateState { auth ->
409432
logger.info("Invalidating authentication state")
410433
if (token != null && token != auth.token) return@updateState
@@ -476,6 +499,7 @@ abstract class AuthService : LifecycleService(), LoginListener {
476499

477500
override fun onBind(intent: Intent): IBinder? {
478501
super.onBind(intent)
502+
shouldUpdateAuth = AtomicBoolean(true)
479503
return AuthServiceBinder()
480504
}
481505

radar-commons-android/src/main/java/org/radarbase/android/auth/LoginActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ abstract class LoginActivity : AppCompatActivity(), LoginListener {
153153
finish()
154154
}
155155

156+
override suspend fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState) = Unit
157+
156158
companion object {
157159
private val logger = LoggerFactory.getLogger(LoginActivity::class.java)
158160
const val ACTION_LOGIN = "org.radarcns.auth.LoginActivity.login"

radar-commons-android/src/main/java/org/radarbase/android/auth/LoginListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ interface LoginListener {
4242
*/
4343
fun loginFailed(manager: LoginManager?, ex: Exception?)
4444

45-
fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState)
45+
suspend fun logoutSucceeded(manager: LoginManager?, authState: AppAuthState)
4646
}

radar-commons-android/src/main/java/org/radarbase/android/auth/portal/ManagementPortalLoginManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class ManagementPortalLoginManager(
155155
if (authState.authenticationSource != SOURCE_TYPE) return
156156
if (disableRefresh) {
157157
authState.clear()
158+
listener.logoutSucceeded(this@ManagementPortalLoginManager, AppAuthState())
158159
} else authState.invalidate()
159160
}
160161

radar-commons-android/src/main/java/org/radarbase/android/config/AppConfigRadarConfiguration.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class AppConfigRadarConfiguration(
8888
}
8989
}
9090

91-
override suspend fun doFetch(maxCacheAge: Long) {
91+
override suspend fun doFetch(maxCacheAgeMillis: Long) {
9292
val (client, appConfig) = configMutex.withLock {
9393
Pair(client, appConfig)
9494
}
@@ -175,6 +175,11 @@ class AppConfigRadarConfiguration(
175175
}
176176
}
177177

178+
override suspend fun reset() {
179+
status.value = RadarConfiguration.RemoteConfigStatus.INITIAL
180+
auth = null
181+
}
182+
178183
private suspend fun updateConfiguration(auth: AppAuthState?, config: SingleRadarConfiguration?) {
179184
val appConfig = config?.let { value ->
180185
val serverConfig = value.optString(BASE_URL_KEY) { baseUrl ->

0 commit comments

Comments
 (0)