Skip to content

Commit 40aa5c0

Browse files
committed
fix(DefaultUIController): guard SSL-error dialog against finishing activity (#1065)
Resolves #1065 The crash reported in #1065 is a WindowManager$BadTokenException raised from DefaultUIController.onShowSslCertificateErrorDialog when AlertDialog is shown after the hosting Activity has already started its finishing/destroyed teardown. The WebView delivers SSL error callbacks on the main thread and there is no guarantee the cached mActivity is still in a state where it can host a dialog. This change short-circuits the path when the activity is null, finishing, or destroyed, calling handler.cancel() in that case so we fail safe rather than crash. The same check is repeated immediately before AlertDialog.show(), and the show() call itself is wrapped in a try/catch for the residual race where the activity transitions between our last check and the platform call.
1 parent 95d48cd commit 40aa5c0

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ public void onPermissionsDeny(String[] permissions, String permissionType, Strin
367367

368368
@Override
369369
public void onShowSslCertificateErrorDialog(final WebView view, final SslErrorHandler handler, final SslError error) {
370+
// Issue #1065: Activity may already be finishing/destroyed by the time the
371+
// WebView delivers the SSL error callback on the main thread. Showing a
372+
// dialog against a stale window token throws WindowManager$BadTokenException.
373+
// Fail safe by cancelling the request when we cannot present UI.
374+
if (mActivity == null || mActivity.isFinishing()
375+
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
376+
handler.cancel();
377+
return;
378+
}
370379
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mActivity);
371380
String sslErrorMessage;
372381
switch (error.getPrimaryError()) {
@@ -402,7 +411,18 @@ public void onClick(DialogInterface dialog, int which) {
402411
handler.cancel();
403412
}
404413
});
405-
alertDialog.show();
414+
// Re-check just before show in case the activity transitioned during string lookup.
415+
if (mActivity.isFinishing()
416+
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
417+
handler.cancel();
418+
return;
419+
}
420+
try {
421+
alertDialog.show();
422+
} catch (android.view.WindowManager.BadTokenException | IllegalStateException e) {
423+
// Activity window went away between our check and show(); fail safe.
424+
handler.cancel();
425+
}
406426

407427

408428
}

0 commit comments

Comments
 (0)