Bug 1391413: Add SuggestedSites.cachedDistributionSites and accessors. r=sebastian

MozReview-Commit-ID: HBqKc8a0tNJ

--HG--
extra : rebase_source : 432c1cdab9bc37473cb9da72b066153a83c45139
This commit is contained in:
Michael Comella 2017-09-07 15:18:41 -07:00
Родитель e61c00ac24
Коммит a13f081faf
1 изменённых файлов: 39 добавлений и 0 удалений

Просмотреть файл

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@ -151,6 +152,7 @@ public class SuggestedSites {
final Distribution distribution;
private File cachedFile;
private Map<String, Site> cachedSites;
private Map<String, Site> cachedDistributionSites; // to be kept in sync with cachedSites.
private Set<String> cachedBlacklist;
public SuggestedSites(Context appContext) {
@ -379,6 +381,30 @@ public class SuggestedSites {
private synchronized void setCachedSites(Map<String, Site> sites) {
cachedSites = Collections.unmodifiableMap(sites);
updateSuggestedSitesLocale(context);
cachedDistributionSites = getDistributionSites(cachedSites, loadFromResource());
}
/**
* Gets the list of distribution sites from the list of all suggested sites and those drawn from the resources.
*
* This isn't the most efficient way to get the distribution sites, especially since we currently call
* {@link #loadFromResource()} an additional time to get our list of sites in resources. However, I
* found this to be the simplest approach. One alternative was to store the is-from-distribution state in
* the saved-site JSON files but the same code that reads these files also reads the distribution files and
* the resource file so each of these would also need explicit `isFromDistribution` flags, which is annoying
* to write and would create a lot of churn. I found the addition of this method and a cached value to be much
* simpler to add (and perhaps later remove).
*/
private static Map<String, Site> getDistributionSites(final Map<String, Site> allSuggestedSites,
final Map<String, Site> suggestedSitesFromResources) {
final Set<String> allSitesURLsCopy = new HashSet<>(allSuggestedSites.keySet());
allSitesURLsCopy.removeAll(suggestedSitesFromResources.keySet());
final Map<String, Site> distributionSites = new HashMap<>(allSitesURLsCopy.size());
for (final String distributionSiteURL : allSitesURLsCopy) {
distributionSites.put(distributionSiteURL, allSuggestedSites.get(distributionSiteURL));
}
return distributionSites;
}
/**
@ -515,6 +541,19 @@ public class SuggestedSites {
return (getSiteForUrl(url) != null);
}
/**
* Returns whether or not the url both represents a suggested site
* and was added to suggested sites by a distribution.
*
* We synchronize over our access to {@link #cachedDistributionSites},
* like we synchronize over all uses of {@link #cachedSites}.
*/
public synchronized boolean containsSiteAndSiteIsFromDistribution(final String url) {
return cachedDistributionSites != null &&
contains(url) &&
cachedDistributionSites.containsKey(url);
}
public String getImageUrlForUrl(String url) {
final Site site = getSiteForUrl(url);
return (site != null ? site.imageUrl : null);