4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 package org.fedorahosted.freeotp.adapters;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashSet;
27 import java.util.List;
30 import java.util.WeakHashMap;
32 import org.fedorahosted.freeotp.R;
34 import android.content.Context;
35 import android.content.res.Resources;
36 import android.view.ActionMode;
37 import android.view.Menu;
38 import android.view.MenuItem;
39 import android.view.View;
40 import android.widget.CompoundButton;
42 public abstract class DeleteActionBarBaseAdapter extends ReorderableBaseAdapter {
43 private final Map<View, CompoundButton> mButtons = new WeakHashMap<View, CompoundButton>();
44 private final Set<Integer> mChecked = new HashSet<Integer>();
45 private ActionMode mActionMode;
48 public void notifyDataSetChanged() {
49 if (mActionMode != null)
52 super.notifyDataSetChanged();
56 public void notifyDataSetInvalidated() {
57 if (mActionMode != null)
60 super.notifyDataSetInvalidated();
64 protected void bindView(View view, int position) {
65 super.bindView(view, position);
66 mButtons.get(view).setChecked(mChecked.contains(position));
70 protected void processView(View view, int type) {
71 super.processView(view, type);
73 CompoundButton cb = getCompoundButton(view);
74 mButtons.put(view, cb);
75 cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
77 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
78 int position = getPositionFromView(buttonView);
81 mChecked.remove(position);
82 if (mChecked.size() == 0 && mActionMode != null)
85 setTitle(buttonView.getContext());
89 if (mChecked.size() == 0) {
90 mActionMode = buttonView.startActionMode(new ActionMode.Callback() {
92 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
97 public void onDestroyActionMode(ActionMode mode) {
99 for (final CompoundButton cb : mButtons.values()) {
100 cb.post(new Runnable() {
103 cb.setChecked(false);
110 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
111 menu.add(R.string.delete).setIcon(android.R.drawable.ic_menu_delete);
116 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
117 // Get the list of all the checked positions
118 // and reverse sort the list.
119 List<Integer> list = new ArrayList<Integer>(mChecked);
120 Collections.sort(list, new Comparator<Integer>() {
122 public int compare(Integer lhs, Integer rhs) {
123 return rhs.intValue() - lhs.intValue();
127 // Delete all the selected tokens (in reverse order!)
128 for (Integer i : list)
137 mChecked.add(position);
138 setTitle(buttonView.getContext());
143 private void setTitle(Context ctx) {
144 if (mActionMode == null || mChecked.size() == 0)
147 Resources res = ctx.getResources();
148 mActionMode.setTitle(res.getQuantityString(R.plurals.tokens_selected,
153 protected abstract CompoundButton getCompoundButton(View view);
154 public abstract void delete(int position);