diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java index 58b8b05b5..40edc6079 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java @@ -81,6 +81,7 @@ public class BriarApplicationImpl extends Application rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO); LOG.info("Created"); + Localizer.getInstance().setLocaleLegacy(this); EmojiManager.install(new GoogleEmojiProvider()); } @@ -104,7 +105,8 @@ public class BriarApplicationImpl extends Application @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); - Localizer.getInstance().setLocale(this); + Localizer.getInstance() + .applicationConfigurationChanged(this); } private void setTheme(Context ctx, SharedPreferences prefs) { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index 358bb2c2f..f36e1ff24 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -23,6 +23,7 @@ public class Localizer { @Nullable private static Localizer INSTANCE; private final Locale systemLocale; + @Nullable private final Locale locale; private Localizer(SharedPreferences sharedPreferences) { @@ -32,11 +33,17 @@ public class Localizer { private Localizer(Locale systemLocale, @Nullable Locale userLocale) { this.systemLocale = systemLocale; - if (userLocale == null) locale = systemLocale; - else locale = userLocale; - setLocaleAndSystemConfiguration(); + locale = userLocale; + setLocaleAndSystemConfiguration(locale); } + private Localizer(Locale systemLocale) { + this.systemLocale = systemLocale; + locale = null; + setLocaleAndSystemConfiguration(systemLocale); + } + + // Instantiate the Localizer. public static synchronized void initialize(SharedPreferences prefs) { if (INSTANCE == null) @@ -45,9 +52,9 @@ public class Localizer { // Reinstantiate the Localizer with the system locale public static synchronized void reinitialize(Context appContext) { - if (INSTANCE != null) { - INSTANCE = new Localizer(INSTANCE.systemLocale, null); - INSTANCE.forceLocale(appContext); + if (INSTANCE != null && INSTANCE.locale != null) { + INSTANCE = new Localizer(INSTANCE.systemLocale); + INSTANCE.forceLocale(appContext, INSTANCE.systemLocale); } } @@ -75,26 +82,25 @@ public class Localizer { // Returns the localized version of context public Context setLocale(Context context) { + if (locale == null || SDK_INT < 17) return context; Resources res = context.getResources(); Configuration conf = res.getConfiguration(); updateConfiguration(conf, locale); - if (SDK_INT >= 17) - return context.createConfigurationContext(conf); - //noinspection deprecation - // Use the old API on < 17 - res.updateConfiguration(conf, res.getDisplayMetrics()); - return context; + return context.createConfigurationContext(conf); + } + + // For API < 17 only. + public void setLocaleLegacy(Context appContext) { + if (SDK_INT >= 17 || locale == null) return; + forceLocale(appContext, locale); } // Forces the update of the resources through the deprecated API. - // Necessary on API >= 17 to update the foreground notification if the - // account was deleted. - public void forceLocale(Context context) { + private void forceLocale(Context context, Locale locale) { Resources res = context.getResources(); Configuration conf = res.getConfiguration(); updateConfiguration(conf, locale); //noinspection deprecation - // Use the old API on < 17 res.updateConfiguration(conf, res.getDisplayMetrics()); } @@ -105,7 +111,8 @@ public class Localizer { conf.locale = locale; } - private void setLocaleAndSystemConfiguration() { + private void setLocaleAndSystemConfiguration(@Nullable Locale locale) { + if (locale == null) return; Locale.setDefault(locale); if (SDK_INT >= 23) return; Configuration systemConfiguration = @@ -116,6 +123,11 @@ public class Localizer { Resources.getSystem().getDisplayMetrics()); } + public void applicationConfigurationChanged(Context appContext) { + setLocaleAndSystemConfiguration(locale); + if (SDK_INT < 17) setLocaleLegacy(appContext); + } + // Indicates whether the language represented by locale // should be offered to the user on this device. // * Android doesn't pick up Asturian on API < 21