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

View File

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

View File

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

View File

@ -2,8 +2,10 @@ package com.cringe_studios.code_guard.util;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
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.CryptoException;
import com.cringe_studios.code_guard.crypto.CryptoParameters;
@ -23,6 +25,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.crypto.SecretKey;
@ -45,11 +48,61 @@ public class OTPDatabase {
return o;
}
public void addOTP(String groupId, OTPData o) {
// TODO: check for code with same name
public void promptAddOTPs(Context context, String groupId, Runnable done, OTPData... data) {
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);
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) {

View File

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

View File

@ -163,4 +163,6 @@
<string name="close">Schließen</string>
<string name="security">Sicherheit</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>

View File

@ -169,6 +169,8 @@
<string name="close">Close</string>
<string name="security">Security</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">
<item>Image from icon pack</item>
<item>Image from gallery</item>