From 81e8870507992be348464242b60432a5de8f142e Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sat, 23 Sep 2023 15:26:43 +0200 Subject: [PATCH] Add lock option, Lock on close, Remove old menu --- .../cringe_authenticator/MainActivity.java | 60 ++------ .../fragment/MenuFragment.java | 132 ------------------ .../util/OTPDatabase.java | 2 +- app/src/main/res/menu/menu_groups.xml | 24 ---- app/src/main/res/menu/menu_groups_edit.xml | 38 ----- app/src/main/res/menu/menu_main.xml | 6 + app/src/main/res/menu/menu_otps.xml | 6 + app/src/main/res/menu/menu_otps_edit.xml | 6 + 8 files changed, 31 insertions(+), 243 deletions(-) delete mode 100644 app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java delete mode 100644 app/src/main/res/menu/menu_groups.xml delete mode 100644 app/src/main/res/menu/menu_groups_edit.xml 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 b905b71..52ba079 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 @@ -1,26 +1,20 @@ package com.cringe_studios.cringe_authenticator; -import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Toast; -import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.PickVisualMediaRequest; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.app.AppCompatDelegate; import androidx.fragment.app.Fragment; import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; @@ -28,8 +22,6 @@ import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeChoice import com.cringe_studios.cringe_authenticator.fragment.AboutFragment; import com.cringe_studios.cringe_authenticator.fragment.GroupFragment; import com.cringe_studios.cringe_authenticator.fragment.HomeFragment; -import com.cringe_studios.cringe_authenticator.fragment.MenuDrawerFragment; -import com.cringe_studios.cringe_authenticator.fragment.MenuFragment; import com.cringe_studios.cringe_authenticator.fragment.NamedFragment; import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment; import com.cringe_studios.cringe_authenticator.model.OTPData; @@ -40,12 +32,10 @@ import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.OTPDatabase; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.StyledDialogBuilder; -import com.cringe_studios.cringe_authenticator.util.ThemeUtil; import com.cringe_studios.cringe_authenticator_library.OTPType; import com.google.mlkit.vision.common.InputImage; import java.io.IOException; -import java.util.Arrays; import java.util.Locale; public class MainActivity extends BaseActivity { @@ -159,17 +149,10 @@ public class MainActivity extends BaseActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(fragment instanceof MenuFragment) { - MenuFragment frag = (MenuFragment) fragment; - getMenuInflater().inflate(frag.isEditing() ? R.menu.menu_groups_edit : R.menu.menu_groups, menu); - if(frag.isEditing() && frag.hasSelectedMultipleItems()) menu.removeItem(R.id.action_edit_group); - return true; - } - if(fragment instanceof GroupFragment) { GroupFragment frag = (GroupFragment) fragment; getMenuInflater().inflate(frag.isEditing() ? R.menu.menu_otps_edit : R.menu.menu_otps, menu); - if(frag.isEditing() && frag.hasSelectedMultipleItems()) menu.removeItem(R.id.action_edit_group); + if(frag.isEditing() && frag.hasSelectedMultipleItems()) menu.removeItem(R.id.action_edit_otp); return true; } @@ -195,14 +178,6 @@ public class MainActivity extends BaseActivity { @Override public void onBackPressed() { Fragment fragment = NavigationUtil.getCurrentFragment(this); - if(fragment instanceof MenuFragment) { - MenuFragment menuFragment = (MenuFragment) fragment; - if(menuFragment.isEditing()) { - menuFragment.finishEditing(); - return; - } - } - if(fragment instanceof GroupFragment) { GroupFragment groupFragment = (GroupFragment) fragment; if(groupFragment.isEditing()) { @@ -282,27 +257,6 @@ public class MainActivity extends BaseActivity { }, false); } - public void addGroup(MenuItem item) { - Fragment frag = NavigationUtil.getCurrentFragment(this); - if(frag instanceof MenuFragment) { - ((MenuFragment) frag).addGroup(); - } - } - - public void editGroup(MenuItem item) { - Fragment frag = NavigationUtil.getCurrentFragment(this); - if(frag instanceof MenuFragment) { - ((MenuFragment) frag).editGroup(); - } - } - - public void deleteGroup(MenuItem item) { - Fragment frag = NavigationUtil.getCurrentFragment(this); - if(frag instanceof MenuFragment) { - ((MenuFragment) frag).removeSelectedGroups(); - } - } - public void addOTP(MenuItem item) { Fragment frag = NavigationUtil.getCurrentFragment(this); if(frag instanceof GroupFragment) { @@ -338,11 +292,21 @@ public class MainActivity extends BaseActivity { } } + public void lockApp(MenuItem item) { + OTPDatabase.unloadDatabase(); + OTPDatabase.promptLoadDatabase(this, () -> {}, () -> {}); + } @Override protected void onStop() { super.onStop(); - // OTPDatabase.unloadDatabase(); + OTPDatabase.unloadDatabase(); + } + + @Override + protected void onStart() { + super.onStart(); + OTPDatabase.promptLoadDatabase(this, () -> {}, () -> {}); } @Override diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java deleted file mode 100644 index ff358f6..0000000 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/MenuFragment.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.cringe_studios.cringe_authenticator.fragment; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.recyclerview.widget.ItemTouchHelper; -import androidx.recyclerview.widget.RecyclerView; - -import com.cringe_studios.cringe_authenticator.R; -import com.cringe_studios.cringe_authenticator.databinding.FragmentMenuBinding; -import com.cringe_studios.cringe_authenticator.grouplist.GroupListAdapter; -import com.cringe_studios.cringe_authenticator.grouplist.GroupListItem; -import com.cringe_studios.cringe_authenticator.util.DialogUtil; -import com.cringe_studios.cringe_authenticator.util.FabUtil; -import com.cringe_studios.cringe_authenticator.util.NavigationUtil; -import com.cringe_studios.cringe_authenticator.util.SettingsUtil; -import com.cringe_studios.cringe_authenticator.util.StyledDialogBuilder; - -import java.util.List; -import java.util.UUID; - -public class MenuFragment extends NamedFragment { - - private FragmentMenuBinding binding; - - private GroupListAdapter groupListAdapter; - - @Override - public String getName() { - return "Menu"; - } - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - binding = FragmentMenuBinding.inflate(inflater); - - groupListAdapter = new GroupListAdapter(requireContext(), binding.menuItems, group -> { - Bundle bundle = new Bundle(); - bundle.putString(GroupFragment.BUNDLE_GROUP, group); - NavigationUtil.navigate(this, GroupFragment.class, bundle); - }, () -> SettingsUtil.setGroups(requireContext(), groupListAdapter.getItems()), requireActivity()::invalidateMenu); - binding.menuItems.setAdapter(groupListAdapter); - - loadGroups(); - - FabUtil.hideFabs(requireActivity()); - - return binding.getRoot(); - } - - private void loadGroups() { - List items = SettingsUtil.getGroups(requireContext()); - - for(String item : items) { - groupListAdapter.add(item); - } - } - - public void addGroup() { - DialogUtil.showCreateGroupDialog(getLayoutInflater(), null, groupName -> { - String id = UUID.randomUUID().toString(); - SettingsUtil.addGroup(requireContext(), id, groupName); - groupListAdapter.add(id); - }, null); - } - - public void editGroup() { - if(!groupListAdapter.isEditing()) return; - - List items = groupListAdapter.getSelectedGroups(); - if(items.size() != 1) return; - - String group = items.get(0).getGroupId(); - - DialogUtil.showCreateGroupDialog(getLayoutInflater(), SettingsUtil.getGroupName(requireContext(), group), newName -> { // TODO: edit group dialog (with "Edit Group" title) - renameGroup(group, newName); - groupListAdapter.finishEditing(); - }, null); - } - - public void removeSelectedGroups() { - if(!groupListAdapter.isEditing()) return; - - new StyledDialogBuilder(requireContext()) - .setTitle(R.string.group_delete_title) - .setMessage(R.string.group_delete_message) - .setPositiveButton(R.string.yes, (d, w) -> { - for(GroupListItem item : groupListAdapter.getSelectedGroups()) { - removeGroup(item.getGroupId()); - } - - groupListAdapter.finishEditing(); - }) - .setNegativeButton(R.string.no, (d, w) -> {}) - .show(); - } - - public void removeGroup(String group) { - SettingsUtil.removeGroup(requireContext(), group); - groupListAdapter.remove(group); - } - - public void renameGroup(String group, String newName) { - SettingsUtil.setGroupName(requireContext(), group, newName); - groupListAdapter.update(group); - } - - public boolean isEditing() { - return groupListAdapter.isEditing(); - } - - public void finishEditing() { - groupListAdapter.finishEditing(); - } - - public boolean hasSelectedMultipleItems() { - return groupListAdapter.getSelectedGroups().size() > 1; - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - this.binding = null; - } - -} diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/OTPDatabase.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/OTPDatabase.java index 92b7bf7..22e284e 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/OTPDatabase.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/OTPDatabase.java @@ -67,7 +67,7 @@ public class OTPDatabase { public static void promptLoadDatabase(Activity ctx, Runnable success, Runnable failure) { if(isDatabaseLoaded()) { - success.run(); + if(success != null) success.run(); return; } diff --git a/app/src/main/res/menu/menu_groups.xml b/app/src/main/res/menu/menu_groups.xml deleted file mode 100644 index fe19bff..0000000 --- a/app/src/main/res/menu/menu_groups.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/menu_groups_edit.xml b/app/src/main/res/menu/menu_groups_edit.xml deleted file mode 100644 index fc5f9e3..0000000 --- a/app/src/main/res/menu/menu_groups_edit.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 045ecb7..f170740 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -14,4 +14,10 @@ android:title="@string/action_about" app:showAsAction="never" android:onClick="openAbout" /> + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_otps.xml b/app/src/main/res/menu/menu_otps.xml index 3fa989d..80d13e6 100644 --- a/app/src/main/res/menu/menu_otps.xml +++ b/app/src/main/res/menu/menu_otps.xml @@ -20,4 +20,10 @@ android:title="@string/action_about" app:showAsAction="never" android:onClick="openAbout" /> + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_otps_edit.xml b/app/src/main/res/menu/menu_otps_edit.xml index 7513df8..ed8af53 100644 --- a/app/src/main/res/menu/menu_otps_edit.xml +++ b/app/src/main/res/menu/menu_otps_edit.xml @@ -41,4 +41,10 @@ android:title="@string/action_about" app:showAsAction="never" android:onClick="openAbout" /> + \ No newline at end of file