Bug 968172 - Trigger a full refresh when locale changes (r=rnewman)

This commit is contained in:
Lucas Rocha 2014-02-21 10:47:45 +00:00
Родитель 448f168e5a
Коммит 379bbdc198
4 изменённых файлов: 49 добавлений и 3 удалений

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

@ -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);

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

@ -699,6 +699,7 @@ public final class HomeConfig {
public interface HomeConfigBackend {
public List<PanelConfig> load();
public void save(List<PanelConfig> 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<PanelConfig> panelConfigs) {
mBackend.save(panelConfigs);
}

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

@ -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.

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

@ -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();