Skip setting the locale if system default is used

This commit is contained in:
goapunk
2018-11-07 17:27:38 +01:00
committed by akwizgran
parent c247e3aa4e
commit 5b77f28ce0
2 changed files with 32 additions and 18 deletions

View File

@@ -81,6 +81,7 @@ public class BriarApplicationImpl extends Application
rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO); rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO);
LOG.info("Created"); LOG.info("Created");
Localizer.getInstance().setLocaleLegacy(this);
EmojiManager.install(new GoogleEmojiProvider()); EmojiManager.install(new GoogleEmojiProvider());
} }
@@ -104,7 +105,8 @@ public class BriarApplicationImpl extends Application
@Override @Override
public void onConfigurationChanged(@NonNull Configuration newConfig) { public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
Localizer.getInstance().setLocale(this); Localizer.getInstance()
.applicationConfigurationChanged(this);
} }
private void setTheme(Context ctx, SharedPreferences prefs) { private void setTheme(Context ctx, SharedPreferences prefs) {

View File

@@ -23,6 +23,7 @@ public class Localizer {
@Nullable @Nullable
private static Localizer INSTANCE; private static Localizer INSTANCE;
private final Locale systemLocale; private final Locale systemLocale;
@Nullable
private final Locale locale; private final Locale locale;
private Localizer(SharedPreferences sharedPreferences) { private Localizer(SharedPreferences sharedPreferences) {
@@ -32,11 +33,17 @@ public class Localizer {
private Localizer(Locale systemLocale, @Nullable Locale userLocale) { private Localizer(Locale systemLocale, @Nullable Locale userLocale) {
this.systemLocale = systemLocale; this.systemLocale = systemLocale;
if (userLocale == null) locale = systemLocale; locale = userLocale;
else locale = userLocale; setLocaleAndSystemConfiguration(locale);
setLocaleAndSystemConfiguration();
} }
private Localizer(Locale systemLocale) {
this.systemLocale = systemLocale;
locale = null;
setLocaleAndSystemConfiguration(systemLocale);
}
// Instantiate the Localizer. // Instantiate the Localizer.
public static synchronized void initialize(SharedPreferences prefs) { public static synchronized void initialize(SharedPreferences prefs) {
if (INSTANCE == null) if (INSTANCE == null)
@@ -45,9 +52,9 @@ public class Localizer {
// Reinstantiate the Localizer with the system locale // Reinstantiate the Localizer with the system locale
public static synchronized void reinitialize(Context appContext) { public static synchronized void reinitialize(Context appContext) {
if (INSTANCE != null) { if (INSTANCE != null && INSTANCE.locale != null) {
INSTANCE = new Localizer(INSTANCE.systemLocale, null); INSTANCE = new Localizer(INSTANCE.systemLocale);
INSTANCE.forceLocale(appContext); INSTANCE.forceLocale(appContext, INSTANCE.systemLocale);
} }
} }
@@ -75,26 +82,25 @@ public class Localizer {
// Returns the localized version of context // Returns the localized version of context
public Context setLocale(Context context) { public Context setLocale(Context context) {
if (locale == null || SDK_INT < 17) return context;
Resources res = context.getResources(); Resources res = context.getResources();
Configuration conf = res.getConfiguration(); Configuration conf = res.getConfiguration();
updateConfiguration(conf, locale); updateConfiguration(conf, locale);
if (SDK_INT >= 17) return context.createConfigurationContext(conf);
return context.createConfigurationContext(conf); }
//noinspection deprecation
// Use the old API on < 17 // For API < 17 only.
res.updateConfiguration(conf, res.getDisplayMetrics()); public void setLocaleLegacy(Context appContext) {
return context; if (SDK_INT >= 17 || locale == null) return;
forceLocale(appContext, locale);
} }
// Forces the update of the resources through the deprecated API. // Forces the update of the resources through the deprecated API.
// Necessary on API >= 17 to update the foreground notification if the private void forceLocale(Context context, Locale locale) {
// account was deleted.
public void forceLocale(Context context) {
Resources res = context.getResources(); Resources res = context.getResources();
Configuration conf = res.getConfiguration(); Configuration conf = res.getConfiguration();
updateConfiguration(conf, locale); updateConfiguration(conf, locale);
//noinspection deprecation //noinspection deprecation
// Use the old API on < 17
res.updateConfiguration(conf, res.getDisplayMetrics()); res.updateConfiguration(conf, res.getDisplayMetrics());
} }
@@ -105,7 +111,8 @@ public class Localizer {
conf.locale = locale; conf.locale = locale;
} }
private void setLocaleAndSystemConfiguration() { private void setLocaleAndSystemConfiguration(@Nullable Locale locale) {
if (locale == null) return;
Locale.setDefault(locale); Locale.setDefault(locale);
if (SDK_INT >= 23) return; if (SDK_INT >= 23) return;
Configuration systemConfiguration = Configuration systemConfiguration =
@@ -116,6 +123,11 @@ public class Localizer {
Resources.getSystem().getDisplayMetrics()); Resources.getSystem().getDisplayMetrics());
} }
public void applicationConfigurationChanged(Context appContext) {
setLocaleAndSystemConfiguration(locale);
if (SDK_INT < 17) setLocaleLegacy(appContext);
}
// Indicates whether the language represented by locale // Indicates whether the language represented by locale
// should be offered to the user on this device. // should be offered to the user on this device.
// * Android doesn't pick up Asturian on API < 21 // * Android doesn't pick up Asturian on API < 21