Skip to content

Commit

Permalink
Add ability to skip duplicates during import
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelschattgen committed Sep 6, 2023
1 parent 5e35cc2 commit b205438
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -26,15 +27,20 @@
import com.beemdevelopment.aegis.vault.VaultEntry;
import com.beemdevelopment.aegis.vault.VaultRepository;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class ImportEntriesActivity extends AegisActivity {
private View _view;
private Menu _menu;
private ImportEntriesAdapter _adapter;
private FabScrollHelper _fabScrollHelper;
Expand All @@ -48,6 +54,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_import_entries);
setSupportActionBar(findViewById(R.id.toolbar));

_view = findViewById(R.id.importEntriesRootView);

ActionBar bar = getSupportActionBar();
bar.setHomeAsUpIndicator(R.drawable.ic_close);
bar.setDisplayHomeAsUpEnabled(true);
Expand Down Expand Up @@ -180,6 +188,7 @@ public void onCanceled() {
}

private void importDatabase(DatabaseImporter.State state) {
List<ImportEntry> importEntries = new ArrayList<>();
DatabaseImporter.Result result;
try {
result = state.convert();
Expand All @@ -191,14 +200,18 @@ private void importDatabase(DatabaseImporter.State state) {

UUIDMap<VaultEntry> entries = result.getEntries();
for (VaultEntry entry : entries.getValues()) {
_adapter.addEntry(new ImportEntry(entry));
ImportEntry importEntry = new ImportEntry(entry);
_adapter.addEntry(importEntry);
importEntries.add(importEntry);
}

List<DatabaseImporterEntryException> errors = result.getErrors();
if (errors.size() > 0) {
String message = getResources().getQuantityString(R.plurals.import_error_dialog, errors.size(), errors.size());
Dialogs.showMultiErrorDialog(this, R.string.import_error_title, message, errors, null);
}

findDuplicates(importEntries);
}

private void showWipeEntriesDialog() {
Expand Down Expand Up @@ -236,6 +249,33 @@ private void saveAndFinish(boolean wipeEntries) {
}
}

private void findDuplicates(List<ImportEntry> importEntries) {
List<UUID> duplicateEntries = new ArrayList<>();
for (ImportEntry importEntry: importEntries) {
boolean exists = _vaultManager.getVault().getEntries().stream().anyMatch(item ->
item.getIssuer().equals(importEntry.getEntry().getIssuer()) &&
Arrays.equals(item.getInfo().getSecret(), importEntry.getEntry().getInfo().getSecret()));

if (exists) {
duplicateEntries.add(importEntry.getEntry().getUUID());
}
}

if (duplicateEntries.size() == 0) {
return;
}

_adapter.setCheckboxStates(duplicateEntries, false);
Snackbar snackbar = Snackbar.make(_view, getResources().getQuantityString(R.plurals.import_duplicate_toast, duplicateEntries.size(), duplicateEntries.size()), Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
_adapter.setCheckboxStates(duplicateEntries, true);
}
});
snackbar.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
_menu = menu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImportEntriesAdapter extends RecyclerView.Adapter<ImportEntryHolder> {
private List<ImportEntry> _entries;
Expand Down Expand Up @@ -67,6 +68,14 @@ public List<ImportEntry> getCheckedEntries() {
return entries;
}

public void setCheckboxStates(List<UUID> uuids, boolean state) {
for (ImportEntry entry : _entries) {
if(uuids.contains(entry.getEntry().getUUID())) {
entry.setIsChecked(state);
}
}
}

public void toggleCheckboxes() {
int checkedEntries = getCheckedEntries().size();
if (checkedEntries == 0 || checkedEntries != _entries.size()) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<string name="share">Share</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="undo">Undo</string>
<string name="unlock">Unlock</string>
<string name="advanced">Advanced</string>
<string name="counter">Counter</string>
Expand Down Expand Up @@ -469,6 +470,10 @@
<string name="pref_panic_trigger_summary">Delete vault when a panic trigger is received from Ripple</string>

<string name="import_vault">Import vault</string>
<plurals name="import_duplicate_toast">
<item quantity="one">Unchecked %d potential duplicate. Please review the list of entries.</item>
<item quantity="other">Unchecked %d potential duplicates. Please review the list of entries.</item>
</plurals>
<string name="importer_help_2fas">Supply a 2FAS Authenticator backup file.</string>
<string name="importer_help_aegis">Supply an Aegis export/backup file.</string>
<string name="importer_help_authenticator_plus">Supply an Authenticator Plus export file obtained through <b>Settings -> Backup &amp; Restore -> Export as Text and HTML</b>.</string>
Expand Down

0 comments on commit b205438

Please sign in to comment.