Bug 1012462 - Part 4: Factor out code to create list of suggested sites (r=rnewman)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 15 Jul 2014 20:54:27 +0100
changeset 216237 7811f4c944c5092ad38281d2d94824dee2c19568
parent 216236 bf73e3732df414dd2cdac1875858d053ae351370
child 216238 6cd4b8d5036c120827894ca0befb96ca789ddf22
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1012462
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1012462 - Part 4: Factor out code to create list of suggested sites (r=rnewman)
mobile/android/base/db/SuggestedSites.java
--- a/mobile/android/base/db/SuggestedSites.java
+++ b/mobile/android/base/db/SuggestedSites.java
@@ -79,23 +79,38 @@ public class SuggestedSites {
         public final String imageUrl;
         public final String bgColor;
 
         public Site(JSONObject json) throws JSONException {
             this.url = json.getString(JSON_KEY_URL);
             this.title = json.getString(JSON_KEY_TITLE);
             this.imageUrl = json.getString(JSON_KEY_IMAGE_URL);
             this.bgColor = json.getString(JSON_KEY_BG_COLOR);
+
+            validate();
         }
 
         public Site(String url, String title, String imageUrl, String bgColor) {
             this.url = url;
             this.title = title;
             this.imageUrl = imageUrl;
             this.bgColor = bgColor;
+
+            validate();
+        }
+
+        private void validate() {
+            // Site instances must have non-empty values for all properties.
+            if (TextUtils.isEmpty(url) ||
+                TextUtils.isEmpty(title) ||
+                TextUtils.isEmpty(imageUrl) ||
+                TextUtils.isEmpty(bgColor)) {
+                throw new IllegalStateException("Suggested sites must have a URL, title, " +
+                                                "image URL, and background color.");
+            }
         }
 
         @Override
         public String toString() {
             return "{ url = " + url + "\n" +
                      "title = " + title + "\n" +
                      "imageUrl = " + imageUrl + "\n" +
                      "bgColor = " + bgColor + " }";
@@ -117,69 +132,72 @@ public class SuggestedSites {
     private Map<String, Site> cachedSites;
     private Locale cachedLocale;
     private Set<String> cachedBlacklist;
 
     public SuggestedSites(Context appContext) {
         context = appContext;
     }
 
-    private String loadFromFile() {
+    private Map<String, Site> loadSites(String jsonString) {
+        if (TextUtils.isEmpty(jsonString)) {
+            return null;
+        }
+
+        Map<String, Site> sites = null;
+
+        try {
+            final JSONArray jsonSites = new JSONArray(jsonString);
+            sites = new LinkedHashMap<String, Site>(jsonSites.length());
+
+            final int count = jsonSites.length();
+            for (int i = 0; i < count; i++) {
+                final Site site = new Site((JSONObject) jsonSites.get(i));
+                sites.put(site.url, site);
+            }
+        } catch (Exception e) {
+            Log.e(LOGTAG, "Failed to refresh suggested sites", e);
+            return null;
+        }
+
+        return sites;
+    }
+
+    private Map<String, Site> loadFromFile() {
         // Do nothing for now
         return null;
     }
 
-    private String loadFromResource() {
+    private Map<String, Site> loadFromResource() {
         try {
-            return RawResource.getAsString(context, R.raw.suggestedsites);
+            return loadSites(RawResource.getAsString(context, R.raw.suggestedsites));
         } catch (IOException e) {
             return null;
         }
     }
 
     /**
      * Refreshes the cached list of sites either from the default raw
      * source or standard file location. This will be called on every
      * cache miss during a {@code get()} call.
      */
     private void refresh() {
         Log.d(LOGTAG, "Refreshing tiles from file");
 
-        String jsonString = loadFromFile();
-        if (TextUtils.isEmpty(jsonString)) {
-            Log.d(LOGTAG, "No suggested sites file, loading from resource.");
-            jsonString = loadFromResource();
+        Map<String, Site> sites = loadFromFile();
+        if (sites == null) {
+            sites = loadFromResource();
         }
 
-        Map<String, Site> sites = null;
-
-        try {
-            final JSONArray jsonSites = new JSONArray(jsonString);
-            sites = new LinkedHashMap<String, Site>(jsonSites.length());
-
-            final int count = jsonSites.length();
-            for (int i = 0; i < count; i++) {
-                final JSONObject jsonSite = (JSONObject) jsonSites.get(i);
-                final String url = jsonSite.getString(JSON_KEY_URL);
-
-                final Site site = new Site(url,
-                                           jsonSite.getString(JSON_KEY_TITLE),
-                                           jsonSite.getString(JSON_KEY_IMAGE_URL),
-                                           jsonSite.getString(JSON_KEY_BG_COLOR));
-
-                sites.put(url, site);
-            }
-
-            Log.d(LOGTAG, "Successfully parsed suggested sites.");
-        } catch (Exception e) {
-            Log.e(LOGTAG, "Failed to refresh suggested sites", e);
+        // Nothing to cache, bail.
+        if (sites == null) {
             return;
         }
 
-        // Update cached list of sites
+        // Update cached list of sites.
         cachedSites = Collections.unmodifiableMap(sites);
         cachedLocale = Locale.getDefault();
     }
 
     private boolean isEnabled() {
         final SharedPreferences prefs = GeckoSharedPrefs.forApp(context);
         return prefs.getBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, true);
     }