Skip to content

(android): Added ability to setCancelable #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/android/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class Notification extends CordovaPlugin {
private static final String ACTION_PROGRESS_START = "progressStart";
private static final String ACTION_PROGRESS_VALUE = "progressValue";
private static final String ACTION_PROGRESS_STOP = "progressStop";
private static final String ACTION_SET_CANCELABLE = "setCancelable";

private static final long BEEP_TIMEOUT = 5000;
private static final long BEEP_WAIT_TINE = 100;
Expand All @@ -69,6 +70,8 @@ public class Notification extends CordovaPlugin {
public ProgressDialog spinnerDialog = null;
public ProgressDialog progressDialog = null;

private boolean isCancelable = true;

/**
* Constructor.
*/
Expand Down Expand Up @@ -122,6 +125,9 @@ else if (action.equals(ACTION_PROGRESS_VALUE)) {
else if (action.equals(ACTION_PROGRESS_STOP)) {
this.progressStop();
}
else if (action.equals(ACTION_SET_CANCELABLE)) {
this.setCancelable(args.optBoolean(0, true));
}
else {
return false;
}
Expand Down Expand Up @@ -174,14 +180,15 @@ public void run() {
*/
public synchronized void alert(final String message, final String title, final String buttonLabel, final CallbackContext callbackContext) {
final CordovaInterface cordova = this.cordova;
final boolean isCancelable = this.isCancelable;

Runnable runnable = new Runnable() {
public void run() {

Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
dlg.setCancelable(isCancelable);
dlg.setPositiveButton(buttonLabel,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Expand Down Expand Up @@ -215,13 +222,14 @@ public void onCancel(DialogInterface dialog)
*/
public synchronized void confirm(final String message, final String title, final JSONArray buttonLabels, final CallbackContext callbackContext) {
final CordovaInterface cordova = this.cordova;
final boolean isCancelable = this.isCancelable;

Runnable runnable = new Runnable() {
public void run() {
Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
dlg.setCancelable(isCancelable);

// First button
if (buttonLabels.length() > 0) {
Expand Down Expand Up @@ -296,6 +304,7 @@ public void onCancel(DialogInterface dialog)
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final CallbackContext callbackContext) {

final CordovaInterface cordova = this.cordova;
final boolean isCancelable = this.isCancelable;

Runnable runnable = new Runnable() {
public void run() {
Expand All @@ -311,7 +320,7 @@ But for some android versions is not visible (for example 5.1.1).
Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
dlg.setCancelable(isCancelable);

dlg.setView(promptInput);

Expand Down Expand Up @@ -409,12 +418,13 @@ public synchronized void activityStart(final String title, final String message)
}
final Notification notification = this;
final CordovaInterface cordova = this.cordova;
final boolean isCancelable = this.isCancelable;
Runnable runnable = new Runnable() {
public void run() {
notification.spinnerDialog = createProgressDialog(cordova); // new ProgressDialog(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
notification.spinnerDialog.setTitle(title);
notification.spinnerDialog.setMessage(message);
notification.spinnerDialog.setCancelable(true);
notification.spinnerDialog.setCancelable(isCancelable);
notification.spinnerDialog.setIndeterminate(true);
notification.spinnerDialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
Expand Down Expand Up @@ -451,13 +461,14 @@ public synchronized void progressStart(final String title, final String message)
}
final Notification notification = this;
final CordovaInterface cordova = this.cordova;
final boolean isCancelable = this.isCancelable;
Runnable runnable = new Runnable() {
public void run() {
notification.progressDialog = createProgressDialog(cordova); // new ProgressDialog(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
notification.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
notification.progressDialog.setTitle(title);
notification.progressDialog.setMessage(message);
notification.progressDialog.setCancelable(true);
notification.progressDialog.setCancelable(isCancelable);
notification.progressDialog.setMax(100);
notification.progressDialog.setProgress(0);
notification.progressDialog.setOnCancelListener(
Expand Down Expand Up @@ -493,6 +504,10 @@ public synchronized void progressStop() {
}
}

public void setCancelable(boolean enable) {
this.isCancelable = enable;
}

@SuppressLint("NewApi")
private Builder createDialog(CordovaInterface cordova) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Expand Down
8 changes: 8 additions & 0 deletions www/android/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,13 @@ module.exports = {
*/
progressValue: function (value) {
exec(null, null, 'Notification', 'progressValue', [ value ]);
},

/**
* Set whether the user can cancel the dialog
* @param {Boolean} enable Enable/Disable cancellable dialog
*/
setCancelable: function (enable) {
exec(null, null, 'Notification', 'setCancelable', [ enable ]);
}
};