@@ -29,7 +29,10 @@ package com.onesignal.core.internal.permissions
2929
3030import android.app.Activity
3131import android.app.AlertDialog
32+ import android.view.WindowManager.BadTokenException
3233import com.onesignal.core.R
34+ import com.onesignal.debug.LogLevel
35+ import com.onesignal.debug.internal.logging.Logging
3336
3437/* *
3538 * A singleton helper which will display the fallback-to-settings alert dialog.
@@ -53,18 +56,26 @@ object AlertDialogPrepromptForAndroidSettings {
5356 val messageTemplate = activity.getString(R .string.permission_not_available_message)
5457 val message = messageTemplate.format(previouslyDeniedPostfix)
5558
56- AlertDialog .Builder (activity)
57- .setTitle(title)
58- .setMessage(message)
59- .setPositiveButton(R .string.permission_not_available_open_settings_option) { dialog, which ->
60- callback.onAccept()
61- }
62- .setNegativeButton(android.R .string.no) { dialog, which ->
63- callback.onDecline()
64- }
65- .setOnCancelListener {
66- callback.onDecline()
67- }
68- .show()
59+ // Try displaying the dialog while handling cases where execution is not possible.
60+ try {
61+ AlertDialog .Builder (activity)
62+ .setTitle(title)
63+ .setMessage(message)
64+ .setPositiveButton(R .string.permission_not_available_open_settings_option) { dialog, which ->
65+ callback.onAccept()
66+ }
67+ .setNegativeButton(android.R .string.no) { dialog, which ->
68+ callback.onDecline()
69+ }
70+ .setOnCancelListener {
71+ callback.onDecline()
72+ }
73+ .show()
74+ } catch (ex: BadTokenException ) {
75+ // If Android is unable to display the dialog, trigger the onDecline callback to maintain
76+ // consistency with the behavior when the dialog is canceled or dismissed without a response.
77+ Logging .log(LogLevel .ERROR , " Alert dialog for Android settings was skipped because the activity was unavailable to display it." )
78+ callback.onDecline()
79+ }
6980 }
7081}
0 commit comments