diff --git a/app/src/main/java/com/beemdevelopment/aegis/db/DatabaseEntry.java b/app/src/main/java/com/beemdevelopment/aegis/db/DatabaseEntry.java index a7b1304d..fa18708a 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/db/DatabaseEntry.java +++ b/app/src/main/java/com/beemdevelopment/aegis/db/DatabaseEntry.java @@ -5,6 +5,7 @@ import com.beemdevelopment.aegis.encoding.Base64Exception; import com.beemdevelopment.aegis.otp.GoogleAuthInfo; import com.beemdevelopment.aegis.otp.OtpInfo; import com.beemdevelopment.aegis.otp.OtpInfoException; +import com.beemdevelopment.aegis.otp.TotpInfo; import com.beemdevelopment.aegis.util.UUIDMap; import org.json.JSONException; @@ -133,11 +134,34 @@ public class DatabaseEntry extends UUIDMap.Value { } DatabaseEntry entry = (DatabaseEntry) o; - return super.equals(entry) - && getName().equals(entry.getName()) + return super.equals(entry) && equivalates(entry); + } + + /** + * Reports whether this entry is equivalent to the given entry. The UUIDs of these + * entries are ignored during the comparison, so they are not necessarily the same + * instance. + */ + public boolean equivalates(DatabaseEntry entry) { + return getName().equals(entry.getName()) && getIssuer().equals(entry.getIssuer()) && Objects.equals(getGroup(), entry.getGroup()) && getInfo().equals(entry.getInfo()) && Arrays.equals(getIcon(), entry.getIcon()); } + + /** + * Reports whether this entry has its values set to the defaults. + */ + public boolean isDefault() { + return equivalates(getDefault()); + } + + public static DatabaseEntry getDefault() { + try { + return new DatabaseEntry(new TotpInfo(null)); + } catch (OtpInfoException e) { + throw new RuntimeException(e); + } + } } diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/EditEntryActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/EditEntryActivity.java index 87911846..782d7b50 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/EditEntryActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/EditEntryActivity.java @@ -133,51 +133,49 @@ public class EditEntryActivity extends AegisActivity { _advancedSettings = findViewById(R.id.expandableLayout); // fill the fields with values if possible - if (_origEntry != null) { - if (_origEntry.hasIcon()) { - Glide.with(this) - .asDrawable() - .load(_origEntry) - .diskCacheStrategy(DiskCacheStrategy.NONE) - .skipMemoryCache(false) - .into(_iconView); - _hasCustomIcon = true; - } else { - TextDrawable drawable = TextDrawableHelper.generate(_origEntry.getIssuer(), _origEntry.getName(), _iconView); - _iconView.setImageDrawable(drawable); - } - - _textName.setText(_origEntry.getName()); - _textIssuer.setText(_origEntry.getIssuer()); - - OtpInfo info = _origEntry.getInfo(); - if (info instanceof TotpInfo) { - _textPeriod.setText(Integer.toString(((TotpInfo) info).getPeriod())); - _rowPeriod.setVisibility(View.VISIBLE); - } else if (info instanceof HotpInfo) { - _textCounter.setText(Long.toString(((HotpInfo) info).getCounter())); - _rowCounter.setVisibility(View.VISIBLE); - } else { - throw new RuntimeException(); - } - _textDigits.setText(Integer.toString(info.getDigits())); - - byte[] secretBytes = _origEntry.getInfo().getSecret(); - if (secretBytes != null) { - char[] secretChars = Base32.encode(secretBytes); - _textSecret.setText(secretChars, 0, secretChars.length); - } - - String type = _origEntry.getInfo().getType(); - _spinnerType.setSelection(getStringResourceIndex(R.array.otp_types_array, type), false); - - String algo = _origEntry.getInfo().getAlgorithm(false); - _spinnerAlgo.setSelection(getStringResourceIndex(R.array.otp_algo_array, algo), false); - - String group = _origEntry.getGroup(); - setGroup(group); + if (_origEntry.hasIcon()) { + Glide.with(this) + .asDrawable() + .load(_origEntry) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .skipMemoryCache(false) + .into(_iconView); + _hasCustomIcon = true; + } else { + TextDrawable drawable = TextDrawableHelper.generate(_origEntry.getIssuer(), _origEntry.getName(), _iconView); + _iconView.setImageDrawable(drawable); } + _textName.setText(_origEntry.getName()); + _textIssuer.setText(_origEntry.getIssuer()); + + OtpInfo info = _origEntry.getInfo(); + if (info instanceof TotpInfo) { + _textPeriod.setText(Integer.toString(((TotpInfo) info).getPeriod())); + _rowPeriod.setVisibility(View.VISIBLE); + } else if (info instanceof HotpInfo) { + _textCounter.setText(Long.toString(((HotpInfo) info).getCounter())); + _rowCounter.setVisibility(View.VISIBLE); + } else { + throw new RuntimeException(); + } + _textDigits.setText(Integer.toString(info.getDigits())); + + byte[] secretBytes = _origEntry.getInfo().getSecret(); + if (secretBytes != null) { + char[] secretChars = Base32.encode(secretBytes); + _textSecret.setText(secretChars, 0, secretChars.length); + } + + String type = _origEntry.getInfo().getType(); + _spinnerType.setSelection(getStringResourceIndex(R.array.otp_types_array, type), false); + + String algo = _origEntry.getInfo().getAlgorithm(false); + _spinnerAlgo.setSelection(getStringResourceIndex(R.array.otp_algo_array, algo), false); + + String group = _origEntry.getGroup(); + setGroup(group); + // update the icon if the text changed _textIssuer.addTextChangedListener(_iconChangeListener); _textName.addTextChangedListener(_iconChangeListener); @@ -331,7 +329,7 @@ public class EditEntryActivity extends AegisActivity { } // close the activity if the entry has not been changed - if (_origEntry != null && !_hasChangedIcon && _origEntry.equals(entry.get())) { + if (!_hasChangedIcon && _origEntry.equals(entry.get())) { super.onBackPressed(); return; } @@ -465,7 +463,6 @@ public class EditEntryActivity extends AegisActivity { throw new ParseException("Secret is not valid base32."); } - // set otp info OtpInfo info; try { switch (type.toLowerCase()) { @@ -494,14 +491,8 @@ public class EditEntryActivity extends AegisActivity { throw new ParseException("The entered info is incorrect: " + e.getMessage()); } - // set database entry info - DatabaseEntry entry; - if (_origEntry == null) { - entry = new DatabaseEntry(info); - } else { - entry = Cloner.clone(_origEntry); - entry.setInfo(info); - } + DatabaseEntry entry = Cloner.clone(_origEntry); + entry.setInfo(info); entry.setIssuer(_textIssuer.getText().toString()); entry.setName(_textName.getText().toString()); diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java index 48cecc99..739779bc 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java @@ -227,9 +227,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene private void startEditProfileActivity(int requestCode, DatabaseEntry entry, boolean isNew) { Intent intent = new Intent(this, EditEntryActivity.class); - if (entry != null) { - intent.putExtra("entry", entry); - } + intent.putExtra("entry", entry != null ? entry : DatabaseEntry.getDefault()); intent.putExtra("isNew", isNew); intent.putExtra("selectedGroup", _selectedGroup); intent.putExtra("groups", new ArrayList<>(_db.getGroups()));