diff --git a/app/src/main/java/com/shunix/encryptor/activity/PasswordListActivity.java b/app/src/main/java/com/shunix/encryptor/activity/PasswordListActivity.java index 39caa6f..7a6278d 100644 --- a/app/src/main/java/com/shunix/encryptor/activity/PasswordListActivity.java +++ b/app/src/main/java/com/shunix/encryptor/activity/PasswordListActivity.java @@ -23,6 +23,40 @@ public class PasswordListActivity extends BaseActivity { private ListView mListView; private Button mAddButton; private PasswordListAdapter mAdapter; + private ActionMode mActionMode; + private int mSelection; + private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { + @Override + public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { + MenuInflater inflater = actionMode.getMenuInflater(); + inflater.inflate(R.menu.context_menu, menu); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { + switch (menuItem.getItemId()) { + case R.id.menu_delete: + deletePwdItem(); + return true; + case R.id.menu_floating: + showFloatingWindow(); + return false; + default: + return false; + } + } + + @Override + public void onDestroyActionMode(ActionMode actionMode) { + mActionMode = null; + } + }; private static final String TAG = PasswordListActivity.class.getName(); @Override @@ -72,20 +106,11 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.menu_setting: startActivity(SettingsActivity.class); return true; - case R.id.menu_floating: - showFloatingWindow(); - return true; default: return super.onOptionsItemSelected(item); } } - private void showFloatingWindow() { - Intent intent = new Intent(FloatingService.INTENT_ACTION); - intent.putExtra(FloatingService.KEY, FloatingService.SHOW_FLOATING_WINDOW); - sendBroadcast(intent); - } - protected void updateUI() { mAdapter.notifyDataSetChanged(); if (mAdapter.getCount() == 0) { @@ -94,33 +119,16 @@ protected void updateUI() { } else { mAddButton.setVisibility(View.GONE); mListView.setVisibility(View.VISIBLE); + mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override - public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { - final int pos = position; - AlertDialog.Builder builder = new AlertDialog.Builder(PasswordListActivity.this); - DialogInterface.OnClickListener onConfirmListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - try { - DeletePasswordTask task = new DeletePasswordTask(PasswordListActivity.this); - task.execute(mAdapter.getItem(pos).name); - } catch (Exception e) { - Log.e(TAG, e.getMessage()); - } - } - }; - DialogInterface.OnClickListener onCancelListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - return; - } - }; - builder.setTitle(getString(R.string.warning)) - .setMessage(getString(R.string.confirm_hint)) - .setPositiveButton(R.string.yes, onConfirmListener) - .setNegativeButton(R.string.no, onCancelListener) - .show(); + public boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) { + if (mActionMode != null) { + return false; + } + mActionMode = startActionMode(mActionModeCallback); + // bad implementation, the mSelection may take side effect to the deletePwdItem() method + mSelection = i; return true; } }); @@ -133,6 +141,44 @@ private void startActivity(Class clazz) { startActivity(intent); } + private void showFloatingWindow() { + Intent intent = new Intent(FloatingService.INTENT_ACTION); + intent.putExtra(FloatingService.KEY, FloatingService.SHOW_FLOATING_WINDOW); + sendBroadcast(intent); + mActionMode.finish(); + } + + private void deletePwdItem() { + AlertDialog.Builder builder = new AlertDialog.Builder(PasswordListActivity.this); + DialogInterface.OnClickListener onConfirmListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + DatabaseManager.PasswordEntity entity = mAdapter.getItem(mSelection); + if (entity == null) { + return; + } + DeletePasswordTask task = new DeletePasswordTask(PasswordListActivity.this); + task.execute(entity.name); + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } + } + }; + DialogInterface.OnClickListener onCancelListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + return; + } + }; + builder.setTitle(getString(R.string.warning)) + .setMessage(getString(R.string.confirm_hint)) + .setPositiveButton(R.string.yes, onConfirmListener) + .setNegativeButton(R.string.no, onCancelListener) + .show(); + mActionMode.finish(); + } + private static class DeletePasswordTask extends AsyncTask { private WeakReference mContext; diff --git a/app/src/main/res/drawable-xhdpi/delete.png b/app/src/main/res/drawable-xhdpi/delete.png new file mode 100644 index 0000000..8d322aa Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/delete.png differ diff --git a/app/src/main/res/drawable-xhdpi/floating.png b/app/src/main/res/drawable-xhdpi/floating.png new file mode 100644 index 0000000..938eff4 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/floating.png differ diff --git a/app/src/main/res/menu/context_menu.xml b/app/src/main/res/menu/context_menu.xml new file mode 100644 index 0000000..58732b1 --- /dev/null +++ b/app/src/main/res/menu/context_menu.xml @@ -0,0 +1,13 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 681117a..cbdde93 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -18,8 +18,4 @@ android:id="@+id/menu_setting" android:showAsAction="never" android:title="@string/settings" /> - \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 0689460..4ab1e8d 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -42,4 +42,5 @@ @string/low_val 悬浮窗 + 删除 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1be3a3d..041ab76 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,4 +42,5 @@ Settings Floating + Delete