mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Made notification.alert and notification.confirm async.
notification.confirm matches iOS and BB widget implementation (invoke callback with result).
This commit is contained in:
parent
072613be99
commit
8663ed412f
@ -16,27 +16,29 @@ function Notification() {
|
|||||||
* Open a native alert dialog, with a customizable title and button text.
|
* Open a native alert dialog, with a customizable title and button text.
|
||||||
*
|
*
|
||||||
* @param {String} message Message to print in the body of the alert
|
* @param {String} message Message to print in the body of the alert
|
||||||
|
* @param {Function} completeCallback The callback that is called when user clicks on a button.
|
||||||
* @param {String} title Title of the alert dialog (default: Alert)
|
* @param {String} title Title of the alert dialog (default: Alert)
|
||||||
* @param {String} buttonLabel Label of the close button (default: OK)
|
* @param {String} buttonLabel Label of the close button (default: OK)
|
||||||
*/
|
*/
|
||||||
Notification.prototype.alert = function(message, title, buttonLabel) {
|
Notification.prototype.alert = function(message, completeCallback, title, buttonLabel) {
|
||||||
var _title = (title || "Alert");
|
var _title = (title || "Alert");
|
||||||
var _buttonLabel = (buttonLabel || "OK");
|
var _buttonLabel = (buttonLabel || "OK");
|
||||||
PhoneGap.exec(null, null, "Notification", "alert", [message,_title,_buttonLabel]);
|
PhoneGap.exec(completeCallback, null, "Notification", "alert", [message,_title,_buttonLabel]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a native confirm dialog, with a customizable title and button text.
|
* Open a native confirm dialog, with a customizable title and button text.
|
||||||
|
* The result that the user selects is returned to the result callback.
|
||||||
*
|
*
|
||||||
* @param {String} message Message to print in the body of the alert
|
* @param {String} message Message to print in the body of the alert
|
||||||
|
* @param {Function} resultCallback The callback that is called when user clicks on a button.
|
||||||
* @param {String} title Title of the alert dialog (default: Confirm)
|
* @param {String} title Title of the alert dialog (default: Confirm)
|
||||||
* @param {String} buttonLabels Comma separated list of the labels of the buttons (default: 'OK,Cancel')
|
* @param {String} buttonLabels Comma separated list of the labels of the buttons (default: 'OK,Cancel')
|
||||||
* @return {Number} The index of the button clicked
|
|
||||||
*/
|
*/
|
||||||
Notification.prototype.confirm = function(message, title, buttonLabels) {
|
Notification.prototype.confirm = function(message, resultCallback, title, buttonLabels) {
|
||||||
var _title = (title || "Confirm");
|
var _title = (title || "Confirm");
|
||||||
var _buttonLabels = (buttonLabels || "OK,Cancel");
|
var _buttonLabels = (buttonLabels || "OK,Cancel");
|
||||||
return PhoneGap.exec(null, null, "Notification", "confirm", [message,_title,_buttonLabels]);
|
PhoneGap.exec(resultCallback, null, "Notification", "confirm", [message,_title,_buttonLabels]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,6 +18,7 @@ import android.content.DialogInterface;
|
|||||||
import android.media.Ringtone;
|
import android.media.Ringtone;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Looper;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,11 +56,14 @@ public class Notification extends Plugin {
|
|||||||
this.vibrate(args.getLong(0));
|
this.vibrate(args.getLong(0));
|
||||||
}
|
}
|
||||||
else if (action.equals("alert")) {
|
else if (action.equals("alert")) {
|
||||||
this.alert(args.getString(0),args.getString(1),args.getString(2));
|
Looper.prepare();
|
||||||
|
this.alert(args.getString(0),args.getString(1),args.getString(2), callbackId);
|
||||||
|
Looper.loop();
|
||||||
}
|
}
|
||||||
else if (action.equals("confirm")) {
|
else if (action.equals("confirm")) {
|
||||||
int i = this.confirm(args.getString(0),args.getString(1),args.getString(2));
|
Looper.prepare();
|
||||||
return new PluginResult(status, i);
|
this.confirm(args.getString(0),args.getString(1),args.getString(2), callbackId);
|
||||||
|
Looper.loop();
|
||||||
}
|
}
|
||||||
else if (action.equals("activityStart")) {
|
else if (action.equals("activityStart")) {
|
||||||
this.activityStart(args.getString(0),args.getString(1));
|
this.activityStart(args.getString(0),args.getString(1));
|
||||||
@ -90,10 +94,10 @@ public class Notification extends Plugin {
|
|||||||
*/
|
*/
|
||||||
public boolean isSynch(String action) {
|
public boolean isSynch(String action) {
|
||||||
if (action.equals("alert")) {
|
if (action.equals("alert")) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
else if (action.equals("confirm")) {
|
else if (action.equals("confirm")) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
else if (action.equals("activityStart")) {
|
else if (action.equals("activityStart")) {
|
||||||
return true;
|
return true;
|
||||||
@ -163,8 +167,12 @@ public class Notification extends Plugin {
|
|||||||
* @param message The message the alert should display
|
* @param message The message the alert should display
|
||||||
* @param title The title of the alert
|
* @param title The title of the alert
|
||||||
* @param buttonLabel The label of the button
|
* @param buttonLabel The label of the button
|
||||||
|
* @param callbackId The callback id
|
||||||
*/
|
*/
|
||||||
public synchronized void alert(String message,String title,String buttonLabel){
|
public synchronized void alert(String message,String title,String buttonLabel, String callbackId) {
|
||||||
|
final Notification notification = this;
|
||||||
|
final String fCallbackId = callbackId;
|
||||||
|
|
||||||
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
|
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
|
||||||
dlg.setMessage(message);
|
dlg.setMessage(message);
|
||||||
dlg.setTitle(title);
|
dlg.setTitle(title);
|
||||||
@ -173,6 +181,7 @@ public class Notification extends Plugin {
|
|||||||
new AlertDialog.OnClickListener() {
|
new AlertDialog.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
|
notification.success(new PluginResult(PluginResult.Status.OK, 0), fCallbackId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
dlg.create();
|
dlg.create();
|
||||||
@ -182,20 +191,20 @@ public class Notification extends Plugin {
|
|||||||
/**
|
/**
|
||||||
* Builds and shows a native Android confirm dialog with given title, message, buttons.
|
* Builds and shows a native Android confirm dialog with given title, message, buttons.
|
||||||
* This dialog only shows up to 3 buttons. Any labels after that will be ignored.
|
* This dialog only shows up to 3 buttons. Any labels after that will be ignored.
|
||||||
|
* The index of the button pressed will be returned to the JavaScript callback identified by callbackId.
|
||||||
*
|
*
|
||||||
* @param message The message the dialog should display
|
* @param message The message the dialog should display
|
||||||
* @param title The title of the dialog
|
* @param title The title of the dialog
|
||||||
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
|
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
|
||||||
* @return The index of the button clicked (1,2 or 3)
|
* @param callbackId The callback id
|
||||||
*/
|
*/
|
||||||
public synchronized int confirm(final String message, final String title, String buttonLabels) {
|
public synchronized void confirm(final String message, final String title, String buttonLabels, String callbackId) {
|
||||||
|
|
||||||
// Create dialog on UI thread
|
|
||||||
final DroidGap ctx = this.ctx;
|
final DroidGap ctx = this.ctx;
|
||||||
final Notification notification = this;
|
final Notification notification = this;
|
||||||
final String[] fButtons = buttonLabels.split(",");
|
final String[] fButtons = buttonLabels.split(",");
|
||||||
Runnable runnable = new Runnable() {
|
final String fCallbackId = callbackId;
|
||||||
public void run() {
|
|
||||||
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
|
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
|
||||||
dlg.setMessage(message);
|
dlg.setMessage(message);
|
||||||
dlg.setTitle(title);
|
dlg.setTitle(title);
|
||||||
@ -207,10 +216,7 @@ public class Notification extends Plugin {
|
|||||||
new AlertDialog.OnClickListener() {
|
new AlertDialog.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
synchronized(notification) {
|
notification.success(new PluginResult(PluginResult.Status.OK, 1), fCallbackId);
|
||||||
notification.confirmResult = 1;
|
|
||||||
notification.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -221,10 +227,7 @@ public class Notification extends Plugin {
|
|||||||
new AlertDialog.OnClickListener() {
|
new AlertDialog.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
synchronized(notification) {
|
notification.success(new PluginResult(PluginResult.Status.OK, 2), fCallbackId);
|
||||||
notification.confirmResult = 2;
|
|
||||||
notification.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -235,10 +238,7 @@ public class Notification extends Plugin {
|
|||||||
new AlertDialog.OnClickListener() {
|
new AlertDialog.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
synchronized(notification) {
|
notification.success(new PluginResult(PluginResult.Status.OK, 3), fCallbackId);
|
||||||
notification.confirmResult = 3;
|
|
||||||
notification.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -247,19 +247,6 @@ public class Notification extends Plugin {
|
|||||||
dlg.create();
|
dlg.create();
|
||||||
dlg.show();
|
dlg.show();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
this.ctx.runOnUiThread(runnable);
|
|
||||||
|
|
||||||
// Wait for dialog to close
|
|
||||||
synchronized(runnable) {
|
|
||||||
try {
|
|
||||||
this.wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.confirmResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show the spinner.
|
* Show the spinner.
|
||||||
|
Loading…
Reference in New Issue
Block a user