From 7f513af9c477f9a434d78b3a9c0a9e5a2b2dfeba Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Wed, 19 Jun 2024 19:14:27 +0530 Subject: [PATCH 1/2] fix broadcast being sent everytime App Lounge opens --- .../java/foundation/e/apps/MainActivity.kt | 15 ------------- .../apps/data/login/PlayStoreAuthenticator.kt | 2 +- .../data/login/api/PlayStoreLoginWrapper.kt | 21 ++++++++++++++++++- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index cedbf9c84..5e9cb490e 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -360,27 +360,12 @@ class MainActivity : AppCompatActivity() { } } - // Broadcast if not gplay type login or successful gplay login - if (gPlayAuthObject == null || gPlayAuthObject.result.isSuccess()) { - broadcastGPlayLogin() - } - if (viewModel.closeAfterLogin && it.isNotEmpty() && it.all { it.result.isSuccess() }) { finishAndRemoveTask() } } } - private fun broadcastGPlayLogin() { - val user = viewModel.getUser().name - Timber.d("Sending broadcast with login type - $user") - val intent = Intent(Constants.ACTION_PARENTAL_CONTROL_APP_LOUNGE_LOGIN).apply { - setPackage(BuildConfig.PACKAGE_NAME_PARENTAL_CONTROL) - putExtra(COLUMN_LOGIN_TYPE, user) - } - sendBroadcast(intent) - } - private fun setupViewModels() { viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java] signInViewModel = ViewModelProvider(this)[SignInViewModel::class.java] diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index 823c84f63..c75c2ecb4 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -61,7 +61,7 @@ class PlayStoreAuthenticator @Inject constructor( get() = loginManagerFactory.createLoginManager(user) private val loginWrapper: PlayStoreLoginWrapper - get() = PlayStoreLoginWrapper(loginManager, user) + get() = PlayStoreLoginWrapper(context, loginManager, user) private val locale: Locale get() = context.resources.configuration.locales[0] diff --git a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt index ddb836357..b4dc7ba1b 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt @@ -17,14 +17,20 @@ package foundation.e.apps.data.login.api +import android.content.Context +import android.content.Intent import com.aurora.gplayapi.data.models.AuthData import com.aurora.gplayapi.data.models.PlayResponse +import foundation.e.apps.BuildConfig +import foundation.e.apps.contract.ParentalControlContract +import foundation.e.apps.data.Constants import foundation.e.apps.data.ResultSupreme import foundation.e.apps.data.enums.User import foundation.e.apps.data.playstore.utils.AC2DMUtil import foundation.e.apps.data.handleNetworkResult import foundation.e.apps.data.login.exceptions.GPlayLoginException import java.util.Locale +import timber.log.Timber /** * Call methods of [GoogleLoginManager] and [AnonymousLoginManager] from here. @@ -36,6 +42,7 @@ import java.util.Locale * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5680 */ class PlayStoreLoginWrapper constructor( + private val context: Context, private val loginManager: PlayStoreLoginManager, private val user: User, ) { @@ -52,7 +59,10 @@ class PlayStoreLoginWrapper constructor( this.exception = when (result) { is ResultSupreme.Timeout -> GPlayLoginException(true, "GPlay API timeout", user) is ResultSupreme.Error -> GPlayLoginException(false, result.message, user) - else -> null + else -> { + broadcastGPlayLogin() + null + } } } } @@ -130,4 +140,13 @@ class PlayStoreLoginWrapper constructor( } } } + + private fun broadcastGPlayLogin() { + Timber.d("Sending broadcast with login type - $user") + val intent = Intent(Constants.ACTION_PARENTAL_CONTROL_APP_LOUNGE_LOGIN).apply { + setPackage(BuildConfig.PACKAGE_NAME_PARENTAL_CONTROL) + putExtra(ParentalControlContract.COLUMN_LOGIN_TYPE, user) + } + context.sendBroadcast(intent) + } } -- GitLab From c73bf5fb15ba1ac167e1fb4167fe37b1e5092f8a Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Wed, 19 Jun 2024 19:28:34 +0530 Subject: [PATCH 2/2] use EventBus instead --- .../java/foundation/e/apps/MainActivity.kt | 21 +++++++++++++++++++ .../apps/data/login/PlayStoreAuthenticator.kt | 2 +- .../data/login/api/PlayStoreLoginWrapper.kt | 20 +++--------------- .../e/apps/utils/eventBus/AppEvent.kt | 2 ++ 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 5e9cb490e..c26250453 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -235,6 +235,10 @@ class MainActivity : AppCompatActivity() { launch { observeAgeLimitRestrictionEvent() } + + launch { + observeSuccessfulLogin() + } } } } @@ -366,6 +370,23 @@ class MainActivity : AppCompatActivity() { } } + private suspend fun observeSuccessfulLogin() { + EventBus.events.filter { + it is AppEvent.SuccessfulLogin + }.collectLatest { + broadcastGPlayLogin(it.data as User) + } + } + + private fun broadcastGPlayLogin(user: User) { + Timber.d("Sending broadcast with login type - $user") + val intent = Intent(Constants.ACTION_PARENTAL_CONTROL_APP_LOUNGE_LOGIN).apply { + setPackage(BuildConfig.PACKAGE_NAME_PARENTAL_CONTROL) + putExtra(COLUMN_LOGIN_TYPE, user) + } + sendBroadcast(intent) + } + private fun setupViewModels() { viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java] signInViewModel = ViewModelProvider(this)[SignInViewModel::class.java] diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index c75c2ecb4..823c84f63 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -61,7 +61,7 @@ class PlayStoreAuthenticator @Inject constructor( get() = loginManagerFactory.createLoginManager(user) private val loginWrapper: PlayStoreLoginWrapper - get() = PlayStoreLoginWrapper(context, loginManager, user) + get() = PlayStoreLoginWrapper(loginManager, user) private val locale: Locale get() = context.resources.configuration.locales[0] diff --git a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt index b4dc7ba1b..4d013b2f0 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginWrapper.kt @@ -17,20 +17,16 @@ package foundation.e.apps.data.login.api -import android.content.Context -import android.content.Intent import com.aurora.gplayapi.data.models.AuthData import com.aurora.gplayapi.data.models.PlayResponse -import foundation.e.apps.BuildConfig -import foundation.e.apps.contract.ParentalControlContract -import foundation.e.apps.data.Constants import foundation.e.apps.data.ResultSupreme import foundation.e.apps.data.enums.User import foundation.e.apps.data.playstore.utils.AC2DMUtil import foundation.e.apps.data.handleNetworkResult import foundation.e.apps.data.login.exceptions.GPlayLoginException +import foundation.e.apps.utils.eventBus.AppEvent +import foundation.e.apps.utils.eventBus.EventBus import java.util.Locale -import timber.log.Timber /** * Call methods of [GoogleLoginManager] and [AnonymousLoginManager] from here. @@ -42,7 +38,6 @@ import timber.log.Timber * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5680 */ class PlayStoreLoginWrapper constructor( - private val context: Context, private val loginManager: PlayStoreLoginManager, private val user: User, ) { @@ -60,7 +55,7 @@ class PlayStoreLoginWrapper constructor( is ResultSupreme.Timeout -> GPlayLoginException(true, "GPlay API timeout", user) is ResultSupreme.Error -> GPlayLoginException(false, result.message, user) else -> { - broadcastGPlayLogin() + EventBus.invokeEvent(AppEvent.SuccessfulLogin(user)) null } } @@ -140,13 +135,4 @@ class PlayStoreLoginWrapper constructor( } } } - - private fun broadcastGPlayLogin() { - Timber.d("Sending broadcast with login type - $user") - val intent = Intent(Constants.ACTION_PARENTAL_CONTROL_APP_LOUNGE_LOGIN).apply { - setPackage(BuildConfig.PACKAGE_NAME_PARENTAL_CONTROL) - putExtra(ParentalControlContract.COLUMN_LOGIN_TYPE, user) - } - context.sendBroadcast(intent) - } } diff --git a/app/src/main/java/foundation/e/apps/utils/eventBus/AppEvent.kt b/app/src/main/java/foundation/e/apps/utils/eventBus/AppEvent.kt index e3db13f9f..9c3cd7e5f 100644 --- a/app/src/main/java/foundation/e/apps/utils/eventBus/AppEvent.kt +++ b/app/src/main/java/foundation/e/apps/utils/eventBus/AppEvent.kt @@ -22,6 +22,7 @@ package foundation.e.apps.utils.eventBus import foundation.e.apps.data.ResultSupreme import foundation.e.apps.data.enums.ResultStatus +import foundation.e.apps.data.enums.User import foundation.e.apps.data.install.models.AppInstall sealed class AppEvent(val data: Any) { @@ -35,4 +36,5 @@ sealed class AppEvent(val data: Any) { class NoInternetEvent(isInternetAvailable: Boolean) : AppEvent(isInternetAvailable) class TooManyRequests : AppEvent(Unit) class AgeLimitRestrictionEvent(type: String) : AppEvent(type) + class SuccessfulLogin(user: User): AppEvent(user) } -- GitLab