Skip to content

Commit e5db933

Browse files
committed
fix up: use try and catch and fire onDecline callback
1 parent df65bae commit e5db933

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/permissions/AlertDialogPrepromptForAndroidSettings.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package com.onesignal.core.internal.permissions
2929

3030
import android.app.Activity
3131
import android.app.AlertDialog
32+
import android.view.WindowManager.BadTokenException
3233
import com.onesignal.core.R
3334
import com.onesignal.debug.LogLevel
3435
import com.onesignal.debug.internal.logging.Logging
@@ -55,13 +56,8 @@ object AlertDialogPrepromptForAndroidSettings {
5556
val messageTemplate = activity.getString(R.string.permission_not_available_message)
5657
val message = messageTemplate.format(previouslyDeniedPostfix)
5758

58-
// ensure the activity that will be showing the dialog is available
59-
if (activity == null || activity.isDestroyed || activity.isFinishing) {
60-
Logging.log(LogLevel.ERROR, "Alert dialog for Android settings was skipped because the activity was unavailable to display it.")
61-
return
62-
}
63-
64-
if (activity != null && !activity.isFinishing) {
59+
// Try displaying the dialog while handling cases where execution is not possible.
60+
try {
6561
AlertDialog.Builder(activity)
6662
.setTitle(title)
6763
.setMessage(message)
@@ -75,6 +71,11 @@ object AlertDialogPrepromptForAndroidSettings {
7571
callback.onDecline()
7672
}
7773
.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()
7879
}
7980
}
8081
}

0 commit comments

Comments
 (0)