Skip to content

Commit

Permalink
Add contextual menu
Browse files Browse the repository at this point in the history
  • Loading branch information
shunix committed Jan 21, 2016
1 parent 2b40445 commit 3d46bac
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
});
Expand All @@ -133,6 +141,44 @@ private void startActivity(Class<? extends Activity> 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<String, Void, Boolean> {
private WeakReference<Context> mContext;

Expand Down
Binary file added app/src/main/res/drawable-xhdpi/delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/floating.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions app/src/main/res/menu/context_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_delete"
android:icon="@drawable/delete"
android:showAsAction="ifRoom"
android:title="@string/delete"/>
<item
android:id="@+id/menu_floating"
android:icon="@drawable/floating"
android:showAsAction="ifRoom"
android:title="@string/floating"/>
</menu>
4 changes: 0 additions & 4 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@
android:id="@+id/menu_setting"
android:showAsAction="never"
android:title="@string/settings" />
<item
android:id="@+id/menu_floating"
android:showAsAction="never"
android:title="@string/floating" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
<item>@string/low_val</item>
</string-array>
<string name="floating">悬浮窗</string>
<string name="delete">删除</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
</string-array>
<string name="settings">Settings</string>
<string name="floating">Floating</string>
<string name="delete">Delete</string>
</resources>

0 comments on commit 3d46bac

Please sign in to comment.