Skip to content

Commit

Permalink
fix up: use try and catch and fire onDecline callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliu9508 committed Nov 4, 2024
1 parent df65bae commit e5db933
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.onesignal.core.internal.permissions

import android.app.Activity
import android.app.AlertDialog
import android.view.WindowManager.BadTokenException
import com.onesignal.core.R
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
Expand All @@ -55,13 +56,8 @@ object AlertDialogPrepromptForAndroidSettings {
val messageTemplate = activity.getString(R.string.permission_not_available_message)
val message = messageTemplate.format(previouslyDeniedPostfix)

// ensure the activity that will be showing the dialog is available
if (activity == null || activity.isDestroyed || activity.isFinishing) {
Logging.log(LogLevel.ERROR, "Alert dialog for Android settings was skipped because the activity was unavailable to display it.")
return
}

if (activity != null && !activity.isFinishing) {
// Try displaying the dialog while handling cases where execution is not possible.
try {
AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
Expand All @@ -75,6 +71,11 @@ object AlertDialogPrepromptForAndroidSettings {
callback.onDecline()
}
.show()
} catch (ex: BadTokenException) {
// If Android is unable to display the dialog, trigger the onDecline callback to maintain
// consistency with the behavior when the dialog is canceled or dismissed without a response.
Logging.log(LogLevel.ERROR, "Alert dialog for Android settings was skipped because the activity was unavailable to display it.")
callback.onDecline()
}
}
}

0 comments on commit e5db933

Please sign in to comment.