MainActivity: Run migrations if required
Thanks to OEMs breaking the MY_PACKAGE_REPLACED API, migrations haven't been running on some devices. To mitigate this, trigger the same via MainActivity too. Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -37,13 +37,13 @@ import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.InstallerEvent
|
||||
import com.aurora.store.data.model.NetworkStatus
|
||||
import com.aurora.store.data.providers.NetworkProvider
|
||||
import com.aurora.store.data.receiver.MigrationReceiver
|
||||
import com.aurora.store.databinding.ActivityMainBinding
|
||||
import com.aurora.store.util.AppUtil
|
||||
import com.aurora.store.util.Preferences
|
||||
import com.aurora.store.util.Preferences.PREFERENCE_DEFAULT_SELECTED_TAB
|
||||
import com.aurora.store.view.ui.sheets.NetworkDialogSheet
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
@@ -65,6 +65,10 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
// Check and run migrations first if required
|
||||
// This is needed thanks to OEMs breaking the MY_PACKAGE_REPLACED API
|
||||
MigrationReceiver.runMigrationsIfRequired(this)
|
||||
|
||||
applyThemeAccent()
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
||||
@@ -20,58 +20,66 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
@AndroidEntryPoint
|
||||
class MigrationReceiver: BroadcastReceiver() {
|
||||
|
||||
private val TAG = MigrationReceiver::class.java.simpleName
|
||||
companion object {
|
||||
private const val TAG = "MigrationReceiver"
|
||||
|
||||
private val prefVersion = 2 // BUMP THIS MANUALLY ON ADDING NEW MIGRATION STEP
|
||||
private const val PREF_VERSION = 2 // 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) {
|
||||
fun runMigrationsIfRequired(context: Context) {
|
||||
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) {
|
||||
if (Preferences.getBoolean(context, PREFERENCE_INTRO) && oldVersion != PREF_VERSION) {
|
||||
val newVersion = upgradeIfNeeded(context, oldVersion)
|
||||
Preferences.putInteger(context, PREFERENCE_MIGRATION_VERSION, newVersion)
|
||||
} else {
|
||||
Log.i(TAG, "No need to run migrations")
|
||||
}
|
||||
}
|
||||
|
||||
private fun upgradeIfNeeded(context: Context, oldVersion: Int): Int {
|
||||
Log.i(TAG, "Upgrading from version $oldVersion to version $PREF_VERSION")
|
||||
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) {
|
||||
CacheWorker.scheduleAutomatedCacheCleanup(context) // !1089
|
||||
if (isSAndAbove()) context.save(PREFERENCE_THEME_ACCENT, 0)
|
||||
context.save(PREFERENCE_DISPENSER_URLS, setOf(Constants.URL_DISPENSER)) // !1117
|
||||
if (Preferences.getInteger(context, PREFERENCE_VENDING_VERSION) == -1) {
|
||||
context.save(PREFERENCE_VENDING_VERSION, 0) // !1049
|
||||
}
|
||||
currentVersion++
|
||||
}
|
||||
|
||||
// 59 -> 60
|
||||
if (currentVersion == 1) {
|
||||
if (CertUtil.isAppGalleryApp(context, context.packageName)) {
|
||||
val dispensers = Preferences.getStringSet(context, PREFERENCE_DISPENSER_URLS)
|
||||
.toMutableSet()
|
||||
.remove(Constants.URL_DISPENSER)
|
||||
context.save(PREFERENCE_DISPENSER_URLS, dispensers)
|
||||
}
|
||||
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")
|
||||
} else {
|
||||
Log.i(TAG, "Finished running required migrations!")
|
||||
}
|
||||
|
||||
return currentVersion
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
CacheWorker.scheduleAutomatedCacheCleanup(context) // !1089
|
||||
if (isSAndAbove()) context.save(PREFERENCE_THEME_ACCENT, 0)
|
||||
context.save(PREFERENCE_DISPENSER_URLS, setOf(Constants.URL_DISPENSER)) // !1117
|
||||
if (Preferences.getInteger(context, PREFERENCE_VENDING_VERSION) == -1) {
|
||||
context.save(PREFERENCE_VENDING_VERSION, 0) // !1049
|
||||
}
|
||||
currentVersion++
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (context != null && intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
|
||||
runMigrationsIfRequired(context)
|
||||
}
|
||||
|
||||
// 59 -> 60
|
||||
if (currentVersion == 1) {
|
||||
if (CertUtil.isAppGalleryApp(context, context.packageName)) {
|
||||
val dispensers = Preferences.getStringSet(context, PREFERENCE_DISPENSER_URLS)
|
||||
.toMutableSet()
|
||||
.remove(Constants.URL_DISPENSER)
|
||||
context.save(PREFERENCE_DISPENSER_URLS, dispensers)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user