7 package com.google.appinventor.components.runtime;
9 import android.app.Activity;
10 import android.app.AlertDialog;
11 import android.app.ProgressDialog;
12 import android.content.Context;
13 import android.content.DialogInterface;
14 import android.graphics.Color;
15 import android.graphics.Typeface;
16 import android.os.Handler;
17 import android.text.Html;
18 import android.text.InputType;
19 import android.text.SpannableString;
20 import android.util.Log;
21 import android.view.Gravity;
22 import android.view.inputmethod.InputMethodManager;
23 import android.widget.EditText;
24 import android.widget.TextView;
25 import android.widget.Toast;
26 import android.view.View;
48 @DesignerComponent(version = YaVersion.NOTIFIER_COMPONENT_VERSION,
49 category = ComponentCategory.USERINTERFACE,
50 description =
"The Notifier component displays alert dialogs, messages, and temporary alerts, " +
51 "and creates Android log entries through the following methods: " +
53 "<li> ShowMessageDialog: displays a message which the user must dismiss by pressing a button.</li>" +
54 "<li> ShowChooseDialog: displays a message two buttons to let the user choose one of two responses, " +
55 "for example, yes or no, after which the AfterChoosing event is raised.</li>" +
56 "<li> ShowTextDialog: lets the user enter text in response to the message, after " +
57 "which the AfterTextInput event is raised. " +
58 "<li> ShowPasswordDialog: lets the user enter password in response to the message, after " +
59 "which the AfterTextInput event is raised. " +
60 "<li> ShowAlert: displays a temporary alert that goes away by itself after a short time.</li>" +
61 "<li> ShowProgressDialog: displays an alert with a loading spinner that cannot be dismissed by " +
62 "the user. It can only be dismissed by using the DismissProgressDialog block.</li>" +
63 "<li> DismissProgressDialog: Dismisses the progress dialog displayed by ShowProgressDialog.</li>" +
64 "<li> LogError: logs an error message to the Android log. </li>" +
65 "<li> LogInfo: logs an info message to the Android log.</li>" +
66 "<li> LogWarning: logs a warning message to the Android log.</li>" +
67 "<li>The messages in the dialogs (but not the alert) can be formatted using the following HTML tags:" +
68 "<b>, <big>, <blockquote>, <br>, <cite>, <dfn>, <div>, " +
69 "<em>, <small>, <strong>, <sub>, <sup>, <tt>. <u></li>" +
70 "<li>You can also use the font tag to specify color, for example, <font color=\"blue\">. Some of the " +
71 "available color names are aqua, black, blue, fuchsia, green, grey, lime, maroon, navy, olive, purple, " +
72 "red, silver, teal, white, and yellow</li>" +
75 iconName =
"images/notifier.png")
80 private static final String LOG_TAG =
"Notifier";
81 private final Activity activity;
82 private final Handler handler;
83 private ProgressDialog progressDialog;
89 private int backgroundColor = Color.DKGRAY;
92 private int textColor = Color.WHITE;
100 super(container.
$form());
102 handler =
new Handler();
103 progressDialog =
null;
115 @
SimpleFunction(description =
"Shows a dialog box with an optional title and message "
116 +
"(use empty strings if they are not wanted). This dialog box contains a spinning "
117 +
"artifact to indicate that the program is working. It cannot be canceled by the user "
118 +
"but must be dismissed by the App Inventor Program by using the DismissProgressDialog "
121 progressDialog(message, title);
127 @
SimpleFunction(description =
"Dismiss a previously displayed ProgressDialog box")
129 if (progressDialog !=
null) {
130 progressDialog.dismiss();
131 progressDialog =
null;
142 if (progressDialog !=
null) {
145 progressDialog = ProgressDialog.show(activity, title, message);
147 progressDialog.setCancelable(
false);
164 public static void oneButtonAlert(Activity activity,String message, String title, String buttonText,
final Runnable callBack) {
165 Log.i(LOG_TAG,
"One button alert " + message);
166 AlertDialog alertDialog =
new AlertDialog.Builder(activity).create();
167 alertDialog.setTitle(title);
169 alertDialog.setCancelable(
false);
170 alertDialog.setMessage(stringToHTML(message));
171 alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
172 buttonText,
new DialogInterface.OnClickListener() {
173 public void onClick(DialogInterface dialog,
int which) {
174 if (callBack !=
null) {
184 public static void oneButtonAlert(Activity activity,String message, String title, String buttonText) {
190 private static SpannableString stringToHTML(String message) {
191 return new SpannableString(Html.fromHtml(message));
208 @SimpleFunction(description =
"Shows a dialog box with two buttons, from which the user can choose. "
209 +
" If cancelable is true there will be an additional CANCEL button. "
210 +
"Pressing a button will raise the AfterChoosing event. The \"choice\" parameter to AfterChoosing "
211 +
"will be the text on the button that was pressed, or \"Cancel\" if the "
212 +
" CANCEL button was pressed.")
214 final String button2Text,
boolean cancelable) {
221 new Runnable() {
public void run() {
AfterChoosing(button1Text);}},
222 new Runnable() {
public void run() {
AfterChoosing(button2Text);}},
236 final String button1Text,
final String button2Text,
boolean cancelable,
237 final Runnable positiveAction,
final Runnable negativeAction,
final Runnable cancelAction) {
238 Log.i(LOG_TAG,
"ShowChooseDialog: " + message);
239 AlertDialog alertDialog =
new AlertDialog.Builder(activity).create();
240 alertDialog.setTitle(title);
242 alertDialog.setCancelable(
false);
243 alertDialog.setMessage(stringToHTML(message));
249 alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, button1Text,
250 new DialogInterface.OnClickListener() {
251 public void onClick(DialogInterface dialog,
int which) {
252 positiveAction.run();
255 alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, button2Text,
256 new DialogInterface.OnClickListener() {
257 public void onClick(DialogInterface dialog,
int which) {
258 negativeAction.run();
266 final String cancelButtonText = activity.getString(android.R.string.cancel);
267 alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, cancelButtonText,
268 new DialogInterface.OnClickListener() {
269 public void onClick(DialogInterface dialog,
int which) {
283 description =
"Event after the user has made a selection for ShowChooseDialog.")
293 description =
"Event raised when the user canceled ShowChooseDialog.")
312 @
SimpleFunction(description =
"Shows a dialog box where the user can enter text, after which the "
313 +
"AfterTextInput event will be raised. If cancelable is true there will be an additional CANCEL button. "
314 +
"Entering text will raise the AfterTextInput event. The \"response\" parameter to AfterTextInput "
315 +
"will be the text that was entered, or \"Cancel\" if the CANCEL button was pressed.")
318 textInputDialog(message, title, cancelable,
false);
334 @
SimpleFunction(description =
"Shows a dialog box where the user can enter password (input is masked), "
335 +
"after which the AfterTextInput event will be raised. If cancelable is true there will be an "
336 +
"additional CANCEL button. Entering password will raise the AfterTextInput event. The \"response\" "
337 +
"parameter to AfterTextInput will be the entered password, or \"Cancel\" if CANCEL button was pressed.")
340 textInputDialog(message, title, cancelable,
true);
359 private void textInputDialog(String message, String title,
boolean cancelable,
boolean maskInput) {
360 final AlertDialog alertDialog =
new AlertDialog.Builder(activity).create();
361 alertDialog.setTitle(title);
362 alertDialog.setMessage(stringToHTML(message));
364 final EditText input =
new EditText(activity);
366 input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
368 alertDialog.setView(input);
370 alertDialog.setCancelable(
false);
371 alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
"OK",
372 new DialogInterface.OnClickListener() {
373 public void onClick(DialogInterface dialog,
int which) {
381 final String cancelButtonText = activity.getString(android.R.string.cancel);
382 alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, cancelButtonText,
383 new DialogInterface.OnClickListener() {
384 public void onClick(DialogInterface dialog,
int which) {
400 InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
401 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
410 description =
"Event raised after the user has responded to ShowTextDialog.")
422 description =
"Event raised when the user canceled ShowTextDialog.")
436 handler.post(
new Runnable() {
454 notifierLength = length;
461 description=
"Specifies the length of time that the alert is shown -- either \"short\" or \"long\".",
464 return notifierLength;
474 @
SimpleProperty(description=
"Specifies the background color for alerts (not dialogs).")
476 backgroundColor = argb;
484 @
SimpleProperty(description =
"Specifies the text color for alerts (not dialogs).",
505 private void toastNow (String message) {
513 Toast toast = Toast.makeText(activity, message, notifierLength);
514 toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2);
515 TextView textView =
new TextView(activity);
516 textView.setBackgroundColor(backgroundColor);
517 textView.setTextColor(textColor);
518 textView.setTextSize(fontsize);
519 Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
520 textView.setTypeface(typeface);
521 textView.setPadding(10, 10, 10, 10);
530 textView.setText(message +
" ");
531 toast.setView(textView);
541 @SimpleFunction(description =
"Writes an error message to the Android system log. " +
542 "See the Google Android documentation for how to access the log.")
544 Log.e(LOG_TAG, message);
553 @
SimpleFunction(description =
"Writes a warning message to the Android log. " +
554 "See the Google Android documentation for how to access the log.")
556 Log.w(LOG_TAG, message);
564 @
SimpleFunction(description =
"Writes an information message to the Android log.")
566 Log.i(LOG_TAG, message);