From 876377d0b20eb3d64c86ad461562f005b9f0b6d3 Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Fri, 2 Jun 2017 14:43:28 -0700 Subject: [PATCH] Catch WindowManager exceptions so they don't crash the system. WindowManager throws an IllegalStateException when adding a view twice; this should never happen (since show() and hide() are always called in the UIThread), but when it does, it should not crash the system. Test: existing CtsAutoFillServiceTestCases pass Fixes: 62262781 Change-Id: Idfd246ff173f4abb606865b7ba8cc367eb6acaad --- .../android/server/autofill/ui/FillUi.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/services/autofill/java/com/android/server/autofill/ui/FillUi.java b/services/autofill/java/com/android/server/autofill/ui/FillUi.java index d566d3d608b42..f00b272a59ace 100644 --- a/services/autofill/java/com/android/server/autofill/ui/FillUi.java +++ b/services/autofill/java/com/android/server/autofill/ui/FillUi.java @@ -398,6 +398,12 @@ final class FillUi { } catch (WindowManager.BadTokenException e) { if (sDebug) Slog.d(TAG, "Filed with with token " + params.token + " gone."); mCallback.onDestroy(); + } catch (IllegalStateException e) { + // WM throws an ISE if mContentView was added twice; this should never happen - + // since show() and hide() are always called in the UIThread - but when it does, + // it should not crash the system. + Slog.e(TAG, "Exception showing window " + params, e); + mCallback.onDestroy(); } } @@ -405,10 +411,18 @@ final class FillUi { * Hides the window. */ void hide() { - if (mShowing) { - mContentView.setOnTouchListener(null); - mWm.removeView(mContentView); - mShowing = false; + try { + if (mShowing) { + mContentView.setOnTouchListener(null); + mWm.removeView(mContentView); + mShowing = false; + } + } catch (IllegalStateException e) { + // WM might thrown an ISE when removing the mContentView; this should never + // happen - since show() and hide() are always called in the UIThread - but if it + // does, it should not crash the system. + Slog.e(TAG, "Exception hiding window ", e); + mCallback.onDestroy(); } }