Bug 1012462 - Part 9: Support locale switching in SuggestedSites (r=rnewman)

This commit is contained in:
Lucas Rocha 2014-07-15 20:54:28 +01:00
parent 213824ecef
commit 96cb30fa73
2 changed files with 35 additions and 3 deletions

View File

@ -8,6 +8,7 @@ package org.mozilla.gecko.db;
import android.content.Context; import android.content.Context;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor; import android.database.Cursor;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder; import android.database.MatrixCursor.RowBuilder;
@ -71,6 +72,9 @@ public class SuggestedSites {
// SharedPreference key for suggested sites that should be hidden. // SharedPreference key for suggested sites that should be hidden.
public static final String PREF_SUGGESTED_SITES_HIDDEN = "suggestedSites.hidden"; public static final String PREF_SUGGESTED_SITES_HIDDEN = "suggestedSites.hidden";
// Locale used to generate the current suggested sites.
public static final String PREF_SUGGESTED_SITES_LOCALE = "suggestedSites.locale";
// File in profile dir with the list of suggested sites. // File in profile dir with the list of suggested sites.
private static final String FILENAME = "suggestedsites.json"; private static final String FILENAME = "suggestedsites.json";
@ -144,7 +148,6 @@ public class SuggestedSites {
private final Distribution distribution; private final Distribution distribution;
private final File file; private final File file;
private Map<String, Site> cachedSites; private Map<String, Site> cachedSites;
private Locale cachedLocale;
private Set<String> cachedBlacklist; private Set<String> cachedBlacklist;
public SuggestedSites(Context appContext) { public SuggestedSites(Context appContext) {
@ -162,6 +165,19 @@ public class SuggestedSites {
this.file = file; this.file = file;
} }
private static boolean isNewLocale(Context context, Locale requestedLocale) {
final SharedPreferences prefs = GeckoSharedPrefs.forProfile(context);
String locale = prefs.getString(PREF_SUGGESTED_SITES_LOCALE, null);
if (locale == null) {
// Initialize config with the current locale
updateSuggestedSitesLocale(context);
return true;
}
return !TextUtils.equals(requestedLocale.toString(), locale);
}
/** /**
* Return the current locale and its fallback (en_US) in order. * Return the current locale and its fallback (en_US) in order.
*/ */
@ -344,7 +360,7 @@ public class SuggestedSites {
private synchronized void setCachedSites(Map<String, Site> sites) { private synchronized void setCachedSites(Map<String, Site> sites) {
cachedSites = Collections.unmodifiableMap(sites); cachedSites = Collections.unmodifiableMap(sites);
cachedLocale = Locale.getDefault(); updateSuggestedSitesLocale(context);
} }
/** /**
@ -366,6 +382,12 @@ public class SuggestedSites {
} }
} }
private static void updateSuggestedSitesLocale(Context context) {
final Editor editor = GeckoSharedPrefs.forProfile(context).edit();
editor.putString(PREF_SUGGESTED_SITES_LOCALE, Locale.getDefault().toString());
editor.commit();
}
private boolean isEnabled() { private boolean isEnabled() {
final SharedPreferences prefs = GeckoSharedPrefs.forApp(context); final SharedPreferences prefs = GeckoSharedPrefs.forApp(context);
return prefs.getBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, true); return prefs.getBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, true);
@ -424,7 +446,15 @@ public class SuggestedSites {
return cursor; return cursor;
} }
if (cachedSites == null || !locale.equals(cachedLocale)) { final boolean isNewLocale = isNewLocale(context, locale);
// Force the suggested sites file in profile dir to be re-generated
// if the locale has changed.
if (isNewLocale) {
file.delete();
}
if (cachedSites == null || isNewLocale) {
Log.d(LOGTAG, "No cached sites, refreshing."); Log.d(LOGTAG, "No cached sites, refreshing.");
refresh(); refresh();
} }

View File

@ -263,6 +263,8 @@ OnSharedPreferenceChangeListener
return; return;
} }
refreshSuggestedSites();
// Cause the current fragment to redisplay, the hard way. // Cause the current fragment to redisplay, the hard way.
// This avoids nonsense with trying to reach inside fragments and force them // This avoids nonsense with trying to reach inside fragments and force them
// to redisplay themselves. // to redisplay themselves.