1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.testingcamera2;
18 
19 import android.content.Context;
20 import android.util.Pair;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ArrayAdapter;
25 import android.widget.CheckBox;
26 import android.widget.CompoundButton;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * A specialized adapter containing an array of checkboxes.
33  *
34  * <p>
35  * This adapter contains an array of pairs, where each pair represents the name and checked
36  * state of a given item.
37  * </p>
38  */
39 public class CheckableListAdapter extends ArrayAdapter<CheckableListAdapter.CheckableItem> {
40     private Context mContext;
41 
CheckableListAdapter(Context context, int resource, List<CheckableItem> objects)42     public CheckableListAdapter(Context context, int resource, List<CheckableItem> objects) {
43         super(context, resource, objects);
44         mContext = context;
45     }
46 
47     public static class CheckableItem {
48         public String name;
49         public boolean isChecked;
50 
CheckableItem(String name, boolean isChecked)51         public CheckableItem(String name, boolean isChecked) {
52             this.name = name;
53             this.isChecked = isChecked;
54         }
55     }
56 
57     @Override
getView(final int position, View convertView, ViewGroup parent)58     public View getView(final int position, View convertView, ViewGroup parent) {
59         CheckBox row = (CheckBox) convertView;
60         if (row == null) {
61             LayoutInflater inflater =
62                     (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
63             row = (CheckBox) inflater.inflate(R.layout.checkable_list_item, parent, false);
64         }
65         CheckableItem item = getItem(position);
66         row.setChecked(item.isChecked);
67         row.setText(item.name);
68 
69         row.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
70             @Override
71             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
72                 CheckableItem item = getItem(position);
73                 item.isChecked = b;
74             }
75         });
76         return row;
77     }
78 
79     /**
80      * Returns a list containing the indexes of the currently checked items.
81      *
82      * @return a {@link java.util.List} of indices.
83      */
getCheckedPositions()84     public List<Integer> getCheckedPositions() {
85         ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
86         int size = getCount();
87         for (int i = 0; i < size; i++) {
88             CheckableItem item = getItem(i);
89             if (item.isChecked) {
90                 checkedPositions.add(i);
91             }
92         }
93 
94         return checkedPositions;
95     }
96 
97     /**
98      * Update the items in this list.  Checked state will be preserved for items that are
99      * still included in the list.
100      *
101      * @param elems a list of strings that represents the names of the items to be included.
102      */
updateItems(String[] elems)103     public void updateItems(String[] elems) {
104         ArrayList<CheckableItem> newList = new ArrayList<CheckableItem>();
105         for (String e : elems) {
106             CheckableItem item = new CheckableItem(e, false);
107             newList.add(item);
108             boolean newItem = true;
109             int size = getCount();
110             for (int i = 0; i < size; i++) {
111                 CheckableItem current = getItem(i);
112                 if (current.name.equals(e) && current.isChecked) {
113                     item.isChecked = true;
114                     newItem = false;
115                 }
116             }
117             if (newItem) {
118                 item.isChecked = newItem;
119             }
120         }
121         clear();
122         addAll(newList);
123         notifyDataSetChanged();
124     }
125 
126 }
127