Prompt on duplicate code name

This commit is contained in:
MrLetsplay 2023-10-06 22:20:30 +02:00
parent 1c4fb08387
commit 8dac0a71ac
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
7 changed files with 96 additions and 30 deletions

View File

@ -95,13 +95,14 @@ public class GroupFragment extends NamedFragment {
public void addOTP(OTPData... data) { public void addOTP(OTPData... data) {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> { OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
OTPDatabase.getLoadedDatabase().promptAddOTPs(requireContext(), groupID, () -> {
try { try {
for(OTPData d : data) OTPDatabase.getLoadedDatabase().addOTP(groupID, d);
OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext())); OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext()));
for(OTPData d : data) otpListAdapter.add(d); for(OTPData d : data) otpListAdapter.add(d);
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
DialogUtil.showErrorDialog(requireContext(), getString(R.string.error_database_save), e); DialogUtil.showErrorDialog(requireContext(), getString(R.string.error_database_save), e);
} }
}, data);
}, null); }, null);
} }
@ -149,19 +150,22 @@ public class GroupFragment extends NamedFragment {
DialogUtil.showChooseGroupDialog(requireContext(), group -> { DialogUtil.showChooseGroupDialog(requireContext(), group -> {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> { OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
try { OTPData[] otps = new OTPData[items.size()];
for(OTPListItem item : items) { for(int i = 0; i < otps.length; i++) {
OTPData data = item.getOTPData(); OTPData data = items.get(i).getOTPData();
OTPDatabase.getLoadedDatabase().addOTP(group, data); otps[i] = data;
otpListAdapter.remove(data);
} }
OTPDatabase.getLoadedDatabase().promptAddOTPs(requireContext(), group, () -> {
try {
OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext())); OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext()));
for(OTPData data : otps) otpListAdapter.remove(data);
saveOTPs(); saveOTPs();
otpListAdapter.finishEditing(); otpListAdapter.finishEditing();
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
DialogUtil.showErrorDialog(requireContext(), e.toString()); DialogUtil.showErrorDialog(requireContext(), e.toString());
} }
}, otps);
}, null); }, null);
saveOTPs(); saveOTPs();
}, null); }, null);

View File

