Bug 1306140 - Pre: move HomeConfig migration loop into separate method to help with testing r?sebastian
MozReview-Commit-ID: 4CDdqXPAae0
--- a/mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/HomeConfigPrefsBackend.java
@@ -22,16 +22,17 @@ import org.mozilla.gecko.home.HomeConfig
import org.mozilla.gecko.home.HomeConfig.State;
import org.mozilla.gecko.util.HardwareUtils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
+import android.support.annotation.CheckResult;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
public class HomeConfigPrefsBackend implements HomeConfigBackend {
private static final String LOGTAG = "GeckoHomeConfigBackend";
// Increment this to trigger a migration.
@@ -332,57 +333,23 @@ public class HomeConfigPrefsBackend impl
// It's okay to ignore this exception, since an invalid reading list
// panel config is equivalent to no reading list panel.
Log.e(LOGTAG, "Exception loading PanelConfig from JSON", e);
}
}
return false;
}
- /**
- * Migrates JSON config data storage.
- *
- * @param context Context used to get shared preferences and create built-in panel.
- * @param jsonString String currently stored in preferences.
- *
- * @return JSONArray array representing new set of panel configs.
- */
- private static synchronized JSONArray maybePerformMigration(Context context, String jsonString) throws JSONException {
- // If the migration is already done, we're at the current version.
- if (sMigrationDone) {
- final JSONObject json = new JSONObject(jsonString);
- return json.getJSONArray(JSON_KEY_PANELS);
- }
-
- // Make sure we only do this version check once.
- sMigrationDone = true;
-
- JSONArray jsonPanels;
- final int version;
+ @CheckResult
+ static synchronized JSONArray migratePrefsFromVersionToVersion(final Context context, final int currentVersion, final int newVersion,
+ final JSONArray jsonPanelsIn, final SharedPreferences.Editor prefsEditor) throws JSONException {
- final SharedPreferences prefs = GeckoSharedPrefs.forProfile(context);
- if (prefs.contains(PREFS_CONFIG_KEY_OLD)) {
- // Our original implementation did not contain versioning, so this is implicitly version 0.
- jsonPanels = new JSONArray(jsonString);
- version = 0;
- } else {
- final JSONObject json = new JSONObject(jsonString);
- jsonPanels = json.getJSONArray(JSON_KEY_PANELS);
- version = json.getInt(JSON_KEY_VERSION);
- }
+ JSONArray jsonPanels = jsonPanelsIn;
- if (version == VERSION) {
- return jsonPanels;
- }
-
- Log.d(LOGTAG, "Performing migration");
-
- final SharedPreferences.Editor prefsEditor = prefs.edit();
-
- for (int v = version + 1; v <= VERSION; v++) {
+ for (int v = currentVersion + 1; v <= newVersion; v++) {
Log.d(LOGTAG, "Migrating to version = " + v);
switch (v) {
case 1:
// Add "Recent Tabs" panel.
addBuiltinPanelConfig(context, jsonPanels,
PanelType.DEPRECATED_RECENT_TABS, Position.FRONT, Position.BACK);
@@ -434,16 +401,61 @@ public class HomeConfigPrefsBackend impl
case 8:
// Similar to "case 5" above, this time 1304777 - once again we lost track
// of the history panel
ensureDefaultPanelForV5orV8(context, jsonPanels);
break;
}
}
+ return jsonPanels;
+ }
+
+ /**
+ * Migrates JSON config data storage.
+ *
+ * @param context Context used to get shared preferences and create built-in panel.
+ * @param jsonString String currently stored in preferences.
+ *
+ * @return JSONArray array representing new set of panel configs.
+ */
+ private static synchronized JSONArray maybePerformMigration(Context context, String jsonString) throws JSONException {
+ // If the migration is already done, we're at the current version.
+ if (sMigrationDone) {
+ final JSONObject json = new JSONObject(jsonString);
+ return json.getJSONArray(JSON_KEY_PANELS);
+ }
+
+ // Make sure we only do this version check once.
+ sMigrationDone = true;
+
+ JSONArray jsonPanels;
+ final int version;
+
+ final SharedPreferences prefs = GeckoSharedPrefs.forProfile(context);
+ if (prefs.contains(PREFS_CONFIG_KEY_OLD)) {
+ // Our original implementation did not contain versioning, so this is implicitly version 0.
+ jsonPanels = new JSONArray(jsonString);
+ version = 0;
+ } else {
+ final JSONObject json = new JSONObject(jsonString);
+ jsonPanels = json.getJSONArray(JSON_KEY_PANELS);
+ version = json.getInt(JSON_KEY_VERSION);
+ }
+
+ if (version == VERSION) {
+ return jsonPanels;
+ }
+
+ Log.d(LOGTAG, "Performing migration");
+
+ final SharedPreferences.Editor prefsEditor = prefs.edit();
+
+ jsonPanels = migratePrefsFromVersionToVersion(context, version, VERSION, jsonPanels, prefsEditor);
+
// Save the new panel config and the new version number.
final JSONObject newJson = new JSONObject();
newJson.put(JSON_KEY_PANELS, jsonPanels);
newJson.put(JSON_KEY_VERSION, VERSION);
prefsEditor.putString(PREFS_CONFIG_KEY, newJson.toString());
prefsEditor.apply();