diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java index f238eaa..ae559cb 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/IntroActivity.java @@ -12,9 +12,9 @@ import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.cringe_studios.cringe_authenticator.databinding.ActivityIntroBinding; +import com.cringe_studios.cringe_authenticator.util.SettingsUtil; public class IntroActivity extends AppCompatActivity { - public static boolean show_logoanimation = false; private static ActivityIntroBinding binding; @@ -22,12 +22,11 @@ public class IntroActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { - SharedPreferences p = getSharedPreferences("appsettings", Activity.MODE_PRIVATE); - show_logoanimation = p.getBoolean("Logoanimation", true); super.onCreate(savedInstanceState); - if (show_logoanimation) { - setContentView(R.layout.activity_intro); - } else { + + if (!SettingsUtil.isIntroVideoEnabled(this)) { + openMainActivity(); + return; } binding = ActivityIntroBinding.inflate(getLayoutInflater()); @@ -63,7 +62,7 @@ public class IntroActivity extends AppCompatActivity { public void onDestroy() { super.onDestroy(); // When the Activity is destroyed, release our MediaPlayer and set it to null. - mMediaPlayer.release(); + if(mMediaPlayer != null) mMediaPlayer.release(); mMediaPlayer = null; } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java index 1e83e03..88ff400 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java @@ -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.QRScannerContract; 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.OTPType; @@ -52,8 +53,12 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // TODO: load configured theme - setTheme(R.style.Theme_CringeAuthenticator_Blue_Green); + Integer themeID = SettingsUtil.THEMES.get(SettingsUtil.getTheme(this)); + if(themeID != null) { + setTheme(themeID); + }else { + setTheme(R.style.Theme_CringeAuthenticator_Blue_Green); + } Executor executor = ContextCompat.getMainExecutor(this); BiometricPrompt prompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() { diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java index 95f8e9b..9d265c5 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java @@ -1,9 +1,13 @@ package com.cringe_studios.cringe_authenticator.fragment; +import android.content.res.TypedArray; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import androidx.annotation.NonNull; 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.databinding.FragmentSettingsBinding; 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 { @@ -24,9 +36,31 @@ public class SettingsFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = FragmentSettingsBinding.inflate(inflater); - binding.switch1.setOnCheckedChangeListener((view, checked) -> { - NavController controller = Navigation.findNavController(binding.getRoot()); - controller.navigate(R.id.FirstFragment); + binding.settingsEnableIntroVideo.setChecked(SettingsUtil.isIntroVideoEnabled(requireContext())); + binding.settingsEnableIntroVideo.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setEnableIntroVideo(requireContext(), checked)); + + 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()); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java index 62df346..c833514 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/SettingsUtil.java @@ -1,18 +1,40 @@ package com.cringe_studios.cringe_authenticator.util; +import android.content.Context; import android.content.SharedPreferences; import androidx.annotation.NonNull; import com.cringe_studios.cringe_authenticator.OTPData; +import com.cringe_studios.cringe_authenticator.R; import com.google.gson.Gson; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; + +import kotlinx.coroutines.flow.SharedFlow; public class SettingsUtil { + public static final Map THEMES; + public static final List THEME_NAMES; + + static { + Map 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 GROUPS_PREFS_NAME = "groups", GENERAL_PREFS_NAME = "general"; @@ -33,4 +55,22 @@ public class SettingsUtil { .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)); + } + } diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 9b789af..4de1696 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -6,20 +6,24 @@ android:layout_height="match_parent" tools:context=".fragment.HomeFragment"> - + android:padding="16dp" + android:orientation="vertical"> - + android:text="Enable intro video" /> - + + + \ No newline at end of file