From 9379f292cf6549b4ae59576a80b436745d14aaa3 Mon Sep 17 00:00:00 2001 From: hasibprince Date: Sat, 28 Jan 2023 00:48:35 +0600 Subject: [PATCH 1/4] fixed: auto update is replaced by manual update --- .../e/apps/settings/SettingsFragment.kt | 2 +- .../e/apps/updates/UpdatesFragment.kt | 16 +++++++++--- .../e/apps/updates/UpdatesViewModel.kt | 6 ++++- .../manager/UpdatesBroadcastReceiver.kt | 13 +++------- .../updates/manager/UpdatesWorkManager.kt | 26 ++----------------- .../utils/modules/PreferenceManagerModule.kt | 6 +++++ 6 files changed, 31 insertions(+), 38 deletions(-) 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 f6ed13a07..2642d20f3 100644 --- a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt +++ b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt @@ -97,7 +97,7 @@ class SettingsFragment : PreferenceFragmentCompat() { UpdatesWorkManager.enqueueWork( it, newValue.toString().toLong(), - ExistingPeriodicWorkPolicy.KEEP + ExistingPeriodicWorkPolicy.REPLACE ) } true diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt index 80273b8c8..1db3ac3a1 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -30,6 +30,7 @@ import androidx.navigation.findNavController import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.WorkInfo import androidx.work.WorkManager import dagger.hilt.android.AndroidEntryPoint @@ -181,7 +182,9 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte ( workInfoList.isNullOrEmpty() || ( - !updatesViewModel.checkWorkInfoListHasAnyUpdatableWork(workInfoList) && + !updatesViewModel.checkWorkInfoListHasAnyUpdatableWork( + workInfoList + ) && updatesViewModel.hasAnyUpdatableApp() ) ) @@ -275,7 +278,12 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte true } binding.button.setOnClickListener { - UpdatesWorkManager.startUpdateAllWork(requireContext().applicationContext) + val interval = updatesViewModel.getUpdateInterval() + UpdatesWorkManager.enqueueWork( + requireContext(), + interval, + ExistingPeriodicWorkPolicy.REPLACE + ) observeUpdateWork() binding.button.isEnabled = false } @@ -283,7 +291,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte private fun observeUpdateWork() { WorkManager.getInstance(requireContext()) - .getWorkInfosByTagLiveData(UpdatesWorkManager.UPDATES_WORK_NAME) + .getWorkInfosByTagLiveData(UpdatesWorkManager.TAG) .observe(viewLifecycleOwner) { binding.button.isEnabled = hasAnyPendingUpdates(it) } @@ -357,6 +365,8 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte } override fun onDestroyView() { + mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner) + isDownloadObserverAdded = false super.onDestroyView() _binding = null } 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 d576b6ae8..d66ca5ace 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesViewModel.kt @@ -31,6 +31,7 @@ import foundation.e.apps.utils.enums.ResultStatus import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.exceptions.CleanApkException import foundation.e.apps.utils.exceptions.GPlayException +import foundation.e.apps.utils.modules.PreferenceManagerModule import foundation.e.apps.utils.parentFragment.LoadingViewModel import kotlinx.coroutines.launch import javax.inject.Inject @@ -38,7 +39,8 @@ import javax.inject.Inject @HiltViewModel class UpdatesViewModel @Inject constructor( private val updatesManagerRepository: UpdatesManagerRepository, - private val fusedAPIRepository: FusedAPIRepository + private val fusedAPIRepository: FusedAPIRepository, + private val preferenceManagerModule: PreferenceManagerModule ) : LoadingViewModel() { val updatesList: MutableLiveData, ResultStatus?>> = MutableLiveData() @@ -133,4 +135,6 @@ class UpdatesViewModel @Inject constructor( ) return updatesList.value?.first?.any { pendingStatesForUpdate.contains(it.status) } == true } + + fun getUpdateInterval() = preferenceManagerModule.getUpdateInterval() } diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt index cb4dff421..bfc8e3b51 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt @@ -20,9 +20,8 @@ package foundation.e.apps.updates.manager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import androidx.preference.PreferenceManager import androidx.work.ExistingPeriodicWorkPolicy -import foundation.e.apps.R +import foundation.e.apps.utils.modules.PreferenceManagerModule import timber.log.Timber class UpdatesBroadcastReceiver : BroadcastReceiver() { @@ -33,13 +32,9 @@ class UpdatesBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Timber.d("onReceive: ${intent.action}") if (intent.action == Intent.ACTION_BOOT_COMPLETED) { - val preferences = PreferenceManager.getDefaultSharedPreferences(context) - val interval = - preferences.getString( - context.getString(R.string.update_check_intervals), - context.getString(R.string.preference_update_interval_default) - )!!.toLong() - UpdatesWorkManager.enqueueWork(context, interval, ExistingPeriodicWorkPolicy.KEEP) + val preferenceManagerModule = PreferenceManagerModule(context) + val interval = preferenceManagerModule.getUpdateInterval() + UpdatesWorkManager.enqueueWork(context, interval, ExistingPeriodicWorkPolicy.REPLACE) } } } diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt index f237e51c6..85e073946 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt @@ -20,29 +20,15 @@ package foundation.e.apps.updates.manager import android.content.Context import android.util.Log import androidx.work.Constraints -import androidx.work.Data import androidx.work.ExistingPeriodicWorkPolicy -import androidx.work.ExistingWorkPolicy import androidx.work.NetworkType -import androidx.work.OneTimeWorkRequest import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager -import java.util.UUID import java.util.concurrent.TimeUnit object UpdatesWorkManager { - const val UPDATES_WORK_NAME = "updates_work" - private const val TAG = "UpdatesManager" - - fun startUpdateAllWork(context: Context): UUID { - val oneTimeWorkRequest = buildOneTimeWorkRequest() - WorkManager.getInstance(context).enqueueUniqueWork( - UPDATES_WORK_NAME, - ExistingWorkPolicy.REPLACE, - buildOneTimeWorkRequest() - ) - return oneTimeWorkRequest.id - } + private const val UPDATES_WORK_NAME = "updates_work" + const val TAG = "UpdatesWorkTag" private fun buildWorkerConstraints() = Constraints.Builder().apply { setRequiresBatteryNotLow(true) @@ -60,14 +46,6 @@ object UpdatesWorkManager { }.build() } - private fun buildOneTimeWorkRequest(): OneTimeWorkRequest { - return OneTimeWorkRequest.Builder(UpdatesWorker::class.java).apply { - setConstraints(buildWorkerConstraints()) - addTag(UPDATES_WORK_NAME) - }.setInputData(Data.Builder().putBoolean(UpdatesWorker.IS_AUTO_UPDATE, false).build()) - .build() - } - fun enqueueWork( context: Context, interval: Long, diff --git a/app/src/main/java/foundation/e/apps/utils/modules/PreferenceManagerModule.kt b/app/src/main/java/foundation/e/apps/utils/modules/PreferenceManagerModule.kt index 0fa6cf9c9..ab738f396 100644 --- a/app/src/main/java/foundation/e/apps/utils/modules/PreferenceManagerModule.kt +++ b/app/src/main/java/foundation/e/apps/utils/modules/PreferenceManagerModule.kt @@ -22,6 +22,7 @@ import android.content.Context import androidx.preference.PreferenceManager import dagger.hilt.android.qualifiers.ApplicationContext import foundation.e.apps.OpenForTesting +import foundation.e.apps.R import foundation.e.apps.utils.Constants.PREFERENCE_SHOW_FOSS import foundation.e.apps.utils.Constants.PREFERENCE_SHOW_GPLAY import foundation.e.apps.utils.Constants.PREFERENCE_SHOW_PWA @@ -54,4 +55,9 @@ class PreferenceManagerModule @Inject constructor( fun autoUpdatePreferred(): Boolean { return preferenceManager.getBoolean("updateInstallAuto", false) } + + fun getUpdateInterval() = preferenceManager.getString( + context.getString(R.string.update_check_intervals), + context.getString(R.string.preference_update_interval_default) + )!!.toLong() } -- GitLab From 3a09289547aced66881aafe05dd494fe11b501e3 Mon Sep 17 00:00:00 2001 From: hasibprince Date: Sat, 28 Jan 2023 01:41:27 +0600 Subject: [PATCH 2/4] fixed: update all button update all button didn't work after coming back from app detail page --- .../main/java/foundation/e/apps/updates/UpdatesFragment.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt index 1db3ac3a1..22d7fd203 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -160,6 +160,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte private fun handleStateNoUpdates(list: List?) { if (!list.isNullOrEmpty()) { binding.button.isEnabled = true + initUpdataAllButton() binding.noUpdates.visibility = View.GONE } else { binding.noUpdates.visibility = View.VISIBLE @@ -277,6 +278,10 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte clearAndRestartGPlayLogin() true } + initUpdataAllButton() + } + + private fun initUpdataAllButton() { binding.button.setOnClickListener { val interval = updatesViewModel.getUpdateInterval() UpdatesWorkManager.enqueueWork( -- GitLab From 8b0664c9c971e3908ef4ac48ecf112593e2b9847 Mon Sep 17 00:00:00 2001 From: hasibprince Date: Sat, 28 Jan 2023 21:48:36 +0600 Subject: [PATCH 3/4] fixed: auto update for settings of disable auto install --- .../e/apps/updates/UpdatesFragment.kt | 7 +------ .../updates/manager/UpdatesWorkManager.kt | 21 +++++++++++++++++++ .../e/apps/updates/manager/UpdatesWorker.kt | 20 ++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt index 22d7fd203..297d2a5ca 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -30,7 +30,6 @@ import androidx.navigation.findNavController import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.WorkInfo import androidx.work.WorkManager import dagger.hilt.android.AndroidEntryPoint @@ -284,11 +283,7 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte private fun initUpdataAllButton() { binding.button.setOnClickListener { val interval = updatesViewModel.getUpdateInterval() - UpdatesWorkManager.enqueueWork( - requireContext(), - interval, - ExistingPeriodicWorkPolicy.REPLACE - ) + UpdatesWorkManager.startUpdateAllWork(requireContext()) observeUpdateWork() binding.button.isEnabled = false } diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt index 85e073946..b3cdd04d6 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt @@ -20,15 +20,36 @@ package foundation.e.apps.updates.manager import android.content.Context import android.util.Log import androidx.work.Constraints +import androidx.work.Data import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.ExistingWorkPolicy import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequest import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager import java.util.concurrent.TimeUnit object UpdatesWorkManager { private const val UPDATES_WORK_NAME = "updates_work" + private const val UPDATES_WORK_USER_NAME = "updates_work_user" const val TAG = "UpdatesWorkTag" + const val USER_TAG = "UpdatesWorkUserTag" + + fun startUpdateAllWork(context: Context) { + WorkManager.getInstance(context).enqueueUniqueWork( + UPDATES_WORK_USER_NAME, + ExistingWorkPolicy.REPLACE, + buildOneTimeWorkRequest() + ) + } + + private fun buildOneTimeWorkRequest(): OneTimeWorkRequest { + return OneTimeWorkRequest.Builder(UpdatesWorker::class.java).apply { + setConstraints(buildWorkerConstraints()) + addTag(USER_TAG) + }.setInputData(Data.Builder().putBoolean(UpdatesWorker.IS_AUTO_UPDATE, false).build()) + .build() + } private fun buildWorkerConstraints() = Constraints.Builder().apply { setRequiresBatteryNotLow(true) diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt index 9d0bec750..f69db619f 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt @@ -11,7 +11,10 @@ import android.util.Base64 import androidx.hilt.work.HiltWorker import androidx.preference.PreferenceManager import androidx.work.CoroutineWorker +import androidx.work.WorkInfo +import androidx.work.WorkManager import androidx.work.WorkerParameters +import androidx.work.await import com.aurora.gplayapi.data.models.AuthData import com.google.gson.Gson import dagger.assisted.Assisted @@ -64,6 +67,10 @@ class UpdatesWorker @AssistedInject constructor( override suspend fun doWork(): Result { return try { isAutoUpdate = params.inputData.getBoolean(IS_AUTO_UPDATE, true) + if (isAutoUpdate && checkManualUpdateRunning()) { + return Result.success() + } + checkForUpdates() Result.success() } catch (e: Throwable) { @@ -76,6 +83,19 @@ class UpdatesWorker @AssistedInject constructor( } } + private suspend fun checkManualUpdateRunning(): Boolean { + val workInfos = + WorkManager.getInstance(context).getWorkInfosByTag(UpdatesWorkManager.USER_TAG).await() + if (workInfos.isNotEmpty()) { + val workInfo = workInfos[0] + Timber.d("Manual update status: workInfo.state=${workInfo.state}, id=${workInfo.id}") + if (workInfo.state == WorkInfo.State.BLOCKED || workInfo.state == WorkInfo.State.ENQUEUED || workInfo.state == WorkInfo.State.RUNNING) { + return true + } + } + return false + } + private fun getUser(): User { return dataStoreManager.getUserType() } -- GitLab From 9a640ab57f02dd59144f251b50553d4e1e706914 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Mon, 30 Jan 2023 08:39:30 +0000 Subject: [PATCH 4/4] minor refactoring --- .../foundation/e/apps/updates/manager/UpdatesWorker.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt index f69db619f..601f7c299 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt @@ -11,7 +11,7 @@ import android.util.Base64 import androidx.hilt.work.HiltWorker import androidx.preference.PreferenceManager import androidx.work.CoroutineWorker -import androidx.work.WorkInfo +import androidx.work.WorkInfo.State import androidx.work.WorkManager import androidx.work.WorkerParameters import androidx.work.await @@ -89,8 +89,9 @@ class UpdatesWorker @AssistedInject constructor( if (workInfos.isNotEmpty()) { val workInfo = workInfos[0] Timber.d("Manual update status: workInfo.state=${workInfo.state}, id=${workInfo.id}") - if (workInfo.state == WorkInfo.State.BLOCKED || workInfo.state == WorkInfo.State.ENQUEUED || workInfo.state == WorkInfo.State.RUNNING) { - return true + return when (workInfo.state) { + State.BLOCKED, State.ENQUEUED, State.RUNNING -> true + else -> false } } return false -- GitLab