Add settings for intro video and theme

This commit is contained in:
MrLetsplay 2023-06-24 23:16:19 +02:00
parent c86b75eac4
commit e75f9c45aa
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 105 additions and 23 deletions

View File

@ -12,9 +12,9 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.cringe_studios.cringe_authenticator.databinding.ActivityIntroBinding; import com.cringe_studios.cringe_authenticator.databinding.ActivityIntroBinding;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil;
public class IntroActivity extends AppCompatActivity { public class IntroActivity extends AppCompatActivity {
public static boolean show_logoanimation = false;
private static ActivityIntroBinding binding; private static ActivityIntroBinding binding;
@ -22,12 +22,11 @@ public class IntroActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
SharedPreferences p = getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
show_logoanimation = p.getBoolean("Logoanimation", true);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (show_logoanimation) {
setContentView(R.layout.activity_intro); if (!SettingsUtil.isIntroVideoEnabled(this)) {
} else { openMainActivity();
return;
} }
binding = ActivityIntroBinding.inflate(getLayoutInflater()); binding = ActivityIntroBinding.inflate(getLayoutInflater());
@ -63,7 +62,7 @@ public class IntroActivity extends AppCompatActivity {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
// When the Activity is destroyed, release our MediaPlayer and set it to null. // When the Activity is destroyed, release our MediaPlayer and set it to null.
mMediaPlayer.release(); if(mMediaPlayer != null) mMediaPlayer.release();
mMediaPlayer = null; mMediaPlayer = null;
} }

View File

@ -36,6 +36,7 @@ import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment;
import com.cringe_studios.cringe_authenticator.scanner.QRScannerActivity; import com.cringe_studios.cringe_authenticator.scanner.QRScannerActivity;
import com.cringe_studios.cringe_authenticator.scanner.QRScannerContract; import com.cringe_studios.cringe_authenticator.scanner.QRScannerContract;
import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.NavigationUtil;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil;
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPType; import com.cringe_studios.cringe_authenticator_library.OTPType;
@ -52,8 +53,12 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// TODO: load configured theme Integer themeID = SettingsUtil.THEMES.get(SettingsUtil.getTheme(this));
setTheme(R.style.Theme_CringeAuthenticator_Blue_Green); if(themeID != null) {
setTheme(themeID);
}else {
setTheme(R.style.Theme_CringeAuthenticator_Blue_Green);
}
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() {

View File

@ -1,9 +1,13 @@
package com.cringe_studios.cringe_authenticator.fragment; package com.cringe_studios.cringe_authenticator.fragment;
import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -14,6 +18,14 @@ import androidx.navigation.Navigation;
import com.cringe_studios.cringe_authenticator.R; import com.cringe_studios.cringe_authenticator.R;
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 java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class SettingsFragment extends Fragment { public class SettingsFragment extends Fragment {
@ -24,9 +36,31 @@ public class SettingsFragment extends Fragment {
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);
binding.switch1.setOnCheckedChangeListener((view, checked) -> { binding.settingsEnableIntroVideo.setChecked(SettingsUtil.isIntroVideoEnabled(requireContext()));
NavController controller = Navigation.findNavController(binding.getRoot()); binding.settingsEnableIntroVideo.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setEnableIntroVideo(requireContext(), checked));
controller.navigate(R.id.FirstFragment);
getResources().getStringArray(R.array.themes);
binding.settingsTheme.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, SettingsUtil.THEME_NAMES.toArray(new String[0])));
binding.settingsTheme.setSelection(SettingsUtil.THEME_NAMES.indexOf(SettingsUtil.getTheme(requireContext())));
binding.settingsTheme.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String theme = SettingsUtil.THEME_NAMES.get(position);
if(theme.equals(SettingsUtil.getTheme(requireContext()))) return;
SettingsUtil.setTheme(requireContext(), theme);
Integer themeID = SettingsUtil.THEMES.get(theme);
if(themeID == null) return;
Log.e("AMOGUS", "REstarting activity");
requireActivity().setTheme(themeID);
requireActivity().recreate();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
}); });
FabUtil.hideFabs(requireActivity()); FabUtil.hideFabs(requireActivity());

View File

@ -1,18 +1,40 @@
package com.cringe_studios.cringe_authenticator.util; package com.cringe_studios.cringe_authenticator.util;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.cringe_studios.cringe_authenticator.OTPData; import com.cringe_studios.cringe_authenticator.OTPData;
import com.cringe_studios.cringe_authenticator.R;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import kotlinx.coroutines.flow.SharedFlow;
public class SettingsUtil { public class SettingsUtil {
public static final Map<String, Integer> THEMES;
public static final List<String> THEME_NAMES;
static {
Map<String, Integer> themes = new LinkedHashMap<>();
themes.put("Blue/Green", R.style.Theme_CringeAuthenticator_Blue_Green);
themes.put("Red/Blue", R.style.Theme_CringeAuthenticator_Red_Blue);
themes.put("Pink/Green", R.style.Theme_CringeAuthenticator_Pink_Green);
themes.put("Blue/Yellow", R.style.Theme_CringeAuthenticator_Blue_Yellow);
themes.put("Green/Yellow", R.style.Theme_CringeAuthenticator_Green_Yellow);
themes.put("Orange/Turquoise", R.style.Theme_CringeAuthenticator_Orange_Turquoise);
THEMES = Collections.unmodifiableMap(themes);
THEME_NAMES = Collections.unmodifiableList(new ArrayList<>(THEMES.keySet()));
}
public static String public static String
GROUPS_PREFS_NAME = "groups", GROUPS_PREFS_NAME = "groups",
GENERAL_PREFS_NAME = "general"; GENERAL_PREFS_NAME = "general";
@ -33,4 +55,22 @@ public class SettingsUtil {
.apply(); .apply();
} }
public static void setEnableIntroVideo(Context ctx, boolean enableIntroVideo) {
SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean("enableIntroVideo", enableIntroVideo).apply();
}
public static boolean isIntroVideoEnabled(Context ctx) {
return ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getBoolean("enableIntroVideo", true);
}
public static void setTheme(Context ctx, String theme) {
SharedPreferences prefs = ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putString("theme", theme).apply();
}
public static String getTheme(Context ctx) {
return ctx.getSharedPreferences(GENERAL_PREFS_NAME, Context.MODE_PRIVATE).getString("theme", THEME_NAMES.get(0));
}
} }

View File

@ -6,20 +6,24 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".fragment.HomeFragment"> tools:context=".fragment.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp"> android:padding="16dp"
android:orientation="vertical">
<Switch <androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch1" android:id="@+id/settings_enable_intro_video"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="Enable intro video" />
android:text="Enable something"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> <Spinner
android:id="@+id/settings_theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>