From 362ec885feea1e7aa126ae7b30a9926316f127b7 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 25 Jan 2016 19:56:26 +0100 Subject: [PATCH] Avoid looking up all locales for system language --- .../calculator/language/Languages.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/language/Languages.java b/app/src/main/java/org/solovyev/android/calculator/language/Languages.java index 06157df2..8b9a240f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/language/Languages.java +++ b/app/src/main/java/org/solovyev/android/calculator/language/Languages.java @@ -21,8 +21,8 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan public static final String SYSTEM_LANGUAGE_CODE = "00"; @Nonnull public static final Language SYSTEM_LANGUAGE = new Language(SYSTEM_LANGUAGE_CODE, Locale.getDefault()); - @Nonnull - private static final Locale[] locales = Locale.getAvailableLocales(); + @Nullable + private static Locale[] locales; @Nonnull private final List list = new ArrayList<>(); @Nonnull @@ -43,7 +43,7 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan @Nullable private static Locale findLocaleById(@Nonnull String id) { - for (Locale locale : locales) { + for (Locale locale : getLocales()) { if (TextUtils.equals(locale.toString(), id)) { return locale; } @@ -57,7 +57,7 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan language = id; } - for (Locale locale : locales) { + for (Locale locale : getLocales()) { if (TextUtils.equals(locale.getLanguage(), language)) { return locale; } @@ -67,6 +67,14 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan return null; } + @Nonnull + public static Locale[] getLocales() { + if (locales == null) { + locales = Locale.getAvailableLocales(); + } + return locales; + } + /** * This method should be called only when default values have been set to application's preferences */ @@ -128,6 +136,10 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan @Nonnull public Language get(@Nonnull String code) { + if (TextUtils.equals(SYSTEM_LANGUAGE.code, code)) { + // quick check to avoid list loading + return SYSTEM_LANGUAGE; + } Language language = findLanguageByCode(code); if (language != null) { return language;