@ -11,7 +11,7 @@ public class OTPData implements Serializable {
public static final String IMAGE_DATA_NONE = "none"; public static final String IMAGE_DATA_NONE = "none";
private final String name; private String name;
private final String issuer; private final String issuer;
private final OTPType type; private final OTPType type;
private final String secret; private final String secret;
@ -41,6 +41,10 @@ public class OTPData implements Serializable {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public String getIssuer() { public String getIssuer() {
return issuer; return issuer;
} }

View File

@ -63,12 +63,13 @@ public class URIHandlerActivity extends BaseActivity {
OTPDatabase.promptLoadDatabase(this, () -> { OTPDatabase.promptLoadDatabase(this, () -> {
DialogUtil.showChooseGroupDialog(this, group -> { DialogUtil.showChooseGroupDialog(this, group -> {
for(OTPData d : data) { for(OTPData d : data) {
OTPDatabase.getLoadedDatabase().addOTP(group, d); OTPDatabase.getLoadedDatabase().promptAddOTPs(this, group, () -> {
try { try {
OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this)); OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this));
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
DialogUtil.showErrorDialog(this, e.toString()); DialogUtil.showErrorDialog(this, e.toString());
} }
}, d);
} }
Toast.makeText(this, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show();
}, this::finishAndRemoveTask); }, this::finishAndRemoveTask);

View File

@ -2,8 +2,10 @@ package com.cringe_studios.code_guard.util;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.util.Log;
import com.cringe_studios.code_guard.BaseActivity; import com.cringe_studios.code_guard.BaseActivity;
import com.cringe_studios.code_guard.R;
import com.cringe_studios.code_guard.crypto.Crypto; import com.cringe_studios.code_guard.crypto.Crypto;
import com.cringe_studios.code_guard.crypto.CryptoException; import com.cringe_studios.code_guard.crypto.CryptoException;
import com.cringe_studios.code_guard.crypto.CryptoParameters; import com.cringe_studios.code_guard.crypto.CryptoParameters;
@ -23,6 +25,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
@ -45,11 +48,61 @@ public class OTPDatabase {
return o; return o;
} }
public void addOTP(String groupId, OTPData o) { public void promptAddOTPs(Context context, String groupId, Runnable done, OTPData... data) {
// TODO: check for code with same name
List<OTPData> os = new ArrayList<>(getOTPs(groupId)); List<OTPData> os = new ArrayList<>(getOTPs(groupId));
os.add(o);
boolean anyDuplicates = false;
boolean[] duplicates = new boolean[data.length];
for(int i = 0; i < data.length; i++) {
OTPData o = data[i];
for (OTPData o2 : os) {
if (Objects.equals(o.getName(), o2.getName()) && Objects.equals(o.getIssuer(), o2.getIssuer())) {
anyDuplicates = true;
duplicates[i] = true;
break;
}
}
}
Log.d("ADD", "Any dupes?" + anyDuplicates);
if (anyDuplicates) {
DialogUtil.showYesNoCancel(context, R.string.error_duplicate_otp_title, R.string.error_duplicate_otp_message, () -> {
for(int i = 0; i < data.length; i++) {
OTPData o = data[i];
if(duplicates[i]) {
String oldName = o.getName();
int count = 1;
boolean hasDuplicates;
do {
o.setName(oldName + " - " + (count++));
hasDuplicates = false;
for(OTPData o2 : os) {
if(Objects.equals(o.getName(), o2.getName()) && Objects.equals(o.getIssuer(), o2.getIssuer())) {
hasDuplicates = true;
break;
}
}
}while(hasDuplicates);
}
}
os.addAll(Arrays.asList(data));
updateOTPs(groupId, os); updateOTPs(groupId, os);
if (done != null) done.run();
}, () -> {
os.addAll(Arrays.asList(data));
updateOTPs(groupId, os);
if (done != null) done.run();
}, null);
return;
}
os.addAll(Arrays.asList(data));
updateOTPs(groupId, os);
if(done != null) done.run();
} }
public void updateOTPs(String groupId, List<OTPData> o) { public void updateOTPs(String groupId, List<OTPData> o) {

View File

@ -28,7 +28,7 @@
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
app:passwordToggleEnabled="true"> app:passwordToggleEnabled="true">
<EditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/confirm_password" android:id="@+id/confirm_password"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -163,4 +163,6 @@
<string name="close">Schließen</string> <string name="close">Schließen</string>
<string name="security">Sicherheit</string> <string name="security">Sicherheit</string>
<string name="localization">Lokalisierung</string> <string name="localization">Lokalisierung</string>
<string name="error_duplicate_otp_message">Ein OTP mit dem Namen des OTPs, das du hinzufügen willst, existiert bereits.\n\nWillst du das neue OTP umbenennen lassen, um sie voneinander zu unterscheiden?</string>
<string name="error_duplicate_otp_title">Doppeltes OTP</string>
</resources> </resources>

View File

@ -169,6 +169,8 @@
<string name="close">Close</string> <string name="close">Close</string>
<string name="security">Security</string> <string name="security">Security</string>
<string name="localization">Localization</string> <string name="localization">Localization</string>
<string name="error_duplicate_otp_message">An OTP with the name of the OTP you\'re trying to add already exists.\n\nDo you want to automatically rename the new OTP to distinguish them from each other?</string>
<string name="error_duplicate_otp_title">Duplicate OTP</string>
<string-array name="edit_otp_choose_image_options"> <string-array name="edit_otp_choose_image_options">
<item>Image from icon pack</item> <item>Image from icon pack</item>
<item>Image from gallery</item> <item>Image from gallery</item>