From d703685df1ebc0ff83852ed2451d3bda8c86b2ba Mon Sep 17 00:00:00 2001 From: k3b Date: Tue, 18 Jul 2017 07:41:55 +0200 Subject: [PATCH] #21: Added more inline doc for localisation. Backport from https://github.com/k3b/ToGoZip/issues/6 --- .../k3b/android/androFotoFinder/Global.java | 2 ++ .../androFotoFinder/SettingsActivity.java | 11 +++++++--- .../k3b/android/widget/LocalizedActivity.java | 22 ++++++++++++------- app/src/main/res/values/arrays.xml | 2 +- app/src/main/res/xml/preferences.xml | 1 + 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/Global.java b/app/src/main/java/de/k3b/android/androFotoFinder/Global.java index 08ee204c..ca507ad1 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/Global.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/Global.java @@ -40,6 +40,7 @@ public class Global { /** LOG_CONTEXT is used as logging source for filtering logging messages that belong to this */ public static final String LOG_CONTEXT = "k3bFoto"; + /** local settings: which language should the gui use */ public static final String PREF_KEY_USER_LOCALE = "user_locale"; /** @@ -133,6 +134,7 @@ public static void debugMemory(String modul, String message) { } } + /** Remember ininial language settings. This allows setting "switch back to device language" after changing app locale */ public static Locale systemLocale = Locale.getDefault(); /** move some pre-defined menu-actions into the "more..." submenue */ diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java b/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java index e3fa9204..6e8460b6 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java @@ -55,14 +55,14 @@ public class SettingsActivity extends PreferenceActivity { private static Boolean sOldEnableNonStandardIptcMediaScanner = null; private SharedPreferences prefsInstance = null; - private ListPreference defaultLocalePreference; + private ListPreference defaultLocalePreference; // #21: Support to change locale at runtime private ListPreference mediaUpdateStrategyPreference; private int INSTALL_REQUEST_CODE = 1927; @Override protected void onCreate(final Bundle savedInstanceState) { - LocalizedActivity.fixLocale(this); + LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime super.onCreate(savedInstanceState); final Intent intent = getIntent(); if (Global.debugEnabled && (intent != null)){ @@ -74,9 +74,9 @@ protected void onCreate(final Bundle savedInstanceState) { .getDefaultSharedPreferences(this); global2Prefs(this.getApplication()); + // #21: Support to change locale at runtime defaultLocalePreference = (ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE); - defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { @@ -119,6 +119,8 @@ public boolean onPreferenceClick(Preference preference) { return false; // donot close } }); + + // #21: Support to change locale at runtime updateSummary(); } @@ -350,6 +352,8 @@ public static void show(Activity parent) { Intent intent = new Intent(parent, SettingsActivity.class); parent.startActivity(intent); } + + // #21: Support to change locale at runtime // This is used to show the status of some preference in the description private void updateSummary() { final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, ""); @@ -359,6 +363,7 @@ private void updateSummary() { about.setTitle(AboutDialogPreference.getAboutTitle(this)); } + // #21: Support to change locale at runtime private void setLanguage(String languageKey) { setPref(languageKey, defaultLocalePreference, R.array.pref_locale_names); } diff --git a/app/src/main/java/de/k3b/android/widget/LocalizedActivity.java b/app/src/main/java/de/k3b/android/widget/LocalizedActivity.java index 3e6c291a..1621f9a0 100644 --- a/app/src/main/java/de/k3b/android/widget/LocalizedActivity.java +++ b/app/src/main/java/de/k3b/android/widget/LocalizedActivity.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2015 by k3b. + * Copyright (c) 2015-2017 by k3b. * - * This file is part of AndroFotoFinder. + * This file is part of AndroFotoFinder and of ToGoZip. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by @@ -19,12 +19,13 @@ package de.k3b.android.widget; -import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; @@ -39,7 +40,6 @@ * * Created by k3b on 07.01.2016. */ - public abstract class LocalizedActivity extends Activity { /** if myLocale != Locale.Default : activity must be recreated in on resume */ private Locale myLocale = null; @@ -57,7 +57,7 @@ protected void onResume() { // Locale has changed by other Activity ? if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) { myLocale = null; - recreate(); + recreate(LocalizedActivity.this); } } @@ -71,7 +71,7 @@ public static void fixLocale(Context context) .getDefaultSharedPreferences(context); String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, ""); Locale locale = Global.systemLocale; // in case that setting=="use android-locale" - if ((language != null) && (!language.isEmpty())) { + if ((language != null) && (language.length() > 0)) { locale = new Locale(language); // overwrite "use android-locale" } @@ -93,9 +93,15 @@ public static void fixLocale(Context context) public static void recreate(Activity child) { Activity context = child; while (context != null) { - context.recreate(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + context.recreate(); + } else { + // https://stackoverflow.com/questions/11495130/android-recreate-functions-in-api-7 + context.startActivity(new Intent(context, context.getClass())); + context.finish(); + } context = context.getParent(); } - } + } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 6bfae864..321155c6 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -15,7 +15,7 @@ XC - + @string/settings_locale_os_language العربية (Arabic) diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 514e35f7..af4db26c 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -5,6 +5,7 @@ android:title="@string/settings_title" android:key="pref_category_first"> +