UpdatesPreference: Setup auto-updates
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import android.content.Intent
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
||||
import androidx.core.content.pm.PackageInfoCompat
|
||||
import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.CoroutineWorker
|
||||
import androidx.work.ExistingPeriodicWorkPolicy.KEEP
|
||||
@@ -17,6 +18,7 @@ import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.WorkerParameters
|
||||
import com.aurora.Constants
|
||||
import com.aurora.extensions.isMAndAbove
|
||||
import com.aurora.gplayapi.data.models.App
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.gplayapi.helpers.AppDetailsHelper
|
||||
@@ -26,50 +28,58 @@ import com.aurora.store.R
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.data.providers.BlacklistProvider
|
||||
import com.aurora.store.util.DownloadWorkerUtil
|
||||
import com.aurora.store.util.Log
|
||||
import com.aurora.store.util.PackageUtil
|
||||
import com.aurora.store.util.Preferences
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_AUTO
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.TimeUnit.HOURS
|
||||
import java.util.concurrent.TimeUnit.MINUTES
|
||||
|
||||
class UpdateWorker(private val appContext: Context, workerParams: WorkerParameters) :
|
||||
CoroutineWorker(appContext, workerParams) {
|
||||
@HiltWorker
|
||||
class UpdateWorker @AssistedInject constructor(
|
||||
val downloadWorkerUtil: DownloadWorkerUtil,
|
||||
@Assisted private val appContext: Context,
|
||||
@Assisted workerParams: WorkerParameters
|
||||
) : CoroutineWorker(appContext, workerParams) {
|
||||
|
||||
companion object {
|
||||
private const val WORK_NAME_CHECK = "WORK_NAME_CHECK"
|
||||
private const val UPDATE_WORKER = "UPDATE_WORKER"
|
||||
|
||||
fun cancelAutomatedCheck(context: Context) {
|
||||
Log.i("Cancelling periodic app updates check!")
|
||||
WorkManager.getInstance(context)
|
||||
.cancelUniqueWork(WORK_NAME_CHECK)
|
||||
Log.i("Cancelling periodic app updates!")
|
||||
WorkManager.getInstance(context).cancelUniqueWork(UPDATE_WORKER)
|
||||
}
|
||||
|
||||
fun scheduleAutomatedCheck(context: Context) {
|
||||
Log.i("Scheduling periodic app updates check!")
|
||||
Log.i("Scheduling periodic app updates!")
|
||||
|
||||
val updateCheckInterval = Preferences.getInteger(
|
||||
context,
|
||||
Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL,
|
||||
PREFERENCE_UPDATES_CHECK_INTERVAL,
|
||||
3
|
||||
).toLong()
|
||||
|
||||
val constraints = Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.UNMETERED)
|
||||
.setRequiresDeviceIdle(true)
|
||||
.setRequiresBatteryNotLow(true)
|
||||
.build()
|
||||
|
||||
if (isMAndAbove()) constraints.setRequiresDeviceIdle(true)
|
||||
|
||||
val workRequest = PeriodicWorkRequestBuilder<UpdateWorker>(
|
||||
repeatInterval = updateCheckInterval,
|
||||
repeatIntervalTimeUnit = HOURS,
|
||||
flexTimeInterval = 30,
|
||||
flexTimeIntervalUnit = MINUTES
|
||||
).setConstraints(constraints).build()
|
||||
).setConstraints(constraints.build()).build()
|
||||
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
workManager.enqueueUniquePeriodicWork(WORK_NAME_CHECK, KEEP, workRequest)
|
||||
workManager.enqueueUniquePeriodicWork(UPDATE_WORKER, KEEP, workRequest)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +104,14 @@ class UpdateWorker(private val appContext: Context, workerParams: WorkerParamete
|
||||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
val autoUpdatesMode = Preferences.getInteger(appContext, PREFERENCE_UPDATES_AUTO, 3)
|
||||
|
||||
// Exit if auto-updates is turned off in settings
|
||||
if (autoUpdatesMode == 0) {
|
||||
Log.i("Auto-updates is disabled, bailing out!")
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
|
||||
if (!isValid(authData)) {
|
||||
@@ -136,10 +154,15 @@ class UpdateWorker(private val appContext: Context, workerParams: WorkerParamete
|
||||
}
|
||||
|
||||
if (updatesList.isNotEmpty()) {
|
||||
Log.i("Found updates, notifying!")
|
||||
val notifyManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE)
|
||||
as NotificationManager
|
||||
notifyManager.notify(notificationID, getUpdateNotification(updatesList))
|
||||
if (autoUpdatesMode == 2) {
|
||||
Log.i("Found updates, notifying!")
|
||||
val notifyManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE)
|
||||
as NotificationManager
|
||||
notifyManager.notify(notificationID, getUpdateNotification(updatesList))
|
||||
} else {
|
||||
Log.i("Found updates, updating!")
|
||||
updatesList.forEach { downloadWorkerUtil.enqueueApp(it) }
|
||||
}
|
||||
return@withContext Result.success()
|
||||
}
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ object Preferences {
|
||||
const val PREFERENCE_VENDING_VERSION = "PREFERENCE_VENDING_VERSION"
|
||||
|
||||
const val PREFERENCE_UPDATES_EXTENDED = "PREFERENCE_UPDATES_EXTENDED"
|
||||
const val PREFERENCE_UPDATES_AUTO = "PREFERENCE_UPDATES_AUTO"
|
||||
const val PREFERENCE_UPDATES_CHECK = "PREFERENCE_UPDATES_CHECK"
|
||||
const val PREFERENCE_UPDATES_CHECK_INTERVAL = "PREFERENCE_UPDATES_CHECK_INTERVAL"
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ import com.aurora.store.util.Preferences.PREFERENCE_SELF_UPDATE
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_SIMILAR
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_THEME_ACCENT
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_THEME_TYPE
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_AUTO
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_EXTENDED
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_VENDING_VERSION
|
||||
@@ -190,7 +190,7 @@ class OnboardingFragment : Fragment(R.layout.fragment_onboarding) {
|
||||
|
||||
/*Updates*/
|
||||
save(PREFERENCE_UPDATES_EXTENDED, false)
|
||||
save(PREFERENCE_UPDATES_CHECK, true)
|
||||
save(PREFERENCE_UPDATES_AUTO, 2)
|
||||
save(PREFERENCE_UPDATES_CHECK_INTERVAL, 3)
|
||||
save(
|
||||
PREFERENCE_SELF_UPDATE,
|
||||
|
||||
@@ -23,15 +23,21 @@ import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.SeekBarPreference
|
||||
import androidx.preference.SwitchPreferenceCompat
|
||||
import com.aurora.store.BuildConfig
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.work.UpdateWorker
|
||||
import com.aurora.store.util.CertUtil
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK
|
||||
import com.aurora.store.util.Preferences
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_SELF_UPDATE
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_AUTO
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
|
||||
import com.aurora.store.util.save
|
||||
import com.aurora.store.view.custom.preference.ListPreferenceMaterialDialogFragmentCompat
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -40,6 +46,9 @@ class UpdatesPreference : PreferenceFragmentCompat() {
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.preferences_updates, rootKey)
|
||||
|
||||
findPreference<SeekBarPreference>(PREFERENCE_UPDATES_CHECK_INTERVAL)?.isEnabled =
|
||||
Preferences.getInteger(requireContext(), PREFERENCE_UPDATES_AUTO, 3) != 0
|
||||
|
||||
findPreference<SwitchPreferenceCompat>(PREFERENCE_SELF_UPDATE)?.let {
|
||||
if (CertUtil.isFDroidApp(requireContext(), BuildConfig.APPLICATION_ID)) {
|
||||
it.isVisible = false
|
||||
@@ -51,17 +60,32 @@ class UpdatesPreference : PreferenceFragmentCompat() {
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<SwitchPreferenceCompat>(PREFERENCE_UPDATES_CHECK)
|
||||
findPreference<ListPreference>(PREFERENCE_UPDATES_AUTO)
|
||||
?.setOnPreferenceChangeListener { _, newValue ->
|
||||
if (newValue.toString().toBoolean()) {
|
||||
UpdateWorker.scheduleAutomatedCheck(requireContext())
|
||||
} else {
|
||||
UpdateWorker.cancelAutomatedCheck(requireContext())
|
||||
when (newValue.toString().toInt()) {
|
||||
0 -> UpdateWorker.cancelAutomatedCheck(requireContext())
|
||||
else -> UpdateWorker.scheduleAutomatedCheck(requireContext())
|
||||
}
|
||||
findPreference<SeekBarPreference>(PREFERENCE_UPDATES_CHECK_INTERVAL)?.isEnabled =
|
||||
newValue.toString().toInt() != 0
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDisplayPreferenceDialog(preference: Preference) {
|
||||
if (preference is ListPreference) {
|
||||
val dialogFragment =
|
||||
ListPreferenceMaterialDialogFragmentCompat.newInstance(preference.getKey())
|
||||
dialogFragment.setTargetFragment(this, 0)
|
||||
dialogFragment.show(
|
||||
parentFragmentManager,
|
||||
ListPreferenceMaterialDialogFragmentCompat.PREFERENCE_DIALOG_FRAGMENT_TAG
|
||||
)
|
||||
} else {
|
||||
super.onDisplayPreferenceDialog(preference)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
view.findViewById<Toolbar>(R.id.toolbar)?.apply {
|
||||
|
||||
Reference in New Issue
Block a user