From 0382dd802594ec12127fd70dfb33827952309aa9 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Tue, 14 Jun 2022 23:52:58 +0600 Subject: [PATCH 1/4] App Lounge: some debug log added --- app/build.gradle | 2 ++ .../foundation/e/apps/AppLoungeApplication.kt | 4 ++++ .../java/foundation/e/apps/MainActivity.kt | 6 ++++++ .../e/apps/MainActivityViewModel.kt | 19 ++++++++++++++++--- .../e/apps/api/fused/FusedAPIRepository.kt | 14 +++++++++++--- .../e/apps/api/gplay/GPlayAPIImpl.kt | 3 +++ .../signin/google/GoogleSignInFragment.kt | 2 ++ 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 09d7acdd5..c05f13918 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -108,6 +108,8 @@ dependencies { implementation "io.coil-kt:coil:1.4.0" implementation 'com.github.Baseflow:PhotoView:2.3.0' + implementation 'com.jakewharton.timber:timber:5.0.1' + //Protobuf and Gson implementation 'com.google.code.gson:gson:2.8.9' implementation "com.google.protobuf:protobuf-java:3.14.0" diff --git a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt index f33fe1e91..f617eebb6 100644 --- a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt +++ b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt @@ -30,6 +30,7 @@ import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch +import timber.log.Timber import java.util.concurrent.Executors import javax.inject.Inject @@ -60,6 +61,9 @@ class AppLoungeApplication : Application(), Configuration.Provider { dataStoreModule.saveTOCStatus(false, "") } } + if(BuildConfig.DEBUG) { + Timber.plant(Timber.DebugTree()) + } } override fun getWorkManagerConfiguration() = diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index f095a39d5..32ca72523 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -48,6 +48,7 @@ import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.CommonUtilsModule import foundation.e.apps.utils.parentFragment.TimeoutFragment import kotlinx.coroutines.launch +import timber.log.Timber import java.io.File import java.util.UUID @@ -96,6 +97,7 @@ class MainActivity : AppCompatActivity() { } } User.UNAVAILABLE -> { + Timber.d(">>> UNAVAILABLE > destorycredentials") viewModel.destroyCredentials(null) } User.GOOGLE -> { @@ -116,6 +118,7 @@ class MainActivity : AppCompatActivity() { binding.fragment.visibility = View.VISIBLE viewModel.userType.observe(this) { user -> + Timber.d(">>> auth: usertype") generateAuthDataBasedOnUserType(user) } @@ -125,8 +128,10 @@ class MainActivity : AppCompatActivity() { // Watch and refresh authentication data viewModel.authDataJson.observe(this) { + Timber.d(">>> auth: authDataJson: ") if (!it.isNullOrEmpty()) { viewModel.generateAuthData() + viewModel.validateAuthData() Log.d(TAG, "Authentication data is available!") } } @@ -134,6 +139,7 @@ class MainActivity : AppCompatActivity() { } viewModel.authValidity.observe(this) { + Timber.d(">>> auth: authvalidity: $it") if (it != true) { Log.d(TAG, "Authentication data validation failed!") viewModel.destroyCredentials { user -> diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index 4e54172e0..8597eee0e 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -54,6 +54,7 @@ import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import ru.beryukhov.reactivenetwork.ReactiveNetwork +import timber.log.Timber import java.io.ByteArrayOutputStream import javax.inject.Inject @@ -123,8 +124,12 @@ class MainActivityViewModel @Inject constructor( * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5404 */ fun retryFetchingTokenAfterTimeout() { + if(userType.value?.contentEquals(User.UNAVAILABLE.name) == true) { + return + } firstAuthDataFetchTime = 0 setFirstTokenFetchTime() + Timber.d(">>> authvalidity postvalue > retryfetching") authValidity.postValue(false) } @@ -155,6 +160,7 @@ class MainActivityViewModel @Inject constructor( */ if (!fusedAPIRepository.fetchAuthData()) { authRequestRunning = false + Timber.d(">>> authvalidity postvalue > getAuthData") authValidity.postValue(false) } } @@ -186,6 +192,7 @@ class MainActivityViewModel @Inject constructor( regenerateFunction(user) } else { Log.d(TAG, "Ask Google user to log in again") + Timber.d(">>> cleared user type") dataStoreModule.clearUserType() } } @@ -196,9 +203,15 @@ class MainActivityViewModel @Inject constructor( fun generateAuthData() { val data = gson.fromJson(authDataJson.value, AuthData::class.java) _authData.value = data - viewModelScope.launch { - authValidity.postValue(isAuthValid(data)) - authRequestRunning = false + } + + fun validateAuthData() { + _authData.value?.let { + viewModelScope.launch { + Timber.d(">>> authvalidity postvalue > validateAuthData") + authValidity.postValue(isAuthValid(it)) + authRequestRunning = false + } } } diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt index 720e48526..30c1156e5 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt @@ -52,7 +52,9 @@ class FusedAPIRepository @Inject constructor( } suspend fun validateAuthData(authData: AuthData): Boolean { - return fusedAPIImpl.validateAuthData(authData) + return authData.authToken.isEmpty() || authData.deviceInfoProvider == null || fusedAPIImpl.validateAuthData( + authData + ) } suspend fun getApplicationDetails( @@ -91,7 +93,10 @@ class FusedAPIRepository @Inject constructor( ) } - suspend fun getCategoriesList(type: Category.Type, authData: AuthData): Triple, String, ResultStatus> { + suspend fun getCategoriesList( + type: Category.Type, + authData: AuthData + ): Triple, String, ResultStatus> { return fusedAPIImpl.getCategoriesList(type, authData) } @@ -107,7 +112,10 @@ class FusedAPIRepository @Inject constructor( return fusedAPIImpl.fetchAuthData(email, aasToken) } - suspend fun getSearchResults(query: String, authData: AuthData): Pair, ResultStatus> { + suspend fun getSearchResults( + query: String, + authData: AuthData + ): Pair, ResultStatus> { return fusedAPIImpl.getSearchResults(query, authData) } diff --git a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt index 254b28a80..475813d77 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt @@ -42,6 +42,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.withContext +import timber.log.Timber import javax.inject.Inject class GPlayAPIImpl @Inject constructor( @@ -65,6 +66,7 @@ class GPlayAPIImpl @Inject constructor( val data = async { tokenRepository.getAuthData() } data.await().let { if (it == null) return@withContext false + Timber.d(">>> auth: gplayapi: fetchAuthData: $it") it.locale = context.resources.configuration.locales[0] // update locale with the default locale from settings dataStoreModule.saveCredentials(it) return@withContext true @@ -83,6 +85,7 @@ class GPlayAPIImpl @Inject constructor( authValidator.isValid() } catch (e: Exception) { e.printStackTrace() + throw e false } } diff --git a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt index b08f430da..73f8c21af 100644 --- a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt +++ b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt @@ -35,6 +35,7 @@ import foundation.e.apps.api.gplay.utils.AC2DMUtil import foundation.e.apps.databinding.FragmentGoogleSigninBinding import foundation.e.apps.setup.signin.SignInViewModel import foundation.e.apps.utils.enums.User +import timber.log.Timber @AndroidEntryPoint class GoogleSignInFragment : Fragment(R.layout.fragment_google_signin) { @@ -88,6 +89,7 @@ class GoogleSignInFragment : Fragment(R.layout.fragment_google_signin) { val email = it.replace("\"".toRegex(), "") viewModel.saveEmailToken(email, oauthToken) viewModel.saveUserType(User.GOOGLE) + Timber.d(">>> saved uer type after google login") } } } -- GitLab From 9439a3543e51b8639e1c5e18d7979593f917b0d8 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 15 Jun 2022 20:38:36 +0600 Subject: [PATCH 2/4] App Lounge: fixed google login issue Fixed: app asks to login for every launch if user logged in with google --- .../main/java/foundation/e/apps/MainActivity.kt | 11 +++++++---- .../foundation/e/apps/MainActivityViewModel.kt | 12 +++++++++--- .../foundation/e/apps/api/gplay/GPlayAPIImpl.kt | 6 ++++-- .../e/apps/api/gplay/token/TokenImpl.kt | 8 +++++++- .../e/apps/settings/SettingsFragment.kt | 15 +++++++++++++-- .../e/apps/setup/signin/SignInViewModel.kt | 6 ++++++ .../e/apps/utils/modules/DataStoreModule.kt | 11 ++++++++++- .../apps/utils/parentFragment/TimeoutFragment.kt | 5 ++++- .../main/res/navigation/navigation_resource.xml | 7 +++++++ 9 files changed, 67 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 32ca72523..e73145ec1 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -118,7 +118,7 @@ class MainActivity : AppCompatActivity() { binding.fragment.visibility = View.VISIBLE viewModel.userType.observe(this) { user -> - Timber.d(">>> auth: usertype") + Timber.d(">>> auth: usertype: $user") generateAuthDataBasedOnUserType(user) } @@ -128,8 +128,8 @@ class MainActivity : AppCompatActivity() { // Watch and refresh authentication data viewModel.authDataJson.observe(this) { - Timber.d(">>> auth: authDataJson: ") - if (!it.isNullOrEmpty()) { + Timber.d(">>> auth: authDataJson: ${it.length}") + if (!it.isNullOrEmpty() && !viewModel.userType.value.isNullOrEmpty() && !viewModel.userType.value.contentEquals(User.UNAVAILABLE.name)) { viewModel.generateAuthData() viewModel.validateAuthData() Log.d(TAG, "Authentication data is available!") @@ -140,6 +140,9 @@ class MainActivity : AppCompatActivity() { viewModel.authValidity.observe(this) { Timber.d(">>> auth: authvalidity: $it") + if(SignInViewModel.isGoogleLoginRunning) { + return@observe + } if (it != true) { Log.d(TAG, "Authentication data validation failed!") viewModel.destroyCredentials { user -> @@ -152,7 +155,7 @@ class MainActivity : AppCompatActivity() { Log.d( TAG, "Displaying timeout from MainActivity on fragment: " + - lastFragment::class.java.name + lastFragment::class.java.name ) lastFragment.onTimeout() } diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index 8597eee0e..d4f8c7ca8 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -124,9 +124,14 @@ class MainActivityViewModel @Inject constructor( * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5404 */ fun retryFetchingTokenAfterTimeout() { - if(userType.value?.contentEquals(User.UNAVAILABLE.name) == true) { - return - } + Timber.d( + ">>> userType: ${userType.value} authDataJson: ${ + authDataJson.value?.length + } authData: ${authData.value?.authToken}" + ) +// if(authDataJson.value.isNullOrEmpty() && authData.value == null) { +// return +// } firstAuthDataFetchTime = 0 setFirstTokenFetchTime() Timber.d(">>> authvalidity postvalue > retryfetching") @@ -202,6 +207,7 @@ class MainActivityViewModel @Inject constructor( fun generateAuthData() { val data = gson.fromJson(authDataJson.value, AuthData::class.java) + Timber.d(">>> generated authdata: ${data.authToken}") _authData.value = data } diff --git a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt index 475813d77..f13a43893 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt @@ -73,8 +73,10 @@ class GPlayAPIImpl @Inject constructor( } } - fun fetchAuthData(email: String, aasToken: String): AuthData { - return tokenRepository.getAuthData(email, aasToken) + suspend fun fetchAuthData(email: String, aasToken: String): AuthData { + val authData = tokenRepository.getAuthData(email, aasToken) + dataStoreModule.saveCredentials(authData) + return authData } suspend fun validateAuthData(authData: AuthData): Boolean { diff --git a/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt b/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt index d392aaff5..9a81158c2 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt @@ -22,6 +22,7 @@ import com.aurora.gplayapi.data.models.AuthData import com.aurora.gplayapi.helpers.AuthHelper import com.google.gson.Gson import foundation.e.apps.api.gplay.utils.GPlayHttpClient +import timber.log.Timber import java.util.Properties import javax.inject.Inject @@ -38,7 +39,12 @@ class TokenImpl @Inject constructor( fun getAuthData(): AuthData? { val playResponse = gPlayHttpClient.postAuth(BASE_URL, gson.toJson(nativeDeviceProperty).toByteArray()) - return gson.fromJson(String(playResponse.responseBytes), AuthData::class.java) + return if(playResponse.isSuccessful) { + gson.fromJson(String(playResponse.responseBytes), AuthData::class.java) + } else { + Timber.d(">>> getAuthData: UNSUCCESSFUL > ${playResponse.errorString}") + null + } } fun getAuthData(email: String, aasToken: String): AuthData { diff --git a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt index 562ec459b..fc58d97b8 100644 --- a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt +++ b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt @@ -20,10 +20,13 @@ package foundation.e.apps.settings import android.content.Intent import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.View import androidx.fragment.app.viewModels import androidx.navigation.findNavController +import androidx.navigation.fragment.findNavController import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.work.ExistingPeriodicWorkPolicy @@ -67,7 +70,13 @@ class SettingsFragment : PreferenceFragmentCompat() { preferenceManager.findPreference(getString(R.string.update_check_intervals)) updateCheckInterval?.setOnPreferenceChangeListener { _, newValue -> Log.d(TAG, "onCreatePreferences: updated Value: $newValue") - context?.let { UpdatesWorkManager.enqueueWork(it, newValue.toString().toLong(), ExistingPeriodicWorkPolicy.REPLACE) } + context?.let { + UpdatesWorkManager.enqueueWork( + it, + newValue.toString().toLong(), + ExistingPeriodicWorkPolicy.REPLACE + ) + } true } @@ -123,7 +132,9 @@ class SettingsFragment : PreferenceFragmentCompat() { binding.logout.setOnClickListener { viewModel.saveUserType(User.UNAVAILABLE) - backToMainActivity() + Handler(Looper.getMainLooper()).postDelayed({ + backToMainActivity() + }, 1500) } } diff --git a/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt b/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt index 8385b519e..19972622e 100644 --- a/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt +++ b/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt @@ -52,13 +52,19 @@ class SignInViewModel @Inject constructor( } } + companion object { + var isGoogleLoginRunning = false + } + fun fetchAuthData() { viewModelScope.launch { + isGoogleLoginRunning = true val email = dataStoreModule.getEmail() val oauthToken = dataStoreModule.getAASToken() if (email.isNotEmpty() && oauthToken.isNotEmpty()) { fetchAuthData(email, oauthToken) } + isGoogleLoginRunning = false } } } diff --git a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt index 5fa284aba..dbe147d87 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt @@ -30,6 +30,7 @@ import foundation.e.apps.utils.enums.User import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking +import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -62,7 +63,9 @@ class DataStoreModule @Inject constructor( */ suspend fun saveCredentials(authData: AuthData) { context.dataStore.edit { - it[AUTHDATA] = gson.toJson(authData) + val authDataJson = gson.toJson(authData) + Timber.d(">>> saving authdata: ${authData.email}") + it[AUTHDATA] = authDataJson } } @@ -112,6 +115,12 @@ class DataStoreModule @Inject constructor( } } + suspend fun saveUserTypeSync(user: User) { + context.dataStore.edit { + it[USERTYPE] = user.name + } + } + fun getAuthDataSync(): String { return runBlocking { authData.first() diff --git a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt index 7900b2380..d240f5525 100644 --- a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt +++ b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt @@ -25,6 +25,7 @@ import androidx.fragment.app.Fragment import com.aurora.gplayapi.data.models.AuthData import foundation.e.apps.MainActivityViewModel import foundation.e.apps.R +import foundation.e.apps.setup.signin.SignInViewModel /* * Parent class (extending fragment) for fragments which can display a timeout dialog @@ -82,7 +83,9 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) { dismissTimeoutDialog() refreshData(authData) } ?: run { - mainActivityViewModel.retryFetchingTokenAfterTimeout() + if (!SignInViewModel.isGoogleLoginRunning) { + mainActivityViewModel.retryFetchingTokenAfterTimeout() + } } } } diff --git a/app/src/main/res/navigation/navigation_resource.xml b/app/src/main/res/navigation/navigation_resource.xml index 2bacd70dc..ad1990ad9 100644 --- a/app/src/main/res/navigation/navigation_resource.xml +++ b/app/src/main/res/navigation/navigation_resource.xml @@ -230,5 +230,12 @@ android:id="@+id/action_global_appPurchaseFragment" app:destination="@id/appPurchaseFragment" /> + + -- GitLab From 83b30adddaf58b0ff8ec3e6ffd87fb1339aae865 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Sat, 18 Jun 2022 18:28:10 +0600 Subject: [PATCH 3/4] App Lounge: Login data flow is updated App Lounge: refactoring login codes --- app/build.gradle | 2 - .../foundation/e/apps/AppLoungeApplication.kt | 4 - .../java/foundation/e/apps/MainActivity.kt | 74 ++-------- .../e/apps/MainActivityViewModel.kt | 129 +++++++++++++++--- .../e/apps/api/fused/FusedAPIImpl.kt | 2 +- .../e/apps/api/fused/FusedAPIRepository.kt | 4 +- .../e/apps/api/gplay/GPlayAPIImpl.kt | 11 +- .../e/apps/api/gplay/GPlayAPIRepository.kt | 2 +- .../e/apps/api/gplay/token/TokenImpl.kt | 4 +- .../e/apps/manager/pkg/InstallerService.kt | 1 - .../e/apps/settings/SettingsFragment.kt | 2 + .../e/apps/setup/signin/SignInViewModel.kt | 38 +----- .../signin/google/GoogleSignInFragment.kt | 2 - .../e/apps/updates/UpdatesViewModel.kt | 3 +- .../e/apps/utils/modules/DataStoreModule.kt | 5 +- .../utils/parentFragment/TimeoutFragment.kt | 3 +- .../res/navigation/navigation_resource.xml | 8 -- 17 files changed, 143 insertions(+), 151 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c05f13918..09d7acdd5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -108,8 +108,6 @@ dependencies { implementation "io.coil-kt:coil:1.4.0" implementation 'com.github.Baseflow:PhotoView:2.3.0' - implementation 'com.jakewharton.timber:timber:5.0.1' - //Protobuf and Gson implementation 'com.google.code.gson:gson:2.8.9' implementation "com.google.protobuf:protobuf-java:3.14.0" diff --git a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt index f617eebb6..f33fe1e91 100644 --- a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt +++ b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt @@ -30,7 +30,6 @@ import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch -import timber.log.Timber import java.util.concurrent.Executors import javax.inject.Inject @@ -61,9 +60,6 @@ class AppLoungeApplication : Application(), Configuration.Provider { dataStoreModule.saveTOCStatus(false, "") } } - if(BuildConfig.DEBUG) { - Timber.plant(Timber.DebugTree()) - } } override fun getWorkManagerConfiguration() = diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index e73145ec1..8f44d8b46 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -44,16 +44,15 @@ import foundation.e.apps.purchase.AppPurchaseFragmentDirections import foundation.e.apps.setup.signin.SignInViewModel import foundation.e.apps.updates.UpdatesNotifier import foundation.e.apps.utils.enums.Status -import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.CommonUtilsModule import foundation.e.apps.utils.parentFragment.TimeoutFragment import kotlinx.coroutines.launch -import timber.log.Timber import java.io.File import java.util.UUID @AndroidEntryPoint class MainActivity : AppCompatActivity() { + private lateinit var signInViewModel: SignInViewModel private lateinit var binding: ActivityMainBinding private val TAG = MainActivity::class.java.simpleName private lateinit var viewModel: MainActivityViewModel @@ -72,7 +71,7 @@ class MainActivity : AppCompatActivity() { var hasInternet = true viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java] - val signInViewModel = ViewModelProvider(this)[SignInViewModel::class.java] + signInViewModel = ViewModelProvider(this)[SignInViewModel::class.java] // navOptions and activityNavController for TOS and SignIn Fragments val navOptions = NavOptions.Builder() @@ -86,31 +85,6 @@ class MainActivity : AppCompatActivity() { } } - fun generateAuthDataBasedOnUserType(user: String) { - if (user.isNotBlank() && viewModel.tocStatus.value == true) { - when (User.valueOf(user)) { - User.ANONYMOUS -> { - if (viewModel.authDataJson.value.isNullOrEmpty() && !viewModel.authRequestRunning) { - Log.d(TAG, "Fetching new authentication data") - viewModel.setFirstTokenFetchTime() - viewModel.getAuthData() - } - } - User.UNAVAILABLE -> { - Timber.d(">>> UNAVAILABLE > destorycredentials") - viewModel.destroyCredentials(null) - } - User.GOOGLE -> { - if (viewModel.authData.value == null && !viewModel.authRequestRunning) { - Log.d(TAG, "Fetching new authentication data") - viewModel.setFirstTokenFetchTime() - signInViewModel.fetchAuthData() - } - } - } - } - } - viewModel.internetConnection.observe(this) { isInternetAvailable -> hasInternet = isInternetAvailable if (isInternetAvailable) { @@ -118,8 +92,7 @@ class MainActivity : AppCompatActivity() { binding.fragment.visibility = View.VISIBLE viewModel.userType.observe(this) { user -> - Timber.d(">>> auth: usertype: $user") - generateAuthDataBasedOnUserType(user) + viewModel.handleAuthDataJson() } signInViewModel.authLiveData.observe(this) { @@ -128,41 +101,24 @@ class MainActivity : AppCompatActivity() { // Watch and refresh authentication data viewModel.authDataJson.observe(this) { - Timber.d(">>> auth: authDataJson: ${it.length}") - if (!it.isNullOrEmpty() && !viewModel.userType.value.isNullOrEmpty() && !viewModel.userType.value.contentEquals(User.UNAVAILABLE.name)) { - viewModel.generateAuthData() - viewModel.validateAuthData() - Log.d(TAG, "Authentication data is available!") - } + viewModel.handleAuthDataJson() } } } viewModel.authValidity.observe(this) { - Timber.d(">>> auth: authvalidity: $it") - if(SignInViewModel.isGoogleLoginRunning) { - return@observe - } - if (it != true) { - Log.d(TAG, "Authentication data validation failed!") - viewModel.destroyCredentials { user -> - if (viewModel.isTimeEligibleForTokenRefresh()) { - generateAuthDataBasedOnUserType(user) - } else { - Log.d(TAG, "Timeout validating auth data!") - val lastFragment = navHostFragment.childFragmentManager.fragments[0] - if (lastFragment is TimeoutFragment) { - Log.d( - TAG, - "Displaying timeout from MainActivity on fragment: " + - lastFragment::class.java.name - ) - lastFragment.onTimeout() - } - } + + viewModel.handleAuthValidity(it) { + Log.d(TAG, "Timeout validating auth data!") + val lastFragment = navHostFragment.childFragmentManager.fragments[0] + if (lastFragment is TimeoutFragment) { + Log.d( + TAG, + "Displaying timeout from MainActivity on fragment: " + + lastFragment::class.java.name + ) + lastFragment.onTimeout() } - } else { - Log.d(TAG, "Authentication data is valid!") } } diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index d4f8c7ca8..a5f15f02d 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -42,6 +42,7 @@ import foundation.e.apps.api.cleanapk.blockedApps.BlockedAppRepository import foundation.e.apps.api.ecloud.EcloudRepository import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp +import foundation.e.apps.api.gplay.utils.AC2DMTask import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.manager.pkg.PkgManagerModule @@ -53,8 +54,8 @@ import foundation.e.apps.utils.modules.CommonUtilsModule.timeoutDurationInMillis import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import ru.beryukhov.reactivenetwork.ReactiveNetwork -import timber.log.Timber import java.io.ByteArrayOutputStream import javax.inject.Inject @@ -67,6 +68,7 @@ class MainActivityViewModel @Inject constructor( private val pkgManagerModule: PkgManagerModule, private val ecloudRepository: EcloudRepository, private val blockedAppRepository: BlockedAppRepository, + private val aC2DMTask: AC2DMTask, ) : ViewModel() { val authDataJson: LiveData = dataStoreModule.authData.asLiveData() @@ -90,6 +92,8 @@ class MainActivityViewModel @Inject constructor( */ var firstAuthDataFetchTime = 0L + var isTokenValidationCompletedOnce = false + // Downloads val downloadList = fusedManagerRepository.getDownloadLiveList() var installInProgress = false @@ -104,15 +108,16 @@ class MainActivityViewModel @Inject constructor( companion object { private const val TAG = "MainActivityViewModel" + private var isGoogleLoginRunning = false } - fun setFirstTokenFetchTime() { + private fun setFirstTokenFetchTime() { if (firstAuthDataFetchTime == 0L) { firstAuthDataFetchTime = SystemClock.uptimeMillis() } } - fun isTimeEligibleForTokenRefresh(): Boolean { + private fun isTimeEligibleForTokenRefresh(): Boolean { return (SystemClock.uptimeMillis() - firstAuthDataFetchTime) <= timeoutDurationInMillis } @@ -124,17 +129,8 @@ class MainActivityViewModel @Inject constructor( * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5404 */ fun retryFetchingTokenAfterTimeout() { - Timber.d( - ">>> userType: ${userType.value} authDataJson: ${ - authDataJson.value?.length - } authData: ${authData.value?.authToken}" - ) -// if(authDataJson.value.isNullOrEmpty() && authData.value == null) { -// return -// } firstAuthDataFetchTime = 0 setFirstTokenFetchTime() - Timber.d(">>> authvalidity postvalue > retryfetching") authValidity.postValue(false) } @@ -165,7 +161,6 @@ class MainActivityViewModel @Inject constructor( */ if (!fusedAPIRepository.fetchAuthData()) { authRequestRunning = false - Timber.d(">>> authvalidity postvalue > getAuthData") authValidity.postValue(false) } } @@ -197,7 +192,6 @@ class MainActivityViewModel @Inject constructor( regenerateFunction(user) } else { Log.d(TAG, "Ask Google user to log in again") - Timber.d(">>> cleared user type") dataStoreModule.clearUserType() } } @@ -206,21 +200,116 @@ class MainActivityViewModel @Inject constructor( } fun generateAuthData() { - val data = gson.fromJson(authDataJson.value, AuthData::class.java) - Timber.d(">>> generated authdata: ${data.authToken}") + val data = jsonToAuthData() _authData.value = data } + private fun jsonToAuthData() = gson.fromJson(authDataJson.value, AuthData::class.java) + fun validateAuthData() { - _authData.value?.let { - viewModelScope.launch { - Timber.d(">>> authvalidity postvalue > validateAuthData") - authValidity.postValue(isAuthValid(it)) + viewModelScope.launch { + jsonToAuthData()?.let { + val isAuthValid = isAuthValid(it) + authValidity.postValue(isAuthValid) authRequestRunning = false } } } + fun handleAuthDataJson() { + val user = userType.value + val json = authDataJson.value + + if (user == null || json == null) { + return + } + + if (isUserNOTLoggedIn(user, json)) { + generateAuthDataBasedOnUserType(user) + } else if (isEligibleToValidateJson(json)) { + validateAuthData() + Log.d(TAG, ">>> Authentication data is available!") + } + } + + private fun isUserNOTLoggedIn(user: String, json: String) = + !user.isNullOrEmpty() && !user.contentEquals(User.UNAVAILABLE.name) && json.isEmpty() + + private fun isEligibleToValidateJson(authDataJson: String?) = + !authDataJson.isNullOrEmpty() && !userType.value.isNullOrEmpty() && !userType.value.contentEquals( + User.UNAVAILABLE.name + ) + + fun handleAuthValidity(isValid: Boolean, handleTimeoOut: () -> Unit) { + if (isGoogleLoginRunning) { + return + } + isTokenValidationCompletedOnce = true + if (!isValid) { + Log.d(TAG, ">>> Authentication data validation failed!") + destroyCredentials { user -> + if (isTimeEligibleForTokenRefresh()) { + generateAuthDataBasedOnUserType(user) + } else { + handleTimeoOut() + } + } + } else { + Log.d(TAG, "Authentication data is valid!") + generateAuthData() + } + } + + private fun generateAuthDataBasedOnUserType(user: String) { + if (user.isNotBlank() && tocStatus.value == true && !isGoogleLoginRunning) { + when (User.valueOf(user)) { + User.ANONYMOUS -> { + if (authDataJson.value.isNullOrEmpty() && !authRequestRunning) { + Log.d(TAG, ">>> Fetching new authentication data") + setFirstTokenFetchTime() + getAuthData() + } + } + User.UNAVAILABLE -> { + destroyCredentials(null) + } + User.GOOGLE -> { + if (authData.value == null && !authRequestRunning) { + Log.d(TAG, ">>> Fetching new authentication data") + setFirstTokenFetchTime() + fetchAuthData() + } + } + } + } + } + + private suspend fun fetchAuthData(email: String, oauthToken: String) { + var responseMap: Map + withContext(Dispatchers.IO) { + val response = aC2DMTask.getAC2DMResponse(email, oauthToken) + responseMap = response + responseMap["Token"]?.let { + if (fusedAPIRepository.fetchAuthData(email, it) == null) { + dataStoreModule.clearUserType() + _errorMessageStringResource.value = R.string.unknown_error + } + } + } + } + + private fun fetchAuthData() { + viewModelScope.launch { + isGoogleLoginRunning = true + val email = dataStoreModule.getEmail() + val oauthToken = dataStoreModule.getAASToken() + if (email.isNotEmpty() && oauthToken.isNotEmpty()) { + fetchAuthData(email, oauthToken) + } + isGoogleLoginRunning = false + } + } + private suspend fun isAuthValid(authData: AuthData): Boolean { return fusedAPIRepository.validateAuthData(authData) } diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt index dc31a00e8..3cabf463c 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt @@ -241,7 +241,7 @@ class FusedAPIImpl @Inject constructor( return gPlayAPIRepository.fetchAuthData() } - suspend fun fetchAuthData(email: String, aasToken: String): AuthData { + suspend fun fetchAuthData(email: String, aasToken: String): AuthData? { return gPlayAPIRepository.fetchAuthData(email, aasToken) } diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt index 30c1156e5..adb1501d8 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt @@ -52,7 +52,7 @@ class FusedAPIRepository @Inject constructor( } suspend fun validateAuthData(authData: AuthData): Boolean { - return authData.authToken.isEmpty() || authData.deviceInfoProvider == null || fusedAPIImpl.validateAuthData( + return authData.authToken.isNotEmpty() && authData.deviceInfoProvider != null && fusedAPIImpl.validateAuthData( authData ) } @@ -108,7 +108,7 @@ class FusedAPIRepository @Inject constructor( return fusedAPIImpl.fetchAuthData() } - suspend fun fetchAuthData(email: String, aasToken: String): AuthData { + suspend fun fetchAuthData(email: String, aasToken: String): AuthData? { return fusedAPIImpl.fetchAuthData(email, aasToken) } diff --git a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt index f13a43893..2c74e4060 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIImpl.kt @@ -42,7 +42,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.withContext -import timber.log.Timber import javax.inject.Inject class GPlayAPIImpl @Inject constructor( @@ -66,17 +65,19 @@ class GPlayAPIImpl @Inject constructor( val data = async { tokenRepository.getAuthData() } data.await().let { if (it == null) return@withContext false - Timber.d(">>> auth: gplayapi: fetchAuthData: $it") it.locale = context.resources.configuration.locales[0] // update locale with the default locale from settings dataStoreModule.saveCredentials(it) return@withContext true } } - suspend fun fetchAuthData(email: String, aasToken: String): AuthData { + suspend fun fetchAuthData(email: String, aasToken: String): AuthData? { val authData = tokenRepository.getAuthData(email, aasToken) - dataStoreModule.saveCredentials(authData) - return authData + if (authData.authToken.isNotEmpty() && authData.deviceInfoProvider != null) { + dataStoreModule.saveCredentials(authData) + return authData + } + return null } suspend fun validateAuthData(authData: AuthData): Boolean { diff --git a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIRepository.kt b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIRepository.kt index 737f206a1..88c9fb610 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIRepository.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/GPlayAPIRepository.kt @@ -36,7 +36,7 @@ class GPlayAPIRepository @Inject constructor( return gPlayAPIImpl.fetchAuthData() } - suspend fun fetchAuthData(email: String, aasToken: String): AuthData { + suspend fun fetchAuthData(email: String, aasToken: String): AuthData? { return gPlayAPIImpl.fetchAuthData(email, aasToken) } diff --git a/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt b/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt index 9a81158c2..e9578de02 100644 --- a/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/gplay/token/TokenImpl.kt @@ -22,7 +22,6 @@ import com.aurora.gplayapi.data.models.AuthData import com.aurora.gplayapi.helpers.AuthHelper import com.google.gson.Gson import foundation.e.apps.api.gplay.utils.GPlayHttpClient -import timber.log.Timber import java.util.Properties import javax.inject.Inject @@ -39,10 +38,9 @@ class TokenImpl @Inject constructor( fun getAuthData(): AuthData? { val playResponse = gPlayHttpClient.postAuth(BASE_URL, gson.toJson(nativeDeviceProperty).toByteArray()) - return if(playResponse.isSuccessful) { + return if (playResponse.isSuccessful) { gson.fromJson(String(playResponse.responseBytes), AuthData::class.java) } else { - Timber.d(">>> getAuthData: UNSUCCESSFUL > ${playResponse.errorString}") null } } diff --git a/app/src/main/java/foundation/e/apps/manager/pkg/InstallerService.kt b/app/src/main/java/foundation/e/apps/manager/pkg/InstallerService.kt index 1ec727682..c275790e8 100644 --- a/app/src/main/java/foundation/e/apps/manager/pkg/InstallerService.kt +++ b/app/src/main/java/foundation/e/apps/manager/pkg/InstallerService.kt @@ -27,7 +27,6 @@ import android.util.Log import androidx.annotation.RequiresApi import dagger.hilt.android.AndroidEntryPoint import foundation.e.apps.manager.fused.FusedManagerRepository -import foundation.e.apps.utils.enums.Status import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import javax.inject.Inject diff --git a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt index fc58d97b8..e20d70942 100644 --- a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt +++ b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt @@ -24,6 +24,7 @@ import android.os.Handler import android.os.Looper import android.util.Log import android.view.View +import android.widget.Toast import androidx.fragment.app.viewModels import androidx.navigation.findNavController import androidx.navigation.fragment.findNavController @@ -132,6 +133,7 @@ class SettingsFragment : PreferenceFragmentCompat() { binding.logout.setOnClickListener { viewModel.saveUserType(User.UNAVAILABLE) + Toast.makeText(requireContext(), "Signin out...", Toast.LENGTH_LONG).show() Handler(Looper.getMainLooper()).postDelayed({ backToMainActivity() }, 1500) diff --git a/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt b/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt index 19972622e..ee0e1ea91 100644 --- a/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt +++ b/app/src/main/java/foundation/e/apps/setup/signin/SignInViewModel.kt @@ -7,20 +7,14 @@ import androidx.lifecycle.asLiveData import androidx.lifecycle.viewModelScope import com.aurora.gplayapi.data.models.AuthData import dagger.hilt.android.lifecycle.HiltViewModel -import foundation.e.apps.api.fused.FusedAPIRepository -import foundation.e.apps.api.gplay.utils.AC2DMTask import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.DataStoreModule -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import javax.inject.Inject @HiltViewModel class SignInViewModel @Inject constructor( private val dataStoreModule: DataStoreModule, - private val aC2DMTask: AC2DMTask, - private val fusedAPIRepository: FusedAPIRepository ) : ViewModel() { val userType: LiveData = dataStoreModule.userType.asLiveData() @@ -30,6 +24,9 @@ class SignInViewModel @Inject constructor( fun saveUserType(user: User) { viewModelScope.launch { dataStoreModule.saveUserType(user) + if (user == User.UNAVAILABLE) { + dataStoreModule.destroyCredentials() + } } } @@ -38,33 +35,4 @@ class SignInViewModel @Inject constructor( dataStoreModule.saveEmail(email, token) } } - - private suspend fun fetchAuthData(email: String, oauthToken: String) { - var responseMap: Map - withContext(Dispatchers.IO) { - val response = aC2DMTask.getAC2DMResponse(email, oauthToken) - responseMap = response - responseMap["Token"]?.let { - val value = fusedAPIRepository.fetchAuthData(email, it) - _authLiveData.postValue(value) - dataStoreModule.saveCredentials(value) - } - } - } - - companion object { - var isGoogleLoginRunning = false - } - - fun fetchAuthData() { - viewModelScope.launch { - isGoogleLoginRunning = true - val email = dataStoreModule.getEmail() - val oauthToken = dataStoreModule.getAASToken() - if (email.isNotEmpty() && oauthToken.isNotEmpty()) { - fetchAuthData(email, oauthToken) - } - isGoogleLoginRunning = false - } - } } diff --git a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt index 73f8c21af..b08f430da 100644 --- a/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt +++ b/app/src/main/java/foundation/e/apps/setup/signin/google/GoogleSignInFragment.kt @@ -35,7 +35,6 @@ import foundation.e.apps.api.gplay.utils.AC2DMUtil import foundation.e.apps.databinding.FragmentGoogleSigninBinding import foundation.e.apps.setup.signin.SignInViewModel import foundation.e.apps.utils.enums.User -import timber.log.Timber @AndroidEntryPoint class GoogleSignInFragment : Fragment(R.layout.fragment_google_signin) { @@ -89,7 +88,6 @@ class GoogleSignInFragment : Fragment(R.layout.fragment_google_signin) { val email = it.replace("\"".toRegex(), "") viewModel.saveEmailToken(email, oauthToken) viewModel.saveUserType(User.GOOGLE) - Timber.d(">>> saved uer type after google login") } } } diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt index 60d513d2a..f630fda7a 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt @@ -18,7 +18,6 @@ package foundation.e.apps.updates -import android.util.Log import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -28,8 +27,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.updates.manager.UpdatesManagerRepository -import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.ResultStatus +import foundation.e.apps.utils.enums.Status import kotlinx.coroutines.launch import javax.inject.Inject diff --git a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt index dbe147d87..a85d03365 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreModule.kt @@ -30,7 +30,6 @@ import foundation.e.apps.utils.enums.User import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking -import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -63,9 +62,7 @@ class DataStoreModule @Inject constructor( */ suspend fun saveCredentials(authData: AuthData) { context.dataStore.edit { - val authDataJson = gson.toJson(authData) - Timber.d(">>> saving authdata: ${authData.email}") - it[AUTHDATA] = authDataJson + it[AUTHDATA] = gson.toJson(authData) } } diff --git a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt index d240f5525..959dc2efb 100644 --- a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt +++ b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt @@ -25,7 +25,6 @@ import androidx.fragment.app.Fragment import com.aurora.gplayapi.data.models.AuthData import foundation.e.apps.MainActivityViewModel import foundation.e.apps.R -import foundation.e.apps.setup.signin.SignInViewModel /* * Parent class (extending fragment) for fragments which can display a timeout dialog @@ -83,7 +82,7 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) { dismissTimeoutDialog() refreshData(authData) } ?: run { - if (!SignInViewModel.isGoogleLoginRunning) { + if (mainActivityViewModel.authValidity.value != null) { // checking at least authvalidity is checked for once mainActivityViewModel.retryFetchingTokenAfterTimeout() } } diff --git a/app/src/main/res/navigation/navigation_resource.xml b/app/src/main/res/navigation/navigation_resource.xml index ad1990ad9..7f5b1045e 100644 --- a/app/src/main/res/navigation/navigation_resource.xml +++ b/app/src/main/res/navigation/navigation_resource.xml @@ -230,12 +230,4 @@ android:id="@+id/action_global_appPurchaseFragment" app:destination="@id/appPurchaseFragment" /> - - - -- GitLab From e33b06cf8a2a10537f9623e6c199f429ca0dca16 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Mon, 20 Jun 2022 13:04:14 +0600 Subject: [PATCH 4/4] App Lounge: fixed lint issues App Lounge: some code refactoring --- .../e/apps/MainActivityViewModel.kt | 67 ++++++++++--------- .../e/apps/api/fused/FusedAPIImpl.kt | 1 - .../e/apps/search/SearchFragment.kt | 1 - .../e/apps/settings/SettingsFragment.kt | 2 +- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index a5f15f02d..b66a7d49d 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -224,7 +224,7 @@ class MainActivityViewModel @Inject constructor( return } - if (isUserNOTLoggedIn(user, json)) { + if (!isUserLoggedIn(user, json)) { generateAuthDataBasedOnUserType(user) } else if (isEligibleToValidateJson(json)) { validateAuthData() @@ -232,8 +232,8 @@ class MainActivityViewModel @Inject constructor( } } - private fun isUserNOTLoggedIn(user: String, json: String) = - !user.isNullOrEmpty() && !user.contentEquals(User.UNAVAILABLE.name) && json.isEmpty() + private fun isUserLoggedIn(user: String, json: String) = + user.isNotEmpty() && !user.contentEquals(User.UNAVAILABLE.name) && json.isNotEmpty() private fun isEligibleToValidateJson(authDataJson: String?) = !authDataJson.isNullOrEmpty() && !userType.value.isNullOrEmpty() && !userType.value.contentEquals( @@ -245,46 +245,47 @@ class MainActivityViewModel @Inject constructor( return } isTokenValidationCompletedOnce = true - if (!isValid) { - Log.d(TAG, ">>> Authentication data validation failed!") - destroyCredentials { user -> - if (isTimeEligibleForTokenRefresh()) { - generateAuthDataBasedOnUserType(user) - } else { - handleTimeoOut() - } - } - } else { + if (isValid) { Log.d(TAG, "Authentication data is valid!") generateAuthData() + return + } + Log.d(TAG, ">>> Authentication data validation failed!") + destroyCredentials { user -> + if (isTimeEligibleForTokenRefresh()) { + generateAuthDataBasedOnUserType(user) + } else { + handleTimeoOut() + } } } private fun generateAuthDataBasedOnUserType(user: String) { - if (user.isNotBlank() && tocStatus.value == true && !isGoogleLoginRunning) { - when (User.valueOf(user)) { - User.ANONYMOUS -> { - if (authDataJson.value.isNullOrEmpty() && !authRequestRunning) { - Log.d(TAG, ">>> Fetching new authentication data") - setFirstTokenFetchTime() - getAuthData() - } - } - User.UNAVAILABLE -> { - destroyCredentials(null) + if (user.isEmpty() || tocStatus.value == false || isGoogleLoginRunning) { + return + } + when (User.valueOf(user)) { + User.ANONYMOUS -> { + if (authDataJson.value.isNullOrEmpty() && !authRequestRunning) { + Log.d(TAG, ">>> Fetching new authentication data") + setFirstTokenFetchTime() + getAuthData() } - User.GOOGLE -> { - if (authData.value == null && !authRequestRunning) { - Log.d(TAG, ">>> Fetching new authentication data") - setFirstTokenFetchTime() - fetchAuthData() - } + } + User.UNAVAILABLE -> { + destroyCredentials(null) + } + User.GOOGLE -> { + if (authData.value == null && !authRequestRunning) { + Log.d(TAG, ">>> Fetching new authentication data") + setFirstTokenFetchTime() + doFetchAuthData() } } } } - private suspend fun fetchAuthData(email: String, oauthToken: String) { + private suspend fun doFetchAuthData(email: String, oauthToken: String) { var responseMap: Map withContext(Dispatchers.IO) { val response = aC2DMTask.getAC2DMResponse(email, oauthToken) @@ -298,13 +299,13 @@ class MainActivityViewModel @Inject constructor( } } - private fun fetchAuthData() { + private fun doFetchAuthData() { viewModelScope.launch { isGoogleLoginRunning = true val email = dataStoreModule.getEmail() val oauthToken = dataStoreModule.getAASToken() if (email.isNotEmpty() && oauthToken.isNotEmpty()) { - fetchAuthData(email, oauthToken) + doFetchAuthData(email, oauthToken) } isGoogleLoginRunning = false } diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt index b33446b89..409dc93eb 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt @@ -983,7 +983,6 @@ class FusedAPIImpl @Inject constructor( it.first.map { app -> app.transformToFusedApp() }, it.second ) - } } diff --git a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt index 2c3c305e3..9089b59d4 100644 --- a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt @@ -52,7 +52,6 @@ import foundation.e.apps.applicationlist.model.ApplicationListRVAdapter import foundation.e.apps.databinding.FragmentSearchBinding import foundation.e.apps.manager.download.data.DownloadProgress import foundation.e.apps.manager.pkg.PkgManagerModule -import foundation.e.apps.utils.enums.ResultStatus import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.PWAManagerModule diff --git a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt index e20d70942..5db6b8b76 100644 --- a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt +++ b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt @@ -133,7 +133,7 @@ class SettingsFragment : PreferenceFragmentCompat() { binding.logout.setOnClickListener { viewModel.saveUserType(User.UNAVAILABLE) - Toast.makeText(requireContext(), "Signin out...", Toast.LENGTH_LONG).show() + Toast.makeText(requireContext(), "Signing out...", Toast.LENGTH_LONG).show() Handler(Looper.getMainLooper()).postDelayed({ backToMainActivity() }, 1500) -- GitLab