Skip to content

Commit

Permalink
Merge pull request #2208 from OneSignal/windowManager-BadTokenException
Browse files Browse the repository at this point in the history
Window manager BadTokenException / WindowLeaked
  • Loading branch information
jinliu9508 authored Nov 4, 2024
2 parents bf615e1 + e5db933 commit a7d5dee
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ 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

/**
* A singleton helper which will display the fallback-to-settings alert dialog.
Expand All @@ -53,18 +56,26 @@ object AlertDialogPrepromptForAndroidSettings {
val messageTemplate = activity.getString(R.string.permission_not_available_message)
val message = messageTemplate.format(previouslyDeniedPostfix)

AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.permission_not_available_open_settings_option) { dialog, which ->
callback.onAccept()
}
.setNegativeButton(android.R.string.no) { dialog, which ->
callback.onDecline()
}
.setOnCancelListener {
callback.onDecline()
}
.show()
// Try displaying the dialog while handling cases where execution is not possible.
try {
AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.permission_not_available_open_settings_option) { dialog, which ->
callback.onAccept()
}
.setNegativeButton(android.R.string.no) { dialog, which ->
callback.onDecline()
}
.setOnCancelListener {
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 a7d5dee

Please sign in to comment.