Add recycle bin setting, remove version, encapsulate field

This commit is contained in:
J-Jamet committed 2018-02-19 17:20:42 +01:00
1 parent d46a6a2ea8
commit aceeb581d4
13 files changed
+115 -63

No files matched your search

@@ -54,16 +54,12 @@ import com.keepassdroid.compat.ActivityCompat;
import com.keepassdroid.database.Database;
import com.keepassdroid.database.PwDatabase;
import com.keepassdroid.database.PwEntry;
import com.keepassdroid.database.PwEntryV4;
import com.keepassdroid.database.exception.SamsungClipboardException;
import com.keepassdroid.database.security.ProtectedString;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.settings.PrefsUtil;
import com.keepassdroid.tasks.UIToastTask;
import com.keepassdroid.utils.EmptyUtils;
import com.keepassdroid.utils.MenuUtil;
import com.keepassdroid.utils.SprEngine;
import com.keepassdroid.utils.SprEngineV4;
import com.keepassdroid.utils.Types;
import com.keepassdroid.utils.Util;
import com.keepassdroid.view.EntryContentsView;
@@ -265,21 +261,9 @@ public class EntryActivity extends LockCloseHideActivity {
// Assign custom fields
entryContentsView.clearExtraFields();
if (mEntry.getVersion() == 4) {
PwEntryV4 entry = (PwEntryV4) mEntry;
SprEngine spr = SprEngineV4.getInstance(pm);
// Display custom strings
if (entry.strings.size() > 0) {
for (Map.Entry<String, ProtectedString> pair : entry.strings.entrySet()) {
String key = pair.getKey();
if (!PwEntryV4.IsStandardString(key)) {
String text = pair.getValue().toString();
entryContentsView.addExtraField(key, spr.compile(text, entry, pm));
}
}
}
}
for (Map.Entry<String, String> field : mEntry.getExtraFields(pm).entrySet()) {
entryContentsView.addExtraField(field.getKey(), field.getValue());
}
// Assign dates
entryContentsView.assignCreationDate(mEntry.getCreationTime());
@@ -246,7 +246,7 @@ public abstract class PwDatabase {
public abstract void setNumRounds(long rounds) throws NumberFormatException;
public abstract boolean appSettingsEnabled();
public abstract boolean algorithmSettingsEnabled();
public abstract PwEncryptionAlgorithm getEncAlgorithm();
@@ -332,6 +332,22 @@ public abstract class PwDatabase {
}
}
/**
* Determine if RecycleBin is available or not for this version of database
* @return true if RecycleBin enable
*/
public boolean isRecycleBinAvailable() {
return false;
}
/**
* Determine if RecycleBin is enable or not
* @return true if RecycleBin enable, false if is not available or not enable
*/
public boolean isRecycleBinEnable() {
return false;
}
/**
* Define if a Group must be delete or recycle
* @param group Group to remove
@@ -75,7 +75,7 @@ public class PwDatabaseV3 extends PwDatabase {
// Algorithm used to encrypt the database
public PwEncryptionAlgorithm algorithm;
public int numKeyEncRounds;
@Override
public PwEncryptionAlgorithm getEncAlgorithm() {
return algorithm;
@@ -262,7 +262,7 @@ public class PwDatabaseV3 extends PwDatabase {
}
@Override
public boolean appSettingsEnabled() {
public boolean algorithmSettingsEnabled() {
return true;
}
@@ -300,7 +300,7 @@ public class PwDatabaseV4 extends PwDatabase {
}
@Override
public boolean appSettingsEnabled() {
public boolean algorithmSettingsEnabled() {
return false;
}
@@ -360,6 +360,16 @@ public class PwDatabaseV4 extends PwDatabase {
recycleBinUUID = recycleBin.uuid;
}
}
@Override
public boolean isRecycleBinAvailable() {
return true;
}
@Override
public boolean isRecycleBinEnable() {
return recycleBinEnabled;
}
@Override
public boolean canRecycle(PwGroup group) {
@@ -20,9 +20,12 @@
package com.keepassdroid.database;
import com.keepassdroid.database.iterator.EntrySearchStringIterator;
import com.keepassdroid.database.security.ProtectedString;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public abstract class PwEntry extends PwNode implements Cloneable {
@@ -69,11 +72,6 @@ public abstract class PwEntry extends PwNode implements Cloneable {
return Type.ENTRY;
}
/**
* Get the version of entry, (ie: 4 if kdbx database version is 4)
*/
public abstract int getVersion();
public void assign(PwEntry source) {
icon = source.icon;
}
@@ -142,6 +140,14 @@ public abstract class PwEntry extends PwNode implements Cloneable {
}
}
/**
* Retrieve extra fields to show, key is the label, value is the value of field
* @param pm Database
* @return Map of label/value
*/
public Map<String, String> getExtraFields(PwDatabase pm) {
return new HashMap<>();
}
public boolean isMetaStream() {
return false;
@@ -193,11 +193,6 @@ public class PwEntryV3 extends PwEntry {
}
}
@Override
public int getVersion() {
return 3;
}
/**
* @return the actual password byte array.
*/
@@ -19,6 +19,11 @@
*/
package com.keepassdroid.database;
import com.keepassdroid.database.security.ProtectedBinary;
import com.keepassdroid.database.security.ProtectedString;
import com.keepassdroid.utils.SprEngine;
import com.keepassdroid.utils.SprEngineV4;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
@@ -29,10 +34,6 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import com.keepassdroid.database.security.ProtectedBinary;
import com.keepassdroid.database.security.ProtectedString;
import com.keepassdroid.utils.SprEngine;
public class PwEntryV4 extends PwEntry implements ITimeLogger {
public static final String STR_TITLE = "Title";
public static final String STR_USERNAME = "UserName";
@@ -124,10 +125,6 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger {
}
}
@Override
public int getVersion() {
return 4;
}
@SuppressWarnings("unchecked")
@Override
@@ -438,7 +435,24 @@ public class PwEntryV4 extends PwEntry implements ITimeLogger {
history.remove(index);
}
}
@Override
public Map<String, String> getExtraFields(PwDatabase pm) {
Map<String, String> fields = super.getExtraFields(pm);
SprEngine spr = SprEngine.getInstance(pm);
// Display custom strings
if (strings.size() > 0) {
for (Map.Entry<String, ProtectedString> pair : strings.entrySet()) {
String key = pair.getKey();
// TODO Add hidden style for protection field
if (!PwEntryV4.IsStandardString(key)) {
String text = pair.getValue().toString();
fields.put(key, spr.compile(text, this, pm));
}
}
}
return fields;
}
private static final long FIXED_LENGTH_SIZE = 128; // Approximate fixed length size
public long getSize() {
@@ -0,0 +1,5 @@
package com.keepassdroid.database;
public enum PwVersion {
V3, V4
}
@@ -23,6 +23,7 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v14.preference.SwitchPreference;
import android.support.v4.app.DialogFragment;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
@@ -69,11 +70,9 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean value = (Boolean) newValue;
if (!value) {
App.getFileHistory().deleteAllKeys();
}
return true;
}
});
@@ -83,15 +82,12 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean value = (Boolean) newValue;
if (value == null) {
value = true;
}
if (!value) {
App.getFileHistory().deleteAll();
}
return true;
}
});
@@ -142,6 +138,7 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
.setPositiveButton(
getResources().getString(android.R.string.yes),
new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(DialogInterface dialog,
int which) {
@@ -178,27 +175,38 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
setPreferencesFromResource(R.xml.db_preferences, rootKey);
Database db = App.getDB();
Preference algorithmPref = findPreference(getString(R.string.algorithm_key));
Preference roundPref = findPreference(getString(R.string.rounds_key));
if (db.Loaded()) {
if (db.pm.algorithmSettingsEnabled()) {
if (!(db.Loaded() && db.pm.appSettingsEnabled())) {
algorithmPref.setEnabled(false);
roundPref.setEnabled(false);
}
Preference roundPref = findPreference(getString(R.string.rounds_key));
roundPref.setEnabled(true);
roundPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
setRounds(App.getDB(), preference);
return true;
}
});
setRounds(db, roundPref);
// TODO Algo
Preference algorithmPref = findPreference(getString(R.string.algorithm_key));
// algorithmPref.setEnabled(true);
setAlgorithm(db, algorithmPref);
}
if (db.pm.isRecycleBinAvailable()) {
SwitchPreference recycleBinPref = (SwitchPreference) findPreference(getString(R.string.recycle_bin_key));
// TODO Recycle
//recycleBinPref.setEnabled(true);
recycleBinPref.setChecked(db.pm.isRecycleBinEnable());
}
if (db.Loaded() && db.pm.appSettingsEnabled()) {
roundPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
setRounds(App.getDB(), preference);
return true;
}
});
setRounds(db, roundPref);
setAlgorithm(db, algorithmPref);
} else {
Log.e(getClass().getName(), "Database isn't ready");
}
break;
default: