From 1a5e861c0ecae98a21b5bcef209b2c3e5bc86922 Mon Sep 17 00:00:00 2001 From: Jeppe Schmidt Date: Sun, 3 Jan 2021 13:34:16 +0100 Subject: [PATCH 1/2] Non-cancelable rationale dialog. * Set the rationale dialog cancelable / not cancelable. * Set rationale dialog custom negative button text. * Set rationale dialog custom positive button text. --- .../android/permissions/Permissions.java | 36 +++++++++++++++++++ .../permissions/PermissionsActivity.java | 30 ++++++++++------ 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/permissions/src/main/java/com/nabinbhandari/android/permissions/Permissions.java b/permissions/src/main/java/com/nabinbhandari/android/permissions/Permissions.java index 35ca0ff..c866c22 100644 --- a/permissions/src/main/java/com/nabinbhandari/android/permissions/Permissions.java +++ b/permissions/src/main/java/com/nabinbhandari/android/permissions/Permissions.java @@ -152,9 +152,12 @@ public static class Options implements Serializable { String settingsText = "Settings"; String rationaleDialogTitle = "Permissions Required"; + String rationaleDialogNegativeButton = null; + String rationaleDialogPositiveButton = null; String settingsDialogTitle = "Permissions Required"; String settingsDialogMessage = "Required permission(s) have been set" + " not to ask again! Please provide them from settings."; + boolean rationaleDialogCancelable = true; boolean sendBlockedToSettings = true; boolean createNewTask = false; @@ -193,6 +196,28 @@ public Options setRationaleDialogTitle(String rationaleDialogTitle) { return this; } + /** + * Sets a custom text for the rationale dialog's negative button. + * + * @param rationaleDialogNegativeButton cancel button text. + * @return same instance. + */ + public Options setRationaleDialogNegativeButton(String rationaleDialogNegativeButton) { + this.rationaleDialogNegativeButton = rationaleDialogNegativeButton; + return this; + } + + /** + * Sets a custom text for the rationale dialog's positive button. + * + * @param rationaleDialogPositiveButton positive button text. + * @return same instance. + */ + public Options setRationaleDialogPositiveButton(String rationaleDialogPositiveButton) { + this.rationaleDialogPositiveButton = rationaleDialogPositiveButton; + return this; + } + /** * Sets the title text of the dialog which asks user to go to settings, in the case when * permission(s) have been set not to ask again. @@ -230,6 +255,17 @@ public Options sendDontAskAgainToSettings(boolean send) { sendBlockedToSettings = send; return this; } + + /** + * Sets whether or not the rationale dialog should be cancelable. + * + * @param cancelable if true, the rationale dialog will be cancelable. + * @return same instance. + */ + public Options setRationaleDialogCancelable(boolean cancelable) { + rationaleDialogCancelable = cancelable; + return this; + } } } diff --git a/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java b/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java index 7ecf972..7c93a7b 100644 --- a/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java +++ b/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java @@ -92,16 +92,26 @@ public void onClick(DialogInterface dialog, int which) { } } }; - new AlertDialog.Builder(this).setTitle(options.rationaleDialogTitle) - .setMessage(rationale) - .setPositiveButton(android.R.string.ok, listener) - .setNegativeButton(android.R.string.cancel, listener) - .setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - deny(); - } - }).create().show(); + + AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(options.rationaleDialogTitle); + builder.setMessage(rationale); + + if(options.rationaleDialogCancelable){ + String negativeButtonText = options.rationaleDialogNegativeButton != null ? options.rationaleDialogNegativeButton : getString(android.R.string.cancel); + builder.setNegativeButton(negativeButtonText, listener) + .setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + deny(); + } + }); + } else { + builder.setCancelable(false); + } + + String positiveButtonText = options.rationaleDialogPositiveButton != null ? options.rationaleDialogPositiveButton : getString(android.R.string.ok); + builder.setPositiveButton(positiveButtonText, listener) + .create().show(); } @SuppressWarnings("NullableProblems") From 3f132d70be370d39a98d063a42a2f02a0ae6b6cc Mon Sep 17 00:00:00 2001 From: Jeppe Schmidt Date: Sun, 3 Jan 2021 13:59:14 +0100 Subject: [PATCH 2/2] Fixed formatting. --- .../nabinbhandari/android/permissions/PermissionsActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java b/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java index 7c93a7b..0dacc4f 100644 --- a/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java +++ b/permissions/src/main/java/com/nabinbhandari/android/permissions/PermissionsActivity.java @@ -96,7 +96,7 @@ public void onClick(DialogInterface dialog, int which) { AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(options.rationaleDialogTitle); builder.setMessage(rationale); - if(options.rationaleDialogCancelable){ + if (options.rationaleDialogCancelable) { String negativeButtonText = options.rationaleDialogNegativeButton != null ? options.rationaleDialogNegativeButton : getString(android.R.string.cancel); builder.setNegativeButton(negativeButtonText, listener) .setOnCancelListener(new DialogInterface.OnCancelListener() {