From f66de7fd78eb76baf82606ee44c538317870424c Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sun, 1 Oct 2023 15:44:01 +0200 Subject: [PATCH] Add manage icon packs dialog --- .../fragment/EditOTPFragment.java | 10 +++- .../fragment/SettingsFragment.java | 26 ++++++++++ .../icon/IconPackItem.java | 31 +++++++++++ .../icon/IconPackListAdapter.java | 52 +++++++++++++++++++ .../cringe_authenticator/icon/IconUtil.java | 6 +++ .../res/layout/dialog_manage_icon_packs.xml | 13 +++++ .../layout/dialog_manage_icon_packs_item.xml | 24 +++++++++ app/src/main/res/values/strings.xml | 3 ++ 8 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackItem.java create mode 100644 app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackListAdapter.java create mode 100644 app/src/main/res/layout/dialog_manage_icon_packs.xml create mode 100644 app/src/main/res/layout/dialog_manage_icon_packs_item.xml diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/EditOTPFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/EditOTPFragment.java index 57e9e80..64be145 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/EditOTPFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/EditOTPFragment.java @@ -12,6 +12,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -21,6 +22,7 @@ import androidx.exifinterface.media.ExifInterface; import com.cringe_studios.cringe_authenticator.MainActivity; import com.cringe_studios.cringe_authenticator.R; import com.cringe_studios.cringe_authenticator.databinding.FragmentEditOtpBinding; +import com.cringe_studios.cringe_authenticator.icon.IconPack; import com.cringe_studios.cringe_authenticator.icon.IconUtil; import com.cringe_studios.cringe_authenticator.model.OTPData; import com.cringe_studios.cringe_authenticator.util.DialogUtil; @@ -36,6 +38,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; +import java.util.List; public class EditOTPFragment extends NamedFragment { @@ -184,7 +187,12 @@ public class EditOTPFragment extends NamedFragment { } private void pickImageFromIconPack() { - // TODO: check if icon packs installed + List packs = IconUtil.loadAllIconPacks(requireContext()); + if(packs.isEmpty()) { + Toast.makeText(requireContext(), R.string.no_icon_packs_installed, Toast.LENGTH_LONG).show(); + return; + } + new PickIconDrawerFragment(icon -> { imageData = Base64.encodeToString(icon.getBytes(), Base64.DEFAULT); updateImage(); 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 d88fd66..f6adec0 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 @@ -8,9 +8,11 @@ import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.recyclerview.widget.LinearLayoutManager; import com.cringe_studios.cringe_authenticator.MainActivity; import com.cringe_studios.cringe_authenticator.R; @@ -20,7 +22,11 @@ import com.cringe_studios.cringe_authenticator.crypto.BiometricKey; import com.cringe_studios.cringe_authenticator.crypto.Crypto; import com.cringe_studios.cringe_authenticator.crypto.CryptoException; import com.cringe_studios.cringe_authenticator.crypto.CryptoParameters; +import com.cringe_studios.cringe_authenticator.databinding.DialogManageIconPacksBinding; import com.cringe_studios.cringe_authenticator.databinding.FragmentSettingsBinding; +import com.cringe_studios.cringe_authenticator.icon.IconPack; +import com.cringe_studios.cringe_authenticator.icon.IconPackListAdapter; +import com.cringe_studios.cringe_authenticator.icon.IconUtil; import com.cringe_studios.cringe_authenticator.util.Appearance; import com.cringe_studios.cringe_authenticator.util.BackupException; import com.cringe_studios.cringe_authenticator.util.BiometricUtil; @@ -32,6 +38,7 @@ import com.cringe_studios.cringe_authenticator.util.StyledDialogBuilder; import com.cringe_studios.cringe_authenticator.util.Theme; import java.util.Arrays; +import java.util.List; import java.util.Locale; import javax.crypto.SecretKey; @@ -256,6 +263,25 @@ public class SettingsFragment extends NamedFragment { binding.settingsLoadIconPack.setOnClickListener(v -> ((MainActivity) requireActivity()).promptPickIconPackLoad()); + binding.settingsManageIconPacks.setOnClickListener(v -> { + List packs = IconUtil.loadAllIconPacks(requireContext()); + if(packs.isEmpty()) { + Toast.makeText(requireContext(), R.string.no_icon_packs_installed, Toast.LENGTH_LONG).show(); + return; + } + + DialogManageIconPacksBinding binding = DialogManageIconPacksBinding.inflate(getLayoutInflater()); + + binding.manageIconPacksList.setLayoutManager(new LinearLayoutManager(requireContext())); + binding.manageIconPacksList.setAdapter(new IconPackListAdapter(requireContext(), IconUtil.loadAllIconPacks(requireContext()))); + + new StyledDialogBuilder(requireContext()) + .setTitle("Manage icon packs") + .setView(binding.getRoot()) + .setPositiveButton(R.string.ok, (d, which) -> {}) + .show(); + }); + return binding.getRoot(); } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackItem.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackItem.java new file mode 100644 index 0000000..feaad07 --- /dev/null +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackItem.java @@ -0,0 +1,31 @@ +package com.cringe_studios.cringe_authenticator.icon; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.cringe_studios.cringe_authenticator.databinding.DialogManageIconPacksItemBinding; + +public class IconPackItem extends RecyclerView.ViewHolder { + + private DialogManageIconPacksItemBinding binding; + + private IconPack pack; + + public IconPackItem(@NonNull DialogManageIconPacksItemBinding binding) { + super(binding.getRoot()); + this.binding = binding; + } + + public DialogManageIconPacksItemBinding getBinding() { + return binding; + } + + public void setPack(IconPack pack) { + this.pack = pack; + } + + public IconPack getPack() { + return pack; + } + +} diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackListAdapter.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackListAdapter.java new file mode 100644 index 0000000..ff7a229 --- /dev/null +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconPackListAdapter.java @@ -0,0 +1,52 @@ +package com.cringe_studios.cringe_authenticator.icon; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.cringe_studios.cringe_authenticator.R; +import com.cringe_studios.cringe_authenticator.databinding.DialogManageIconPacksItemBinding; +import com.cringe_studios.cringe_authenticator.util.DialogUtil; + +import java.util.List; + +public class IconPackListAdapter extends RecyclerView.Adapter { + + private Context context; + + private LayoutInflater inflater; + + private List packs; + + public IconPackListAdapter(Context context, List packs) { + this.context = context; + this.inflater = LayoutInflater.from(context); + this.packs = packs; + } + + @NonNull + @Override + public IconPackItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new IconPackItem(DialogManageIconPacksItemBinding.inflate(inflater, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull IconPackItem holder, int position) { + IconPack pack = packs.get(position); + holder.setPack(pack); + + holder.getBinding().iconPackName.setText(pack.getMetadata().getName()); + + holder.getBinding().iconPackDelete.setOnClickListener(view -> { + DialogUtil.showYesNo(context, R.string.delete_pack_title, R.string.delete_pack_message, () -> IconUtil.removeIconPack(context, pack.getMetadata().getUuid()), null); + }); + } + + @Override + public int getItemCount() { + return packs.size(); + } +} diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconUtil.java index 1e085be..c857870 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/icon/IconUtil.java @@ -103,6 +103,12 @@ public class IconUtil { return meta; } + public static void removeIconPack(Context context, String uuid) { + File packFile = new File(getIconPacksDir(context), uuid); + packFile.delete(); + loadedPacks.remove(uuid); + } + private static IconPackMetadata loadPackMetadata(Context context, Uri uri) throws IconPackException { try(InputStream in = context.getContentResolver().openInputStream(uri)) { if(in == null) throw new IconPackException("Failed to read icon pack"); diff --git a/app/src/main/res/layout/dialog_manage_icon_packs.xml b/app/src/main/res/layout/dialog_manage_icon_packs.xml new file mode 100644 index 0000000..543c755 --- /dev/null +++ b/app/src/main/res/layout/dialog_manage_icon_packs.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_manage_icon_packs_item.xml b/app/src/main/res/layout/dialog_manage_icon_packs_item.xml new file mode 100644 index 0000000..312c531 --- /dev/null +++ b/app/src/main/res/layout/dialog_manage_icon_packs_item.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a21209..a54e676 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -104,4 +104,7 @@ Algorithm Digits Show Images + Delete pack + Do you want to delete the icon pack? + No icon packs are currently installed \ No newline at end of file