From 184de1931eca54f6ff9ba21e366630db88dfa7b9 Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Fri, 15 Sep 2017 19:56:10 +0200 Subject: [PATCH] Make use of opoc/LanguagePreference (fixes #169) --- app/build.gradle | 18 +++++++++++ .../gsantner/opoc/ui/LanguagePreference.java | 31 ++++++++++++------- app/src/main/res/xml/preferences__master.xml | 6 +--- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index df28b437..ad5c2122 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,6 +32,7 @@ android { buildConfigField "boolean", "IS_TEST_BUILD", "false" buildConfigField "boolean", "IS_GPLAY_BUILD", "false" + buildConfigField("String[]", "APPLICATION_LANGUAGES", '{' + getUsedAndroidLanguages().collect {"\"${it}\""}.join(",") + '}') vectorDrawables.useSupportLibrary = true } @@ -92,6 +93,23 @@ dependencies { annotationProcessor "com.jakewharton:butterknife-compiler:${version_lib.butterknife}" } +@SuppressWarnings(["UnnecessaryQualifiedReference", "SpellCheckingInspection"]) +static String[] getUsedAndroidLanguages() { + Set langs = new HashSet<>() + new File('.').eachFileRecurse(groovy.io.FileType.DIRECTORIES) { + final foldername = it.name + if (foldername.startsWith('values-') && !it.canonicalPath.contains("build" + File.separator + "intermediates")) { + new File(it.toString()).eachFileRecurse(groovy.io.FileType.FILES) { + if (it.name.toLowerCase().endsWith(".xml") && it.getCanonicalFile().getText('UTF-8').contains(" @@ -55,18 +53,25 @@ import android.os.Build; import android.preference.ListPreference; import android.util.AttributeSet; +import net.gsantner.opoc.util.ContextUtils; + import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; -import net.gsantner.opoc.util.ContextUtils; - /** * A {@link android.preference.ListPreference} that displays a list of languages to select from */ -@SuppressWarnings({"unused", "SpellCheckingInspection"}) +@SuppressWarnings({"unused", "SpellCheckingInspection", "WeakerAccess"}) public class LanguagePreference extends ListPreference { + private static final String SYSTEM_LANGUAGE_CODE = ""; + public static String SYSTEM_LANGUAGE_NAME = "System"; + + // The language of res/values/ -> (usually English) + public static String DEFAULT_LANGUAGE_NAME = "English"; + public static String DEFAULT_LANGUAGE_CODE = "en"; + public LanguagePreference(Context context) { super(context); init(context, null); @@ -99,13 +104,15 @@ public class LanguagePreference extends ListPreference { } private void init(Context context, AttributeSet attrs) { + setDefaultValue(SYSTEM_LANGUAGE_CODE); + // Fetch readable details - ContextUtils ContextUtils = new ContextUtils(context); + ContextUtils contextUtils = new ContextUtils(context); List languages = new ArrayList<>(); - Object bcof = ContextUtils.getBuildConfigValue("APPLICATION_LANGUAGES"); + Object bcof = contextUtils.getBuildConfigValue("APPLICATION_LANGUAGES"); if (bcof instanceof String[]) { for (String langId : (String[]) bcof) { - Locale locale = ContextUtils.getLocaleByAndroidCode(langId); + Locale locale = contextUtils.getLocaleByAndroidCode(langId); languages.add(summarizeLocale(locale) + ";" + langId); } } @@ -120,10 +127,10 @@ public class LanguagePreference extends ListPreference { entries[i + 2] = languages.get(i).split(";")[0]; entryval[i + 2] = languages.get(i).split(";")[1]; } - entries[0] = "System"; - entryval[0] = ""; - entries[1] = "English"; - entryval[1] = "en"; + entries[0] = SYSTEM_LANGUAGE_NAME; + entryval[0] = SYSTEM_LANGUAGE_CODE; + entries[1] = DEFAULT_LANGUAGE_NAME; + entryval[1] = DEFAULT_LANGUAGE_CODE; setEntries(entries); setEntryValues(entryval); diff --git a/app/src/main/res/xml/preferences__master.xml b/app/src/main/res/xml/preferences__master.xml index 47324b73..4efbbd9b 100644 --- a/app/src/main/res/xml/preferences__master.xml +++ b/app/src/main/res/xml/preferences__master.xml @@ -17,16 +17,12 @@ android:summary="@string/pref_desc__sub_nav_slider" android:title="@string/pref_title__sub_nav_slider"/> - -