From eba1dbc8fafa019fbca09fb98702482c0eaaaa57 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Fri, 12 Apr 2019 21:46:40 +0200 Subject: [PATCH] Move touch, fix tree construction, refactor getPwDatabase --- .../keepass/activities/EntryActivity.java | 31 +++++---- .../keepass/activities/EntryEditActivity.java | 32 +++++---- .../keepass/activities/GroupActivity.java | 8 ++- .../keepass/adapters/NodeAdapter.java | 7 +- .../database/action/node/AddEntryRunnable.kt | 8 ++- .../database/action/node/AddGroupRunnable.kt | 7 +- .../database/action/node/CopyEntryRunnable.kt | 1 + .../action/node/DeleteEntryRunnable.kt | 1 + .../action/node/DeleteGroupRunnable.java | 1 + .../action/node/UpdateEntryRunnable.kt | 2 +- .../action/node/UpdateGroupRunnable.kt | 2 +- .../keepass/database/element/Database.java | 68 ++++++++++++++----- .../keepass/database/element/PwDatabase.java | 65 +++++++----------- .../database/element/PwDatabaseV4.java | 26 +++---- .../database/element/PwEntryInterface.java | 10 +-- .../keepass/database/element/PwEntryV3.java | 13 ---- .../keepass/database/element/PwEntryV4.java | 16 ++--- .../keepass/database/element/PwGroupV3.java | 4 -- .../keepass/database/element/PwGroupV4.java | 25 +++---- .../keepass/database/element/PwNode.java | 5 -- 20 files changed, 168 insertions(+), 164 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.java index 75150c9f6..722fe4248 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.java @@ -45,7 +45,6 @@ import com.kunzisoft.keepass.activities.lock.LockingHideActivity; import com.kunzisoft.keepass.app.App; import com.kunzisoft.keepass.database.ExtraFields; import com.kunzisoft.keepass.database.element.Database; -import com.kunzisoft.keepass.database.element.PwDatabase; import com.kunzisoft.keepass.database.element.PwEntryInterface; import com.kunzisoft.keepass.database.element.PwNodeId; import com.kunzisoft.keepass.database.security.ProtectedString; @@ -126,6 +125,9 @@ public class EntryActivity extends LockingHideActivity { return; } + // Update last access time. + mEntry.touch(false, false); + // Retrieve the textColor to tint the icon int[] attrs = {R.attr.textColorInverse}; TypedArray ta = getTheme().obtainStyledAttributes(attrs); @@ -133,9 +135,6 @@ public class EntryActivity extends LockingHideActivity { // Refresh Menu contents in case onCreateMenuOptions was called before mEntry was set invalidateOptionsMenu(); - - // Update last access time. - mEntry.touch(false, false); // Get views titleIconView = findViewById(R.id.entry_icon); @@ -156,8 +155,10 @@ public class EntryActivity extends LockingHideActivity { fillData(); invalidateOptionsMenu(); + Database database = App.getDB(); // Start to manage field reference to copy a value from ref - mEntry.startToManageFieldReferences(App.getDB().getPwDatabase()); + if (database != null) + database.startManageEntry(mEntry); boolean containsUsernameToCopy = mEntry.getUsername().length() > 0; @@ -232,7 +233,9 @@ public class EntryActivity extends LockingHideActivity { startService(intent); } - mEntry.stopToManageFieldReferences(); + + if (database != null) + database.stopManageEntry(mEntry); } firstLaunchOfActivity = false; } @@ -311,13 +314,12 @@ public class EntryActivity extends LockingHideActivity { } protected void fillData() { - Database db = App.getDB(); - PwDatabase pm = db.getPwDatabase(); - - mEntry.startToManageFieldReferences(pm); - - // Assign title icon - db.getDrawFactory().assignDatabaseIconTo(this, titleIconView, mEntry.getIcon(), iconColor); + Database database = App.getDB(); + if (database != null) { + database.startManageEntry(mEntry); + // Assign title icon + database.getDrawFactory().assignDatabaseIconTo(this, titleIconView, mEntry.getIcon(), iconColor); + } // Assign title text titleView.setText(PwEntryInterface.getVisualTitle(mEntry)); @@ -395,7 +397,8 @@ public class EntryActivity extends LockingHideActivity { entryContentsView.assignExpiresDate(getString(R.string.never)); } - mEntry.stopToManageFieldReferences(); + if (database != null) + database.stopManageEntry(mEntry); } @Override diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java index 56d280eca..2eee05260 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java @@ -47,13 +47,12 @@ import com.kunzisoft.keepass.database.action.node.AddEntryRunnable; import com.kunzisoft.keepass.database.action.node.AfterActionNodeFinishRunnable; import com.kunzisoft.keepass.database.action.node.UpdateEntryRunnable; import com.kunzisoft.keepass.database.element.Database; -import com.kunzisoft.keepass.database.element.PwDatabase; import com.kunzisoft.keepass.database.element.PwDate; import com.kunzisoft.keepass.database.element.PwEntryInterface; -import com.kunzisoft.keepass.database.element.PwIcon; -import com.kunzisoft.keepass.database.element.PwNodeId; import com.kunzisoft.keepass.database.element.PwGroupInterface; +import com.kunzisoft.keepass.database.element.PwIcon; import com.kunzisoft.keepass.database.element.PwIconStandard; +import com.kunzisoft.keepass.database.element.PwNodeId; import com.kunzisoft.keepass.database.security.ProtectedString; import com.kunzisoft.keepass.dialogs.GeneratePasswordDialogFragment; import com.kunzisoft.keepass.dialogs.IconPickerDialogFragment; @@ -87,6 +86,7 @@ public class EntryEditActivity extends LockingHideActivity private Database database; protected PwEntryInterface mEntry; + protected PwGroupInterface mParent; protected PwEntryInterface mCallbackNewEntry; protected boolean mIsNew; protected PwIconStandard mSelectedIconStandard; @@ -179,18 +179,17 @@ public class EntryEditActivity extends LockingHideActivity TypedArray ta = getTheme().obtainStyledAttributes(attrs); iconColor = ta.getColor(0, Color.WHITE); - mSelectedIconStandard = database.getPwDatabase().getIconFactory().getUnknownIcon(); + mSelectedIconStandard = database.getIconFactory().getUnknownIcon(); - PwDatabase pm = database.getPwDatabase(); if (keyEntry == null) { PwNodeId parentId = intent.getParcelableExtra(KEY_PARENT); - PwGroupInterface parent = pm.getGroupById(parentId); - mEntry = database.createEntry(parent); + mParent = database.getGroupById(parentId); + mEntry = database.createEntry(); mIsNew = true; // Add the default icon database.getDrawFactory().assignDefaultDatabaseIconTo(this, entryIconView, iconColor); } else { - mEntry = pm.getEntryById(keyEntry); + mEntry = database.getEntryById(keyEntry); mIsNew = false; fillData(); } @@ -277,6 +276,7 @@ public class EntryEditActivity extends LockingHideActivity task = new AddEntryRunnable(EntryEditActivity.this, database, mCallbackNewEntry, + mParent, afterActionNodeFinishRunnable, !getReadOnly()); } else { @@ -416,13 +416,14 @@ public class EntryEditActivity extends LockingHideActivity } protected PwEntryInterface populateNewEntry() { - PwDatabase db = App.getDB().getPwDatabase(); + Database database = App.getDB(); PwEntryInterface newEntry = mEntry.duplicate(); - newEntry.startToManageFieldReferences(db); - - newEntry.createBackup(db); + if (database != null) { + database.startManageEntry(newEntry); + database.createBackupOf(newEntry); + } newEntry.setLastAccessTime(new PwDate()); newEntry.setLastModificationTime(new PwDate()); @@ -448,7 +449,8 @@ public class EntryEditActivity extends LockingHideActivity } } - newEntry.stopToManageFieldReferences(); + if (database != null) + database.stopManageEntry(newEntry); return newEntry; } @@ -462,7 +464,7 @@ public class EntryEditActivity extends LockingHideActivity return mSelectedIconStandard; else { if (mIsNew) { - return database.getPwDatabase().getIconFactory().getKeyIcon(); + return database.getIconFactory().getKeyIcon(); } else { // Keep previous icon, if no new one was selected @@ -512,7 +514,7 @@ public class EntryEditActivity extends LockingHideActivity assignIconView(); // Don't start the field reference manager, we want to see the raw ref - mEntry.stopToManageFieldReferences(); + App.getDB().stopManageEntry(mEntry); entryTitleView.setText(mEntry.getTitle()); entryUserNameView.setText(mEntry.getUsername()); diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java index 41e55b309..847dc4e19 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java @@ -255,6 +255,9 @@ public class GroupActivity extends LockingActivity return; } + // Update last access time. + mCurrentGroup.touch(false, false); + toolbar.setTitle(""); setSupportActionBar(toolbar); @@ -976,14 +979,17 @@ public class GroupActivity extends LockingActivity case CREATION: // If group creation // Build the group - PwGroupInterface newGroup = database.createGroup(mCurrentGroup); + PwGroupInterface newGroup = database.createGroup(); newGroup.setTitle(name); newGroup.setIcon(icon); + // Not really needed here because added in runnable but safe + newGroup.setParent(mCurrentGroup); // If group created save it in the database new Thread(new AddGroupRunnable(this, App.getDB(), newGroup, + mCurrentGroup, new AfterAddNodeRunnable(), !getReadOnly()) ).start(); diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java index 4fbb1179e..7eb164d61 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java @@ -250,7 +250,9 @@ public class NodeAdapter extends RecyclerView.Adapter { holder.subText.setVisibility(View.GONE); if (subNode.getType().equals(PwNodeInterface.Type.ENTRY)) { PwEntryInterface entry = (PwEntryInterface) subNode; - entry.startToManageFieldReferences(database.getPwDatabase()); + + if (database != null) + database.startManageEntry(entry); holder.text.setText(PwEntryInterface.getVisualTitle(entry)); @@ -260,7 +262,8 @@ public class NodeAdapter extends RecyclerView.Adapter { holder.subText.setText(username); } - entry.stopToManageFieldReferences(); + if (database != null) + database.stopManageEntry(entry); } // Assign image and text size diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddEntryRunnable.kt index edbca87f2..e770e13d6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddEntryRunnable.kt @@ -1,5 +1,5 @@ /* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. + * Copyright 2019 Jeremy Jamet / Kunzisoft. * * This file is part of KeePass DX. * @@ -22,17 +22,21 @@ package com.kunzisoft.keepass.database.action.node import android.support.v4.app.FragmentActivity import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.PwEntryInterface +import com.kunzisoft.keepass.database.element.PwGroupInterface class AddEntryRunnable constructor( context: FragmentActivity, database: Database, private val mNewEntry: PwEntryInterface, + private val mParent: PwGroupInterface, finishRunnable: AfterActionNodeFinishRunnable?, save: Boolean) : ActionNodeDatabaseRunnable(context, database, finishRunnable, save) { override fun nodeAction() { - database.addEntryTo(mNewEntry, mNewEntry.parent) + mNewEntry.touch(true, true) + mParent.touch(true, true) + database.addEntryTo(mNewEntry, mParent) } override fun nodeFinish(isSuccess: Boolean, message: String?): ActionNodeValues { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddGroupRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddGroupRunnable.kt index 79c2ddfc5..5daf7eb05 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddGroupRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/AddGroupRunnable.kt @@ -27,17 +27,20 @@ class AddGroupRunnable constructor( context: FragmentActivity, database: Database, private val mNewGroup: PwGroupInterface, + private val mParent: PwGroupInterface, afterAddNodeRunnable: AfterActionNodeFinishRunnable?, save: Boolean) : ActionNodeDatabaseRunnable(context, database, afterAddNodeRunnable, save) { override fun nodeAction() { - database.addGroupTo(mNewGroup, mNewGroup.parent) + mNewGroup.touch(true, true) + mParent.touch(true, true) + database.addGroupTo(mNewGroup, mParent) } override fun nodeFinish(isSuccess: Boolean, message: String?): ActionNodeValues { if (!isSuccess) { - database.removeGroupFrom(mNewGroup, mNewGroup.parent) + database.removeGroupFrom(mNewGroup, mParent) } return ActionNodeValues(isSuccess, message, null, mNewGroup) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/CopyEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/CopyEntryRunnable.kt index 680424625..c04c79a8a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/CopyEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/CopyEntryRunnable.kt @@ -38,6 +38,7 @@ class CopyEntryRunnable constructor( override fun nodeAction() { // Update entry with new values + mNewParent.touch(false, true) mEntryCopied = database.copyEntry(mEntryToCopy, mNewParent) mEntryCopied?.apply { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteEntryRunnable.kt index 8bb6fbb18..0a8b4c2b4 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteEntryRunnable.kt @@ -38,6 +38,7 @@ class DeleteEntryRunnable constructor( override fun nodeAction() { mParent = mEntryToDelete.parent + mParent?.touch(false, true) // Remove Entry from parent mRecycle = database.canRecycle(mEntryToDelete) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteGroupRunnable.java b/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteGroupRunnable.java index ab94178f6..251dc5245 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteGroupRunnable.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/DeleteGroupRunnable.java @@ -46,6 +46,7 @@ public class DeleteGroupRunnable extends ActionNodeDatabaseRunnable { @Override public void nodeAction() { mParent = mGroupToDelete.getParent(); + mParent.touch(false, true); // Remove Group from parent mRecycle = getDatabase().canRecycle(mGroupToDelete); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt index 74e81ddf5..399e185d8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt @@ -37,8 +37,8 @@ class UpdateEntryRunnable constructor( override fun nodeAction() { // Update entry with new values - database.updateEntry(mOldEntry, mNewEntry) mOldEntry.touch(true, true) + database.updateEntry(mOldEntry, mNewEntry) } override fun nodeFinish(isSuccess: Boolean, message: String?): ActionNodeValues { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateGroupRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateGroupRunnable.kt index eb4e9e326..4d97a6846 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateGroupRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateGroupRunnable.kt @@ -37,8 +37,8 @@ class UpdateGroupRunnable constructor( override fun nodeAction() { // Update group with new values - database.updateGroup(mOldGroup, mNewGroup) mOldGroup.touch(true, true) + database.updateGroup(mOldGroup, mNewGroup) } override fun nodeFinish(isSuccess: Boolean, message: String?): ActionNodeValues { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.java b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.java index 558ec298a..6421b419b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.java @@ -74,9 +74,7 @@ public class Database { public boolean loaded = false; - public Database() { - - } + public Database() {} public Database(String databasePath) { // TODO Test with kdb extension @@ -135,6 +133,10 @@ public class Database { return drawFactory; } + public PwIconFactory getIconFactory() { + return pwDatabase.getIconFactory(); + } + public void loadData(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status) throws IOException, FileNotFoundException, InvalidDBException { loadData(ctx, uri, password, keyfile, status, !Importer.DEBUG); } @@ -261,7 +263,7 @@ public class Database { public PwEntryInterface getEntryFrom(Cursor cursor) { PwIconFactory iconFactory = getPwDatabase().getIconFactory(); - PwEntryInterface pwEntry = createEntry(null); + PwEntryInterface pwEntry = createEntry(); try { switch (getPwDatabase().getVersion()) { case V3: @@ -269,9 +271,9 @@ public class Database { break; case V4: // TODO invert field reference manager - pwEntry.startToManageFieldReferences(getPwDatabase()); + startManageEntry(pwEntry); ((EntryCursorV4) cursor).populateEntry((PwEntryV4) pwEntry, iconFactory); - pwEntry.stopToManageFieldReferences(); + stopManageEntry(pwEntry); break; } } catch (Exception e) { @@ -554,13 +556,13 @@ public class Database { } } - public PwEntryInterface createEntry(@Nullable PwGroupInterface parent) { + public PwEntryInterface createEntry() { try { switch (getPwDatabase().getVersion()) { case V3: - return new PwEntryV3((PwGroupV3) parent); + return new PwEntryV3(); case V4: - return new PwEntryV4((PwGroupV4) parent); + return new PwEntryV4(); } } catch (Exception e) { Log.e(TAG, "This version of PwEntry can't be created", e); @@ -568,14 +570,14 @@ public class Database { return null; } - public PwGroupInterface createGroup(PwGroupInterface parent) { + public PwGroupInterface createGroup() { PwGroupInterface newPwGroup = null; try { switch (getPwDatabase().getVersion()) { case V3: - newPwGroup = new PwGroupV3((PwGroupV3) parent); + newPwGroup = new PwGroupV3(); case V4: - newPwGroup = new PwGroupV4((PwGroupV4) parent); + newPwGroup = new PwGroupV4(); } newPwGroup.setNodeId(pwDatabase.newGroupId()); } catch (Exception e) { @@ -584,6 +586,14 @@ public class Database { return newPwGroup; } + public PwEntryInterface getEntryById(PwNodeId id) { + return pwDatabase.entryIndexes.get(id); + } + + public PwGroupInterface getGroupById(PwNodeId id) { + return pwDatabase.groupIndexes.get(id); + } + public void addEntryTo(PwEntryInterface entry, PwGroupInterface parent) { try { getPwDatabase().addEntryTo(entry, parent); @@ -718,7 +728,8 @@ public class Database { public void deleteEntry(PwEntryInterface entry) { try { - getPwDatabase().deleteEntry(entry); + PwGroupInterface parent = entry.getParent(); + removeEntryFrom(entry, parent); } catch (Exception e) { Log.e(TAG, "This version of PwEntry can't be deleted", e); } @@ -730,14 +741,15 @@ public class Database { new EntryHandler() { @Override public boolean operate(PwEntryInterface entry) { - getPwDatabase().deleteEntry(entry); + deleteEntry(entry); return true; } }, new GroupHandler() { @Override public boolean operate(PwGroupInterface group) { - getPwDatabase().deleteGroup(group); + PwGroupInterface parent = group.getParent(); + removeGroupFrom(group, parent); return true; } }); @@ -772,7 +784,7 @@ public class Database { public void undoDeleteEntry(PwEntryInterface entry, PwGroupInterface parent) { try { - getPwDatabase().undoDeleteEntry(entry, parent); + getPwDatabase().undoDeleteEntryFrom(entry, parent); } catch (Exception e) { Log.e(TAG, "This version of database can't undo the deletion of this version of PwEntry", e); } @@ -785,4 +797,28 @@ public class Database { Log.e(TAG, "This version of database can't undo the deletion of this version of PwGroup", e); } } + + public void startManageEntry(PwEntryInterface entry) { + switch (getPwDatabase().getVersion()) { + case V4: + ((PwEntryV4) entry).startToManageFieldReferences((PwDatabaseV4) getPwDatabase()); + break; + } + } + + public void stopManageEntry(PwEntryInterface entry) { + switch (getPwDatabase().getVersion()) { + case V4: + ((PwEntryV4) entry).stopToManageFieldReferences(); + break; + } + } + + public void createBackupOf(PwEntryInterface entry) { + switch (getPwDatabase().getVersion()) { + case V4: + ((PwEntryV4) entry).createBackup((PwDatabaseV4) getPwDatabase()); + break; + } + } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java index 1cd37daa0..f137815c7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java @@ -19,6 +19,8 @@ */ package com.kunzisoft.keepass.database.element; +import android.support.annotation.Nullable; + import com.kunzisoft.keepass.database.exception.InvalidKeyFileException; import com.kunzisoft.keepass.database.exception.KeyFileEmptyException; import com.kunzisoft.keepass.utils.MemUtil; @@ -275,6 +277,10 @@ public abstract class PwDatabase { this.groupIndexes.put(group.getNodeId(), group); } + public void removeGroupIndex(PwGroupInterface group) { + this.groupIndexes.remove(group.getNodeId()); + } + public int numberOfGroups() { return groupIndexes.size(); } @@ -291,6 +297,10 @@ public abstract class PwDatabase { this.entryIndexes.put(entry.getNodeId(), entry); } + public void removeEntryIndex(PwEntryInterface entry) { + this.entryIndexes.remove(entry.getNodeId()); + } + public int numberOfEntries() { return entryIndexes.size(); } @@ -301,69 +311,44 @@ public abstract class PwDatabase { * ------------------------------------- */ - protected void addGroupTo(PwGroupInterface newGroup, PwGroupInterface parent) { - // Add tree to parent tree - if ( parent == null ) { - parent = rootGroup; - } - - parent.addChildGroup(newGroup); + protected void addGroupTo(PwGroupInterface newGroup, @Nullable PwGroupInterface parent) { + // Add tree to parent tree + if (parent != null) + parent.addChildGroup(newGroup); newGroup.setParent(parent); addGroupIndex(newGroup); - - parent.touch(true, true); } - protected void removeGroupFrom(PwGroupInterface remove, PwGroupInterface parent) { + protected void removeGroupFrom(PwGroupInterface groupToRemove, PwGroupInterface parent) { // Remove tree from parent tree if (parent != null) { - parent.removeChildGroup(remove); + parent.removeChildGroup(groupToRemove); } - groupIndexes.remove(remove.getNodeId()); + removeGroupIndex(groupToRemove); } - protected void addEntryTo(PwEntryInterface newEntry, PwGroupInterface parent) { + protected void addEntryTo(PwEntryInterface newEntry, @Nullable PwGroupInterface parent) { // Add entry to parent - if (parent != null) { - parent.addChildEntry(newEntry); - } + if (parent != null) + parent.addChildEntry(newEntry); newEntry.setParent(parent); - - entryIndexes.put(newEntry.getNodeId(), newEntry); + addEntryIndex(newEntry); } - protected void removeEntryFrom(PwEntryInterface remove, PwGroupInterface parent) { + protected void removeEntryFrom(PwEntryInterface entryToRemove, PwGroupInterface parent) { // Remove entry for parent if (parent != null) { - parent.removeChildEntry(remove); + parent.removeChildEntry(entryToRemove); } - entryIndexes.remove(remove.getNodeId()); + removeEntryIndex(entryToRemove); } - protected void deleteGroup(PwGroupInterface group) { - PwGroupInterface parent = group.getParent(); - removeGroupFrom(group, parent); - parent.touch(false, true); - } - - protected void deleteEntry(PwEntryInterface entry) { - PwGroupInterface parent = entry.getParent(); - removeEntryFrom(entry, parent); - parent.touch(false, true); - } - /* - public void removeGroup(PwGroupV3 tree) { - tree.parent.childGroups.remove(tree); - groups.remove(tree); - } - */ - // TODO Delete group public void undoDeleteGroup(PwGroupInterface group, PwGroupInterface origParent) { addGroupTo(group, origParent); } - public void undoDeleteEntry(PwEntryInterface entry, PwGroupInterface origParent) { + public void undoDeleteEntryFrom(PwEntryInterface entry, PwGroupInterface origParent) { addEntryTo(entry, origParent); } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java index 67148ef19..066d952c2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java @@ -116,15 +116,15 @@ public class PwDatabaseV4 extends PwDatabase { new EntryHandler() { @Override public boolean operate(PwEntryInterface entry) { - entryIndexes.put(entry.getNodeId(), entry); - return false; + addEntryIndex(entry); + return true; } }, new GroupHandler() { @Override public boolean operate(PwGroupInterface group) { - groupIndexes.put(group.getNodeId(), group); - return false; + addGroupIndex(group); + return true; } }); } @@ -601,12 +601,10 @@ public class PwDatabaseV4 extends PwDatabase { PwGroupInterface parent = group.getParent(); removeGroupFrom(group, parent); - parent.touch(false, true); PwGroupInterface recycleBin = getRecycleBin(); addGroupTo(group, recycleBin); - group.touch(false, true); // TODO ? group.touchLocation(); } @@ -616,12 +614,10 @@ public class PwDatabaseV4 extends PwDatabase { PwGroupInterface parent = entry.getParent(); removeEntryFrom(entry, parent); - parent.touch(false, true); PwGroupInterface recycleBin = getRecycleBin(); addEntryTo(entry, recycleBin); - - entry.touch(false, true); + entry.touchLocation(); } @@ -651,15 +647,15 @@ public class PwDatabaseV4 extends PwDatabase { this.deletedObjects.add(deletedObject); } - @Override - public void deleteEntry(PwEntryInterface entry) { - super.deleteEntry(entry); - deletedObjects.add(new PwDeletedObject((UUID) entry.getNodeId().getId())); + @Override + protected void removeEntryFrom(PwEntryInterface entryToRemove, PwGroupInterface parent) { + super.removeEntryFrom(entryToRemove, parent); + deletedObjects.add(new PwDeletedObject((UUID) entryToRemove.getNodeId().getId())); } @Override - public void undoDeleteEntry(PwEntryInterface entry, PwGroupInterface origParent) { - super.undoDeleteEntry(entry, origParent); + public void undoDeleteEntryFrom(PwEntryInterface entry, PwGroupInterface origParent) { + super.undoDeleteEntryFrom(entry, origParent); deletedObjects.remove(new PwDeletedObject((UUID) entry.getNodeId().getId())); } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryInterface.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryInterface.java index 3857ce6c0..f0896c663 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryInterface.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryInterface.java @@ -69,16 +69,8 @@ public interface PwEntryInterface extends PwNodeInterface, SmallTimeInterface { */ boolean isMetaStream(); - /** - * Create a backup of entry - */ - void createBackup(PwDatabase db); - void touchLocation(); - void startToManageFieldReferences(PwDatabase db); - void stopToManageFieldReferences(); - PwEntryInterface duplicate(); String PMS_TAN_ENTRY = ""; @@ -91,7 +83,7 @@ public interface PwEntryInterface extends PwNodeInterface, SmallTimeInterface { /** * {@inheritDoc} * Get the display title from an entry,
- * {@link #startToManageFieldReferences(PwDatabase)} and {@link #stopToManageFieldReferences()} must be called + * {@link #startManageEntry()} and {@link #stopManageEntry()} must be called * before and after {@link #getVisualTitle(PwEntryInterface entry)} */ static String getVisualTitle(boolean isTan, String title, String userName, String url, String id) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java index 51b007e03..d09ef347b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java @@ -97,10 +97,6 @@ public class PwEntryV3 extends PwNode implements PwEntryInterface { public PwEntryV3() { super(); } - - public PwEntryV3(PwGroupV3 parent) { - super(parent); - } public PwEntryV3(Parcel parcel) { super(parcel); @@ -318,9 +314,6 @@ public class PwEntryV3 extends PwNode implements PwEntryInterface { return icon.isMetaStreamIcon(); } - @Override - public void createBackup(PwDatabase db) {} - @Override public boolean isSearchingEnabled() { return false; @@ -331,12 +324,6 @@ public class PwEntryV3 extends PwNode implements PwEntryInterface { return false; } - @Override - public void startToManageFieldReferences(PwDatabase db) {} - - @Override - public void stopToManageFieldReferences() {} - @Override public void touchLocation() {} diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java index c027ccd3a..89217fde9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java @@ -71,10 +71,6 @@ public class PwEntryV4 extends PwNode implements ITimeLogger, PwEntryInte public PwEntryV4() { super(); } - - public PwEntryV4(PwGroupV4 parent) { - super(parent); - } public PwEntryV4(Parcel parcel) { super(parcel); @@ -181,13 +177,11 @@ public class PwEntryV4 extends PwNode implements ITimeLogger, PwEntryInte return Type.ENTRY; } - @Override - public void startToManageFieldReferences(PwDatabase db) { - this.mDatabase = (PwDatabaseV4) db; + public void startToManageFieldReferences(PwDatabaseV4 db) { + this.mDatabase = db; this.mDecodeRef = true; } - @Override public void stopToManageFieldReferences() { this.mDatabase = null; this.mDecodeRef = false; @@ -463,15 +457,13 @@ public class PwEntryV4 extends PwNode implements ITimeLogger, PwEntryInte return size; } - @Override - public void createBackup(PwDatabase db) { + public void createBackup(PwDatabaseV4 db) { PwEntryV4 copy = clone(); copy.history = new ArrayList<>(); history.add(copy); if (db != null) - if (db instanceof PwDatabaseV4) - maintainBackups((PwDatabaseV4) db); + maintainBackups(db); } private boolean maintainBackups(PwDatabaseV4 db) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV3.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV3.java index 7f14bda43..1bbf4f1c3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV3.java @@ -46,10 +46,6 @@ public class PwGroupV3 extends PwNode implements PwGroupInterface { super(); } - public PwGroupV3(PwGroupV3 parent) { - super(parent); - } - public PwGroupV3(Parcel in) { super(in); title = in.readString(); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java index 8dd1c8c66..f93729ac5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java @@ -40,7 +40,7 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter private String title = ""; private PwIconCustom customIcon = PwIconCustom.ZERO; private long usageCount = 0; - private PwDate parentGroupLastMod = new PwDate(); + private PwDate locationChangeDate = new PwDate(); private Map customData = new HashMap<>(); private boolean expires = false; private String notes = ""; @@ -58,11 +58,6 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter public PwGroupV4() { super(); } - - public PwGroupV4(PwGroupV4 parent) { - super(parent); - parentGroupLastMod = new PwDate(); - } public PwGroupV4(String title, PwIconStandard icon) { super(); @@ -75,7 +70,7 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter title = in.readString(); customIcon = in.readParcelable(PwIconCustom.class.getClassLoader()); usageCount = in.readLong(); - parentGroupLastMod = in.readParcelable(PwDate.class.getClassLoader()); + locationChangeDate = in.readParcelable(PwDate.class.getClassLoader()); // TODO customData = MemUtil.readStringParcelableMap(in); expires = in.readByte() != 0; notes = in.readString(); @@ -94,7 +89,7 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter dest.writeString(title); dest.writeParcelable(customIcon, flags); dest.writeLong(usageCount); - dest.writeParcelable(parentGroupLastMod, flags); + dest.writeParcelable(locationChangeDate, flags); // TODO MemUtil.writeStringParcelableMap(dest, customData); dest.writeByte((byte) (expires ? 1 : 0)); dest.writeString(notes); @@ -122,7 +117,7 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter title = source.title; customIcon = source.customIcon; usageCount = source.usageCount; - parentGroupLastMod = source.parentGroupLastMod; + locationChangeDate = source.locationChangeDate; customData = source.customData; expires = source.expires; @@ -145,7 +140,7 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter // name is clone automatically (IMMUTABLE) newGroup.customIcon = new PwIconCustom(this.customIcon); // newGroup.usageCount stay the same in copy - newGroup.parentGroupLastMod = this.parentGroupLastMod.clone(); + newGroup.locationChangeDate = this.locationChangeDate.clone(); // TODO customData make copy from hashmap newGroup.customData = (HashMap) this.customData.clone(); // newGroup.expires stay the same in copy @@ -170,14 +165,20 @@ public class PwGroupV4 extends PwNode implements ITimeLogger, PwGroupInter return Type.GROUP; } + @Override + public void setParent(PwGroupInterface parent) { + super.setParent(parent); + locationChangeDate = new PwDate(); + } + @Override public PwDate getLocationChanged() { - return parentGroupLastMod; + return locationChangeDate; } @Override public void setLocationChanged(PwDate date) { - parentGroupLastMod = date; + locationChangeDate = date; } @Override diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwNode.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwNode.java index ee6feba33..4a0d75621 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwNode.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwNode.java @@ -44,11 +44,6 @@ public abstract class PwNode implements PwNodeInterface, Parcelable, Clo protected PwNode() {} - protected PwNode(PwGroupInterface parent) { - this(); - this.parent = parent; - } - protected PwNode(Parcel in) { nodeId = in.readParcelable(PwNodeId.class.getClassLoader()); // TODO better technique ?