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 2df9f4d9b..7ad780f6f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java @@ -37,6 +37,7 @@ import android.support.annotation.RequiresApi; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; +import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -56,6 +57,7 @@ import com.kunzisoft.keepass.database.PwIcon; import com.kunzisoft.keepass.database.PwIconStandard; import com.kunzisoft.keepass.database.PwNode; import com.kunzisoft.keepass.database.action.AddGroupRunnable; +import com.kunzisoft.keepass.database.action.CopyEntryRunnable; import com.kunzisoft.keepass.database.action.DeleteEntryRunnable; import com.kunzisoft.keepass.database.action.DeleteGroupRunnable; import com.kunzisoft.keepass.database.action.UpdateGroupRunnable; @@ -257,6 +259,65 @@ public class GroupActivity extends ListNodesActivity return true; } + @Override + public boolean onCopyMenuClick(PwNode node) { + startActionMode(new ActionMode.Callback() { + @Override + public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { + actionMode.setTitle(R.string.where); + actionMode.getMenuInflater().inflate(R.menu.node_paste_menu, menu); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { + actionMode.finish(); + switch (menuItem.getItemId()) { + + case R.id.menu_paste: + switch (node.getType()) { + case GROUP: + break; + case ENTRY: + copyNode((PwEntry) node, mCurrentGroup); + break; + } + return true; + } + return true; + } + + @Override + public void onDestroyActionMode(ActionMode actionMode) { + + } + }); + // TODO COPY + return false; + } + + private void copyNode(PwEntry entryToCopy, PwGroup newParent) { + CopyEntryRunnable task = new CopyEntryRunnable(this, App.getDB(), entryToCopy, newParent, + new AfterAddNode(new Handler())); + task.setUpdateProgressTaskStatus( + new UpdateProgressTaskStatus(this, + SaveDatabaseProgressTaskDialogFragment.start( + getSupportFragmentManager()) + )); + new Thread(task).start(); + } + + @Override + public boolean onMoveMenuClick(PwNode node) { + // TODO MOVE + return false; + } + @Override public boolean onDeleteMenuClick(PwNode node) { switch (node.getType()) { @@ -270,6 +331,29 @@ public class GroupActivity extends ListNodesActivity return true; } + private void deleteGroup(PwGroup group) { + //TODO Verify trash recycle bin + DeleteGroupRunnable task = new DeleteGroupRunnable(this, App.getDB(), group, + new AfterDeleteNode(new Handler(), group)); + task.setUpdateProgressTaskStatus( + new UpdateProgressTaskStatus(this, + SaveDatabaseProgressTaskDialogFragment.start( + getSupportFragmentManager()) + )); + new Thread(task).start(); + } + + private void deleteEntry(PwEntry entry) { + DeleteEntryRunnable task = new DeleteEntryRunnable(this, App.getDB(), entry, + new AfterDeleteNode(new Handler(), entry)); + task.setUpdateProgressTaskStatus( + new UpdateProgressTaskStatus(this, + SaveDatabaseProgressTaskDialogFragment.start( + getSupportFragmentManager()) + )); + new Thread(task).start(); + } + @Override protected void onResume() { super.onResume(); @@ -420,31 +504,6 @@ public class GroupActivity extends ListNodesActivity addNodeButtonView.hideButton(); } - private void deleteEntry(PwEntry entry) { - Handler handler = new Handler(); - DeleteEntryRunnable task = new DeleteEntryRunnable(this, App.getDB(), entry, - new AfterDeleteNode(handler, entry)); - task.setUpdateProgressTaskStatus( - new UpdateProgressTaskStatus(this, - SaveDatabaseProgressTaskDialogFragment.start( - getSupportFragmentManager()) - )); - new Thread(task).start(); - } - - private void deleteGroup(PwGroup group) { - //TODO Verify trash recycle bin - Handler handler = new Handler(); - DeleteGroupRunnable task = new DeleteGroupRunnable(this, App.getDB(), group, - new AfterDeleteNode(handler, group)); - task.setUpdateProgressTaskStatus( - new UpdateProgressTaskStatus(this, - SaveDatabaseProgressTaskDialogFragment.start( - getSupportFragmentManager()) - )); - new Thread(task).start(); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { 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 e13e8bbaf..f3d772b9e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java @@ -237,6 +237,8 @@ public class NodeAdapter extends RecyclerView.Adapter { public interface NodeMenuListener { boolean onOpenMenuClick(PwNode node); boolean onEditMenuClick(PwNode node); + boolean onCopyMenuClick(PwNode node); + boolean onMoveMenuClick(PwNode node); boolean onDeleteMenuClick(PwNode node); } @@ -264,7 +266,9 @@ public class NodeAdapter extends RecyclerView.Adapter { private static final int MENU_OPEN = Menu.FIRST; private static final int MENU_EDIT = MENU_OPEN + 1; - private static final int MENU_DELETE = MENU_EDIT + 1; + private static final int MENU_COPY = MENU_EDIT + 1; + private static final int MENU_MOVE = MENU_COPY + 1; + private static final int MENU_DELETE = MENU_MOVE + 1; private PwNode node; private NodeMenuListener menuListener; @@ -282,6 +286,12 @@ public class NodeAdapter extends RecyclerView.Adapter { // Edition clearMenu = contextMenu.add(Menu.NONE, MENU_EDIT, Menu.NONE, R.string.menu_edit); clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener); + // Copy + clearMenu = contextMenu.add(Menu.NONE, MENU_COPY, Menu.NONE, R.string.menu_copy); + clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener); + // Move + clearMenu = contextMenu.add(Menu.NONE, MENU_MOVE, Menu.NONE, R.string.menu_move); + clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener); // Deletion clearMenu = contextMenu.add(Menu.NONE, MENU_DELETE, Menu.NONE, R.string.menu_delete); clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener); @@ -298,6 +308,10 @@ public class NodeAdapter extends RecyclerView.Adapter { return menuListener.onOpenMenuClick(node); case MENU_EDIT: return menuListener.onEditMenuClick(node); + case MENU_COPY: + return menuListener.onCopyMenuClick(node); + case MENU_MOVE: + return menuListener.onMoveMenuClick(node); case MENU_DELETE: return menuListener.onDeleteMenuClick(node); default: diff --git a/app/src/main/java/com/kunzisoft/keepass/database/Database.java b/app/src/main/java/com/kunzisoft/keepass/database/Database.java index 37a42d074..cbf2ac781 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/Database.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/Database.java @@ -51,6 +51,9 @@ import java.io.OutputStream; import java.io.SyncFailedException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; + +import javax.annotation.Nullable; public class Database { @@ -659,6 +662,42 @@ public class Database { } } + public @Nullable PwEntry copyEntry(PwEntry entryToCopy, PwGroup newParent) { + try { + // TODO encapsulate + switch (getPwDatabase().getVersion()) { + case V3: + PwEntryV3 entryV3Copied = ((PwEntryV3) entryToCopy).clone(); + entryV3Copied.setUUID(UUID.randomUUID()); + entryV3Copied.setParent((PwGroupV3) newParent); + return entryV3Copied; + case V4: + PwEntryV4 entryV4Copied = ((PwEntryV4) entryToCopy).clone(); + entryV4Copied.setUUID(UUID.randomUUID()); + entryV4Copied.setParent((PwGroupV4) newParent); + return entryV4Copied; + } + } catch (Exception e) { + Log.e(TAG, "This version of PwEntry can't be updated", e); + } + return null; + } + + // TODO copy group + public void copyGroup(PwGroup groupToCopy, PwGroup newParent) { + + } + + // TODO move entry + public void moveEntry(PwEntry entryToMove, PwGroup newParent) { + + } + + // TODO move group + public void moveGroup(PwGroup groupToMove, PwGroup newParent) { + + } + public void deleteEntry(PwEntry entry) { try { switch (getPwDatabase().getVersion()) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/CopyEntryRunnable.java b/app/src/main/java/com/kunzisoft/keepass/database/action/CopyEntryRunnable.java new file mode 100644 index 000000000..29fddf988 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/CopyEntryRunnable.java @@ -0,0 +1,99 @@ +/* + * Copyright 2018 Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.database.action; + +import android.content.Context; +import android.util.Log; + +import com.kunzisoft.keepass.database.Database; +import com.kunzisoft.keepass.database.PwEntry; +import com.kunzisoft.keepass.database.PwGroup; + +public class CopyEntryRunnable extends RunnableOnFinish { + + private static final String TAG = CopyEntryRunnable.class.getName(); + + private Database mDb; + private PwEntry mEntryToCopy; + private PwEntry mEntryCopied; + private PwGroup mNewParent; + private Context mContext; + private boolean mDontSave; + + public CopyEntryRunnable(Context context, Database db, PwEntry oldE, PwGroup newParent, AfterActionNodeOnFinish afterAddNode) { + this(context, db, oldE, newParent, afterAddNode, false); + } + + public CopyEntryRunnable(Context context, Database db, PwEntry oldE, PwGroup newParent, AfterActionNodeOnFinish afterAddNode, boolean dontSave) { + super(afterAddNode); + + this.mDb = db; + this.mEntryToCopy = oldE; + this.mNewParent = newParent; + this.mContext = context; + this.mDontSave = dontSave; + + this.mFinish = new AfterCopy(afterAddNode); + } + + @Override + public void run() { + // Update entry with new values + mEntryCopied = mDb.copyEntry(mEntryToCopy, mNewParent); + mDb.addEntryTo(mEntryCopied, mNewParent); + + if (mEntryCopied != null) { + mEntryCopied.touch(true, true); + + // Commit to disk + SaveDBRunnable save = new SaveDBRunnable(mContext, mDb, mFinish, mDontSave); + save.run(); + } else { + Log.e(TAG, "Unable to create a copy of the entry"); + } + } + + private class AfterCopy extends OnFinishRunnable { + + AfterCopy(OnFinishRunnable finish) { + super(finish); + } + + @Override + public void run() { + if ( !mSuccess ) { + // If we fail to save, try to delete the copy + try { + mDb.deleteEntry(mEntryCopied); + } catch (Exception e) { + Log.i(TAG, "Unable to delete the copied entry"); + } + } + // TODO Better callback + AfterActionNodeOnFinish afterAddNode = + (AfterActionNodeOnFinish) super.mOnFinish; + afterAddNode.mSuccess = mSuccess; + afterAddNode.mMessage = mMessage; + afterAddNode.run(null, mEntryCopied); + + super.run(); + } + } +} diff --git a/app/src/main/res/menu/node_menu.xml b/app/src/main/res/menu/node_menu.xml new file mode 100644 index 000000000..7834d0297 --- /dev/null +++ b/app/src/main/res/menu/node_menu.xml @@ -0,0 +1,49 @@ + + + + + + + diff --git a/app/src/main/res/menu/node_paste_menu.xml b/app/src/main/res/menu/node_paste_menu.xml new file mode 100644 index 000000000..2f482141c --- /dev/null +++ b/app/src/main/res/menu/node_paste_menu.xml @@ -0,0 +1,26 @@ + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index e606f3927..f3e4e8e4c 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -21,6 +21,7 @@ #00ffffff #ffffff #000000 + #0e0e0e #ffa726 #fb8c00 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a16ca4335..3341ea272 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -121,9 +121,12 @@ Application settings Form filling Database settings - Delete Donate Edit + Copy + Move + Paste + Delete Hide Pass Lock Database Open @@ -131,6 +134,7 @@ Show pass Remove the fingerprint key Go to URL + Where ? Minus Never No search results diff --git a/app/src/main/res/values/style_blue.xml b/app/src/main/res/values/style_blue.xml index 66ec63ff1..94cea587d 100644 --- a/app/src/main/res/values/style_blue.xml +++ b/app/src/main/res/values/style_blue.xml @@ -29,6 +29,7 @@ @color/blue_lighter @color/background_light @style/KeepassDXStyle.Toolbar.Blue + @style/KeepassDXStyle.ActionMode.Blue + + + + @@ -41,6 +42,10 @@ @color/colorTextInverse @color/purple_lighter + + + + + + +