1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/setup/activities/ClientRecordArrayAdapter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,158 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.setup.activities; 1.9 + 1.10 +import java.util.ArrayList; 1.11 +import java.util.Collection; 1.12 +import java.util.List; 1.13 + 1.14 +import org.mozilla.gecko.R; 1.15 +import org.mozilla.gecko.sync.repositories.domain.ClientRecord; 1.16 + 1.17 +import android.content.Context; 1.18 +import android.view.View; 1.19 +import android.view.View.OnClickListener; 1.20 +import android.view.ViewGroup; 1.21 +import android.widget.ArrayAdapter; 1.22 +import android.widget.CheckBox; 1.23 +import android.widget.ImageView; 1.24 +import android.widget.TextView; 1.25 + 1.26 +public class ClientRecordArrayAdapter extends ArrayAdapter<ClientRecord> { 1.27 + public static final String LOG_TAG = "ClientRecArrayAdapter"; 1.28 + 1.29 + private boolean[] checkedItems; 1.30 + private SendTabActivity sendTabActivity; 1.31 + 1.32 + public ClientRecordArrayAdapter(Context context, 1.33 + int textViewResourceId) { 1.34 + super(context, textViewResourceId, new ArrayList<ClientRecord>()); 1.35 + this.checkedItems = new boolean[0]; 1.36 + this.sendTabActivity = (SendTabActivity) context; 1.37 + } 1.38 + 1.39 + public synchronized void setClientRecordList(final Collection<ClientRecord> clientRecordList) { 1.40 + this.clear(); 1.41 + this.checkedItems = new boolean[clientRecordList.size()]; 1.42 + for (ClientRecord clientRecord : clientRecordList) { 1.43 + this.add(clientRecord); 1.44 + } 1.45 + this.notifyDataSetChanged(); 1.46 + } 1.47 + 1.48 + /** 1.49 + * If we have only a single client record in the list, mark it as checked. 1.50 + */ 1.51 + public synchronized void checkItem(final int position, boolean checked) throws ArrayIndexOutOfBoundsException { 1.52 + if (position < 0 || 1.53 + position >= checkedItems.length) { 1.54 + throw new ArrayIndexOutOfBoundsException(position); 1.55 + } 1.56 + 1.57 + if (setRowChecked(position, true)) { 1.58 + this.notifyDataSetChanged(); 1.59 + } 1.60 + } 1.61 + 1.62 + /** 1.63 + * Set the specified row to the specified checked state. 1.64 + * @param position an index. 1.65 + * @param checked whether the checkbox should be checked. 1.66 + * @return <code>true</code> if the state changed, <code>false</code> if the 1.67 + * box was already in the requested state. 1.68 + */ 1.69 + protected synchronized boolean setRowChecked(int position, boolean checked) { 1.70 + boolean current = checkedItems[position]; 1.71 + if (current == checked) { 1.72 + return false; 1.73 + } 1.74 + 1.75 + checkedItems[position] = checked; 1.76 + sendTabActivity.enableSend(getNumCheckedGUIDs() > 0); 1.77 + 1.78 + return true; 1.79 + } 1.80 + 1.81 + @Override 1.82 + public View getView(final int position, View convertView, ViewGroup parent) { 1.83 + final Context context = this.getContext(); 1.84 + 1.85 + // Reuse View objects if they exist. 1.86 + View row = convertView; 1.87 + if (row == null) { 1.88 + row = View.inflate(context, R.layout.sync_list_item, null); 1.89 + setSelectable(row, true); 1.90 + row.setBackgroundResource(android.R.drawable.menuitem_background); 1.91 + } 1.92 + 1.93 + final ClientRecord clientRecord = this.getItem(position); 1.94 + ImageView clientType = (ImageView) row.findViewById(R.id.img); 1.95 + TextView clientName = (TextView) row.findViewById(R.id.client_name); 1.96 + 1.97 + // Set up checkbox and restore stored state. 1.98 + CheckBox checkbox = (CheckBox) row.findViewById(R.id.check); 1.99 + checkbox.setChecked(checkedItems[position]); 1.100 + setSelectable(checkbox, false); 1.101 + 1.102 + clientName.setText(clientRecord.name); 1.103 + clientType.setImageResource(getImage(clientRecord)); 1.104 + 1.105 + row.setOnClickListener(new OnClickListener() { 1.106 + @Override 1.107 + public void onClick(View view) { 1.108 + final CheckBox item = (CheckBox) view.findViewById(R.id.check); 1.109 + 1.110 + // Update the checked item, both in the UI and in our internal state. 1.111 + final boolean checked = !item.isChecked(); // Because it hasn't happened yet. 1.112 + item.setChecked(checked); 1.113 + setRowChecked(position, checked); 1.114 + } 1.115 + }); 1.116 + 1.117 + return row; 1.118 + } 1.119 + 1.120 + /** 1.121 + * Get list of checked GUIDs. 1.122 + * 1.123 + * @return non-null list. 1.124 + */ 1.125 + public synchronized List<String> getCheckedGUIDs() { 1.126 + final List<String> guids = new ArrayList<String>(); 1.127 + for (int i = 0; i < checkedItems.length; i++) { 1.128 + if (checkedItems[i]) { 1.129 + guids.add(this.getItem(i).guid); 1.130 + } 1.131 + } 1.132 + return guids; 1.133 + } 1.134 + 1.135 + /** 1.136 + * Get number of checked GUIDs. 1.137 + * 1.138 + * @return non-negative integer. 1.139 + */ 1.140 + public synchronized int getNumCheckedGUIDs() { 1.141 + int numCheckedGUIDs = 0; 1.142 + for (int i = 0; i < checkedItems.length; i++) { 1.143 + if (checkedItems[i]) { 1.144 + numCheckedGUIDs += 1; 1.145 + } 1.146 + } 1.147 + return numCheckedGUIDs; 1.148 + } 1.149 + 1.150 + private int getImage(ClientRecord record) { 1.151 + if ("mobile".equals(record.type)) { 1.152 + return R.drawable.sync_mobile; 1.153 + } 1.154 + return R.drawable.sync_desktop; 1.155 + } 1.156 + 1.157 + private void setSelectable(View view, boolean selectable) { 1.158 + view.setClickable(selectable); 1.159 + view.setFocusable(selectable); 1.160 + } 1.161 +}