Upate locale setting, Update version

This commit is contained in:
MrLetsplay 2023-10-14 19:32:34 +02:00
parent 0307043843
commit 09a76c29fe
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
8 changed files with 64 additions and 18 deletions

View File

@ -11,8 +11,8 @@ android {
applicationId "com.cringe_studios.code_guard" applicationId "com.cringe_studios.code_guard"
minSdk 21 minSdk 21
targetSdk 34 targetSdk 34
versionCode 4 versionCode 5
versionName "1.0.0_alpha3" versionName "1.0.0_beta1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.cringe_studios.code_guard.unlock.UnlockContract; 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.SettingsUtil;
import com.cringe_studios.code_guard.util.ThemeUtil; import com.cringe_studios.code_guard.util.ThemeUtil;
@ -46,10 +47,9 @@ public class BaseActivity extends AppCompatActivity {
startUnlockActivity.launch(null); startUnlockActivity.launch(null);
} }
public void setLocale(Locale locale) { public void setLocale(AppLocale locale) {
Locale.setDefault(locale);
Configuration config = new Configuration(); Configuration config = new Configuration();
config.setLocale(locale); config.setLocale(locale == AppLocale.SYSTEM_DEFAULT ? Locale.getDefault() : locale.getLocale());
getResources().updateConfiguration(config, getResources().getDisplayMetrics()); getResources().updateConfiguration(config, getResources().getDisplayMetrics());
} }

View File

@ -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.IconPack;
import com.cringe_studios.code_guard.icon.IconPackListAdapter; import com.cringe_studios.code_guard.icon.IconPackListAdapter;
import com.cringe_studios.code_guard.icon.IconUtil; 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.Appearance;
import com.cringe_studios.code_guard.util.BackupException; import com.cringe_studios.code_guard.util.BackupException;
import com.cringe_studios.code_guard.util.BiometricUtil; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale;
import javax.crypto.SecretKey; 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) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FragmentSettingsBinding.inflate(inflater); binding = FragmentSettingsBinding.inflate(inflater);
Locale[] locales = new Locale[] {Locale.ENGLISH, Locale.GERMAN};
String[] localeNames = new String[locales.length]; String[] localeNames = new String[AppLocale.values().length];
for(int i = 0; i < locales.length; i++) { for(int i = 0; i < localeNames.length; i++) {
localeNames[i] = locales[i].getDisplayName(locales[i]); localeNames[i] = AppLocale.values()[i].getName(requireContext());
} }
binding.settingsLanguage.setAdapter(new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, localeNames)); 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() { binding.settingsLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 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; if(locale.equals(SettingsUtil.getLocale(requireContext()))) return;
SettingsUtil.setLocale(requireContext(), locale); SettingsUtil.setLocale(requireContext(), locale);

View File

@ -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;
}
}

View File

@ -11,7 +11,6 @@ import com.google.gson.Gson;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale;
public class SettingsUtil { 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); 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) { public static AppLocale getLocale(Context ctx) {
return new Locale(ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("locale", Locale.ENGLISH.getLanguage())); 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) { public static void enableSuperSecretHamburgers(Context ctx) {

View File

@ -12,7 +12,7 @@
android:orderInCategory="100" android:orderInCategory="100"
android:icon="@drawable/baseline_add_24" android:icon="@drawable/baseline_add_24"
android:title="@string/action_new_group" android:title="@string/action_new_group"
app:showAsAction="ifRoom"> app:showAsAction="always">
<menu> <menu>
<item <item
android:id="@+id/action_otp_input" android:id="@+id/action_otp_input"

View File

@ -166,4 +166,5 @@
<string name="error_duplicate_otp_message">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?</string> <string name="error_duplicate_otp_message">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?</string>
<string name="error_duplicate_otp_title">Doppeltes OTP</string> <string name="error_duplicate_otp_title">Doppeltes OTP</string>
<string name="biometric_encryption_unavailable">Biometrische Authentifizierung ist deaktiviert, da sie auf deinem Gerät nicht eingerichtet oder nicht verfügbar ist</string> <string name="biometric_encryption_unavailable">Biometrische Authentifizierung ist deaktiviert, da sie auf deinem Gerät nicht eingerichtet oder nicht verfügbar ist</string>
<string name="locale_system_default">Systemstandard</string>
</resources> </resources>

View File

@ -85,6 +85,7 @@
<string name="appearance_dark">Dark</string> <string name="appearance_dark">Dark</string>
<string name="appearance_light">Light</string> <string name="appearance_light">Light</string>
<string name="appearance_follow_system">System default</string> <string name="appearance_follow_system">System default</string>
<string name="locale_system_default">System default</string>
<string name="backup_create_title">Create backup</string> <string name="backup_create_title">Create backup</string>
<string-array name="backup_create"> <string-array name="backup_create">
<item>Create with current password</item> <item>Create with current password</item>