From 09a76c29fe384696e8024937fcf33c0368ceaf82 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sat, 14 Oct 2023 19:32:34 +0200 Subject: [PATCH] Upate locale setting, Update version --- app/build.gradle | 4 +- .../code_guard/BaseActivity.java | 6 +-- .../code_guard/fragment/SettingsFragment.java | 13 +++--- .../code_guard/util/AppLocale.java | 41 +++++++++++++++++++ .../code_guard/util/SettingsUtil.java | 14 ++++--- app/src/main/res/menu/menu_otps.xml | 2 +- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/com/cringe_studios/code_guard/util/AppLocale.java diff --git a/app/build.gradle b/app/build.gradle index 740c70e..084119e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,8 @@ android { applicationId "com.cringe_studios.code_guard" minSdk 21 targetSdk 34 - versionCode 4 - versionName "1.0.0_alpha3" + versionCode 5 + versionName "1.0.0_beta1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/cringe_studios/code_guard/BaseActivity.java b/app/src/main/java/com/cringe_studios/code_guard/BaseActivity.java index 4f9ff89..4cfb475 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/BaseActivity.java +++ b/app/src/main/java/com/cringe_studios/code_guard/BaseActivity.java @@ -9,6 +9,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.cringe_studios.code_guard.unlock.UnlockContract; +import com.cringe_studios.code_guard.util.AppLocale; import com.cringe_studios.code_guard.util.SettingsUtil; import com.cringe_studios.code_guard.util.ThemeUtil; @@ -46,10 +47,9 @@ public class BaseActivity extends AppCompatActivity { startUnlockActivity.launch(null); } - public void setLocale(Locale locale) { - Locale.setDefault(locale); + public void setLocale(AppLocale locale) { Configuration config = new Configuration(); - config.setLocale(locale); + config.setLocale(locale == AppLocale.SYSTEM_DEFAULT ? Locale.getDefault() : locale.getLocale()); getResources().updateConfiguration(config, getResources().getDisplayMetrics()); } diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/SettingsFragment.java index e9474ca..f6bbd4f 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/SettingsFragment.java @@ -27,6 +27,7 @@ import com.cringe_studios.code_guard.databinding.FragmentSettingsBinding; import com.cringe_studios.code_guard.icon.IconPack; import com.cringe_studios.code_guard.icon.IconPackListAdapter; import com.cringe_studios.code_guard.icon.IconUtil; +import com.cringe_studios.code_guard.util.AppLocale; import com.cringe_studios.code_guard.util.Appearance; import com.cringe_studios.code_guard.util.BackupException; import com.cringe_studios.code_guard.util.BiometricUtil; @@ -40,7 +41,6 @@ import com.cringe_studios.code_guard.util.Theme; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Locale; import javax.crypto.SecretKey; @@ -58,19 +58,18 @@ public class SettingsFragment extends NamedFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = FragmentSettingsBinding.inflate(inflater); - Locale[] locales = new Locale[] {Locale.ENGLISH, Locale.GERMAN}; - String[] localeNames = new String[locales.length]; - for(int i = 0; i < locales.length; i++) { - localeNames[i] = locales[i].getDisplayName(locales[i]); + String[] localeNames = new String[AppLocale.values().length]; + for(int i = 0; i < localeNames.length; i++) { + localeNames[i] = AppLocale.values()[i].getName(requireContext()); } binding.settingsLanguage.setAdapter(new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, localeNames)); - binding.settingsLanguage.setSelection(Arrays.asList(locales).indexOf(SettingsUtil.getLocale(requireContext()))); + binding.settingsLanguage.setSelection(Arrays.asList(AppLocale.values()).indexOf(SettingsUtil.getLocale(requireContext()))); binding.settingsLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - Locale locale = locales[position]; + AppLocale locale = AppLocale.values()[position]; if(locale.equals(SettingsUtil.getLocale(requireContext()))) return; SettingsUtil.setLocale(requireContext(), locale); diff --git a/app/src/main/java/com/cringe_studios/code_guard/util/AppLocale.java b/app/src/main/java/com/cringe_studios/code_guard/util/AppLocale.java new file mode 100644 index 0000000..c0ac5ff --- /dev/null +++ b/app/src/main/java/com/cringe_studios/code_guard/util/AppLocale.java @@ -0,0 +1,41 @@ +package com.cringe_studios.code_guard.util; + +import android.content.Context; + +import androidx.annotation.StringRes; + +import com.cringe_studios.code_guard.R; + +import java.util.Locale; + +public enum AppLocale { + + SYSTEM_DEFAULT(R.string.locale_system_default), + ENGLISH(Locale.ENGLISH), + GERMAN(Locale.GERMAN), + ; + + @StringRes + private final int name; + + private final Locale locale; + + AppLocale(@StringRes int name) { + this.name = name; + this.locale = null; + } + + AppLocale(Locale locale) { + this.name = 0; + this.locale = locale; + } + + public String getName(Context context) { + return locale == null ? context.getString(name) : locale.getDisplayName(locale); + } + + public Locale getLocale() { + return locale; + } + +} diff --git a/app/src/main/java/com/cringe_studios/code_guard/util/SettingsUtil.java b/app/src/main/java/com/cringe_studios/code_guard/util/SettingsUtil.java index f1d753c..ad08ec2 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/util/SettingsUtil.java +++ b/app/src/main/java/com/cringe_studios/code_guard/util/SettingsUtil.java @@ -11,7 +11,6 @@ import com.google.gson.Gson; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Locale; public class SettingsUtil { @@ -222,13 +221,18 @@ public class SettingsUtil { } } - public static void setLocale(Context ctx, Locale locale) { + public static void setLocale(Context ctx, AppLocale locale) { SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE); - prefs.edit().putString("locale", locale.getLanguage()).apply(); + prefs.edit().putString("locale", locale.name()).apply(); } - public static Locale getLocale(Context ctx) { - return new Locale(ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("locale", Locale.ENGLISH.getLanguage())); + public static AppLocale getLocale(Context ctx) { + String lang = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("locale", AppLocale.ENGLISH.name()); + try { + return AppLocale.valueOf(lang); + }catch(IllegalArgumentException e) { + return AppLocale.SYSTEM_DEFAULT; + } } public static void enableSuperSecretHamburgers(Context ctx) { diff --git a/app/src/main/res/menu/menu_otps.xml b/app/src/main/res/menu/menu_otps.xml index 2570f3a..097e56f 100644 --- a/app/src/main/res/menu/menu_otps.xml +++ b/app/src/main/res/menu/menu_otps.xml @@ -12,7 +12,7 @@ android:orderInCategory="100" android:icon="@drawable/baseline_add_24" android:title="@string/action_new_group" - app:showAsAction="ifRoom"> + app:showAsAction="always"> Ein OTP mit dem Namen des OTPs, das du hinzufügen willst, existiert bereits.\n\nWillst du das neue OTP umbenennen lassen, um sie voneinander zu unterscheiden? Doppeltes OTP Biometrische Authentifizierung ist deaktiviert, da sie auf deinem Gerät nicht eingerichtet oder nicht verfügbar ist + Systemstandard \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c5c1c5..f1adc34 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -85,6 +85,7 @@ Dark Light System default + System default Create backup Create with current password