-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInputDialog.java
More file actions
652 lines (585 loc) · 23.9 KB
/
InputDialog.java
File metadata and controls
652 lines (585 loc) · 23.9 KB
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
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
/**
* A proxy class of AlertDialog that provides an input box.
* Some specified interfaces and listeners are designed for you
* to simplify you work of controlling input settings or validation
* responses.
* Created by Cookizz
*/
public class InputDialog {
private AlertDialog dialog;
private EditText editText;
private InputDialog(AlertDialog dialog, EditText editText) {
this.dialog = dialog;
this.editText = editText;
}
public void show() {
dialog.show();
}
/**
* Dismiss this dialog, removing it from the screen. This method can be
* invoked safely from any thread.
*/
public void dismiss() {
dialog.dismiss();
}
/**
* Hide the dialog, but not dismiss it.
*/
public void hide() {
dialog.hide();
}
/**
* Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will
* also call your {@link DialogInterface.OnCancelListener} (if registered).
*/
public void cancel() {
dialog.cancel();
}
/**
* Retrieve the current Window for the activity. This can be used to
* directly access parts of the Window API that are not available
* through Activity/Screen.
*
* @return Window The current window, or null if the activity is not
* visual.
*/
public Window getWindow() {
return dialog.getWindow();
}
/**
* Retrieve the Context this Dialog is running in.
*
* @return Context The Context used by the Dialog.
*/
public Context getContext() {
return dialog.getContext();
}
/**
* Sets a listener to be invoked when the dialog is shown.
* @param listener The {@link DialogInterface.OnShowListener} to use.
*/
public void setOnShowListener(DialogInterface.OnShowListener listener) {
dialog.setOnShowListener(listener);
}
/**
* Set resId to 0 if you don't want an icon.
*
* @param resId the resourceId of the drawable to use as the icon or 0
* if you don't want an icon.
*/
public void setIcon(int resId) {
dialog.setIcon(resId);
}
public void setIcon(Drawable res) {
dialog.setIcon(res);
}
/**
* Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
*
* @param attrId ID of a theme attribute that points to a drawable resource.
*/
public void setIconAttributes(int attrId) {
dialog.setIconAttribute(attrId);
}
/**
* Set the title text for this dialog's window.
*
* @param title The new text to display in the title.
*/
public void setTitle(CharSequence title) {
dialog.setTitle(title);
}
/**
* Set the title text for this dialog's window. The text is retrieved
* from the resources with the supplied identifier.
*
* @param titleId the title's text resource identifier
*/
public void setTitle(int titleId) {
dialog.setTitle(titleId);
}
/**
* @see Builder#setCustomTitle(View)
*/
public void setCustomTitle(View customTitleView) {
dialog.setCustomTitle(customTitleView);
}
/**
* Gets one of the buttons used in the dialog. Returns null if the specified
* button does not exist or the dialog has not yet been fully created (for
* example, via {@link #show()}
*
* @param whichButton The identifier of the button that should be returned.
* For example, this can be
* {@link DialogInterface#BUTTON_POSITIVE}.
* @return The button from the dialog, or null if a button does not exist.
*/
public Button getButton(int whichButton) {
return dialog.getButton(whichButton);
}
/**
* @return Whether the dialog is currently showing.
*/
public boolean isShowing() {
return dialog.isShowing();
}
/**
* Finds a child view with the given identifier. Returns null if the
* specified child view does not exist or the dialog has not yet been fully
* created (for example, via {@link #show()}
*
* @param id the identifier of the view to find
* @return The view with the given id or null.
*/
public View findViewById(int id) {
return dialog.findViewById(id);
}
/**
* Retrieve the current text content in the input box of dialog.
* @return The text content.
*/
public CharSequence getInputText() {
Editable editable = editText.getText();
return editable.toString();
}
public static class Builder {
private InputDialog inputDialog; // the proxy dialog
private AlertDialog.Builder builder;
private AlertDialog alertDialog;
private ButtonActionListener positiveButtonActionListener;
private ButtonActionListener negativeButtonActionListener;
private ButtonActionListener neutralButtonActionListener;
private OnCancelListener onCancelListener;
private OnDismissListener onDismissListener;
private ButtonActionIntercepter buttonActionIntercepter;
private int customizedLayoutResId;
private int editTextId;
private CharSequence inputDefaultText;
private CharSequence inputHint;
private int inputMaxWords = -1;
private boolean interceptAutoPopupInputMethod;
private ButtonHandler mHandler;
public Builder(Context context) {
builder = new AlertDialog.Builder(context);
}
public Builder(Context context, int style) {
builder = new AlertDialog.Builder(context, style);
}
public Builder setTitle(CharSequence title) {
builder.setTitle(title);
return this;
}
public Builder setInputDefaultText(CharSequence text) {
inputDefaultText = text;
return this;
}
public Builder setInputHint(CharSequence text) {
inputHint = text;
return this;
}
/**
* Set max words limitation of the input box.
*/
public Builder setInputMaxWords(int maxWords) {
this.inputMaxWords = maxWords;
return this;
}
/**
* Set a listener to be invoked when the positive button of the dialog is pressed.
*
* @param textId The resource id of the text to display in the positive button
* @param listener The {@link InputDialog.ButtonActionListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setPositiveButton(final int textId, final ButtonActionListener listener) {
positiveButtonActionListener = listener;
builder.setPositiveButton(textId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (positiveButtonActionListener != null) {
positiveButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Set a listener to be invoked when the positive button of the dialog is pressed.
*
* @param text The text to display in the positive button
* @param listener The {@link InputDialog.ButtonActionListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setPositiveButton(final CharSequence text, final ButtonActionListener listener) {
positiveButtonActionListener = listener;
builder.setPositiveButton(text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (positiveButtonActionListener != null) {
positiveButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Set a listener to be invoked when the negative button of the dialog is pressed.
*
* @param textId The resource id of the text to display in the negative button
* @param listener The {@link InputDialog.ButtonActionListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNegativeButton(final int textId, final ButtonActionListener listener) {
negativeButtonActionListener = listener;
builder.setNegativeButton(textId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (negativeButtonActionListener != null) {
negativeButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Set a listener to be invoked when the negative button of the dialog is pressed.
*
* @param text The text to display in the negative button
* @param listener The {@link InputDialog.ButtonActionListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNegativeButton(final CharSequence text, final ButtonActionListener listener) {
negativeButtonActionListener = listener;
builder.setNegativeButton(text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (negativeButtonActionListener != null) {
negativeButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Set a listener to be invoked when the neutral button of the dialog is pressed.
*
* @param textId The resource id of the text to display in the neutral button
* @param listener The {@link DialogInterface.OnClickListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNeutralButton(final int textId, final ButtonActionListener listener) {
neutralButtonActionListener = listener;
builder.setNeutralButton(textId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (neutralButtonActionListener != null) {
neutralButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Set a listener to be invoked when the neutral button of the dialog is pressed.
*
* @param text The text to display in the neutral button
* @param listener The {@link DialogInterface.OnClickListener} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setNeutralButton(final CharSequence text, final ButtonActionListener listener) {
neutralButtonActionListener = listener;
builder.setNeutralButton(text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean intercept = onInterceptButtonAction(which, inputDialog.getInputText());
if (!intercept) {
if (neutralButtonActionListener != null) {
neutralButtonActionListener.onClick(inputDialog.getInputText());
}
}
}
});
return this;
}
/**
* Sets whether the dialog is cancelable or not. Default is true.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setCancelable(boolean cancelable) {
alertDialog.setCancelable(cancelable);
return this;
}
/**
* Sets the callback that will be called if the dialog is canceled.
*
* <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
* being canceled or one of the supplied choices being selected.
* If you are interested in listening for all cases where the dialog is dismissed
* and not just when it is canceled, see
* {@link #setOnDismissListener(InputDialog.OnDismissListener)
* setOnDismissListener}.</p>
*
* @return This Builder object to allow for chaining of calls to set methods
* @see #setCancelable(boolean)
* @see #setOnDismissListener(InputDialog.OnDismissListener)
*/
public Builder setOnCancelListener(OnCancelListener listener) {
onCancelListener = listener;
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface d) {
if (onCancelListener != null) {
onCancelListener.onCancel(inputDialog.getInputText());
}
}
});
return this;
}
/**
* Sets the callback that will be called when the dialog is dismissed for any reason.
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setOnDismissListener(OnDismissListener listener) {
onDismissListener = listener;
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface d) {
hideSoftInput(alertDialog);
if (onDismissListener != null) {
onDismissListener.onDismiss(inputDialog.getInputText());
}
}
});
return this;
}
/**
* Used to intercept the inherent behaviors of AlertDialog
* after a button is clicked.
* The behaviors include dismiss-after-button-clicked and
* the #onClick(CharSequence) callback.
*
* @param intercepter The {@link InputDialog.ButtonActionIntercepter} to use.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder interceptButtonAction(ButtonActionIntercepter intercepter) {
buttonActionIntercepter = intercepter;
return this;
}
/**
* Used to disable the automatically-popup-soft-input-keyboard behavior
* when the dialog shows.
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder disableAutoPopupSoftInput() {
interceptAutoPopupInputMethod = true;
return this;
}
/**
* Set a custom view resource to be the contents of the Dialog. The
* resource will be inflated, adding all top-level views to the screen.
*
* @param layoutResId Resource ID to be inflated.
* @return This Builder object to allow for chaining of calls to set
* methods
*/
public Builder setView(int layoutResId, int editTextId) {
customizedLayoutResId = layoutResId;
this.editTextId = editTextId;
return this;
}
/**
* Creates a {@link InputDialog} with the arguments supplied to this builder and
* {@link Dialog#show()}'s the dialog.
*/
public InputDialog show() {
initLayout();
alertDialog = builder.show();
initEditText(alertDialog);
popupSoftInput(alertDialog);
replaceHandler(alertDialog);
return createInputDialog(alertDialog);
}
/**
* Creates a {@link InputDialog} with the arguments supplied to this builder. It does not
* {@link Dialog#show()} the dialog. This allows the user to do any extra processing
* before displaying the dialog. Use {@link #show()} if you don't have any other processing
* to do and want this to be created and displayed.
*/
public InputDialog create() {
initLayout();
alertDialog = builder.create();
initEditText(alertDialog);
popupSoftInput(alertDialog);
replaceHandler(alertDialog);
return createInputDialog(alertDialog);
}
private boolean onInterceptButtonAction(int buttonType, CharSequence text) {
if (buttonActionIntercepter != null) {
boolean intercept = buttonActionIntercepter.onInterceptButtonAction(buttonType, text);
if (intercept) {
mHandler.interceptButtonAction();
return true;
}
}
return false;
}
private void initLayout() {
if (customizedLayoutResId == 0) {
builder.setView(R.layout.dialog_input_layout);
} else {
builder.setView(customizedLayoutResId);
}
}
private void initEditText(AlertDialog dialog) {
EditText input = obtainEditText(dialog);
if (inputMaxWords >= 0) {
input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(inputMaxWords)});
}
if (!TextUtils.isEmpty(inputDefaultText)) {
input.setText(inputDefaultText);
// Move text cursor to the end if there are default texts.
int len = inputMaxWords >= 0 ? Math.min(inputMaxWords, inputDefaultText.length()) : inputDefaultText.length();
input.setSelection(len);
}
if (!TextUtils.isEmpty(inputHint)) {
input.setHint(inputHint);
}
}
private void popupSoftInput(final AlertDialog dialog) {
if (interceptAutoPopupInputMethod) {
return;
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
EditText input = obtainEditText(dialog);
InputMethodManager imm = (InputMethodManager) dialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input, 0);
}
}, 100);
}
private void hideSoftInput(final AlertDialog dialog) {
if (interceptAutoPopupInputMethod) {
return;
}
InputMethodManager im = (InputMethodManager) dialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
EditText input = obtainEditText(dialog);
im.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
/**
* Replace {@link AlertDialog}'s inner handler by {@link ButtonHandler} via java reflection.
* Used to intercept the inherent behavior when a button is clicked.
*/
private void replaceHandler(AlertDialog dialog) {
mHandler = new ButtonHandler(dialog);
try {
Field fController = dialog.getClass().getDeclaredField("mAlert");
fController.setAccessible(true);
Object controller = fController.get(dialog);
Field fHandler = controller.getClass().getDeclaredField("mHandler");
fHandler.setAccessible(true);
fHandler.set(controller, mHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
private InputDialog createInputDialog(AlertDialog dialog) {
inputDialog = new InputDialog(dialog, obtainEditText(dialog));
return inputDialog;
}
private EditText obtainEditText(AlertDialog dialog) {
if (editTextId == 0) {
return (EditText) dialog.findViewById(R.id.input);
} else {
return (EditText) dialog.findViewById(editTextId);
}
}
/**
* ButtonHandler substitute for intercepting
*/
private static final class ButtonHandler extends Handler {
// Button clicks have Message.what as the BUTTON{1,2,3} constant
private static final int MSG_DISMISS_DIALOG = 1;
private WeakReference<DialogInterface> mDialog;
private boolean shouldInterceptButtonAction;
public ButtonHandler(DialogInterface dialog) {
mDialog = new WeakReference<>(dialog);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
break;
case MSG_DISMISS_DIALOG:
if (shouldInterceptButtonAction) {
shouldInterceptButtonAction = false;
break;
}
((DialogInterface) msg.obj).dismiss();
break;
}
}
public void interceptButtonAction() {
shouldInterceptButtonAction = true;
}
}
}
public interface ButtonActionListener {
void onClick(CharSequence inputText);
}
public interface OnCancelListener {
void onCancel(CharSequence inputText);
}
public interface OnDismissListener {
void onDismiss(CharSequence inputText);
}
/**
* Inherent button behavior intercepter.
*/
public interface ButtonActionIntercepter {
/**
* @param whichButton The type of button, including DialogInterface.BUTTON_POSITIVE,
* DialogInterface.BUTTON_NEGATIVE, DialogInterface.BUTTON_NEUTRAL
* @return true if you need to intercept the inherent behavior, vise versa.
*/
boolean onInterceptButtonAction(int whichButton, CharSequence inputText);
}
}