|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.setup.activities; |
|
6 |
|
7 import java.util.ArrayList; |
|
8 import java.util.Collection; |
|
9 import java.util.List; |
|
10 |
|
11 import org.mozilla.gecko.R; |
|
12 import org.mozilla.gecko.sync.repositories.domain.ClientRecord; |
|
13 |
|
14 import android.content.Context; |
|
15 import android.view.View; |
|
16 import android.view.View.OnClickListener; |
|
17 import android.view.ViewGroup; |
|
18 import android.widget.ArrayAdapter; |
|
19 import android.widget.CheckBox; |
|
20 import android.widget.ImageView; |
|
21 import android.widget.TextView; |
|
22 |
|
23 public class ClientRecordArrayAdapter extends ArrayAdapter<ClientRecord> { |
|
24 public static final String LOG_TAG = "ClientRecArrayAdapter"; |
|
25 |
|
26 private boolean[] checkedItems; |
|
27 private SendTabActivity sendTabActivity; |
|
28 |
|
29 public ClientRecordArrayAdapter(Context context, |
|
30 int textViewResourceId) { |
|
31 super(context, textViewResourceId, new ArrayList<ClientRecord>()); |
|
32 this.checkedItems = new boolean[0]; |
|
33 this.sendTabActivity = (SendTabActivity) context; |
|
34 } |
|
35 |
|
36 public synchronized void setClientRecordList(final Collection<ClientRecord> clientRecordList) { |
|
37 this.clear(); |
|
38 this.checkedItems = new boolean[clientRecordList.size()]; |
|
39 for (ClientRecord clientRecord : clientRecordList) { |
|
40 this.add(clientRecord); |
|
41 } |
|
42 this.notifyDataSetChanged(); |
|
43 } |
|
44 |
|
45 /** |
|
46 * If we have only a single client record in the list, mark it as checked. |
|
47 */ |
|
48 public synchronized void checkItem(final int position, boolean checked) throws ArrayIndexOutOfBoundsException { |
|
49 if (position < 0 || |
|
50 position >= checkedItems.length) { |
|
51 throw new ArrayIndexOutOfBoundsException(position); |
|
52 } |
|
53 |
|
54 if (setRowChecked(position, true)) { |
|
55 this.notifyDataSetChanged(); |
|
56 } |
|
57 } |
|
58 |
|
59 /** |
|
60 * Set the specified row to the specified checked state. |
|
61 * @param position an index. |
|
62 * @param checked whether the checkbox should be checked. |
|
63 * @return <code>true</code> if the state changed, <code>false</code> if the |
|
64 * box was already in the requested state. |
|
65 */ |
|
66 protected synchronized boolean setRowChecked(int position, boolean checked) { |
|
67 boolean current = checkedItems[position]; |
|
68 if (current == checked) { |
|
69 return false; |
|
70 } |
|
71 |
|
72 checkedItems[position] = checked; |
|
73 sendTabActivity.enableSend(getNumCheckedGUIDs() > 0); |
|
74 |
|
75 return true; |
|
76 } |
|
77 |
|
78 @Override |
|
79 public View getView(final int position, View convertView, ViewGroup parent) { |
|
80 final Context context = this.getContext(); |
|
81 |
|
82 // Reuse View objects if they exist. |
|
83 View row = convertView; |
|
84 if (row == null) { |
|
85 row = View.inflate(context, R.layout.sync_list_item, null); |
|
86 setSelectable(row, true); |
|
87 row.setBackgroundResource(android.R.drawable.menuitem_background); |
|
88 } |
|
89 |
|
90 final ClientRecord clientRecord = this.getItem(position); |
|
91 ImageView clientType = (ImageView) row.findViewById(R.id.img); |
|
92 TextView clientName = (TextView) row.findViewById(R.id.client_name); |
|
93 |
|
94 // Set up checkbox and restore stored state. |
|
95 CheckBox checkbox = (CheckBox) row.findViewById(R.id.check); |
|
96 checkbox.setChecked(checkedItems[position]); |
|
97 setSelectable(checkbox, false); |
|
98 |
|
99 clientName.setText(clientRecord.name); |
|
100 clientType.setImageResource(getImage(clientRecord)); |
|
101 |
|
102 row.setOnClickListener(new OnClickListener() { |
|
103 @Override |
|
104 public void onClick(View view) { |
|
105 final CheckBox item = (CheckBox) view.findViewById(R.id.check); |
|
106 |
|
107 // Update the checked item, both in the UI and in our internal state. |
|
108 final boolean checked = !item.isChecked(); // Because it hasn't happened yet. |
|
109 item.setChecked(checked); |
|
110 setRowChecked(position, checked); |
|
111 } |
|
112 }); |
|
113 |
|
114 return row; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Get list of checked GUIDs. |
|
119 * |
|
120 * @return non-null list. |
|
121 */ |
|
122 public synchronized List<String> getCheckedGUIDs() { |
|
123 final List<String> guids = new ArrayList<String>(); |
|
124 for (int i = 0; i < checkedItems.length; i++) { |
|
125 if (checkedItems[i]) { |
|
126 guids.add(this.getItem(i).guid); |
|
127 } |
|
128 } |
|
129 return guids; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Get number of checked GUIDs. |
|
134 * |
|
135 * @return non-negative integer. |
|
136 */ |
|
137 public synchronized int getNumCheckedGUIDs() { |
|
138 int numCheckedGUIDs = 0; |
|
139 for (int i = 0; i < checkedItems.length; i++) { |
|
140 if (checkedItems[i]) { |
|
141 numCheckedGUIDs += 1; |
|
142 } |
|
143 } |
|
144 return numCheckedGUIDs; |
|
145 } |
|
146 |
|
147 private int getImage(ClientRecord record) { |
|
148 if ("mobile".equals(record.type)) { |
|
149 return R.drawable.sync_mobile; |
|
150 } |
|
151 return R.drawable.sync_desktop; |
|
152 } |
|
153 |
|
154 private void setSelectable(View view, boolean selectable) { |
|
155 view.setClickable(selectable); |
|
156 view.setFocusable(selectable); |
|
157 } |
|
158 } |