Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
5 package org.mozilla.gecko.sync.setup.activities;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.List;
11 import org.mozilla.gecko.R;
12 import org.mozilla.gecko.sync.repositories.domain.ClientRecord;
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;
23 public class ClientRecordArrayAdapter extends ArrayAdapter<ClientRecord> {
24 public static final String LOG_TAG = "ClientRecArrayAdapter";
26 private boolean[] checkedItems;
27 private SendTabActivity sendTabActivity;
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 }
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 }
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 }
54 if (setRowChecked(position, true)) {
55 this.notifyDataSetChanged();
56 }
57 }
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 }
72 checkedItems[position] = checked;
73 sendTabActivity.enableSend(getNumCheckedGUIDs() > 0);
75 return true;
76 }
78 @Override
79 public View getView(final int position, View convertView, ViewGroup parent) {
80 final Context context = this.getContext();
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 }
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);
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);
99 clientName.setText(clientRecord.name);
100 clientType.setImageResource(getImage(clientRecord));
102 row.setOnClickListener(new OnClickListener() {
103 @Override
104 public void onClick(View view) {
105 final CheckBox item = (CheckBox) view.findViewById(R.id.check);
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 });
114 return row;
115 }
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 }
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 }
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 }
154 private void setSelectable(View view, boolean selectable) {
155 view.setClickable(selectable);
156 view.setFocusable(selectable);
157 }
158 }