diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c33bb0580..a61c39671 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -159,5 +159,14 @@ + + + + + + + diff --git a/app/src/main/java/com/aurora/store/data/receiver/MigrationReceiver.kt b/app/src/main/java/com/aurora/store/data/receiver/MigrationReceiver.kt new file mode 100644 index 000000000..f7979e96f --- /dev/null +++ b/app/src/main/java/com/aurora/store/data/receiver/MigrationReceiver.kt @@ -0,0 +1,54 @@ +package com.aurora.store.data.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.util.Log +import com.aurora.store.data.work.CleanCacheWorker +import com.aurora.store.util.Preferences +import com.aurora.store.util.Preferences.PREFERENCE_INTRO +import com.aurora.store.util.Preferences.PREFERENCE_MIGRATION_VERSION +import dagger.hilt.android.AndroidEntryPoint + +@AndroidEntryPoint +class MigrationReceiver: BroadcastReceiver() { + + private val TAG = MigrationReceiver::class.java.simpleName + + private val prefVersion = 1 // BUMP THIS MANUALLY ON ADDING NEW MIGRATION STEP + + override fun onReceive(context: Context?, intent: Intent?) { + if (context != null && intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) { + val oldVersion = Preferences.getInteger(context, PREFERENCE_MIGRATION_VERSION) + + // Run required migrations on version upgrade for existing installs + if (Preferences.getBoolean(context, PREFERENCE_INTRO) && oldVersion != prefVersion) { + val newVersion = upgradeIfNeeded(context, oldVersion) + Preferences.putInteger(context, PREFERENCE_MIGRATION_VERSION, newVersion) + } + } + } + + private fun upgradeIfNeeded(context: Context, oldVersion: Int): Int { + Log.i(TAG, "Upgrading from version $oldVersion to version $prefVersion") + var currentVersion = oldVersion + + // Add new migrations / defaults below this point + // Always bump currentVersion at the end of migration for next release + + // 58 -> 59 + if (currentVersion == 0) { + CleanCacheWorker.scheduleAutomatedCacheCleanup(context) + currentVersion++ + } + + // Add new migrations / defaults above this point. + if (currentVersion != prefVersion) { + Log.e(TAG, "Upgrading to version $prefVersion left it at $currentVersion instead") + } else { + Log.i(TAG, "Finished running required migrations!") + } + + return currentVersion + } +} diff --git a/app/src/main/java/com/aurora/store/util/Preferences.kt b/app/src/main/java/com/aurora/store/util/Preferences.kt index 65935a66a..231309c66 100644 --- a/app/src/main/java/com/aurora/store/util/Preferences.kt +++ b/app/src/main/java/com/aurora/store/util/Preferences.kt @@ -60,6 +60,8 @@ object Preferences { const val PREFERENCE_UPDATES_AUTO = "PREFERENCE_UPDATES_AUTO" const val PREFERENCE_UPDATES_CHECK_INTERVAL = "PREFERENCE_UPDATES_CHECK_INTERVAL" + const val PREFERENCE_MIGRATION_VERSION = "PREFERENCE_MIGRATION_VERSION" + private var prefs: SharedPreferences? = null fun getPrefs(context: Context): SharedPreferences {