diff --git a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java index de39bf755..aa21c4422 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java +++ b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java @@ -60,6 +60,10 @@ public final class NewPipeSettings { isFirstRun = true; } + // first run migrations, then setDefaultValues, since the latter requires the correct types + SettingMigrations.initMigrations(context, isFirstRun); + + // readAgain is true so that if new settings are added their default value is set PreferenceManager.setDefaultValues(context, R.xml.main_settings, true); PreferenceManager.setDefaultValues(context, R.xml.video_audio_settings, true); PreferenceManager.setDefaultValues(context, R.xml.download_settings, true); @@ -72,8 +76,6 @@ public final class NewPipeSettings { saveDefaultVideoDownloadDirectory(context); saveDefaultAudioDownloadDirectory(context); - - SettingMigrations.initMigrations(context, isFirstRun); } static void saveDefaultVideoDownloadDirectory(final Context context) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingMigrations.java b/app/src/main/java/org/schabi/newpipe/settings/SettingMigrations.java index f96c24a40..b0b9567d8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingMigrations.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingMigrations.java @@ -19,12 +19,16 @@ import java.util.Set; import static org.schabi.newpipe.MainActivity.DEBUG; +/** + * In order to add a migration, follow these steps, given P is the previous version:
+ * - in the class body add a new {@code MIGRATION_P_P+1 = new Migration(P, P+1) { ... }} and put in + * the {@code migrate()} method the code that need to be run when migrating from P to P+1
+ * - add {@code MIGRATION_P_P+1} at the end of {@link SettingMigrations#SETTING_MIGRATIONS}
+ * - increment {@link SettingMigrations#VERSION}'s value by 1 (so it should become P+1) + */ public final class SettingMigrations { + private static final String TAG = SettingMigrations.class.toString(); - /** - * Version number for preferences. Must be incremented every time a migration is necessary. - */ - public static final int VERSION = 3; private static SharedPreferences sp; public static final Migration MIGRATION_0_1 = new Migration(0, 1) { @@ -84,8 +88,17 @@ public final class SettingMigrations { final String showSearchSuggestionsKey = context.getString(R.string.show_search_suggestions_key); + + boolean addAllSearchSuggestionTypes; + try { + addAllSearchSuggestionTypes = sp.getBoolean(showSearchSuggestionsKey, true); + } catch (final ClassCastException e) { + // just in case it was not a boolean for some reason, let's consider it a "true" + addAllSearchSuggestionTypes = true; + } + final Set showSearchSuggestionsValueList = new HashSet<>(); - if (sp.getBoolean(showSearchSuggestionsKey, true)) { + if (addAllSearchSuggestionTypes) { // if the preference was true, all suggestions will be shown, otherwise none Collections.addAll(showSearchSuggestionsValueList, context.getResources() .getStringArray(R.array.show_search_suggestions_value_list)); @@ -109,6 +122,11 @@ public final class SettingMigrations { MIGRATION_3_4, }; + /** + * Version number for preferences. Must be incremented every time a migration is necessary. + */ + public static final int VERSION = 4; + public static void initMigrations(final Context context, final boolean isFirstRun) { // setup migrations and check if there is something to do