mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 22:01:30 +00:00
Bug 1009587 - Part 3: Hold a strong reference to cached suggested sites (r=mfinkle)
This commit is contained in:
parent
934dce9fa1
commit
46bae56365
@ -14,7 +14,6 @@ import android.text.TextUtils;
|
||||
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;
|
||||
@ -42,8 +41,7 @@ import org.mozilla.gecko.util.RawResource;
|
||||
*
|
||||
* Under the hood, {@code SuggestedSites} keeps reference to the
|
||||
* parsed list of sites to avoid reparsing the JSON file on every
|
||||
* {@code get()} call. This cached list is a soft reference and can
|
||||
* garbage collected at any moment.
|
||||
* {@code get()} call.
|
||||
*
|
||||
* The default list of suggested sites is stored in a raw Android
|
||||
* resource ({@code R.raw.suggestedsites}) which is dynamically
|
||||
@ -89,11 +87,10 @@ public class SuggestedSites {
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private SoftReference<Map<String, Site>> cachedSites;
|
||||
private Map<String, Site> cachedSites;
|
||||
|
||||
public SuggestedSites(Context appContext) {
|
||||
context = appContext;
|
||||
cachedSites = new SoftReference<Map<String, Site>>(null);
|
||||
}
|
||||
|
||||
private String loadFromFile() {
|
||||
@ -114,7 +111,7 @@ public class SuggestedSites {
|
||||
* source or standard file location. This will be called on every
|
||||
* cache miss during a {@code get()} call.
|
||||
*/
|
||||
private Map<String, Site> refresh() {
|
||||
private void refresh() {
|
||||
Log.d(LOGTAG, "Refreshing tiles from file");
|
||||
|
||||
String jsonString = loadFromFile();
|
||||
@ -145,14 +142,11 @@ public class SuggestedSites {
|
||||
Log.d(LOGTAG, "Successfully parsed suggested sites.");
|
||||
} catch (Exception e) {
|
||||
Log.e(LOGTAG, "Failed to refresh suggested sites", e);
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update cached list of sites
|
||||
cachedSites = new SoftReference<Map<String, Site>>(Collections.unmodifiableMap(sites));
|
||||
|
||||
// Return the refreshed list
|
||||
return sites;
|
||||
cachedSites = Collections.unmodifiableMap(sites);
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
@ -161,12 +155,11 @@ public class SuggestedSites {
|
||||
}
|
||||
|
||||
private Site getSiteForUrl(String url) {
|
||||
Map<String, Site> sites = cachedSites.get();
|
||||
if (sites == null) {
|
||||
if (cachedSites == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sites.get(url);
|
||||
return cachedSites.get(url);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -193,23 +186,22 @@ public class SuggestedSites {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
Map<String, Site> sites = cachedSites.get();
|
||||
if (sites == null) {
|
||||
if (cachedSites == null) {
|
||||
Log.d(LOGTAG, "No cached sites, refreshing.");
|
||||
sites = refresh();
|
||||
refresh();
|
||||
}
|
||||
|
||||
// Return empty cursor if there was an error when
|
||||
// loading the suggested sites or the list is empty.
|
||||
if (sites == null || sites.isEmpty()) {
|
||||
if (cachedSites == null || cachedSites.isEmpty()) {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
final int sitesCount = sites.size();
|
||||
final int sitesCount = cachedSites.size();
|
||||
Log.d(LOGTAG, "Number of suggested sites: " + sitesCount);
|
||||
|
||||
final int maxCount = Math.min(limit, sitesCount);
|
||||
for (Site site : sites.values()) {
|
||||
for (Site site : cachedSites.values()) {
|
||||
if (cursor.getCount() == maxCount) {
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user