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 f6d20b35d31ed5ca97747217af7903353c339cd4..01bba9b3483eb636a990f95968aa265968aad2e7 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() { } @@ -390,6 +392,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 6225da2e85a36d3164be3dc9b3d61245384a12bf..640b1a2ae3f6e31a638b247421d02fbb7f716616 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 0000000000000000000000000000000000000000..6b3eedaca2646644f841acec8dc72a56a2992f6a --- /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" +}