Add language select in settings

This commit is contained in:
MrLetsplay 2023-07-03 20:50:09 +02:00
parent 9f8f3ffb7a
commit 0af97052e2
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
6 changed files with 57 additions and 7 deletions

View File

@ -55,9 +55,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setLocale("de"); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); TODO: enable secure flag
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Integer themeID = SettingsUtil.THEMES.get(SettingsUtil.getTheme(this)); Integer themeID = SettingsUtil.THEMES.get(SettingsUtil.getTheme(this));
if(themeID != null) { if(themeID != null) {
@ -66,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
setTheme(R.style.Theme_CringeAuthenticator_Blue_Green); setTheme(R.style.Theme_CringeAuthenticator_Blue_Green);
} }
setLocale(SettingsUtil.getLocale(this));
Executor executor = ContextCompat.getMainExecutor(this); Executor executor = ContextCompat.getMainExecutor(this);
BiometricPrompt prompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() { BiometricPrompt prompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override @Override
@ -110,8 +110,7 @@ public class MainActivity extends AppCompatActivity {
}); });
} }
private void setLocale(String lang) { public void setLocale(Locale locale) {
Locale locale = new Locale(lang);
Locale.setDefault(locale); Locale.setDefault(locale);
Configuration config = new Configuration(); Configuration config = new Configuration();
config.locale = locale; config.locale = locale;

View File

@ -14,10 +14,14 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.biometric.BiometricManager; import androidx.biometric.BiometricManager;
import com.cringe_studios.cringe_authenticator.MainActivity;
import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding; import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding;
import com.cringe_studios.cringe_authenticator.util.FabUtil; import com.cringe_studios.cringe_authenticator.util.FabUtil;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil;
import java.util.Arrays;
import java.util.Locale;
public class SettingsFragment extends NamedFragment { public class SettingsFragment extends NamedFragment {
private FragmentSettingsBinding binding; private FragmentSettingsBinding binding;
@ -32,6 +36,32 @@ 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];
for(int i = 0; i < locales.length; i++) {
localeNames[i] = locales[i].getDisplayName(locales[i]);
}
binding.settingsLanguage.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, localeNames));
binding.settingsLanguage.setSelection(Arrays.asList(locales).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];
if(locale.equals(SettingsUtil.getLocale(requireContext()))) return;
SettingsUtil.setLocale(requireContext(), locale);
((MainActivity) requireActivity()).setLocale(locale);
requireActivity().recreate();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
binding.settingsEnableIntroVideo.setChecked(SettingsUtil.isIntroVideoEnabled(requireContext())); binding.settingsEnableIntroVideo.setChecked(SettingsUtil.isIntroVideoEnabled(requireContext()));
binding.settingsEnableIntroVideo.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setEnableIntroVideo(requireContext(), checked)); binding.settingsEnableIntroVideo.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setEnableIntroVideo(requireContext(), checked));

View File

@ -14,6 +14,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
public class SettingsUtil { public class SettingsUtil {
@ -130,6 +131,15 @@ public class SettingsUtil {
return ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("theme", THEME_NAMES.get(0)); return ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("theme", THEME_NAMES.get(0));
} }
public static void setLocale(Context ctx, Locale locale) {
SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putString("locale", locale.getLanguage()).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 void enableSuperSecretHamburgers(Context ctx) { public static void enableSuperSecretHamburgers(Context ctx) {
ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).edit().putBoolean("iLikeHamburgers", true).apply(); ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).edit().putBoolean("iLikeHamburgers", true).apply();
} }

View File

@ -11,17 +11,24 @@
android:padding="16dp" android:padding="16dp"
android:orientation="vertical"> android:orientation="vertical">
<Spinner
android:id="@+id/settings_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/settings_enable_intro_video" android:id="@+id/settings_enable_intro_video"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Enable intro video" /> android:text="@string/settings_enable_intro_video" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/settings_biometric_lock" android:id="@+id/settings_biometric_lock"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Require biometric unlock" /> android:text="@string/settings_biometric_lock" />
<Spinner <Spinner
android:id="@+id/settings_theme" android:id="@+id/settings_theme"

View File

@ -34,6 +34,8 @@
<string name="otp_delete_title">Löschen?</string> <string name="otp_delete_title">Löschen?</string>
<string name="otp_delete_message">OTP löschen?</string> <string name="otp_delete_message">OTP löschen?</string>
<string name="edit_group_title">Gruppe bearbeiten</string> <string name="edit_group_title">Gruppe bearbeiten</string>
<string name="settings_enable_intro_video">Intro-Video zeigen</string>
<string name="settings_biometric_lock">Biometrische Authentifizierung aktivieren</string>
<string-array name="view_edit_delete"> <string-array name="view_edit_delete">
<item>Anzeigen</item> <item>Anzeigen</item>
<item>Bearbeiten</item> <item>Bearbeiten</item>

View File

@ -74,6 +74,8 @@
<string name="otp_delete_title">Delete?</string> <string name="otp_delete_title">Delete?</string>
<string name="otp_delete_message">Delete this?</string> <string name="otp_delete_message">Delete this?</string>
<string name="edit_group_title">Edit Group</string> <string name="edit_group_title">Edit Group</string>
<string name="settings_enable_intro_video">Enable intro video</string>
<string name="settings_biometric_lock">Require biometric unlock</string>
<string-array name="view_edit_delete"> <string-array name="view_edit_delete">
<item>View</item> <item>View</item>
<item>Edit</item> <item>Edit</item>