From b958754720b580c9360ed16af4204ed6dcf3e078 Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Thu, 29 May 2014 13:04:35 +0100 Subject: [PATCH] Bug 1009587 - Part 1: Store suggested sites in a Map instead of a List (r=mfinkle) --- mobile/android/base/db/SuggestedSites.java | 30 ++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/mobile/android/base/db/SuggestedSites.java b/mobile/android/base/db/SuggestedSites.java index 01b269552393..bec607998e35 100644 --- a/mobile/android/base/db/SuggestedSites.java +++ b/mobile/android/base/db/SuggestedSites.java @@ -16,8 +16,9 @@ import android.util.Log; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; -import java.util.ArrayList; +import java.util.Map; import org.json.JSONArray; import org.json.JSONException; @@ -88,11 +89,11 @@ public class SuggestedSites { } private final Context context; - private SoftReference> cachedSites; + private SoftReference> cachedSites; public SuggestedSites(Context appContext) { context = appContext; - cachedSites = new SoftReference>(null); + cachedSites = new SoftReference>(null); } private String loadFromFile() { @@ -113,7 +114,7 @@ public class SuggestedSites { * source or standard file location. This will be called on every * cache miss during a {@code get()} call. */ - private List refresh() { + private Map refresh() { Log.d(LOGTAG, "Refreshing tiles from file"); String jsonString = loadFromFile(); @@ -122,22 +123,23 @@ public class SuggestedSites { jsonString = loadFromResource(); } - List sites = null; + Map sites = null; try { final JSONArray jsonSites = new JSONArray(jsonString); - sites = new ArrayList(jsonSites.length()); + sites = new LinkedHashMap(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(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.add(site); + sites.put(url, site); } Log.d(LOGTAG, "Successfully parsed suggested sites."); @@ -147,7 +149,7 @@ public class SuggestedSites { } // Update cached list of sites - cachedSites = new SoftReference>(Collections.unmodifiableList(sites)); + cachedSites = new SoftReference>(Collections.unmodifiableMap(sites)); // Return the refreshed list return sites; @@ -182,7 +184,7 @@ public class SuggestedSites { return cursor; } - List sites = cachedSites.get(); + Map sites = cachedSites.get(); if (sites == null) { Log.d(LOGTAG, "No cached sites, refreshing."); sites = refresh(); @@ -197,9 +199,11 @@ public class SuggestedSites { final int sitesCount = sites.size(); Log.d(LOGTAG, "Number of suggested sites: " + sitesCount); - final int count = Math.min(limit, sitesCount); - for (int i = 0; i < count; i++) { - final Site site = sites.get(i); + final int maxCount = Math.min(limit, sitesCount); + for (Site site : sites.values()) { + if (cursor.getCount() == maxCount) { + break; + } if (excludeUrls != null && excludeUrls.contains(site.url)) { continue;