You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/03/21 04:18:41 UTC

android commit: CB-2675: Add prompt dialog to Notification API

Updated Branches:
  refs/heads/master d2e4e35c3 -> b028ad360


CB-2675: Add prompt dialog to Notification API


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/b028ad36
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/b028ad36
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/b028ad36

Branch: refs/heads/master
Commit: b028ad3604b834d91aa7a8096206ee6135e5afbd
Parents: d2e4e35
Author: mbillau <mi...@gmail.com>
Authored: Tue Mar 19 12:09:48 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Wed Mar 20 23:14:42 2013 -0400

----------------------------------------------------------------------
 framework/src/org/apache/cordova/Notification.java |  104 +++++++++++++++
 1 files changed, 104 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/b028ad36/framework/src/org/apache/cordova/Notification.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/Notification.java b/framework/src/org/apache/cordova/Notification.java
index 87fce9d..9d96062 100755
--- a/framework/src/org/apache/cordova/Notification.java
+++ b/framework/src/org/apache/cordova/Notification.java
@@ -24,6 +24,7 @@ import org.apache.cordova.api.CordovaPlugin;
 import org.apache.cordova.api.PluginResult;
 import org.json.JSONArray;
 import org.json.JSONException;
+import org.json.JSONObject;
 import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.content.Context;
@@ -32,6 +33,7 @@ import android.media.Ringtone;
 import android.media.RingtoneManager;
 import android.net.Uri;
 import android.os.Vibrator;
+import android.widget.EditText;
 
 /**
  * This class provides access to notifications on the device.
@@ -71,6 +73,10 @@ public class Notification extends CordovaPlugin {
             this.confirm(args.getString(0), args.getString(1), args.getJSONArray(2), callbackContext);
             return true;
         }
+        else if (action.equals("prompt")) {
+            this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), callbackContext);
+            return true;
+        }
         else if (action.equals("activityStart")) {
             this.activityStart(args.getString(0), args.getString(1));
         }
@@ -254,6 +260,104 @@ public class Notification extends CordovaPlugin {
     }
 
     /**
+     * Builds and shows a native Android prompt dialog with given title, message, buttons.
+     * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
+     * The following results are returned to the JavaScript callback identified by callbackId:
+     *     buttonIndex			Index number of the button selected
+     *     input1				The text entered in the prompt dialog box
+     *
+     * @param message           The message the dialog should display
+     * @param title             The title of the dialog
+     * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
+     * @param callbackContext   The callback context.
+     */
+    public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final CallbackContext callbackContext) {
+    	
+        final CordovaInterface cordova = this.cordova;
+        final EditText promptInput =  new EditText(cordova.getActivity());
+
+        Runnable runnable = new Runnable() {
+            public void run() {
+                AlertDialog.Builder dlg = new AlertDialog.Builder(cordova.getActivity());
+                dlg.setMessage(message);
+                dlg.setTitle(title);
+                dlg.setCancelable(true);
+                
+                dlg.setView(promptInput);
+                
+                final JSONObject result = new JSONObject();
+                
+                // First button
+                if (buttonLabels.length() > 0) {
+                    try {
+						dlg.setNegativeButton(buttonLabels.getString(0),
+						        new AlertDialog.OnClickListener() {
+						            public void onClick(DialogInterface dialog, int which) {
+						                dialog.dismiss();
+						                try {
+											result.put("buttonIndex",1);
+							                result.put("input1", promptInput.getText());
+										} catch (JSONException e) { e.printStackTrace(); }
+						                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
+						            }
+						        });
+					} catch (JSONException e) { }
+                }
+
+                // Second button
+                if (buttonLabels.length() > 1) {
+                    try {
+						dlg.setNeutralButton(buttonLabels.getString(1),
+						        new AlertDialog.OnClickListener() {
+						            public void onClick(DialogInterface dialog, int which) {
+						                dialog.dismiss();
+						                try {
+											result.put("buttonIndex",2);
+							                result.put("input1", promptInput.getText());
+										} catch (JSONException e) { e.printStackTrace(); }
+						                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
+						            }
+						        });
+					} catch (JSONException e) { }
+                }
+
+                // Third button
+                if (buttonLabels.length() > 2) {
+                    try {
+						dlg.setPositiveButton(buttonLabels.getString(2),
+						        new AlertDialog.OnClickListener() {
+						            public void onClick(DialogInterface dialog, int which) {
+						                dialog.dismiss();
+						                try {
+											result.put("buttonIndex",3);
+							                result.put("input1", promptInput.getText());
+										} catch (JSONException e) { e.printStackTrace(); }
+						                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
+						            }
+						        }
+						        );
+					} catch (JSONException e) { }
+                }
+                dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
+                    public void onCancel(DialogInterface dialog)
+                    {
+                        dialog.dismiss();
+		                try {
+							result.put("buttonIndex",0);
+			                result.put("input1", promptInput.getText());
+						} catch (JSONException e) { e.printStackTrace(); }
+		                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
+                    }
+                });
+
+                dlg.create();
+                dlg.show();
+                
+            };
+        };
+        this.cordova.getActivity().runOnUiThread(runnable);
+    }
+    /**
      * Show the spinner.
      *
      * @param title     Title of the dialog