diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 78fba17b62274128da0d3923a806e658feb3208e..05e9d66c5812295a2a363721d41f667e5a8e12af 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 e262b6e06b6ac7c31fd260e58ccd086005f84368..562ec459bc6f0d5f220483bffb124bdc0a52b6ce 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 cd6116e1eb5801805e3aa02bb120045af2172960..eb03b02d953800a39b6b5422844900a9c5f1240c 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 0000000000000000000000000000000000000000..c4b566402ac0503525b0b29f9e03ec272dc1cc15 --- /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.ExistingPeriodicWorkPolicy +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 64% 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 86cd6b51365a42839fb22c189c5626ac04fc33e3..577969c289f8e2af4c5fd10c7ef2f328ea2bd678 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,27 @@ 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.ExistingWorkPolicy import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequest import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager -import foundation.e.apps.R 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 +55,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,