-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMvMessages.java
986 lines (879 loc) · 30.9 KB
/
MvMessages.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*
* AndroidWithoutStupid Java Library
* Created by V. Subhash
* http://www.VSubhash.com
* Released as Public Domain Software in 2014
*/
package com.vsubhash.droid.androidwithoutstupid;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* This class provides methods for easily displaying toast and notification
* messages. To use the methods, create an instance of this class in your
* activity and call the methods of the instance.
*
*
<pre>MvMessages oMessages = new MvMessages(this);
oMessages.showMessage("Hello, world!");
oMessages.showNotification("New Message",
"5 unread e-mail messages ",
"Check mail");
</pre>
*
*
*
*
* @author V. Subhash (<a href="http://www.VSubhash.com/">www.VSubhash.com</a>)
* @version 2017.09.01
*
*/
public class MvMessages {
public static String ERR_TAG = "ANDROIDWITHOUTSTUPID.LOGD";
public static int iMVM_NOTIFICATION_ID = 232343;
Activity mCallingActivity = null;
Context mCallingContext = null;
@SuppressWarnings("rawtypes")
Class mNotificationClass = null;
/**
* Notification manager for this activity.
*/
public NotificationManager mNotificationManager = null;
/**
* Resource ID of the notification ID
*/
public int mNotificationIcon = 0;
/**
* Notification ID
*/
public int mNotificationID = iMVM_NOTIFICATION_ID;
/**
* Constructs an instance of this class for use with specified activity.
*
* @param aActivity
* with which this instance needs to work with
*/
public MvMessages(Activity aActivity) {
if (aActivity != null) {
mCallingActivity = aActivity;
mCallingContext = mCallingActivity.getApplicationContext();
mNotificationClass = mCallingActivity.getClass();
mNotificationManager = (NotificationManager) mCallingContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
public MvMessages(Context aoCallingContext) {
if (aoCallingContext != null) {
mCallingContext = aoCallingContext;
mNotificationManager = (NotificationManager) mCallingContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
public MvMessages(Context aoCallingContext, @SuppressWarnings("rawtypes") Class aoNotificationClass) {
if (aoCallingContext != null) {
mCallingContext = aoCallingContext;
mNotificationClass = aoNotificationClass;
mNotificationManager = (NotificationManager) mCallingContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
/**
* Constructs an instance of this class for use with specified activity and
* notification icon.
*
* @param aActivity
* activity with which this instance needs to work with
* @param aNotificationIcon
* {@link Drawable} icon resource that needs to be used with the
* notifications.
*/
public MvMessages(Activity aActivity, int aNotificationIcon) {
this(aActivity);
mNotificationIcon = aNotificationIcon;
}
/**
* Write specified message to log. This is a static method.
*
* @param asMessage the message
*/
public static void logMessage(String asMessage) {
Log.d(ERR_TAG, "\n" + asMessage);
}
/**
* Display a toast message. This is an instance method.
* @param asMessage the message
*/
public void showMessage(String asMessage) {
if (mCallingContext != null) {
Toast.makeText(mCallingContext, asMessage, Toast.LENGTH_LONG).show();
}
}
/**
* Displays specified message from specified activity. [This is is a static
* method.]
*
* @param aoCallingActivity
* activity from which the message should pop up
* @param asMessage
* text that needs to displayed as the message
*/
public static void showMessage(Activity aoCallingActivity, String asMessage) {
Context aContext;
aContext = aoCallingActivity.getApplicationContext();
if (aContext != null) {
Toast.makeText(aContext, asMessage, Toast.LENGTH_LONG).show();
}
}
/**
* Display a notification. When the end-user selects the notification, the
* activity that launched the notification will be launched.
*
* @param asTicker
* text that flashes by when the notification first appears by
* @param asTitle
* text appearing as the title of the notification
* @param asMessage
* the message
* @param aiIconID
* resource ID of the notification icon
* @return created notification instance
*/
public Notification showNotification(String asTicker, String asTitle, String asMessage, int aiIconID) {
Intent oNotificationIntent;
if (mNotificationClass == null) {
return(null);
} else {
oNotificationIntent =
new Intent(mCallingContext,
mNotificationClass);
return(showNotification(asTicker, asTitle, asMessage, aiIconID, oNotificationIntent));
}
}
/**
* Shows a notification with specified settings.
*
* @param asTicker
* ticker text used by the notification
* @param asTitle
* title of the notification
* @param asMessage
* message of the notification
* @param aiIconID
* icon used to display the notification
* @param aoIntentToLaunch
* Intent that needs to be launched when the notification is selected
* @return notification instance that is created
*/
public Notification showNotification(String asTicker, String asTitle, String asMessage, int aiIconID, Intent aoIntentToLaunch) {
return(this.showNotification(asTicker, asTitle, asMessage, aiIconID, iMVM_NOTIFICATION_ID, aoIntentToLaunch));
}
/* public void setLargeIcon(Notification aoNotification, int aiDrawable) {
if (Build.VERSION.SDK_INT < 11) { return; }
Bitmap oBmp = BitmapFactory.decodeResource(mCallingContext.getResources(), aiDrawable);
if (oBmp != null) {
Field oField = MvReflection.getField("largeIcon", aoNotification);
try {
oField.set(aoNotification, oBmp);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
/**
* Display a notification and sets the activity that needs to be launched when
* the user selects the notification.
*
* @param asTicker
* text that flashes by when the notification first appears by
* @param asTitle
* text appearing as the title of the notification
* @param asMessage
* the message
* @param aiIconID
* resource ID of the notification icon
* @param aiNotificationID
* request code of the notification
* @param aoIntentToLaunch
* Intent that needs to be launched when the notification is selected
* @return created notification instance
*/
public Notification showNotification(
String asTicker, String asTitle, String asMessage,
int aiIconID, int aiNotificationID, Intent aoIntentToLaunch) {
return(this.showNotification(asTicker, asTitle, asMessage, aiIconID, aiNotificationID, aoIntentToLaunch, false));
}
/**
* Display a notification and sets the activity that needs to be launched when
* the user selects the notification.
*
* @param asTicker
* text that flashes by when the notification first appears by
* @param asTitle
* text appearing as the title of the notification
* @param asMessage
* the message
* @param aiIconID
* resource ID of the notification icon
* @param aiNotificationID
* request code of the notification
* @param aoIntentToLaunch
* Intent that needs to be launched when the notification is selected
* @param bLaunchIntentBroadcast
* Whether the notification should launch an activity or broadcast
* Intent
* @return created notification instance
*/
public Notification showNotification(
String asTicker, String asTitle, String asMessage,
int aiIconID, int aiNotificationID, Intent aoIntentToLaunch,
boolean bLaunchIntentBroadcast) {
mNotificationIcon = aiIconID;
mNotificationID = aiNotificationID;
PendingIntent oNotificationPendingIntent;
if (bLaunchIntentBroadcast) {
oNotificationPendingIntent =
PendingIntent.getBroadcast(
mCallingContext,
mNotificationID,
aoIntentToLaunch,
0);
//MvMessages.logMessage("Set broadcast");
} else {
aoIntentToLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
oNotificationPendingIntent =
PendingIntent.getActivity(
mCallingContext,
mNotificationID,
aoIntentToLaunch,
PendingIntent.FLAG_UPDATE_CURRENT);
//MvMessages.logMessage("Set activity launch");
}
Notification oNotification =
new Notification(
aiIconID,
asTicker,
System.currentTimeMillis());
oNotification.setLatestEventInfo(
mCallingContext,
asTitle,
asMessage,
oNotificationPendingIntent);
mNotificationManager.notify(aiNotificationID, oNotification);
return(oNotification);
}
/**
* Sets an intent that needs to be launched when the user interacts with a
* notification.
*
* @param aoNotification
* notification that needs to be modified
* @param aoNotificationIntent
* Intent that needs to be launched
*/
public void setNotificationContentLaunchIntent(Notification aoNotification, Intent aoNotificationIntent) {
aoNotification.contentIntent =
PendingIntent.getActivity(
mCallingContext,
0,
aoNotificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager.notify(mNotificationID, aoNotification);
}
/**
*
* Updates a notification with specified ticker text.
*
* @param aoNotification
* notification that needs to be updated
* @param asTickerText
* text that needs to be dsiplayed
*/
public void updateNotification(Notification aoNotification, String asTickerText) {
aoNotification.tickerText = asTickerText;
aoNotification.when = System.currentTimeMillis()+300;
mNotificationManager.notify(mNotificationID, aoNotification);
}
/**
* Sets specified text for title and content for specified notification.
*
* @param aoNotification Notification that needs to be updated.
* @param asTitleText New title text.
* @param asContentText New content text.
*/
public void updateNotification(
Notification aoNotification,
String asTitleText,
String asContentText) {
aoNotification.setLatestEventInfo(
mCallingContext,
asTitleText,
asContentText,
aoNotification.contentIntent);
aoNotification.when = System.currentTimeMillis();
mNotificationManager.notify(mNotificationID, aoNotification);
}
/**
* Sets specified text for ticker, title, and content text of specified
* notification.
*
* @param aoNotification
* Notification that needs to be updated.
* @param asTickerText
* New ticker text.
* @param asTitleText
* New title text.
* @param asContentText
* New content text.
*/
public void updateNotification(
Notification aoNotification,
String asTickerText,
String asTitleText,
String asContentText) {
aoNotification.tickerText = asTickerText;
aoNotification.setLatestEventInfo(
mCallingContext,
asTitleText,
asContentText,
aoNotification.contentIntent);
aoNotification.when = System.currentTimeMillis();
mNotificationManager.notify(mNotificationID, aoNotification);
}
public void clearNotification(int aiNotificationID) {
mNotificationManager.cancel(aiNotificationID);
}
/**
* Returns a menu dialog containing options from specified array, and if
* specified automatically displays the menu dialog.
*
* @param asTitle
* title of the dialog
* @param asaOptions
* options to be listed in the menu
* @param aoListener
* {@link DialogInterface.OnClickListener} listener that handles user
* interactions with the dialog
* @param abAutoShow
* whether the menu needs to be automatically displayed after it is
* created
* @return the new menu
*/
public AlertDialog.Builder showOptionsMenu(
String asTitle, String[] asaOptions,
DialogInterface.OnClickListener aoListener, boolean abAutoShow) {
AlertDialog.Builder alert;
alert = new AlertDialog.Builder(mCallingActivity);
alert.setInverseBackgroundForced(true);
alert.setTitle(asTitle);
if (asaOptions !=null) {
if (asaOptions.length > 0) {
alert.setItems(asaOptions, aoListener);
if (abAutoShow) {
alert.show();
}
}
}
return(alert);
}
/**
* Returns a menu dialog containing options from specified list, and if
* specified automatically displays the menu dialog.
*
* @param asTitle
* title of the dialog
* @param aoOptionsList
* options to be listed in the menu
* @param aoListener
* {@link DialogInterface.OnClickListener} listener that handles user
* interactions with the dialog
* @param abAutoShow
* whether the menu needs to be automatically displayed after it is
* created
* @return the new menu
*/
public AlertDialog.Builder showOptionsMenu(String asTitle, List<String> aoOptionsList,
DialogInterface.OnClickListener aoListener, boolean abAutoShow) {
String[] arrsOptions = new String[] {};
if (aoOptionsList != null) {
if (aoOptionsList.size() > 0) {
arrsOptions = aoOptionsList.toArray(new String[aoOptionsList.size()]);
}
}
return(showOptionsMenu(asTitle, arrsOptions, aoListener, abAutoShow));
}
/**
* Displays a menu dialog containing options from specified array.
*
* @param asTitle
* title of the menu
* @param asaOptions
* array containing the text content of the options
* @param aoListener
* "onclick" listener for the menu options
*/
public void showOptionsMenu(String asTitle, String[] asaOptions,
DialogInterface.OnClickListener aoListener) {
showOptionsMenu(asTitle, asaOptions, aoListener, true);
}
/**
* Displays a menu dialog containing options from specified list.
*
* @param asTitle
* title of the menu
* @param aoOptionsList
* list containing the text content of the options
* @param aoListener
* "onclick" listener for the menu options
*/
public void showOptionsMenu(String asTitle, List<String> aoOptionsList,
DialogInterface.OnClickListener aoListener) {
showOptionsMenu(asTitle, aoOptionsList, aoListener, true);
}
/**
* This class represents a dialog with an automcomplete text entry box and
* drop-down menu.
*
*/
public class MvPromptResult {
/**
* Represents the dialog box.
*/
public AlertDialog moPrompt;
/**
* Represents the autocomplete text entry box.
*/
public AutoCompleteTextView moAnswer;
/**
* Represents the drop-down menu of options.
*/
public Spinner moAnswerOptions;
public MvPromptResult() {
super();
}
/**
* Displays the menu dialog and waits.
*/
public void show() {
this.moPrompt.show();
}
}
/**
* Displays a dialog prompt for entering text. The dialog contains an
* autocomplete text box and a clear button. This method requires a layout for
* the dialog and an image (drawable) for the erase button. The dialog layout
* should contain an autocomplete text box and an image view. The text entered
* by the end-user will be in {@link MvPromptResult#moAnswer moAnswer}
* (after using {@link EditText#getText()}.toString() on the returned value).
*
* <img src="{@docRoot}/doc-images/SCREENSHOT-AnswerPrompt.png"
* class="aws-img-right"
* alt="Dialog displayed by showAnswerPrompt()">
* This method requires a dialog layout with a text box and an image button.
*
*<pre><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#AA050505"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/llAnswer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="@+id/actvAnswer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:background="@android:color/white"
android:ems="8"
android:hint="le name of the fruit"
android:paddingLeft="3dp"
android:text=""
android:textColor="@android:color/black"
android:textColorHint="@android:color/darker_gray" >
<requestFocus />
</AutoCompleteTextView>
<ImageView
android:id="@+id/ivAnswerErase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/broom" />
</LinearLayout>
</ScrollView>
* </pre>
*
*
* The following code snippet uses the ID of the above layout and the IDs of
* text box and erase button specified in the layout to display an answer
* prompt.
*
*
<pre>MvPromptResult oPrompt; // class-level declaration
...
oPrompt
= oink.showAnswerPrompt(
// asTitle
"Un Fruit",
// asQuestion
"Name your favorite fruit",
// asAnswer (default answer)
"apple",
//aiDialogLayoutID
R.layout.answer_prompt_layout,
// aiEraseButtonID
R.id.ivAnswerErase,
// aiAnswerID
R.id.actvAnswer,
// Button captions
"OK", "Cancel",
// Button click handlers
new DialogInterface.OnClickListener() {
{@literal @}Override
public void onClick(DialogInterface aoDialog, int aiWhich) {
oink.showMessage(
"You specified le " +
oPrompt.moAnswer.getText().toString());
}
},
new DialogInterface.OnClickListener() {
{@literal @}Override
public void onClick(DialogInterface aoDialog, int aiWhich) {
oink.showMessage("You did not specify un fruit");
}
});
oPrompt.show();
</pre>
*
* <img src="{@docRoot}/doc-images/SCREENSHOT-AnswerPrompt-with-autosuggestions.png"
* class="aws-img-center"
* alt="Dialog displayed by showAnswerPrompt() with autosuggestions">
* To provide auto-suggestions, an array adapter needs to be set for the text
* box.
*
<pre>String[] arSuggestedFruits
= { "apple", "banana", "orange", "pineapple", "strawberry" };
ArrayAdapter<String> oUrlOptionsAdapter =
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line,
arSuggestedFruits);
oPrompt.moAnswer.setAdapter(oUrlOptionsAdapter);
</pre>
*
*
* @param asTitle
* title of the dialog
* @param asQuestion
* instruction in the dialog
* @param asAnswer
* default answer in the dialog
* @param aiDialogLayoutID
* layout of the dialog
* @param aiEraseButtonID
* ID of the erase button in the dialog layout
* @param aiAnswerID
* ID of the text box in the dialog layout
* @param asOkButtonCaption
* caption of the OK button
* @param asCancelButtonCaption
* caption of the cancel button
* @param aoOkHandler
* click-event handler of the OK button
* @param aoCancelHandler
* click-event handler of the cancel button
* @return dialog created by this method
*/
public MvPromptResult showAnswerPrompt(
String asTitle,
String asQuestion,
String asAnswer,
int aiDialogLayoutID,
int aiEraseButtonID,
int aiAnswerID,
String asOkButtonCaption,
String asCancelButtonCaption,
DialogInterface.OnClickListener aoOkHandler,
DialogInterface.OnClickListener aoCancelHandler) {
LayoutInflater li = this.mCallingActivity.getLayoutInflater();
View oAlertDialogView = li.inflate(aiDialogLayoutID, null);
ImageView aivEraseButton = (ImageView) oAlertDialogView.findViewById(aiEraseButtonID);
final AutoCompleteTextView aactvAnswer = (AutoCompleteTextView) oAlertDialogView.findViewById(aiAnswerID);
aactvAnswer.setText(asAnswer);
AlertDialog.Builder adb = new AlertDialog.Builder(this.mCallingActivity);
adb.setTitle(asTitle);
adb.setMessage(asQuestion);
adb.setView(oAlertDialogView);
adb.setPositiveButton(asOkButtonCaption, aoOkHandler);
adb.setNegativeButton(asCancelButtonCaption, aoCancelHandler);
AlertDialog adRet = adb.create();
aivEraseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
aactvAnswer.setText("");
}
});
final MvPromptResult oResult = new MvPromptResult();
oResult.moAnswer = aactvAnswer;
oResult.moPrompt = adRet;
aactvAnswer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
oResult.moPrompt.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return false;
}
});
return(oResult);
}
/**
* Displays a dialog prompt for entering text. The dialog contains
* an autocomplete text box, a clear button and drop-down list of
* options.
*
* @param asTitle title of the dialog
* @param asQuestion instruction displayed in the dialog
* @param asAnswer default text to displayed in the text box
* @param arsOptionsListValues options to be displayed in the drop-down list
* @param aiDefaultOptionIndex index of the default option in the drop-down list
* @param aiDialogLayoutID layout of the dialog
* @param aiOptionsListLayoutID layout of a option in the drop-down list
* @param aiEraseButtonID ID of the erase button drawable
* @param aiAnswerID id of the text box (AutoCompleteTextView) in the dialog layout
* @param aiOptionsListID id of the drop-down list (Spinner) in the dialog layout
* @param asOkButtonCaption caption of the OK button
* @param asCancelButtonCaption caption of the cancel button
* @param aoOkHandler event handler of the OK button
* @param aoCancelHandler event handler of the cancel button
* @return dialog created by this method
*/
public MvPromptResult showAnswerPrompt(
String asTitle,
String asQuestion,
String asAnswer,
String[] arsOptionsListValues,
int aiDefaultOptionIndex,
int aiDialogLayoutID,
int aiOptionsListLayoutID,
int aiEraseButtonID,
int aiAnswerID,
int aiOptionsListID,
String asOkButtonCaption,
String asCancelButtonCaption,
DialogInterface.OnClickListener aoOkHandler,
DialogInterface.OnClickListener aoCancelHandler) {
LayoutInflater li = this.mCallingActivity.getLayoutInflater();
View oAlertDialogView = li.inflate(aiDialogLayoutID, null);
ImageView aivEraseButton = (ImageView) oAlertDialogView.findViewById(aiEraseButtonID);
final AutoCompleteTextView aactvAnswer = (AutoCompleteTextView) oAlertDialogView.findViewById(aiAnswerID);
Spinner aoAnswerOptions = (Spinner) oAlertDialogView.findViewById(aiOptionsListID);
ArrayAdapter<String> oOptionsAdapter = new ArrayAdapter<String> (this.mCallingActivity, aiOptionsListLayoutID, arsOptionsListValues);
oOptionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
aoAnswerOptions.setAdapter(oOptionsAdapter);
if (aiDefaultOptionIndex < arsOptionsListValues.length) {
aoAnswerOptions.setSelection(aiDefaultOptionIndex);
}
aactvAnswer.setText(asAnswer);
AlertDialog.Builder adb = new AlertDialog.Builder(this.mCallingActivity);
adb.setTitle(asTitle);
adb.setMessage(asQuestion);
adb.setView(oAlertDialogView);
adb.setPositiveButton(asOkButtonCaption, aoOkHandler);
adb.setNegativeButton(asCancelButtonCaption, aoCancelHandler);
final AlertDialog adRet = adb.create();
aivEraseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
aactvAnswer.setText("");
}
});
final MvPromptResult oResult = new MvPromptResult();
oResult.moAnswer = aactvAnswer;
oResult.moAnswerOptions = aoAnswerOptions;
oResult.moPrompt = adRet;
aactvAnswer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
oResult.moPrompt.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return false;
}
});
return(oResult);
}
/**
* Prompts for a yes-or-no choice for specified question.
*
* @param asTitle
* text displayed in the title of the prompt
* @param asMessage
* text displayed as the message of the prompt
* @return a {@link MvMessages.DialogInterfaceCaller} instance handling user
* interactivity of the prompt
*/
public DialogInterfaceCaller showPrompt(String asTitle, String asMessage) {
DialogInterfaceCaller oDialog = new DialogInterfaceCaller(mCallingActivity, asTitle, asMessage);
return(oDialog);
}
/**
* This class helps in using the {@link MvMessages#showPrompt(String, String)}
* method.
*
* @author V. Subhash
* @version 1.0
*/
public class DialogInterfaceCaller {
AlertDialog.Builder oAlertBuilder;
public DialogInterface.OnClickListener onOkay = null;
public DialogInterface.OnClickListener onNotOkay = null;
/**
* Constructs an instance of this class for use with specified activity.
*
* @param aoCallingActivity activity with which this instance needs to be used
* @param asTitle title of the prompt
* @param asMessage message of the prompt
*/
public DialogInterfaceCaller(Activity aoCallingActivity, String asTitle, String asMessage) {
oAlertBuilder = new AlertDialog.Builder(aoCallingActivity);
oAlertBuilder
.setTitle(asTitle)
.setMessage(asMessage);
}
/**
* Sets up the event handlers and displays the prompt.
*/
public void show() {
if (onNotOkay == null) {
oAlertBuilder.setNegativeButton("No", null);
} else {
oAlertBuilder.setNegativeButton("No", onNotOkay);
}
if (onOkay != null) {
oAlertBuilder
.setPositiveButton("Yes", onOkay)
.show();
}
}
}
/**
* Creates a menu containing specified options and icons, and displays it if
* specified. Use a layout such as the following:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:orientation="horizontal" >
<ImageView
android:id="@+id/menu_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:baselineAlignBottom="true"
android:padding="5dip" />
<TextView
android:id="@+id/menu_option_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:background="@android:color/transparent"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:textColor="@android:color/white"
android:textSize="22dip" />
</LinearLayout>
</pre>
*
* @param asMenuTitle
* title of the menu
* @param aoMenuOptionsIconList
* list containing menu option {@link Drawable icons}
* @param asMenuOptionsTextList
* list containing menu option text
* @param aiRowLayoutId
* layout used to display the menu options
* @param aiOptionIconId
* ImageView in the layout that needs to be used by the menu options
* @param aiOptionTextId
* TextView in the layout that needs to be used by the menu options
* @param aoMenuOptionClickListener
* "onclick" listener for the menu options
* @param abAutoShow
* whether the menu should be automatically displayed after it is
* constructed
* @return the new menu
*/
public AlertDialog.Builder showOptionsMenuWithIcons(
String asMenuTitle,
final ArrayList<Drawable> aoMenuOptionsIconList,
final ArrayList<String> asMenuOptionsTextList,
final int aiRowLayoutId,
final int aiOptionIconId, final int aiOptionTextId,
DialogInterface.OnClickListener aoMenuOptionClickListener,
boolean abAutoShow) {
AlertDialog.Builder oMenu = new AlertDialog.Builder(mCallingActivity);
class MvMenuOptionContents {
ImageView icon;
TextView title;
}
ListAdapter oListAdaptor = new ArrayAdapter<String>(
mCallingActivity.getApplicationContext(),
aiRowLayoutId,
asMenuOptionsTextList) {
@Override
public View getView(int aiIndex, View aoOptionView, ViewGroup aoParentViewGroup) {
MvMenuOptionContents oOptionContents;
if (aoOptionView == null) {
aoOptionView = LayoutInflater.from(mCallingActivity.getApplicationContext()).inflate(aiRowLayoutId, null);
oOptionContents = new MvMenuOptionContents();
oOptionContents.icon = (ImageView) aoOptionView.findViewById(aiOptionIconId);
oOptionContents.title = (TextView) aoOptionView.findViewById(aiOptionTextId);
aoOptionView.setTag(oOptionContents);
//return super.getView(position, convertView, parent);
} else {
oOptionContents = (MvMenuOptionContents) aoOptionView.getTag();
}
oOptionContents.icon.setImageDrawable(aoMenuOptionsIconList.get(aiIndex));
oOptionContents.title.setText(asMenuOptionsTextList.get(aiIndex));
return(aoOptionView);
// return super.getView(aiIndex, aoOptionView, aoParentViewGroup);
}
};
oMenu.setTitle(asMenuTitle);
oMenu.setAdapter(oListAdaptor, aoMenuOptionClickListener);
if (abAutoShow) {
oMenu.show();
}
return(oMenu);
}
}