summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorMatthew Wong <wongma@protonmail.ch>2015-08-11 19:56:32 -0400
committerMatthew Wong <wongma@protonmail.ch>2015-08-14 17:36:51 -0400
commitf4c0ee607e909caaacf52367901652ea89b16581 (patch)
tree8c22dc74c2dc7f373a2a17c06e1f27745201daaf /app/src/main/java
parentb75190fcd7926c3994f8191996661c19b7b12466 (diff)
Multi-select deletion of apps in settings page
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java92
2 files changed, 90 insertions, 4 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java
index 10684f45..54f0ade4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java
+++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java
@@ -8,7 +8,6 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
-import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@@ -149,7 +148,6 @@ public class AutofillPreferenceActivity extends AppCompatActivity {
args.putInt("position", recyclerAdapter.getPosition(packageName));
df.setArguments(args);
df.show(getFragmentManager(), "autofill_dialog");
-
// TODO if called from dialog 'Settings' button, should activity finish at OK?
}
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java
index bd2835e5..14530879 100644
--- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java
+++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java
@@ -5,8 +5,11 @@ import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
+import android.support.v7.view.ActionMode;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
@@ -15,13 +18,19 @@ import android.widget.TextView;
import com.zeapo.pwdstore.R;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.TreeSet;
public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecyclerAdapter.ViewHolder> {
private ArrayList<ApplicationInfo> apps;
private PackageManager pm;
private AutofillPreferenceActivity activity;
+ private final Set<Integer> selectedItems;
+ private ActionMode actionMode;
- public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
+ public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public View view;
public TextView name;
public TextView secondary;
@@ -35,11 +44,30 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
secondary = (TextView) view.findViewById(R.id.secondary_text);
icon = (ImageView) view.findViewById(R.id.app_icon);
view.setOnClickListener(this);
+ view.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
- activity.showDialog(packageName, name.getText().toString());
+ if (actionMode != null) {
+ toggleSelection(getAdapterPosition(), view);
+ if (selectedItems.isEmpty()) {
+ actionMode.finish();
+ }
+ } else {
+ activity.showDialog(packageName, name.getText().toString());
+ }
+ }
+
+ @Override
+ public boolean onLongClick(View v) {
+ if (actionMode != null) {
+ return false;
+ }
+ toggleSelection(getAdapterPosition(), view);
+ // Start the CAB using the ActionMode.Callback
+ actionMode = activity.startSupportActionMode(actionModeCallback);
+ return true;
}
}
@@ -47,6 +75,7 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
this.apps = apps;
this.pm = pm;
this.activity = activity;
+ this.selectedItems = new TreeSet<>(Collections.reverseOrder());
}
@Override
@@ -80,6 +109,8 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
}
holder.icon.setImageDrawable(pm.getApplicationIcon(app));
holder.packageName = app.packageName;
+
+ holder.view.setSelected(selectedItems.contains(position));
}
@Override
@@ -105,4 +136,61 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
}
return -1;
}
+
+ private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
+ @Override
+ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+ mode.getMenuInflater().inflate(R.menu.context_pass, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
+ return false;
+ }
+
+ @Override
+ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.menu_delete_password:
+ // don't ask for confirmation
+ for (int position : selectedItems) {
+ remove(position);
+ }
+ mode.finish(); // Action picked, so close the CAB
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ @Override
+ public void onDestroyActionMode(ActionMode mode) {
+ for (Iterator it = selectedItems.iterator(); it.hasNext();) {
+ // need the setSelected line in onBind
+ notifyItemChanged((Integer) it.next());
+ it.remove();
+ }
+ actionMode = null;
+ }
+ };
+
+ public void remove(int position) {
+ ApplicationInfo applicationInfo = this.apps.get(position);
+ SharedPreferences prefs
+ = activity.getApplicationContext().getSharedPreferences("autofill", Context.MODE_PRIVATE);
+ prefs.edit().remove(applicationInfo.packageName).apply();
+
+ this.apps.remove(position);
+ this.notifyItemRemoved(position);
+ }
+
+ public void toggleSelection(int position, View view) {
+ if (!selectedItems.remove(position)) {
+ selectedItems.add(position);
+ view.setSelected(true);
+ } else {
+ view.setSelected(false);
+ }
+ }
}