Skip to content

Guard SSL error dialog with isFinishing in DefaultUIController (closes #1065)#1087

Open
jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf:fix/ssl-dialog-isfinishing-issue-1065
Open

Guard SSL error dialog with isFinishing in DefaultUIController (closes #1065)#1087
jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf:fix/ssl-dialog-isfinishing-issue-1065

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented Apr 23, 2026

Guard the SSL error dialog with isFinishing in DefaultUIController

Closes #1065

What changed

agentweb-core/src/main/java/com/just/agentweb/DefaultUIController.java

onShowSslCertificateErrorDialog now uses the same mActivity == null || mActivity.isFinishing() guard that every other dialog method in this class already has (onLoading, onCancelLoading, onForceDownloadAlert, and so on). When the activity is on its way out we cancel the SSL handler instead of attempting to attach a dialog to a dead window token.

This is the exact crash the reporter captured:

android.view.WindowManager$BadTokenException: Unable to add window
  -- token android.os.BinderProxy@... is not valid; is your activity running?
  at com.just.agentweb.DefaultUIController.onShowSslCertificateErrorDialog

Key snippet

public void onShowSslCertificateErrorDialog(final WebView view, final SslErrorHandler handler, final SslError error) {
    Activity mActivity;
    if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) {
        handler.cancel();
        return;
    }
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mActivity);
    ...
}

Cancelling the handler matches the existing user choice of "Cancel" on the dialog, which is the safe default for an SSL error.

@jim-daf jim-daf marked this pull request as ready for review April 23, 2026 09:48
Copilot AI review requested due to automatic review settings April 23, 2026 09:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a reported WindowManager$BadTokenException crash by adding an early Activity lifecycle guard before showing the SSL certificate error dialog in DefaultUIController.

Changes:

  • Add an mActivity == null || mActivity.isFinishing() guard to onShowSslCertificateErrorDialog.
  • Cancel the SslErrorHandler when the host Activity is finishing, instead of attempting to show a dialog.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +371 to +374
if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) {
// Issue #1065: Avoid BadTokenException when the host activity has
// already started finishing. Mirror the guard used by every other
// dialog method in this class (onLoading, onCancelLoading, ...).
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new guard only checks isFinishing(), but other dialog methods in this class also guard against Activity.isDestroyed() (API >= 17). Without the isDestroyed() check, this method can still attempt to show a dialog during/after destruction (e.g., configuration change), which can still throw BadTokenException. Consider adding the same Build.VERSION.SDK_INT / isDestroyed() early-return here (and cancel the SslErrorHandler in that path as well).

Suggested change
if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) {
// Issue #1065: Avoid BadTokenException when the host activity has
// already started finishing. Mirror the guard used by every other
// dialog method in this class (onLoading, onCancelLoading, ...).
if ((mActivity = this.mActivity) == null
|| mActivity.isFinishing()
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
// Avoid BadTokenException when the host activity is not in a valid
// state to show dialogs, including the destroyed state on API 17+.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

友盟上统计了个崩溃

2 participants