Assign missing ID to update the work

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2025-06-11 17:45:24 +08:00
parent 3377774caf
commit e8e87f835f
2 changed files with 56 additions and 31 deletions

View File

@@ -32,6 +32,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.util.UUID
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MINUTES
import javax.inject.Inject
@@ -49,6 +50,37 @@ class UpdateHelper @Inject constructor(
private const val UPDATE_WORKER = "UPDATE_WORKER"
private const val EXPEDITED_UPDATE_WORKER = "EXPEDITED_UPDATE_WORKER"
fun getAutoUpdateWork(context: Context): PeriodicWorkRequest {
val updateCheckInterval = Preferences.getInteger(
context,
PREFERENCE_UPDATES_CHECK_INTERVAL,
3
).toLong()
val constraints = Constraints.Builder()
if (Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_METERED, true)) {
constraints.setRequiredNetworkType(NetworkType.UNMETERED)
}
if (Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_BATTERY, true)) {
constraints.setRequiresBatteryNotLow(true)
}
if (isMAndAbove && Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_IDLE, true)) {
constraints.setRequiresDeviceIdle(true)
}
return PeriodicWorkRequestBuilder<UpdateWorker>(
repeatInterval = updateCheckInterval,
repeatIntervalTimeUnit = HOURS,
flexTimeInterval = 30,
flexTimeIntervalUnit = MINUTES
).setConstraints(constraints.build())
.setId(UUID.nameUUIDFromBytes(UPDATE_WORKER.toByteArray()))
.build()
}
}
private val TAG = UpdateHelper::class.java.simpleName
@@ -142,7 +174,7 @@ class UpdateHelper @Inject constructor(
.enqueueUniquePeriodicWork(
UPDATE_WORKER,
ExistingPeriodicWorkPolicy.KEEP,
getAutoUpdateWork()
getAutoUpdateWork(context)
)
}
@@ -152,36 +184,8 @@ class UpdateHelper @Inject constructor(
*/
fun updateAutomatedCheck() {
Log.i(TAG, "Updating periodic app updates!")
WorkManager.getInstance(context).updateWork(getAutoUpdateWork())
}
private fun getAutoUpdateWork(): PeriodicWorkRequest {
val updateCheckInterval = Preferences.getInteger(
context,
PREFERENCE_UPDATES_CHECK_INTERVAL,
3
).toLong()
val constraints = Constraints.Builder()
if (Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_METERED, true)) {
constraints.setRequiredNetworkType(NetworkType.UNMETERED)
}
if (Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_BATTERY, true)) {
constraints.setRequiresBatteryNotLow(true)
}
if (isMAndAbove && Preferences.getBoolean(context, PREFERENCES_UPDATES_RESTRICTIONS_IDLE, true)) {
constraints.setRequiresDeviceIdle(true)
}
return PeriodicWorkRequestBuilder<UpdateWorker>(
repeatInterval = updateCheckInterval,
repeatIntervalTimeUnit = HOURS,
flexTimeInterval = 30,
flexTimeIntervalUnit = MINUTES
).setConstraints(constraints.build()).build()
runCatching { WorkManager.getInstance(context).updateWork(getAutoUpdateWork(context)) }
.onFailure { Log.e(TAG, "Failed to update periodic app updates!", it) }
}
private suspend fun deleteInvalidUpdates() {

View File

@@ -6,14 +6,18 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.content.getSystemService
import androidx.work.WorkManager
import com.aurora.Constants
import com.aurora.extensions.isOAndAbove
import com.aurora.store.data.helper.UpdateHelper.Companion.getAutoUpdateWork
import com.aurora.store.data.model.UpdateMode
import com.aurora.store.data.work.CacheWorker
import com.aurora.store.util.CertUtil
import com.aurora.store.util.Preferences
import com.aurora.store.util.Preferences.PREFERENCE_DISPENSER_URLS
import com.aurora.store.util.Preferences.PREFERENCE_INTRO
import com.aurora.store.util.Preferences.PREFERENCE_MIGRATION_VERSION
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_AUTO
import com.aurora.store.util.Preferences.PREFERENCE_VENDING_VERSION
import com.aurora.store.util.save
import dagger.hilt.android.AndroidEntryPoint
@@ -79,6 +83,23 @@ class MigrationReceiver : BroadcastReceiver() {
currentVersion++
}
// 68 -> 69
if (currentVersion == 3) {
val updateMode = UpdateMode.entries[
Preferences.getInteger(
context,
PREFERENCE_UPDATES_AUTO,
UpdateMode.DISABLED.ordinal
)]
if (updateMode != UpdateMode.DISABLED) {
runCatching {
WorkManager.getInstance(context).updateWork(getAutoUpdateWork(context))
}.onFailure { Log.e(TAG, "Failed to migrate app updates!", it) }
}
currentVersion++
}
// Add new migrations / defaults above this point.
if (currentVersion != PREF_VERSION) {
Log.e(TAG, "Upgrading to version $PREF_VERSION left it at $currentVersion instead")