Bug 917480 - Part 4: handle resetting to the system default locale. r=nalexander

This commit is contained in:
Richard Newman 2014-04-29 13:41:38 -07:00
parent a3baca5b53
commit dab6073463
4 changed files with 46 additions and 16 deletions

View File

@ -50,9 +50,10 @@ public class BrowserLocaleManager implements LocaleManager {
private static final String FALLBACK_LOCALE_TAG = "en-US";
// This is volatile because we don't impose restrictions
// These are volatile because we don't impose restrictions
// over which thread calls our methods.
private volatile Locale currentLocale = null;
private volatile Locale systemLocale = Locale.getDefault();
private AtomicBoolean inited = new AtomicBoolean(false);
private boolean systemLocaleDidChange = false;
@ -131,6 +132,12 @@ public class BrowserLocaleManager implements LocaleManager {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// We don't trust Locale.getDefault() here, because we make a
// habit of mutating it! Use the one Android supplies, because
// that gets regularly reset.
// The default value of systemLocale is fine, because we haven't
// yet swizzled Locale during static initialization.
systemLocale = context.getResources().getConfiguration().locale;
systemLocaleDidChange = true;
}
};
@ -220,6 +227,20 @@ public class BrowserLocaleManager implements LocaleManager {
return resultant;
}
@Override
public void resetToSystemLocale(Context context) {
// Wipe the pref.
final SharedPreferences settings = getSharedPreferences(context);
settings.edit().remove(PREF_LOCALE).commit();
// Apply the system locale.
updateLocale(context, systemLocale);
// Tell Gecko.
GeckoEvent ev = GeckoEvent.createBroadcastEvent(EVENT_LOCALE_CHANGED, "");
GeckoAppShell.sendEventToGecko(ev);
}
/**
* This is public to allow for an activity to force the
* current locale to be applied if necessary (e.g., when
@ -284,8 +305,12 @@ public class BrowserLocaleManager implements LocaleManager {
final Locale locale = parseLocaleCode(localeCode);
return updateLocale(context, locale);
}
private String updateLocale(Context context, final Locale locale) {
// Fast path.
if (defaultLocale.equals(locale)) {
if (Locale.getDefault().equals(locale)) {
return null;
}

View File

@ -18,4 +18,5 @@ public interface LocaleManager {
void updateConfiguration(Context context, Locale locale);
String setSelectedLocale(Context context, String localeCode);
boolean systemLocaleDidChange();
void resetToSystemLocale(Context context);
}

View File

@ -717,11 +717,6 @@ public class GeckoPreferences
* onActivityResult mechanism: see {@link BrowserApp#onActivityResult(int, int, Intent)}.
*/
private boolean onLocaleSelected(final String newValue) {
if (newValue.equals("")) {
// TODO: reset our locale to match system.
return false;
}
final Context context = getApplicationContext();
// LocaleManager operations need to occur on the background thread.
@ -731,8 +726,13 @@ public class GeckoPreferences
@Override
public void run() {
final LocaleManager localeManager = BrowserLocaleManager.getInstance();
if (null == localeManager.setSelectedLocale(context, newValue)) {
localeManager.updateConfiguration(context, Locale.getDefault());
if (newValue.equals("")) {
BrowserLocaleManager.getInstance().resetToSystemLocale(context);
} else {
if (null == localeManager.setSelectedLocale(context, newValue)) {
localeManager.updateConfiguration(context, Locale.getDefault());
}
}
ThreadUtils.postToUiThread(new Runnable() {

View File

@ -1612,14 +1612,18 @@ var BrowserApp = {
#endif
case "Locale:Changed":
// The value provided to Locale:Changed should be a BCP47 language tag
// understood by Gecko -- for example, "es-ES" or "de".
console.log("Locale:Changed: " + aData);
if (aData) {
// The value provided to Locale:Changed should be a BCP47 language tag
// understood by Gecko -- for example, "es-ES" or "de".
console.log("Locale:Changed: " + aData);
Services.prefs.setCharPref("general.useragent.locale", aData);
} else {
// Resetting.
console.log("Switching to system locale.");
Services.prefs.clearUserPref("general.useragent.locale");
}
// TODO: do we need to be more nuanced here -- e.g., checking for the
// OS locale -- or should it always be false on Fennec?
Services.prefs.setBoolPref("intl.locale.matchOS", false);
Services.prefs.setCharPref("general.useragent.locale", aData);
Services.prefs.setBoolPref("intl.locale.matchOS", !aData);
break;
default: