From 84d6c68ea20630d85b54c23ff54d8f2938a0a228 Mon Sep 17 00:00:00 2001 From: Yash Garg Date: Mon, 12 Jun 2023 17:06:53 +0000 Subject: [PATCH] fix(1288): add AP widget database migration --- app/build.gradle.kts | 4 +- .../e/blisslauncher/core/Preferences.java | 14 ++++++ .../launcher/tasks/LoadDatabaseTask.java | 3 ++ .../features/widgets/WidgetMigration.kt | 50 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/foundation/e/blisslauncher/features/widgets/WidgetMigration.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 22fd33e3e3..203c43b7a9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -12,8 +12,8 @@ plugins { // Manifest version information val versionMajor = 1 -val versionMinor = 11 -val versionPatch = 0 +val versionMinor = 12 +val versionPatch = 2 val localProps = Properties() val localPropsFile = project.rootProject.file("local.properties") diff --git a/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java b/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java index 70e6db98d6..11e74fa80d 100644 --- a/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/Preferences.java @@ -54,6 +54,8 @@ public class Preferences { private static final String ADDED_ECLOUD_WIDGET = "added_ecloud_widget"; private static final String ADDED_PRIVACY_WIDGET = "added_privacy_widget"; + private static final String AP_MIGRATION_1 = "ap_migration_1"; + private Preferences() { } @@ -406,6 +408,18 @@ public class Preferences { getPrefs(context).edit().putBoolean(ADDED_PRIVACY_WIDGET, true).apply(); } + public static void setRemovedPrivacyWidget(Context context) { + getPrefs(context).edit().putBoolean(ADDED_PRIVACY_WIDGET, false).apply(); + } + + public static void setApMigration1Status(Context context, boolean status) { + getPrefs(context).edit().putBoolean(AP_MIGRATION_1, status).apply(); + } + + public static boolean getApMigration1Status(Context context) { + return getPrefs(context).getBoolean(AP_MIGRATION_1, false); + } + public static boolean getAddedPrivacyWidget(Context context) { return getPrefs(context).getBoolean(ADDED_PRIVACY_WIDGET, false); } diff --git a/app/src/main/java/foundation/e/blisslauncher/features/launcher/tasks/LoadDatabaseTask.java b/app/src/main/java/foundation/e/blisslauncher/features/launcher/tasks/LoadDatabaseTask.java index 6225da2e85..640b1a2ae3 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/launcher/tasks/LoadDatabaseTask.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/launcher/tasks/LoadDatabaseTask.java @@ -5,6 +5,8 @@ import foundation.e.blisslauncher.core.database.LauncherDB; import foundation.e.blisslauncher.core.database.model.LauncherItem; import foundation.e.blisslauncher.core.migrate.Migration; import foundation.e.blisslauncher.features.launcher.AppProvider; +import foundation.e.blisslauncher.features.widgets.WidgetMigration; + import java.util.List; public class LoadDatabaseTask extends AsyncTask> { @@ -22,6 +24,7 @@ public class LoadDatabaseTask extends AsyncTask> @Override protected List doInBackground(Void... voids) { Migration.migrateSafely(mAppProvider.getContext()); + WidgetMigration.migrateAdvancedPrivacy(mAppProvider.getContext()); return LauncherDB.getDatabase(mAppProvider.getContext()).launcherDao().getAllItems(); } diff --git a/app/src/main/java/foundation/e/blisslauncher/features/widgets/WidgetMigration.kt b/app/src/main/java/foundation/e/blisslauncher/features/widgets/WidgetMigration.kt new file mode 100644 index 0000000000..6b3eedaca2 --- /dev/null +++ b/app/src/main/java/foundation/e/blisslauncher/features/widgets/WidgetMigration.kt @@ -0,0 +1,50 @@ +package foundation.e.blisslauncher.features.widgets + +import android.appwidget.AppWidgetManager +import android.content.ComponentName +import android.content.Context +import foundation.e.blisslauncher.core.Preferences +import foundation.e.blisslauncher.core.database.LauncherDB +import foundation.e.blisslauncher.features.widgets.DefaultWidgets.privacyWidget +import timber.log.Timber + +object WidgetMigration { + @JvmStatic + fun migrateAdvancedPrivacy(context: Context) { + Timber.tag(TAG).d("Starting widget migration for Advanced Privacy") + val addedAp = Preferences.getAddedPrivacyWidget(context) + val oldComponent = + ComponentName("foundation.e.advancedprivacy", "foundation.e.privacycentralapp.Widget") + + val shouldMigrate = migrate(context, addedAp, oldComponent) + Timber.tag(TAG).d("Should migrate?: $shouldMigrate") + + if (shouldMigrate && !Preferences.getApMigration1Status(context)) { + Preferences.setRemovedPrivacyWidget(context) + Preferences.setApMigration1Status(context, true) + } + } + + private fun migrate(context: Context, isAdded: Boolean, oldComponent: ComponentName): Boolean { + val widgetDao = LauncherDB.getDatabase(context).widgetDao() + var containsInDB = false + + for (widget in widgetDao.all) { + val info = AppWidgetManager.getInstance(context).getAppWidgetInfo(widget.id) + Timber.tag(TAG).d("Widget Id: ${widget.id} | Widget info: $info") + + if (info != null && (info.provider == oldComponent || info.provider == privacyWidget)) { + Timber.tag(TAG).d("Migrating widget ${info.provider}") + containsInDB = true + } + } + + if (containsInDB && isAdded) { + Preferences.setApMigration1Status(context, true) + } + + return !containsInDB && isAdded + } + + private const val TAG = "WidgetMigration" +} -- GitLab