From 80c98a3435e8930426a7ccfd6397afce9d09aee7 Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Fri, 21 Feb 2014 10:47:45 +0000 Subject: [PATCH] Bug 968172 - Trigger a full refresh when locale changes (r=rnewman) --- mobile/android/base/BrowserApp.java | 3 ++ mobile/android/base/home/HomeConfig.java | 5 ++++ .../base/home/HomeConfigInvalidator.java | 16 +++++++++-- .../base/home/HomeConfigPrefsBackend.java | 28 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/mobile/android/base/BrowserApp.java b/mobile/android/base/BrowserApp.java index 10e1dc01b8f..f9ea47ae4e9 100644 --- a/mobile/android/base/BrowserApp.java +++ b/mobile/android/base/BrowserApp.java @@ -24,6 +24,7 @@ import org.mozilla.gecko.health.BrowserHealthReporter; import org.mozilla.gecko.health.HealthRecorder; import org.mozilla.gecko.health.SessionInformation; import org.mozilla.gecko.home.BrowserSearch; +import org.mozilla.gecko.home.HomeConfigInvalidator; import org.mozilla.gecko.home.HomePager; import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; import org.mozilla.gecko.home.SearchEngine; @@ -1641,6 +1642,8 @@ abstract public class BrowserApp extends GeckoApp public void onLocaleReady(final String locale) { super.onLocaleReady(locale); + HomeConfigInvalidator.getInstance().onLocaleReady(locale); + if (mMenu != null) { mMenu.clear(); onCreateOptionsMenu(mMenu); diff --git a/mobile/android/base/home/HomeConfig.java b/mobile/android/base/home/HomeConfig.java index 8a82bd9ea1d..14ff45b94e8 100644 --- a/mobile/android/base/home/HomeConfig.java +++ b/mobile/android/base/home/HomeConfig.java @@ -699,6 +699,7 @@ public final class HomeConfig { public interface HomeConfigBackend { public List load(); public void save(List entries); + public String getLocale(); public void setOnChangeListener(OnChangeListener listener); } @@ -718,6 +719,10 @@ public final class HomeConfig { return mBackend.load(); } + public String getLocale() { + return mBackend.getLocale(); + } + public void save(List panelConfigs) { mBackend.save(panelConfigs); } diff --git a/mobile/android/base/home/HomeConfigInvalidator.java b/mobile/android/base/home/HomeConfigInvalidator.java index 4d4d8636569..aad5bb0d30a 100644 --- a/mobile/android/base/home/HomeConfigInvalidator.java +++ b/mobile/android/base/home/HomeConfigInvalidator.java @@ -17,6 +17,7 @@ import static org.mozilla.gecko.home.HomeConfig.createBuiltinPanelConfig; import android.content.Context; import android.os.Handler; +import android.text.TextUtils; import android.util.Log; import org.json.JSONException; @@ -79,8 +80,16 @@ public class HomeConfigInvalidator implements GeckoEventListener { GeckoAppShell.getEventDispatcher().registerEventListener(EVENT_HOMEPANELS_UPDATE, this); } - public void refreshAll() { - handlePanelUpdate(null); + public void onLocaleReady(final String locale) { + ThreadUtils.getBackgroundHandler().post(new Runnable() { + @Override + public void run() { + final String configLocale = mHomeConfig.getLocale(); + if (configLocale == null || !configLocale.equals(locale)) { + handlePanelUpdate(null); + } + } + }); } @Override @@ -128,7 +137,8 @@ public class HomeConfigInvalidator implements GeckoEventListener { } /** - * Schedules a panel update in HomeConfig. Runs in the gecko thread. + * Schedules a panel update in HomeConfig. Runs in the gecko or + * background thread. * * @param panelConfig the target PanelConfig instance or NULL to refresh * all HomeConfig entries. diff --git a/mobile/android/base/home/HomeConfigPrefsBackend.java b/mobile/android/base/home/HomeConfigPrefsBackend.java index 1e7c3b05d9b..428f28980e9 100644 --- a/mobile/android/base/home/HomeConfigPrefsBackend.java +++ b/mobile/android/base/home/HomeConfigPrefsBackend.java @@ -29,11 +29,13 @@ import android.util.Log; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import java.util.Locale; class HomeConfigPrefsBackend implements HomeConfigBackend { private static final String LOGTAG = "GeckoHomeConfigBackend"; private static final String PREFS_CONFIG_KEY = "home_panels"; + private static final String PREFS_LOCALE_KEY = "home_locale"; private final Context mContext; private PrefsListener mPrefsListener; @@ -136,9 +138,35 @@ class HomeConfigPrefsBackend implements HomeConfigBackend { final String jsonString = jsonPanelConfigs.toString(); editor.putString(PREFS_CONFIG_KEY, jsonString); + editor.putString(PREFS_LOCALE_KEY, Locale.getDefault().toString()); editor.commit(); } + @Override + public String getLocale() { + final SharedPreferences prefs = getSharedPreferences(); + + String locale = prefs.getString(PREFS_LOCALE_KEY, null); + if (locale == null) { + // Initialize config with the current locale + final String currentLocale = Locale.getDefault().toString(); + + final SharedPreferences.Editor editor = prefs.edit(); + editor.putString(PREFS_LOCALE_KEY, currentLocale); + editor.commit(); + + // If the user has saved HomeConfig before, return null this + // one time to trigger a refresh and ensure we use the + // correct locale for the saved state. For more context, + // see HomeConfigInvalidator.onLocaleReady(). + if (!prefs.contains(PREFS_CONFIG_KEY)) { + locale = currentLocale; + } + } + + return locale; + } + @Override public void setOnChangeListener(OnChangeListener listener) { final SharedPreferences prefs = getSharedPreferences();