From 07fc20bc0d27a528b7cb13ca2006599c509fa56b Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Wed, 15 Nov 2017 11:48:26 +0100 Subject: [PATCH] Add in-app language switcher Closes #53 --- .../andotp/Activities/MainActivity.java | 3 ++- .../andotp/Activities/SettingsActivity.java | 3 ++- .../andotp/Activities/ThemedActivity.java | 27 ++++++++++++++++++- .../flocke/andotp/Utilities/Settings.java | 10 +++++++ .../flocke/andotp/Utilities/Tools.java | 10 +++++++ app/src/main/res/values/settings.xml | 15 +++++++++++ app/src/main/res/values/strings_settings.xml | 14 ++++++++++ app/src/main/res/xml/preferences.xml | 8 ++++++ 8 files changed, 87 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java index 8842813a..617b349f 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java @@ -366,7 +366,8 @@ public class MainActivity extends BaseActivity key.equals(getString(R.string.settings_key_tap_to_reveal)) || key.equals(getString(R.string.settings_key_label_scroll))) { adapter.notifyDataSetChanged(); - } else if (key.equals(getString(R.string.settings_key_theme))) { + } else if (key.equals(getString(R.string.settings_key_theme)) || + key.equals(getString(R.string.settings_key_lang))) { recreate(); } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java index 0024b0e8..6f85ccad 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/SettingsActivity.java @@ -85,7 +85,8 @@ public class SettingsActivity extends BaseActivity } public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { - if (key.equals(getString(R.string.settings_key_theme))) { + if (key.equals(getString(R.string.settings_key_theme)) || + key.equals(getString(R.string.settings_key_lang))) { recreate(); } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/ThemedActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/ThemedActivity.java index 71cbe2a9..1824dbad 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/ThemedActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/ThemedActivity.java @@ -22,12 +22,15 @@ package org.shadowice.flocke.andotp.Activities; +import android.content.res.Configuration; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import org.shadowice.flocke.andotp.R; import org.shadowice.flocke.andotp.Utilities.Settings; +import java.util.Locale; + public abstract class ThemedActivity extends AppCompatActivity { public Settings settings; @@ -35,6 +38,20 @@ public abstract class ThemedActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { settings = new Settings(this); + setTheme(); + setLocale(); + + super.onCreate(savedInstanceState); + } + + @Override + public void onResume() { + setLocale(); + + super.onResume(); + } + + public void setTheme() { String theme = settings.getTheme(); if (theme.equals("light")) { @@ -44,7 +61,15 @@ public abstract class ThemedActivity extends AppCompatActivity { } else if (theme.equals("black")) { setTheme(R.style.AppTheme_Black_NoActionBar); } + } - super.onCreate(savedInstanceState); + public void setLocale() { + Locale locale = settings.getLang(); + Locale.setDefault(locale); + + Configuration config = new Configuration(); + config.locale = locale; + + getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java index 6aaa1889..b9a9a4e6 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java @@ -36,6 +36,7 @@ import java.io.File; import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.util.Collections; +import java.util.Locale; import java.util.Set; import static org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference.KEY_ALIAS; @@ -202,6 +203,15 @@ public class Settings { return settings.getStringSet(getResString(R.string.settings_key_panic), Collections.emptySet()); } + public Locale getLang() { + String lang = getString(R.string.settings_key_lang, R.string.settings_default_lang); + + if (lang.equals("system")) + return Tools.getSystemLocale(); + else + return new Locale(lang); + } + public String getTheme() { return getString(R.string.settings_key_theme, R.string.settings_default_theme); } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java index ef434d77..faefd08e 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java @@ -29,9 +29,11 @@ import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.net.Uri; +import android.os.Build; import android.os.Environment; import java.io.File; +import java.util.Locale; public class Tools { /* Checks if external storage is available for read and write */ @@ -70,4 +72,12 @@ public class Tools { File dir = new File(path); return dir.exists() || dir.mkdirs(); } + + public static Locale getSystemLocale() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Resources.getSystem().getConfiguration().getLocales().get(0); + } else { + return Resources.getSystem().getConfiguration().locale; + } + } } diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index fc4146e7..f6146bf8 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -11,6 +11,7 @@ pref_auth_pin_hash pref_panic + pref_lang pref_theme pref_label_size pref_label_scroll @@ -30,6 +31,7 @@ none + system light 8 @@ -46,6 +48,19 @@ settings + + system + en + cs + de + es + fr + gl + nl + pl + ru + + light dark diff --git a/app/src/main/res/values/strings_settings.xml b/app/src/main/res/values/strings_settings.xml index 3202f1f7..aebf17e4 100644 --- a/app/src/main/res/values/strings_settings.xml +++ b/app/src/main/res/values/strings_settings.xml @@ -14,6 +14,7 @@ PIN Panic Trigger + Language Theme Label font size Scroll label @@ -64,6 +65,19 @@ Reset app settings + + System default + English + Czech + German + Spanish + French + Galician + Dutch + Polish + Russian + + Light theme Dark theme diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index fdfc2aef..8c32e7ea 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -37,6 +37,14 @@ + +