From 36e72ae8b4a5110c50f6b9052257ebe28e240b8e Mon Sep 17 00:00:00 2001 From: 8bhsolutions <48874658+8bhsolutions@users.noreply.github.com> Date: Mon, 10 Feb 2020 18:22:40 +1100 Subject: [PATCH 1/3] Added ability to setCancelable --- src/android/Notification.java | 20 +++++++++++++++----- www/android/notification.js | 8 ++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/android/Notification.java b/src/android/Notification.java index 8e34e540..f17694f7 100644 --- a/src/android/Notification.java +++ b/src/android/Notification.java @@ -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; @@ -69,6 +70,8 @@ public class Notification extends CordovaPlugin { public ProgressDialog spinnerDialog = null; public ProgressDialog progressDialog = null; + private boolean isCancelable = true; + /** * Constructor. */ @@ -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; } @@ -181,7 +187,7 @@ 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) { @@ -221,7 +227,7 @@ 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) { @@ -311,7 +317,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); @@ -414,7 +420,7 @@ 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() { @@ -457,7 +463,7 @@ public void run() { 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( @@ -493,6 +499,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; diff --git a/www/android/notification.js b/www/android/notification.js index 5562a9fe..9f481e5c 100644 --- a/www/android/notification.js +++ b/www/android/notification.js @@ -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 ]); } }; From 9252d09a31f3e93113f4ac7a84defd3df0ea34e5 Mon Sep 17 00:00:00 2001 From: 8bhsolutions <48874658+8bhsolutions@users.noreply.github.com> Date: Mon, 10 Feb 2020 22:12:47 +1100 Subject: [PATCH 2/3] Fix formatting problems causing test failures --- src/android/Notification.java | 2 +- www/android/notification.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/Notification.java b/src/android/Notification.java index f17694f7..7ccdd946 100644 --- a/src/android/Notification.java +++ b/src/android/Notification.java @@ -61,7 +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 String ACTION_SET_CANCELABLE = "setCancelable"; private static final long BEEP_TIMEOUT = 5000; private static final long BEEP_WAIT_TINE = 100; diff --git a/www/android/notification.js b/www/android/notification.js index 9f481e5c..c6ff0cc4 100644 --- a/www/android/notification.js +++ b/www/android/notification.js @@ -76,7 +76,7 @@ module.exports = { * Set whether the user can cancel the dialog * @param {Boolean} enable Enable/Disable cancellable dialog */ - setCancelable: function(enable) { + setCancelable: function (enable) { exec(null, null, 'Notification', 'setCancelable', [ enable ]); } }; From dca5efc46db3446ccb79a50f0a97967086cbdb0a Mon Sep 17 00:00:00 2001 From: 8bhsolutions <48874658+8bhsolutions@users.noreply.github.com> Date: Wed, 18 Mar 2020 16:02:38 +1100 Subject: [PATCH 3/3] Fix possible threading issue * Set local final variable for isCancelable --- src/android/Notification.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/android/Notification.java b/src/android/Notification.java index 7ccdd946..ebee0153 100644 --- a/src/android/Notification.java +++ b/src/android/Notification.java @@ -180,6 +180,7 @@ 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() { @@ -221,6 +222,7 @@ 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() { @@ -302,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() { @@ -415,6 +418,7 @@ 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); @@ -457,6 +461,7 @@ 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);