Bug 949174/964375/952311 - Factor out code to create built-in panel configs (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 05 Feb 2014 14:14:51 +0000
changeset 178001 299bcc7ae1fe60acd1de52cf82961cfa1f43fdde
parent 178000 3ce5275b17ed117820e5011456702142a8ee5b9f
child 178002 cfd371a2c63bb3b874f9ed97d9c914ee48cbf0d6
push id5439
push userffxbld
push dateMon, 17 Mar 2014 23:08:15 +0000
treeherdermozilla-aurora@c0befb3c8038 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs949174, 964375, 952311
milestone30.0a1
Bug 949174/964375/952311 - Factor out code to create built-in panel configs (r=margaret)
mobile/android/base/home/HomeConfig.java
mobile/android/base/home/HomeConfigPrefsBackend.java
--- a/mobile/android/base/home/HomeConfig.java
+++ b/mobile/android/base/home/HomeConfig.java
@@ -1,15 +1,17 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
+import org.mozilla.gecko.R;
+
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.content.Context;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.text.TextUtils;
@@ -537,16 +539,22 @@ public final class HomeConfig {
     }
 
     public interface HomeConfigBackend {
         public List<PanelConfig> load();
         public void save(List<PanelConfig> entries);
         public void setOnChangeListener(OnChangeListener listener);
     }
 
+    // UUIDs used to create PanelConfigs for default built-in panels
+    private static final String TOP_SITES_PANEL_ID = "4becc86b-41eb-429a-a042-88fe8b5a094e";
+    private static final String BOOKMARKS_PANEL_ID = "7f6d419a-cd6c-4e34-b26f-f68b1b551907";
+    private static final String READING_LIST_PANEL_ID = "20f4549a-64ad-4c32-93e4-1dcef792733b";
+    private static final String HISTORY_PANEL_ID = "f134bf20-11f7-4867-ab8b-e8e705d7fbe8";
+
     private final HomeConfigBackend mBackend;
 
     public HomeConfig(HomeConfigBackend backend) {
         mBackend = backend;
     }
 
     public List<PanelConfig> load() {
         return mBackend.load();
@@ -555,12 +563,48 @@ public final class HomeConfig {
     public void save(List<PanelConfig> entries) {
         mBackend.save(entries);
     }
 
     public void setOnChangeListener(OnChangeListener listener) {
         mBackend.setOnChangeListener(listener);
     }
 
+    public static PanelConfig createBuiltinPanelConfig(Context context, PanelType panelType) {
+        return createBuiltinPanelConfig(context, panelType, EnumSet.noneOf(PanelConfig.Flags.class));
+    }
+
+    public static PanelConfig createBuiltinPanelConfig(Context context, PanelType panelType, EnumSet<PanelConfig.Flags> flags) {
+        int titleId = 0;
+        String id = null;
+
+        switch(panelType) {
+            case TOP_SITES:
+                titleId = R.string.home_top_sites_title;
+                id = TOP_SITES_PANEL_ID;
+                break;
+
+            case BOOKMARKS:
+                titleId = R.string.bookmarks_title;
+                id = BOOKMARKS_PANEL_ID;
+                break;
+
+            case HISTORY:
+                titleId = R.string.home_history_title;
+                id = HISTORY_PANEL_ID;
+                break;
+
+            case READING_LIST:
+                titleId = R.string.reading_list_title;
+                id = READING_LIST_PANEL_ID;
+                break;
+
+            case DYNAMIC:
+                throw new IllegalArgumentException("createBuiltinPanelConfig() is only for built-in panels");
+        }
+
+        return new PanelConfig(panelType, context.getString(titleId), id, flags);
+    }
+
     public static HomeConfig getDefault(Context context) {
         return new HomeConfig(new HomeConfigPrefsBackend(context));
     }
 }
--- a/mobile/android/base/home/HomeConfigPrefsBackend.java
+++ b/mobile/android/base/home/HomeConfigPrefsBackend.java
@@ -8,16 +8,18 @@ package org.mozilla.gecko.home;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.home.HomeConfig.HomeConfigBackend;
 import org.mozilla.gecko.home.HomeConfig.OnChangeListener;
 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
 import org.mozilla.gecko.home.HomeConfig.PanelType;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 
+import static org.mozilla.gecko.home.HomeConfig.createBuiltinPanelConfig;
+
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.preference.PreferenceManager;
@@ -29,57 +31,43 @@ import java.util.Collections;
 import java.util.EnumSet;
 import java.util.List;
 
 class HomeConfigPrefsBackend implements HomeConfigBackend {
     private static final String LOGTAG = "GeckoHomeConfigBackend";
 
     private static final String PREFS_KEY = "home_panels";
 
-    // UUIDs used to create PanelConfigs for default built-in panels 
-    private static final String TOP_SITES_PANEL_ID = "4becc86b-41eb-429a-a042-88fe8b5a094e";
-    private static final String BOOKMARKS_PANEL_ID = "7f6d419a-cd6c-4e34-b26f-f68b1b551907";
-    private static final String READING_LIST_PANEL_ID = "20f4549a-64ad-4c32-93e4-1dcef792733b";
-    private static final String HISTORY_PANEL_ID = "f134bf20-11f7-4867-ab8b-e8e705d7fbe8";
-
     private final Context mContext;
     private PrefsListener mPrefsListener;
     private OnChangeListener mChangeListener;
 
     public HomeConfigPrefsBackend(Context context) {
         mContext = context;
     }
 
     private SharedPreferences getSharedPreferences() {
         return PreferenceManager.getDefaultSharedPreferences(mContext);
     }
 
     private List<PanelConfig> loadDefaultConfig() {
         final ArrayList<PanelConfig> panelConfigs = new ArrayList<PanelConfig>();
 
-        panelConfigs.add(new PanelConfig(PanelType.TOP_SITES,
-                                         mContext.getString(R.string.home_top_sites_title),
-                                         TOP_SITES_PANEL_ID,
-                                         EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL)));
+        panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.TOP_SITES,
+                                                  EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL)));
 
-        panelConfigs.add(new PanelConfig(PanelType.BOOKMARKS,
-                                         mContext.getString(R.string.bookmarks_title),
-                                         BOOKMARKS_PANEL_ID));
+        panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.BOOKMARKS));
 
         // We disable reader mode support on low memory devices. Hence the
         // reading list panel should not show up on such devices.
         if (!HardwareUtils.isLowMemoryPlatform()) {
-            panelConfigs.add(new PanelConfig(PanelType.READING_LIST,
-                                             mContext.getString(R.string.reading_list_title),
-                                             READING_LIST_PANEL_ID));
+            panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.READING_LIST));
         }
 
-        final PanelConfig historyEntry = new PanelConfig(PanelType.HISTORY,
-                                                         mContext.getString(R.string.home_history_title),
-                                                         HISTORY_PANEL_ID);
+        final PanelConfig historyEntry = createBuiltinPanelConfig(mContext, PanelType.HISTORY);
 
         // On tablets, the history panel is the last.
         // On phones, the history panel is the first one.
         if (HardwareUtils.isTablet()) {
             panelConfigs.add(historyEntry);
         } else {
             panelConfigs.add(0, historyEntry);
         }