@@ -29,7 +29,10 @@ package com.onesignal.core.internal.permissions
29
29
30
30
import android.app.Activity
31
31
import android.app.AlertDialog
32
+ import android.view.WindowManager.BadTokenException
32
33
import com.onesignal.core.R
34
+ import com.onesignal.debug.LogLevel
35
+ import com.onesignal.debug.internal.logging.Logging
33
36
34
37
/* *
35
38
* A singleton helper which will display the fallback-to-settings alert dialog.
@@ -53,18 +56,26 @@ object AlertDialogPrepromptForAndroidSettings {
53
56
val messageTemplate = activity.getString(R .string.permission_not_available_message)
54
57
val message = messageTemplate.format(previouslyDeniedPostfix)
55
58
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
+ }
69
80
}
70
81
}
0 commit comments