From 96cb30fa73f1912bd9da67a6477378e1727bbb0b Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Tue, 15 Jul 2014 20:54:28 +0100 Subject: [PATCH] Bug 1012462 - Part 9: Support locale switching in SuggestedSites (r=rnewman) --- mobile/android/base/db/SuggestedSites.java | 36 +++++++++++++++++-- .../base/preferences/GeckoPreferences.java | 2 ++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/mobile/android/base/db/SuggestedSites.java b/mobile/android/base/db/SuggestedSites.java index d0b42005f44..fc1bc6d59e5 100644 --- a/mobile/android/base/db/SuggestedSites.java +++ b/mobile/android/base/db/SuggestedSites.java @@ -8,6 +8,7 @@ package org.mozilla.gecko.db; import android.content.Context; import android.content.ContentResolver; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.database.MatrixCursor; import android.database.MatrixCursor.RowBuilder; @@ -71,6 +72,9 @@ public class SuggestedSites { // SharedPreference key for suggested sites that should be 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. private static final String FILENAME = "suggestedsites.json"; @@ -144,7 +148,6 @@ public class SuggestedSites { private final Distribution distribution; private final File file; private Map cachedSites; - private Locale cachedLocale; private Set cachedBlacklist; public SuggestedSites(Context appContext) { @@ -162,6 +165,19 @@ public class SuggestedSites { 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. */ @@ -344,7 +360,7 @@ public class SuggestedSites { private synchronized void setCachedSites(Map 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() { final SharedPreferences prefs = GeckoSharedPrefs.forApp(context); return prefs.getBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, true); @@ -424,7 +446,15 @@ public class SuggestedSites { 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."); refresh(); } diff --git a/mobile/android/base/preferences/GeckoPreferences.java b/mobile/android/base/preferences/GeckoPreferences.java index c1bf4a147ad..d407ef31599 100644 --- a/mobile/android/base/preferences/GeckoPreferences.java +++ b/mobile/android/base/preferences/GeckoPreferences.java @@ -263,6 +263,8 @@ OnSharedPreferenceChangeListener return; } + refreshSuggestedSites(); + // Cause the current fragment to redisplay, the hard way. // This avoids nonsense with trying to reach inside fragments and force them // to redisplay themselves.