From cacf739f6d5af79ddfe623cc764b07ff8a6136f6 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sat, 14 May 2016 20:30:55 -0400 Subject: add edit long press action --- .../java/com/zeapo/pwdstore/PasswordStore.java | 8 +++ .../java/com/zeapo/pwdstore/crypto/PgpHandler.java | 67 +++++++++++++++++++++- .../pwdstore/utils/PasswordRecyclerAdapter.java | 22 ++++++- app/src/main/res/menu/context_pass.xml | 7 ++- 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 96e58d89..adfd62ff 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -405,6 +405,14 @@ public class PasswordStore extends AppCompatActivity { startActivityForResult(intent, PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY); } + public void editPassword(PasswordItem item) { + Intent intent = new Intent(this, PgpHandler.class); + intent.putExtra("NAME", item.toString()); + intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath()); + intent.putExtra("Operation", "EDIT"); + startActivityForResult(intent, PgpHandler.REQUEST_CODE_EDIT); + } + public void createPassword() { if (!PasswordRepository.isInitialized()) { new AlertDialog.Builder(this) diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java index fd92a1f7..a80e4800 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java @@ -65,6 +65,7 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne public static final int REQUEST_CODE_DECRYPT_AND_VERIFY = 9913; public static final int REQUEST_CODE_GET_KEY = 9914; public static final int REQUEST_CODE_GET_KEY_IDS = 9915; + public static final int REQUEST_CODE_EDIT = 9916; public final class Constants { public static final String TAG = "Keychain"; @@ -146,7 +147,8 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne public void editPassword() { - // if in encrypt or (in decrypt and password is invisible), do nothing + // if in encrypt or in decrypt and password is invisible + // (because !showPassword, so this will instantly close), do nothing if (findViewById(R.id.crypto_password_show) == null || findViewById(R.id.crypto_container).getVisibility() != View.VISIBLE) return; @@ -353,6 +355,10 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne case REQUEST_CODE_GET_KEY_IDS: getKeyIds(data); break; + case REQUEST_CODE_EDIT: { + edit(data); + break; + } } } else if (resultCode == RESULT_CANCELED) { setResult(RESULT_CANCELED, data); @@ -456,6 +462,39 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne setResult(RESULT_OK); finish(); } + + // edit + if (requestCode == REQUEST_CODE_EDIT && os != null) { + try { + if (returnToCiphertextField) { + findViewById(R.id.progress_bar).setVisibility(View.GONE); + findViewById(R.id.progress_bar_label).setVisibility(View.GONE); + + findViewById(R.id.crypto_container).setVisibility(View.VISIBLE); + + Typeface monoTypeface = Typeface.createFromAsset(getAssets(), "fonts/sourcecodepro.ttf"); + String[] passContent = os.toString("UTF-8").split("\n"); + ((TextView) findViewById(R.id.crypto_password_show)) + .setTypeface(monoTypeface); + ((TextView) findViewById(R.id.crypto_password_show)) + .setText(passContent[0]); + + String extraContent = os.toString("UTF-8").replaceFirst(".*\n", ""); + if (extraContent.length() != 0) { + ((TextView) findViewById(R.id.crypto_extra_show)) + .setTypeface(monoTypeface); + ((TextView) findViewById(R.id.crypto_extra_show)) + .setText(extraContent); + } + + editPassword(); + } else { + Log.d("PGPHANDLER", "Error message after decrypt : " + os.toString()); + } + } catch (UnsupportedEncodingException e) { + Log.e(Constants.TAG, "UnsupportedEncodingException", e); + } + } break; } case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { @@ -560,6 +599,24 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne } + public void edit(Intent data) { + // exactly same as decrypt, only we want a different callback + data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); + + findViewById(R.id.progress_bar).setVisibility(View.VISIBLE); + + try { + InputStream is = FileUtils.openInputStream(new File(getIntent().getExtras().getString("FILE_PATH"))); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService()); + api.executeApiAsync(data, is, os, new PgpCallback(true, os, REQUEST_CODE_EDIT)); + } catch (Exception e) { + e.printStackTrace(); + } + } + // TODO (low priority but still...) android M potential permissions crashes @Override public void onBound(IOpenPgpService2 service) { @@ -591,6 +648,14 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne // String keys = keyIDs.split(",").length > 1 ? keyIDs : keyIDs.split(",")[0]; // ((TextView) findViewById(R.id.crypto_key_ids)).setText(keys); // } + } else if (extra.getString("Operation").equals("EDIT")) { + setContentView(R.layout.decrypt_layout); + ((TextView) findViewById(R.id.crypto_password_file)).setText(extra.getString("NAME")); + String cat = new File(extra.getString("FILE_PATH").replace(PasswordRepository.getWorkTree().getAbsolutePath(), "")) + .getParentFile().getName(); + + ((TextView) findViewById(R.id.crypto_password_category)).setText(cat + "/"); + edit(new Intent()); } } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java index c9158b30..45dd0d0b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java @@ -25,6 +25,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter selectedItems; private ActionMode mActionMode; + private Boolean canEdit; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and @@ -85,6 +86,14 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter= 1 && canEdit) { + canEdit = false; + mActionMode.invalidate(); } } else { listener.onFragmentInteraction(pass); @@ -99,8 +108,10 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter(selectedItems)); mode.finish(); // Action picked, so close the CAB return true; + case R.id.menu_edit_password: + activity.editPassword(values.get(selectedItems.iterator().next())); + mode.finish(); + return true; default: return false; } diff --git a/app/src/main/res/menu/context_pass.xml b/app/src/main/res/menu/context_pass.xml index 009e3fd4..9b8f892c 100644 --- a/app/src/main/res/menu/context_pass.xml +++ b/app/src/main/res/menu/context_pass.xml @@ -5,7 +5,12 @@ + + -- cgit v1.2.3