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