From b27ee1e9bf62933241771d686ced8a7774e58679 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 11 May 2022 22:00:43 +0600 Subject: [PATCH 1/2] App Lounge: fixed action for Update all --- app/src/main/AndroidManifest.xml | 2 +- .../e/apps/settings/SettingsFragment.kt | 4 +- .../e/apps/updates/UpdatesFragment.kt | 5 +- .../manager/UpdatesBroadcastReceiver.kt | 45 +++++++++++++++++ ...pdatesManager.kt => UpdatesWorkManager.kt} | 50 ++++++++----------- 5 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt rename app/src/main/java/foundation/e/apps/updates/manager/{UpdatesManager.kt => UpdatesWorkManager.kt} (57%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 78fba17b6..05e9d66c5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -63,7 +63,7 @@ - + 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 e262b6e06..562ec459b 100644 --- a/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt +++ b/app/src/main/java/foundation/e/apps/settings/SettingsFragment.kt @@ -36,7 +36,7 @@ import foundation.e.apps.MainActivityViewModel import foundation.e.apps.R import foundation.e.apps.databinding.CustomPreferenceBinding import foundation.e.apps.setup.signin.SignInViewModel -import foundation.e.apps.updates.manager.UpdatesManager +import foundation.e.apps.updates.manager.UpdatesWorkManager import foundation.e.apps.utils.enums.User import javax.inject.Inject @@ -67,7 +67,7 @@ class SettingsFragment : PreferenceFragmentCompat() { preferenceManager.findPreference(getString(R.string.update_check_intervals)) updateCheckInterval?.setOnPreferenceChangeListener { _, newValue -> Log.d(TAG, "onCreatePreferences: updated Value: $newValue") - context?.let { UpdatesManager().enqueueWork(it, newValue.toString().toLong(), ExistingPeriodicWorkPolicy.REPLACE) } + context?.let { UpdatesWorkManager.enqueueWork(it, newValue.toString().toLong(), 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 cd6116e1e..eb03b02d9 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -41,6 +41,7 @@ import foundation.e.apps.applicationlist.model.ApplicationListRVAdapter import foundation.e.apps.databinding.FragmentUpdatesBinding import foundation.e.apps.manager.download.data.DownloadProgress import foundation.e.apps.manager.pkg.PkgManagerModule +import foundation.e.apps.updates.manager.UpdatesWorkManager import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.PWAManagerModule @@ -78,9 +79,7 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface { if (hasInternet) { updatesViewModel.getUpdates(data) binding.button.setOnClickListener { - updatesViewModel.updatesList.value?.forEach { app -> - getApplication(app) - } + UpdatesWorkManager.startUpdateAllWork(requireContext().applicationContext) } } } 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 new file mode 100644 index 000000000..8cd60f931 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesBroadcastReceiver.kt @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2019-2022 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.updates.manager + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.util.Log +import androidx.preference.PreferenceManager +import androidx.work.* +import foundation.e.apps.R + +class UpdatesBroadcastReceiver : BroadcastReceiver() { + companion object { + private const val TAG = "UpdatesManager" + } + + override fun onReceive(context: Context, intent: Intent) { + Log.d(TAG, "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) + } + } +} diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesManager.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt similarity index 57% rename from app/src/main/java/foundation/e/apps/updates/manager/UpdatesManager.kt rename to app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt index 86cd6b513..f05323866 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesManager.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorkManager.kt @@ -17,37 +17,21 @@ package foundation.e.apps.updates.manager -import android.content.BroadcastReceiver import android.content.Context -import android.content.Intent import android.util.Log -import androidx.preference.PreferenceManager -import androidx.work.Constraints -import androidx.work.ExistingPeriodicWorkPolicy -import androidx.work.NetworkType -import androidx.work.PeriodicWorkRequest -import androidx.work.WorkManager -import foundation.e.apps.R +import androidx.work.* import java.util.concurrent.TimeUnit -class UpdatesManager : BroadcastReceiver() { +object UpdatesWorkManager { + const val UPDATES_WORK_NAME = "updates_work" + private const val TAG = "UpdatesManager" - companion object { - const val UPDATES_WORK_NAME = "updates_work" - private const val TAG = "UpdatesManager" - } - - override fun onReceive(context: Context, intent: Intent) { - Log.d(TAG, "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() - enqueueWork(context, interval, ExistingPeriodicWorkPolicy.KEEP) - } + fun startUpdateAllWork(context: Context) { + WorkManager.getInstance(context).enqueueUniqueWork( + UPDATES_WORK_NAME, + ExistingWorkPolicy.KEEP, + buildOneTimeWorkRequest() + ) } private fun buildWorkerConstraints() = Constraints.Builder().apply { @@ -65,7 +49,17 @@ class UpdatesManager : BroadcastReceiver() { }.build() } - fun enqueueWork(context: Context, interval: Long, existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy) { + private fun buildOneTimeWorkRequest(): OneTimeWorkRequest { + return OneTimeWorkRequest.Builder(UpdatesWorker::class.java).apply { + setConstraints(buildWorkerConstraints()) + }.build() + } + + fun enqueueWork( + context: Context, + interval: Long, + existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy + ) { Log.i(TAG, "UpdatesWorker interval: $interval hours") WorkManager.getInstance(context).enqueueUniquePeriodicWork( UPDATES_WORK_NAME, @@ -73,4 +67,4 @@ class UpdatesManager : BroadcastReceiver() { ) Log.i(TAG, "UpdatesWorker started") } -} +} \ No newline at end of file -- GitLab From 5076760ac7733fc37203629bdca6478540ace232 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 11 May 2022 22:06:23 +0600 Subject: [PATCH 2/2] App Lounge: fixed lint issues --- .../e/apps/updates/manager/UpdatesBroadcastReceiver.kt | 2 +- .../e/apps/updates/manager/UpdatesWorkManager.kt | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) 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 8cd60f931..c4b566402 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 @@ -22,7 +22,7 @@ import android.content.Context import android.content.Intent import android.util.Log import androidx.preference.PreferenceManager -import androidx.work.* +import androidx.work.ExistingPeriodicWorkPolicy import foundation.e.apps.R class UpdatesBroadcastReceiver : BroadcastReceiver() { 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 f05323866..577969c28 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 @@ -19,7 +19,13 @@ package foundation.e.apps.updates.manager import android.content.Context import android.util.Log -import androidx.work.* +import androidx.work.Constraints +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 { @@ -67,4 +73,4 @@ object UpdatesWorkManager { ) Log.i(TAG, "UpdatesWorker started") } -} \ No newline at end of file +} -- GitLab