1a85831df1721dda712be5154fb57404d8df1758aScott Kennedy/*
2a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * Copyright (C) 2013 Google Inc.
3a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * Licensed to The Android Open Source Project.
4a85831df1721dda712be5154fb57404d8df1758aScott Kennedy *
5a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
6a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * you may not use this file except in compliance with the License.
7a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * You may obtain a copy of the License at
8a85831df1721dda712be5154fb57404d8df1758aScott Kennedy *
9a85831df1721dda712be5154fb57404d8df1758aScott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
10a85831df1721dda712be5154fb57404d8df1758aScott Kennedy *
11a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * Unless required by applicable law or agreed to in writing, software
12a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
13a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * See the License for the specific language governing permissions and
15a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * limitations under the License.
16a85831df1721dda712be5154fb57404d8df1758aScott Kennedy */
17a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
18a85831df1721dda712be5154fb57404d8df1758aScott Kennedypackage com.android.mail.ui;
19a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
20a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport com.android.mail.R;
21a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport com.google.common.collect.ImmutableList;
22a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport com.google.common.collect.Sets;
23a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
24a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.app.AlertDialog;
25a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.app.Dialog;
26a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.app.DialogFragment;
27a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.content.Context;
28a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.content.DialogInterface;
29a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.content.DialogInterface.OnClickListener;
30a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.os.Bundle;
31a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.view.LayoutInflater;
32a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.view.View;
33a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.view.ViewGroup;
34a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.widget.AdapterView;
35a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.widget.AdapterView.OnItemClickListener;
36a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.widget.BaseAdapter;
37a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.widget.CheckedTextView;
38a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport android.widget.ListView;
39a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
40a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
41a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport java.lang.ref.WeakReference;
42a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport java.util.ArrayList;
43a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport java.util.Collection;
44a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport java.util.List;
45a85831df1721dda712be5154fb57404d8df1758aScott Kennedyimport java.util.Set;
46a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
47a85831df1721dda712be5154fb57404d8df1758aScott Kennedy/**
48a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * A {@link DialogFragment} that shows multiple values, and lets you select 0 to
49a85831df1721dda712be5154fb57404d8df1758aScott Kennedy * {@link #MAX_SELECTED_VALUES} of them.
50a85831df1721dda712be5154fb57404d8df1758aScott Kennedy */
51a85831df1721dda712be5154fb57404d8df1758aScott Kennedypublic abstract class LimitedMultiSelectDialogFragment extends DialogFragment {
52a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    public interface LimitedMultiSelectDialogListener {
53a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        void onSelectionChanged(Set<String> selectedValues);
54a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    }
55a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
56a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    private static final String ARG_ENTRIES = "entries";
57a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    private static final String ARG_ENTRY_VALUES = "entryValues";
58a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    private static final String ARG_SELECTED_VALUES = "selectedValues";
59a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
60a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    private WeakReference<LimitedMultiSelectDialogListener> mListener = null;
61a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
62f0ea4849bf7a2c11f99ca0b42307ae8ba665b1dcPaul Westbrook    // Public no-args constructor needed for fragment re-instantiation
63f0ea4849bf7a2c11f99ca0b42307ae8ba665b1dcPaul Westbrook    public LimitedMultiSelectDialogFragment() {}
64a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    /**
65a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     * Populates the arguments on a {@link LimitedMultiSelectDialogFragment}.
66a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     */
67a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    protected static void populateArguments(final LimitedMultiSelectDialogFragment fragment,
68a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            final ArrayList<String> entries, final ArrayList<String> entryValues,
69a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            final ArrayList<String> selectedValues) {
70a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final Bundle args = new Bundle(3);
71a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        args.putStringArrayList(ARG_ENTRIES, entries);
72a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        args.putStringArrayList(ARG_ENTRY_VALUES, entryValues);
73a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        args.putStringArrayList(ARG_SELECTED_VALUES, selectedValues);
74a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        fragment.setArguments(args);
75a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    }
76a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
77a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    @Override
78a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    public Dialog onCreateDialog(final Bundle savedInstanceState) {
79a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final List<String> selectedValuesList =
80a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                getArguments().getStringArrayList(ARG_SELECTED_VALUES);
81a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final Set<String> selectedValues = Sets.newHashSet(selectedValuesList);
82a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
83a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final List<String> entryValues = getArguments().getStringArrayList(ARG_ENTRY_VALUES);
84a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
85a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final LimitedMultiSelectAdapter adapter = new LimitedMultiSelectAdapter(
86a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                getActivity(), getArguments().getStringArrayList(ARG_ENTRIES), entryValues,
87a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                getMaxSelectedValues());
88a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        adapter.setSelected(selectedValues);
89a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
90a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        final ListView listView = new ListView(getActivity());
91a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
92a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        listView.setAdapter(adapter);
93a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        listView.setOnItemClickListener(new OnItemClickListener() {
94a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            @Override
95a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            public void onItemClick(
96a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    final AdapterView<?> parent, final View view, final int position,
97a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    final long id) {
98a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                final String entryValue = (String) parent.getItemAtPosition(position);
99a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
100a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                if (selectedValues.contains(entryValue)) {
101a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    // Remove / uncheck
102a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    selectedValues.remove(entryValue);
103a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    adapter.removeSelected(entryValue);
104a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                } else {
105a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    // Add / check
106a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    selectedValues.add(entryValue);
107a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    adapter.addSelected(entryValue);
108a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                }
109a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
110a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                getArguments().putStringArrayList(ARG_SELECTED_VALUES,
111a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                        new ArrayList<String>(selectedValues));
112a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
113a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                adapter.notifyDataSetChanged();
114a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
115a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        });
116a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
117a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        // Set initial check states
118a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        for (final String value : selectedValues) {
119a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            for (int j = 0; j < entryValues.size(); j++) {
120a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                if (entryValues.get(j).equals(value)) {
121a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    listView.setItemChecked(j, true);
122a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                }
123a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
124a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
125a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
126a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        return new AlertDialog.Builder(getActivity()).setTitle(getDialogTitle())
127a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                .setView(listView)
12813e2f30d5ae4b8ce14578f53933f7e66b62386a2Scott Kennedy                .setPositiveButton(R.string.ok, new OnClickListener() {
129a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    @Override
130a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    public void onClick(final DialogInterface dialog, final int which) {
131a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                        if (mListener != null) {
132a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                            final LimitedMultiSelectDialogListener listener = mListener.get();
133a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                            if (listener != null) {
134a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                                listener.onSelectionChanged(selectedValues);
135a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                            }
136a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                        }
137a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    }
138a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                })
139a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                .setNegativeButton(R.string.cancel, new OnClickListener() {
140a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    @Override
141a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    public void onClick(final DialogInterface dialog, final int which) {
142a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                        dismiss();
143a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                    }
144a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                })
145a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                .create();
146a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    }
147a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
148a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    public void setListener(final LimitedMultiSelectDialogListener listener) {
149a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        mListener = new WeakReference<LimitedMultiSelectDialogListener>(listener);
150a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    }
151a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
152a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    private static class LimitedMultiSelectAdapter extends BaseAdapter {
153a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        private final Context mContext;
154a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
155a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        private final List<String> mEntries;
156a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        private final List<String> mEntryValues;
157a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
158a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        private final Set<String> mSelectedValues;
159a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
160a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        private final int mMaxSelectedValues;
161a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
162a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public LimitedMultiSelectAdapter(final Context context, final List<String> entries,
163a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                final List<String> entryValues, final int maxSelectedValues) {
164a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mContext = context;
165a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
166a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mEntries = ImmutableList.copyOf(entries);
167a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mEntryValues = ImmutableList.copyOf(entryValues);
168a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
169a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mSelectedValues = Sets.newHashSetWithExpectedSize(maxSelectedValues);
170a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
171a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mMaxSelectedValues = maxSelectedValues;
172a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
173a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            if (mEntries.size() != mEntryValues.size()) {
174a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                throw new IllegalArgumentException("Each entry must have a corresponding value");
175a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
176a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
177a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
178a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
179a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public int getCount() {
180a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return mEntries.size();
181a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
182a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
183a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
184a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public String getItem(final int position) {
185a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return mEntryValues.get(position);
186a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
187a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
188a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
189a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public long getItemId(final int position) {
190a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return getItem(position).hashCode();
191a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
192a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
193a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
194a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public int getItemViewType(final int position) {
195a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return 0;
196a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
197a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
198a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
199a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public View getView(final int position, final View convertView, final ViewGroup parent) {
200a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            final CheckedTextView checkedTextView;
201a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
202a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            if (convertView == null) {
203a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                checkedTextView = (CheckedTextView) LayoutInflater.from(mContext)
204a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                        .inflate(R.layout.select_dialog_multichoice_holo, null);
205a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            } else {
206a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                checkedTextView = (CheckedTextView) convertView;
207a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
208a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
209a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            checkedTextView.setText(mEntries.get(position));
210a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            checkedTextView.setEnabled(isEnabled(position));
211a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
212a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return checkedTextView;
213a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
214a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
215a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
216a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public int getViewTypeCount() {
217a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return 1;
218a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
219a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
220a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
221a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public boolean hasStableIds() {
222a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return true;
223a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
224a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
225a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
226a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public boolean isEmpty() {
227a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return mEntries.isEmpty();
228a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
229a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
230a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
231a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public boolean areAllItemsEnabled() {
232a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            // If we have less than the maximum number selected, everything is
233a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            // enabled
234a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            if (mMaxSelectedValues > mSelectedValues.size()) {
235a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                return true;
236a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
237a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
238a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return false;
239a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
240a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
241a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        @Override
242a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public boolean isEnabled(final int position) {
243a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            // If we have less than the maximum selected, everything is enabled
244a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            if (mMaxSelectedValues > mSelectedValues.size()) {
245a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                return true;
246a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
247a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
248a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            // If we have the maximum selected, only the selected rows are
249a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            // enabled
250a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            if (mSelectedValues.contains(getItem(position))) {
251a85831df1721dda712be5154fb57404d8df1758aScott Kennedy                return true;
252a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            }
253a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
254a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            return false;
255a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
256a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
257a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public void setSelected(final Collection<String> selectedValues) {
258a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mSelectedValues.clear();
259a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mSelectedValues.addAll(selectedValues);
260a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            notifyDataSetChanged();
261a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
262a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
263a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public void addSelected(final String selectedValue) {
264a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mSelectedValues.add(selectedValue);
265a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            notifyDataSetChanged();
266a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
267a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
268a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        public void removeSelected(final String selectedValue) {
269a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            mSelectedValues.remove(selectedValue);
270a85831df1721dda712be5154fb57404d8df1758aScott Kennedy            notifyDataSetChanged();
271a85831df1721dda712be5154fb57404d8df1758aScott Kennedy        }
272a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    }
273a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
274a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    /**
275a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     * Gets a unique String to be used as a tag for this Fragment.
276a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     */
277a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    protected abstract String getFragmentTag();
278a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
279a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    /**
280a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     * Gets the maximum number of values that may be selected.
281a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     */
282a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    protected abstract int getMaxSelectedValues();
283a85831df1721dda712be5154fb57404d8df1758aScott Kennedy
284a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    /**
285a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     * Gets the title of the dialog.
286a85831df1721dda712be5154fb57404d8df1758aScott Kennedy     */
287a85831df1721dda712be5154fb57404d8df1758aScott Kennedy    protected abstract String getDialogTitle();
288a85831df1721dda712be5154fb57404d8df1758aScott Kennedy}
289