Add lock option, Lock on close, Remove old menu

This commit is contained in:
MrLetsplay 2023-09-23 15:26:43 +02:00
parent 76e12e8f6f
commit 81e8870507
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
8 changed files with 31 additions and 243 deletions

View File

@ -1,26 +1,20 @@
package com.cringe_studios.cringe_authenticator; package com.cringe_studios.cringe_authenticator;
import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest; import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts; import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.cringe_studios.cringe_authenticator.databinding.ActivityMainBinding; 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.AboutFragment;
import com.cringe_studios.cringe_authenticator.fragment.GroupFragment; import com.cringe_studios.cringe_authenticator.fragment.GroupFragment;
import com.cringe_studios.cringe_authenticator.fragment.HomeFragment; 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.NamedFragment;
import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment; import com.cringe_studios.cringe_authenticator.fragment.SettingsFragment;
import com.cringe_studios.cringe_authenticator.model.OTPData; 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.OTPDatabase;
import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.SettingsUtil;
import com.cringe_studios.cringe_authenticator.util.StyledDialogBuilder; 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.cringe_studios.cringe_authenticator_library.OTPType;
import com.google.mlkit.vision.common.InputImage; import com.google.mlkit.vision.common.InputImage;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
public class MainActivity extends BaseActivity { public class MainActivity extends BaseActivity {
@ -159,17 +149,10 @@ public class MainActivity extends BaseActivity {
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragment = NavigationUtil.getCurrentFragment(this); 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) { if(fragment instanceof GroupFragment) {
GroupFragment frag = (GroupFragment) fragment; GroupFragment frag = (GroupFragment) fragment;
getMenuInflater().inflate(frag.isEditing() ? R.menu.menu_otps_edit : R.menu.menu_otps, menu); 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; return true;
} }
@ -195,14 +178,6 @@ public class MainActivity extends BaseActivity {
@Override @Override
public void onBackPressed() { public void onBackPressed() {
Fragment fragment = NavigationUtil.getCurrentFragment(this); Fragment fragment = NavigationUtil.getCurrentFragment(this);
if(fragment instanceof MenuFragment) {
MenuFragment menuFragment = (MenuFragment) fragment;
if(menuFragment.isEditing()) {
menuFragment.finishEditing();
return;
}
}
if(fragment instanceof GroupFragment) { if(fragment instanceof GroupFragment) {
GroupFragment groupFragment = (GroupFragment) fragment; GroupFragment groupFragment = (GroupFragment) fragment;
if(groupFragment.isEditing()) { if(groupFragment.isEditing()) {
@ -282,27 +257,6 @@ public class MainActivity extends BaseActivity {
}, false); }, 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) { public void addOTP(MenuItem item) {
Fragment frag = NavigationUtil.getCurrentFragment(this); Fragment frag = NavigationUtil.getCurrentFragment(this);
if(frag instanceof GroupFragment) { if(frag instanceof GroupFragment) {
@ -338,11 +292,21 @@ public class MainActivity extends BaseActivity {
} }
} }
public void lockApp(MenuItem item) {
OTPDatabase.unloadDatabase();
OTPDatabase.promptLoadDatabase(this, () -> {}, () -> {});
}
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
// OTPDatabase.unloadDatabase(); OTPDatabase.unloadDatabase();
}
@Override
protected void onStart() {
super.onStart();
OTPDatabase.promptLoadDatabase(this, () -> {}, () -> {});
} }
@Override @Override

View File

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

View File

@ -67,7 +67,7 @@ public class OTPDatabase {
public static void promptLoadDatabase(Activity ctx, Runnable success, Runnable failure) { public static void promptLoadDatabase(Activity ctx, Runnable success, Runnable failure) {
if(isDatabaseLoaded()) { if(isDatabaseLoaded()) {
success.run(); if(success != null) success.run();
return; return;
} }

View File

@ -1,24 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.cringe_studios.cringe_authenticator.MainActivity">
<item
android:id="@+id/action_new_group"
android:orderInCategory="100"
android:icon="@drawable/baseline_add_24"
android:title="@string/action_new_group"
android:onClick="addGroup"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
android:onClick="openSettings" />
<item
android:id="@+id/action_about"
android:orderInCategory="100"
android:title="@string/action_about"
app:showAsAction="never"
android:onClick="openAbout" />
</menu>

View File

@ -1,38 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_new_group"
android:orderInCategory="100"
android:icon="@drawable/baseline_add_24"
android:title="@string/action_new_group"
android:onClick="addGroup"
app:showAsAction="always" />
<item
android:id="@+id/action_edit_group"
android:orderInCategory="100"
android:icon="@drawable/baseline_edit_24"
android:title="Edit Group"
android:onClick="editGroup"
app:showAsAction="always" />
<item
android:id="@+id/action_delete_group"
android:orderInCategory="100"
android:icon="@drawable/baseline_delete_24"
android:title="Delete Group"
android:onClick="deleteGroup"
app:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
android:onClick="openSettings" />
<item
android:id="@+id/action_about"
android:orderInCategory="100"
android:title="@string/action_about"
app:showAsAction="never"
android:onClick="openAbout" />
</menu>

View File

@ -14,4 +14,10 @@
android:title="@string/action_about" android:title="@string/action_about"
app:showAsAction="never" app:showAsAction="never"
android:onClick="openAbout" /> android:onClick="openAbout" />
<item
android:id="@+id/action_lock"
android:orderInCategory="100"
android:title="Lock"
app:showAsAction="never"
android:onClick="lockApp" />
</menu> </menu>

View File

@ -20,4 +20,10 @@
android:title="@string/action_about" android:title="@string/action_about"
app:showAsAction="never" app:showAsAction="never"
android:onClick="openAbout" /> android:onClick="openAbout" />
<item
android:id="@+id/action_lock"
android:orderInCategory="100"
android:title="Lock"
app:showAsAction="never"
android:onClick="lockApp" />
</menu> </menu>

View File

@ -41,4 +41,10 @@
android:title="@string/action_about" android:title="@string/action_about"
app:showAsAction="never" app:showAsAction="never"
android:onClick="openAbout" /> android:onClick="openAbout" />
<item
android:id="@+id/action_lock"
android:orderInCategory="100"
android:title="Lock"
app:showAsAction="never"
android:onClick="lockApp" />
</menu> </menu>