1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import java.util.ArrayList;
public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener {
private LayoutInflater mInflater;
private final ArrayList<Boolean> mCheckStates;
private SaveKeyringParcel mSaveKeyringParcel;
public static final String[] USER_IDS_PROJECTION = new String[]{
UserIds._ID,
UserIds.USER_ID,
UserIds.RANK,
UserIds.VERIFIED,
UserIds.IS_PRIMARY,
UserIds.IS_REVOKED
};
private static final int INDEX_ID = 0;
private static final int INDEX_USER_ID = 1;
private static final int INDEX_RANK = 2;
private static final int INDEX_VERIFIED = 3;
private static final int INDEX_IS_PRIMARY = 4;
private static final int INDEX_IS_REVOKED = 5;
public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes,
SaveKeyringParcel saveKeyringParcel) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
mCheckStates = showCheckBoxes ? new ArrayList<Boolean>() : null;
mSaveKeyringParcel = saveKeyringParcel;
}
public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes) {
this(context, c, flags, showCheckBoxes, null);
}
public UserIdsAdapter(Context context, Cursor c, int flags, SaveKeyringParcel saveKeyringParcel) {
this(context, c, flags, false, saveKeyringParcel);
}
public UserIdsAdapter(Context context, Cursor c, int flags) {
this(context, c, flags, false, null);
}
@Override
public Cursor swapCursor(Cursor newCursor) {
if (mCheckStates != null) {
mCheckStates.clear();
if (newCursor != null) {
int count = newCursor.getCount();
mCheckStates.ensureCapacity(count);
// initialize to true (use case knowledge: we usually want to sign all uids)
for (int i = 0; i < count; i++) {
newCursor.moveToPosition(i);
int verified = newCursor.getInt(INDEX_VERIFIED);
mCheckStates.add(verified != Certs.VERIFIED_SECRET);
}
}
}
return super.swapCursor(newCursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView vName = (TextView) view.findViewById(R.id.userId);
TextView vAddress = (TextView) view.findViewById(R.id.address);
TextView vComment = (TextView) view.findViewById(R.id.comment);
ImageView vVerified = (ImageView) view.findViewById(R.id.certified);
ImageView vHasChanges = (ImageView) view.findViewById(R.id.has_changes);
ImageView vEditImage = (ImageView) view.findViewById(R.id.edit_image);
String userId = cursor.getString(INDEX_USER_ID);
String[] splitUserId = KeyRing.splitUserId(userId);
if (splitUserId[0] != null) {
vName.setText(splitUserId[0]);
} else {
vName.setText(R.string.user_id_no_name);
}
if (splitUserId[1] != null) {
vAddress.setText(splitUserId[1]);
vAddress.setVisibility(View.VISIBLE);
} else {
vAddress.setVisibility(View.GONE);
}
if (splitUserId[2] != null) {
vComment.setText(splitUserId[2]);
vComment.setVisibility(View.VISIBLE);
} else {
vComment.setVisibility(View.GONE);
}
boolean isPrimary = cursor.getInt(INDEX_IS_PRIMARY) != 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
// for edit key
if (mSaveKeyringParcel != null) {
boolean changeUserId = (mSaveKeyringParcel.changePrimaryUserId != null
&& mSaveKeyringParcel.changePrimaryUserId.equals(userId));
boolean revoke = (mSaveKeyringParcel.revokeUserIds.contains(userId));
if (changeUserId) {
isPrimary = !isPrimary;
}
if (revoke) {
if (!isRevoked) {
isRevoked = true;
}
}
if (changeUserId || revoke) {
vHasChanges.setVisibility(View.VISIBLE);
} else {
vHasChanges.setVisibility(View.GONE);
}
vEditImage.setVisibility(View.VISIBLE);
} else {
vHasChanges.setVisibility(View.GONE);
vEditImage.setVisibility(View.GONE);
}
if (isRevoked) {
// set revocation icon (can this even be primary?)
vVerified.setImageResource(R.drawable.key_certify_revoke);
// disable and strike through text for revoked user ids
vName.setEnabled(false);
vAddress.setEnabled(false);
vName.setText(OtherHelper.strikeOutText(vName.getText()));
vAddress.setText(OtherHelper.strikeOutText(vAddress.getText()));
} else {
vName.setEnabled(true);
vAddress.setEnabled(true);
// verified: has been verified
// isPrimary: show small star icon for primary user ids
int verified = cursor.getInt(INDEX_VERIFIED);
switch (verified) {
case Certs.VERIFIED_SECRET:
vVerified.setImageResource(isPrimary
? R.drawable.key_certify_primary_ok_depth0
: R.drawable.key_certify_ok_depth0);
break;
case Certs.VERIFIED_SELF:
vVerified.setImageResource(isPrimary
? R.drawable.key_certify_primary_ok_self
: R.drawable.key_certify_ok_self);
break;
default:
vVerified.setImageResource(R.drawable.key_certify_error);
break;
}
}
// don't care further if checkboxes aren't shown
if (mCheckStates == null) {
return;
}
final CheckBox vCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
final int position = cursor.getPosition();
vCheckBox.setOnCheckedChangeListener(null);
vCheckBox.setChecked(mCheckStates.get(position));
vCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mCheckStates.set(position, b);
}
});
vCheckBox.setClickable(false);
}
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
CheckBox box = ((CheckBox) view.findViewById(R.id.checkBox));
if (box != null) {
box.toggle();
}
}
public ArrayList<String> getSelectedUserIds() {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < mCheckStates.size(); i++) {
if (mCheckStates.get(i)) {
mCursor.moveToPosition(i);
result.add(mCursor.getString(INDEX_USER_ID));
}
}
return result;
}
public String getUserId(int position) {
mCursor.moveToPosition(position);
return mCursor.getString(INDEX_USER_ID);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.view_key_userids_item, null);
// only need to do this once ever, since mShowCheckBoxes is final
view.findViewById(R.id.checkBox).setVisibility(mCheckStates != null ? View.VISIBLE : View.GONE);
return view;
}
// Disable selection of items for lists without checkboxes, http://stackoverflow.com/a/4075045
@Override
public boolean areAllItemsEnabled() {
if (mCheckStates == null && mSaveKeyringParcel == null) {
return false;
} else {
return super.areAllItemsEnabled();
}
}
// Disable selection of items for lists without checkboxes, http://stackoverflow.com/a/4075045
@Override
public boolean isEnabled(int position) {
if (mCheckStates == null && mSaveKeyringParcel == null) {
return false;
} else {
return super.isEnabled(position);
}
}
}
|