forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PromptService.java
734 lines (651 loc) · 29 KB
/
PromptService.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import org.mozilla.gecko.util.GeckoEventResponder;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.DateTimePicker;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class PromptService implements OnClickListener, OnCancelListener, OnItemClickListener, GeckoEventResponder {
private static final String LOGTAG = "GeckoPromptService";
private static LayoutInflater sInflater;
private static SynchronousQueue<String> sPromptQueue = new SynchronousQueue<String>();
private String[] mButtons;
private PromptInput[] mInputs;
private boolean[] mSelected;
private AlertDialog mDialog;
private final int mGroupPaddingSize;
private final int mLeftRightTextWithIconPadding;
private final int mTopBottomTextWithIconPadding;
private final int mIconTextPadding;
private final int mIconSize;
private final int mInputPaddingSize;
private final int mMinRowSize;
PromptService() {
sInflater = LayoutInflater.from(GeckoApp.mAppContext);
Resources res = GeckoApp.mAppContext.getResources();
mGroupPaddingSize = (int) (res.getDimension(R.dimen.prompt_service_group_padding_size));
mLeftRightTextWithIconPadding = (int) (res.getDimension(R.dimen.prompt_service_left_right_text_with_icon_padding));
mTopBottomTextWithIconPadding = (int) (res.getDimension(R.dimen.prompt_service_top_bottom_text_with_icon_padding));
mIconTextPadding = (int) (res.getDimension(R.dimen.prompt_service_icon_text_padding));
mIconSize = (int) (res.getDimension(R.dimen.prompt_service_icon_size));
mInputPaddingSize = (int) (res.getDimension(R.dimen.prompt_service_inputs_padding));
mMinRowSize = (int) (res.getDimension(R.dimen.prompt_service_min_list_item_height));
GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:Show", this);
}
void destroy() {
GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:Show", this);
}
private static String formatDateString(String dateFormat, Calendar calendar) {
return new SimpleDateFormat(dateFormat).format(calendar.getTime());
}
private class PromptInput {
private final JSONObject mJSONInput;
private final String mLabel;
private final String mType;
private final String mId;
private final String mHint;
private final boolean mAutofocus;
private final String mValue;
private View mView;
public PromptInput(JSONObject aJSONInput) {
mJSONInput = aJSONInput;
mLabel = getSafeString(aJSONInput, "label");
mType = getSafeString(aJSONInput, "type");
String id = getSafeString(aJSONInput, "id");
mId = TextUtils.isEmpty(id) ? mType : id;
mHint = getSafeString(aJSONInput, "hint");
mValue = getSafeString(aJSONInput, "value");
mAutofocus = getSafeBool(aJSONInput, "autofocus");
}
public View getView() throws UnsupportedOperationException {
if (mType.equals("checkbox")) {
CheckBox checkbox = new CheckBox(GeckoApp.mAppContext);
checkbox.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
checkbox.setText(mLabel);
checkbox.setChecked(getSafeBool(mJSONInput, "checked"));
mView = (View)checkbox;
} else if (mType.equals("date")) {
try {
DateTimePicker input = new DateTimePicker(GeckoApp.mAppContext, "yyyy-MM-dd", mValue,
DateTimePicker.PickersState.DATE);
input.toggleCalendar(true);
mView = (View)input;
} catch (UnsupportedOperationException ex) {
// We can't use our custom version of the DatePicker widget because the sdk is too old.
// But we can fallback on the native one.
DatePicker input = new DatePicker(GeckoApp.mAppContext);
try {
if (!TextUtils.isEmpty(mValue)) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mValue));
input.updateDate(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
} catch (Exception e) {
Log.e(LOGTAG, "error parsing format string: " + e);
}
mView = (View)input;
}
} else if (mType.equals("week")) {
DateTimePicker input = new DateTimePicker(GeckoApp.mAppContext, "yyyy-'W'ww", mValue,
DateTimePicker.PickersState.WEEK);
mView = (View)input;
} else if (mType.equals("time")) {
TimePicker input = new TimePicker(GeckoApp.mAppContext);
input.setIs24HourView(DateFormat.is24HourFormat(GeckoApp.mAppContext));
GregorianCalendar calendar = new GregorianCalendar();
if (!TextUtils.isEmpty(mValue)) {
try {
calendar.setTime(new SimpleDateFormat("kk:mm").parse(mValue));
} catch (Exception e) { }
}
input.setCurrentHour(calendar.get(GregorianCalendar.HOUR_OF_DAY));
input.setCurrentMinute(calendar.get(GregorianCalendar.MINUTE));
mView = (View)input;
} else if (mType.equals("datetime-local") || mType.equals("datetime")) {
DateTimePicker input = new DateTimePicker(GeckoApp.mAppContext, "yyyy-MM-dd kk:mm", mValue,
DateTimePicker.PickersState.DATETIME);
input.toggleCalendar(true);
mView = (View)input;
} else if (mType.equals("month")) {
DateTimePicker input = new DateTimePicker(GeckoApp.mAppContext, "yyyy-MM", mValue,
DateTimePicker.PickersState.MONTH);
mView = (View)input;
} else if (mType.equals("textbox") || mType.equals("password")) {
EditText input = new EditText(GeckoApp.mAppContext);
int inputtype = InputType.TYPE_CLASS_TEXT;
if (mType.equals("password")) {
inputtype |= InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
}
input.setInputType(inputtype);
input.setText(mValue);
if (!TextUtils.isEmpty(mHint)) {
input.setHint(mHint);
}
if (mAutofocus) {
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((InputMethodManager) GeckoApp.mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v, 0);
}
}
});
input.requestFocus();
}
mView = (View)input;
} else if (mType.equals("menulist")) {
Spinner spinner = new Spinner(GeckoApp.mAppContext);
try {
String[] listitems = getStringArray(mJSONInput, "values");
if (listitems.length > 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(GeckoApp.mAppContext, android.R.layout.simple_dropdown_item_1line, listitems);
spinner.setAdapter(adapter);
int selectedIndex = getSafeInt(mJSONInput, "selected");
spinner.setSelection(selectedIndex);
}
} catch(Exception ex) { }
mView = (View)spinner;
} else if (mType.equals("label")) {
// not really an input, but a way to add labels and such to the dialog
TextView view = new TextView(GeckoApp.mAppContext);
view.setText(Html.fromHtml(mLabel));
mView = view;
}
return mView;
}
public String getId() {
return mId;
}
public String getValue() {
if (mType.equals("checkbox")) {
CheckBox checkbox = (CheckBox)mView;
return checkbox.isChecked() ? "true" : "false";
} else if (mType.equals("textbox") || mType.equals("password")) {
EditText edit = (EditText)mView;
return edit.getText().toString();
} else if (mType.equals("menulist")) {
Spinner spinner = (Spinner)mView;
return Integer.toString(spinner.getSelectedItemPosition());
} else if (mType.equals("time")) {
TimePicker tp = (TimePicker)mView;
GregorianCalendar calendar =
new GregorianCalendar(0,0,0,tp.getCurrentHour(),tp.getCurrentMinute());
return formatDateString("kk:mm",calendar);
} else if (mType.equals("label")) {
return "";
} else if (android.os.Build.VERSION.SDK_INT < 11 && mType.equals("date")) {
// We can't use the custom DateTimePicker with a sdk older than 11.
// Fallback on the native DatePicker.
DatePicker dp = (DatePicker)mView;
GregorianCalendar calendar =
new GregorianCalendar(dp.getYear(),dp.getMonth(),dp.getDayOfMonth());
return formatDateString("yyyy-MM-dd",calendar);
} else {
DateTimePicker dp = (DateTimePicker)mView;
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(dp.getTimeInMillis());
if (mType.equals("date")) {
return formatDateString("yyyy-MM-dd",calendar);
} else if (mType.equals("week")) {
return formatDateString("yyyy-'W'ww",calendar);
} else if (mType.equals("datetime-local")) {
return formatDateString("yyyy-MM-dd kk:mm",calendar);
} else if (mType.equals("datetime")) {
calendar.set(GregorianCalendar.ZONE_OFFSET,0);
calendar.setTimeInMillis(dp.getTimeInMillis());
return formatDateString("yyyy-MM-dd kk:mm",calendar);
} else if (mType.equals("month")) {
return formatDateString("yyyy-MM",calendar);
}
}
return "";
}
}
// GeckoEventListener implementation
@Override
public void handleMessage(String event, final JSONObject message) {
// The dialog must be created on the UI thread.
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
processMessage(message);
}
});
}
// GeckoEventResponder implementation
@Override
public String getResponse() {
// we only handle one kind of message in handleMessage, and this is the
// response we provide for that message
String promptServiceResult = "";
try {
promptServiceResult = waitForReturn();
} catch (InterruptedException e) { }
return promptServiceResult;
}
private View applyInputStyle(View view) {
view.setPadding(mInputPaddingSize, 0, mInputPaddingSize, 0);
return view;
}
public void show(String aTitle, String aText, PromptListItem[] aMenuList, boolean aMultipleSelection) {
ThreadUtils.assertOnUiThread();
// treat actions that show a dialog as if preventDefault by content to prevent panning
GeckoApp.mAppContext.getLayerView().abortPanning();
AlertDialog.Builder builder = new AlertDialog.Builder(GeckoApp.mAppContext);
if (!TextUtils.isEmpty(aTitle)) {
builder.setTitle(aTitle);
}
if (!TextUtils.isEmpty(aText)) {
builder.setMessage(aText);
}
int length = mInputs == null ? 0 : mInputs.length;
if (aMenuList != null && aMenuList.length > 0) {
int resourceId = android.R.layout.simple_list_item_1;
if (mSelected != null && mSelected.length > 0) {
if (aMultipleSelection) {
resourceId = R.layout.select_dialog_multichoice;
} else {
resourceId = android.R.layout.select_dialog_singlechoice;
}
}
PromptListAdapter adapter = new PromptListAdapter(GeckoApp.mAppContext, resourceId, aMenuList);
if (mSelected != null && mSelected.length > 0) {
if (aMultipleSelection) {
adapter.listView = (ListView) sInflater.inflate(R.layout.select_dialog_list, null);
adapter.listView.setOnItemClickListener(this);
builder.setInverseBackgroundForced(true);
adapter.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter.listView.setAdapter(adapter);
builder.setView(adapter.listView);
} else {
int selectedIndex = -1;
for (int i = 0; i < mSelected.length; i++) {
if (mSelected[i]) {
selectedIndex = i;
break;
}
}
mSelected = null;
builder.setSingleChoiceItems(adapter, selectedIndex, this);
}
} else {
builder.setAdapter(adapter, this);
mSelected = null;
}
} else if (length == 1) {
try {
ScrollView view = new ScrollView(GeckoApp.mAppContext);
view.addView(mInputs[0].getView());
builder.setView(applyInputStyle(view));
} catch(UnsupportedOperationException ex) {
// We cannot display these input widgets with this sdk version,
// do not display any dialog and finish the prompt now.
finishDialog("{\"button\": -1}");
return;
}
} else if (length > 1) {
try {
LinearLayout linearLayout = new LinearLayout(GeckoApp.mAppContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < length; i++) {
View content = mInputs[i].getView();
linearLayout.addView(content);
}
ScrollView view = new ScrollView(GeckoApp.mAppContext);
view.addView(linearLayout);
builder.setView(applyInputStyle(view));
} catch(UnsupportedOperationException ex) {
// We cannot display these input widgets with this sdk version,
// do not display any dialog and finish the prompt now.
finishDialog("{\"button\": -1}");
return;
}
}
length = mButtons == null ? 0 : mButtons.length;
if (length > 0) {
builder.setPositiveButton(mButtons[0], this);
if (length > 1) {
builder.setNeutralButton(mButtons[1], this);
if (length > 2) {
builder.setNegativeButton(mButtons[2], this);
}
}
}
mDialog = builder.create();
mDialog.setOnCancelListener(PromptService.this);
mDialog.show();
}
@Override
public void onClick(DialogInterface aDialog, int aWhich) {
ThreadUtils.assertOnUiThread();
JSONObject ret = new JSONObject();
try {
int button = -1;
ListView list = mDialog.getListView();
if (list != null || mSelected != null) {
button = aWhich;
if (mSelected != null) {
JSONArray selected = new JSONArray();
for (int i = 0; i < mSelected.length; i++) {
selected.put(mSelected[i]);
}
ret.put("button", selected);
} else {
ret.put("button", button);
}
} else {
switch(aWhich) {
case DialogInterface.BUTTON_POSITIVE : button = 0; break;
case DialogInterface.BUTTON_NEUTRAL : button = 1; break;
case DialogInterface.BUTTON_NEGATIVE : button = 2; break;
}
ret.put("button", button);
}
if (mInputs != null) {
for (int i = 0; i < mInputs.length; i++) {
ret.put(mInputs[i].getId(), mInputs[i].getValue());
}
}
} catch(Exception ex) {
Log.i(LOGTAG, "Error building return: " + ex);
}
if (mDialog != null) {
mDialog.dismiss();
}
finishDialog(ret.toString());
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ThreadUtils.assertOnUiThread();
mSelected[position] = !mSelected[position];
}
@Override
public void onCancel(DialogInterface aDialog) {
ThreadUtils.assertOnUiThread();
JSONObject ret = new JSONObject();
try {
ret.put("button", -1);
} catch(Exception ex) { }
finishDialog(ret.toString());
}
public static String waitForReturn() throws InterruptedException {
String value;
while (null == (value = sPromptQueue.poll(1, TimeUnit.MILLISECONDS))) {
GeckoAppShell.processNextNativeEvent();
}
return value;
}
public void finishDialog(String aReturn) {
mInputs = null;
mButtons = null;
mDialog = null;
mSelected = null;
try {
sPromptQueue.put(aReturn);
} catch(Exception ex) {
Log.d(LOGTAG, "sPromptQueue not ready yet");
}
}
private void processMessage(JSONObject geckoObject) {
String title = getSafeString(geckoObject, "title");
String text = getSafeString(geckoObject, "text");
mButtons = getStringArray(geckoObject, "buttons");
JSONArray inputs = getSafeArray(geckoObject, "inputs");
mInputs = new PromptInput[inputs.length()];
for (int i = 0; i < mInputs.length; i++) {
try {
mInputs[i] = new PromptInput(inputs.getJSONObject(i));
} catch(Exception ex) { }
}
PromptListItem[] menuitems = getListItemArray(geckoObject, "listitems");
mSelected = getBooleanArray(geckoObject, "selected");
boolean multiple = getSafeBool(geckoObject, "multiple");
show(title, text, menuitems, multiple);
}
private static String getSafeString(JSONObject json, String key) {
try {
return json.getString(key);
} catch (Exception e) {
return "";
}
}
private static JSONArray getSafeArray(JSONObject json, String key) {
try {
return json.getJSONArray(key);
} catch (Exception e) {
return new JSONArray();
}
}
private static boolean getSafeBool(JSONObject json, String key) {
try {
return json.getBoolean(key);
} catch (Exception e) {
return false;
}
}
private static int getSafeInt(JSONObject json, String key ) {
try {
return json.getInt(key);
} catch (Exception e) {
return 0;
}
}
private String[] getStringArray(JSONObject aObject, String aName) {
JSONArray items = getSafeArray(aObject, aName);
int length = items.length();
String[] list = new String[length];
for (int i = 0; i < length; i++) {
try {
list[i] = items.getString(i);
} catch(Exception ex) { }
}
return list;
}
private boolean[] getBooleanArray(JSONObject aObject, String aName) {
JSONArray items = new JSONArray();
try {
items = aObject.getJSONArray(aName);
} catch(Exception ex) { return null; }
int length = items.length();
boolean[] list = new boolean[length];
for (int i = 0; i < length; i++) {
try {
list[i] = items.getBoolean(i);
} catch(Exception ex) { }
}
return list;
}
private PromptListItem[] getListItemArray(JSONObject aObject, String aName) {
JSONArray items = getSafeArray(aObject, aName);
int length = items.length();
PromptListItem[] list = new PromptListItem[length];
for (int i = 0; i < length; i++) {
try {
list[i] = new PromptListItem(items.getJSONObject(i));
} catch(Exception ex) { }
}
return list;
}
public static class PromptListItem {
public final String label;
public final boolean isGroup;
public final boolean inGroup;
public final boolean disabled;
public final int id;
public final boolean isParent;
// This member can't be accessible from JS, see bug 733749.
public Drawable icon;
PromptListItem(JSONObject aObject) {
label = getSafeString(aObject, "label");
isGroup = getSafeBool(aObject, "isGroup");
inGroup = getSafeBool(aObject, "inGroup");
disabled = getSafeBool(aObject, "disabled");
id = getSafeInt(aObject, "id");
isParent = getSafeBool(aObject, "isParent");
}
public PromptListItem(String aLabel) {
label = aLabel;
isGroup = false;
inGroup = false;
disabled = false;
id = 0;
isParent = false;
}
}
public class PromptListAdapter extends ArrayAdapter<PromptListItem> {
private static final int VIEW_TYPE_ITEM = 0;
private static final int VIEW_TYPE_GROUP = 1;
private static final int VIEW_TYPE_COUNT = 2;
public ListView listView;
private int mResourceId = -1;
private Drawable mBlankDrawable = null;
private Drawable mMoreDrawable = null;
PromptListAdapter(Context context, int textViewResourceId, PromptListItem[] objects) {
super(context, textViewResourceId, objects);
mResourceId = textViewResourceId;
}
@Override
public int getItemViewType(int position) {
PromptListItem item = getItem(position);
return (item.isGroup ? VIEW_TYPE_GROUP : VIEW_TYPE_ITEM);
}
@Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
private Drawable getMoreDrawable(Resources res) {
if (mMoreDrawable == null) {
mMoreDrawable = res.getDrawable(android.R.drawable.ic_menu_more);
}
return mMoreDrawable;
}
private Drawable getBlankDrawable(Resources res) {
if (mBlankDrawable == null) {
mBlankDrawable = res.getDrawable(R.drawable.blank);
}
return mBlankDrawable;
}
private void maybeUpdateIcon(PromptListItem item, TextView t) {
if (item.icon == null && !item.inGroup && !item.isParent) {
t.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
return;
}
Drawable d = null;
Resources res = GeckoApp.mAppContext.getResources();
// Set the padding between the icon and the text.
t.setCompoundDrawablePadding(mIconTextPadding);
if (item.icon != null) {
// We want the icon to be of a specific size. Some do not
// follow this rule so we have to resize them.
Bitmap bitmap = ((BitmapDrawable) item.icon).getBitmap();
d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, mIconSize, mIconSize, true));
} else if (item.inGroup) {
// We don't currently support "indenting" items with icons
d = getBlankDrawable(res);
}
Drawable moreDrawable = null;
if (item.isParent) {
moreDrawable = getMoreDrawable(res);
}
if (d != null || moreDrawable != null) {
t.setCompoundDrawablesWithIntrinsicBounds(d, null, moreDrawable, null);
}
}
private void maybeUpdateCheckedState(int position, PromptListItem item, ViewHolder viewHolder) {
viewHolder.textView.setEnabled(!item.disabled && !item.isGroup);
viewHolder.textView.setClickable(item.isGroup || item.disabled);
if (mSelected == null)
return;
CheckedTextView ct;
try {
ct = (CheckedTextView) viewHolder.textView;
// Apparently just using ct.setChecked(true) doesn't work, so this
// is stolen from the android source code as a way to set the checked
// state of these items
if (listView != null)
listView.setItemChecked(position, mSelected[position]);
} catch (Exception e) {
return;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PromptListItem item = getItem(position);
ViewHolder viewHolder = null;
if (convertView == null) {
int resourceId = mResourceId;
if (item.isGroup) {
resourceId = R.layout.list_item_header;
}
convertView = sInflater.inflate(resourceId, null);
convertView.setMinimumHeight(mMinRowSize);
TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder = new ViewHolder(tv, tv.getPaddingLeft(), tv.getPaddingRight(),
tv.getPaddingTop(), tv.getPaddingBottom());
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(item.label);
maybeUpdateCheckedState(position, item, viewHolder);
maybeUpdateIcon(item, viewHolder.textView);
return convertView;
}
private class ViewHolder {
public final TextView textView;
public final int paddingLeft;
public final int paddingRight;
public final int paddingTop;
public final int paddingBottom;
ViewHolder(TextView aTextView, int aLeft, int aRight, int aTop, int aBottom) {
textView = aTextView;
paddingLeft = aLeft;
paddingRight = aRight;
paddingTop = aTop;
paddingBottom = aBottom;
}
}
}
}