From 12a06ce6ea907cac974a909d0921e6f74d538d1a Mon Sep 17 00:00:00 2001 From: Arthur Hung Date: Mon, 22 Jul 2019 15:35:17 +0800 Subject: [PATCH] Dispose InputChannel when dispose InputEventReceiver There would be an error if an InputChannel didn't be disposed before finalized. The client InputChannel created by ViewRootImpl or SystemUI that would also create an InputEventReceiver to receive input events, and hold the client InputChannel, so if the InputEventReceiver is going to be disposed, the InputChannel should be disposed as well. Test: manual Bug: 128679213 Change-Id: I24c16f032403e8a982a84a5e0adbfabcdc016f0f --- .../service/wallpaper/WallpaperService.java | 14 +++----------- .../java/android/view/InputEventReceiver.java | 6 +++++- core/java/android/view/ViewRootImpl.java | 19 ++++++------------- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java index 60dbf84d555cc..e784ad3e31880 100644 --- a/core/java/android/service/wallpaper/WallpaperService.java +++ b/core/java/android/service/wallpaper/WallpaperService.java @@ -196,7 +196,6 @@ public abstract class WallpaperService extends Service { final WindowManager.LayoutParams mLayout = new WindowManager.LayoutParams(); IWindowSession mSession; - InputChannel mInputChannel; final Object mLock = new Object(); boolean mOffsetMessageEnqueued; @@ -819,11 +818,11 @@ public abstract class WallpaperService extends Service { mLayout.setTitle(WallpaperService.this.getClass().getName()); mLayout.windowAnimations = com.android.internal.R.style.Animation_Wallpaper; - mInputChannel = new InputChannel(); + InputChannel inputChannel = new InputChannel(); if (mSession.addToDisplay(mWindow, mWindow.mSeq, mLayout, View.VISIBLE, mDisplay.getDisplayId(), mWinFrame, mContentInsets, mStableInsets, - mOutsets, mDisplayCutout, mInputChannel, + mOutsets, mDisplayCutout, inputChannel, mInsetsState) < 0) { Log.w(TAG, "Failed to add window while updating wallpaper surface."); return; @@ -831,7 +830,7 @@ public abstract class WallpaperService extends Service { mCreated = true; mInputEventReceiver = new WallpaperInputEventReceiver( - mInputChannel, Looper.myLooper()); + inputChannel, Looper.myLooper()); } mSurfaceHolder.mSurfaceLock.lock(); @@ -1267,13 +1266,6 @@ public abstract class WallpaperService extends Service { } mSurfaceHolder.mSurface.release(); mCreated = false; - - // Dispose the input channel after removing the window so the Window Manager - // doesn't interpret the input channel being closed as an abnormal termination. - if (mInputChannel != null) { - mInputChannel.dispose(); - mInputChannel = null; - } } } diff --git a/core/java/android/view/InputEventReceiver.java b/core/java/android/view/InputEventReceiver.java index 7260a658a0271..ed8492e482c7b 100644 --- a/core/java/android/view/InputEventReceiver.java +++ b/core/java/android/view/InputEventReceiver.java @@ -102,7 +102,11 @@ public abstract class InputEventReceiver { nativeDispose(mReceiverPtr); mReceiverPtr = 0; } - mInputChannel = null; + + if (mInputChannel != null) { + mInputChannel.dispose(); + mInputChannel = null; + } mMessageQueue = null; } diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 354cc9645635e..f28c4e26e5b62 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -399,7 +399,6 @@ public final class ViewRootImpl implements ViewParent, @UnsupportedAppUsage final View.AttachInfo mAttachInfo; - InputChannel mInputChannel; InputQueue.Callback mInputQueueCallback; InputQueue mInputQueue; @UnsupportedAppUsage @@ -884,9 +883,10 @@ public final class ViewRootImpl implements ViewParent, // manager, to make sure we do the relayout before receiving // any other events from the system. requestLayout(); + InputChannel inputChannel = null; if ((mWindowAttributes.inputFeatures & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) { - mInputChannel = new InputChannel(); + inputChannel = new InputChannel(); } mForceDecorViewVisibility = (mWindowAttributes.privateFlags & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0; @@ -897,14 +897,14 @@ public final class ViewRootImpl implements ViewParent, res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), mTmpFrame, mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, - mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel, + mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, inputChannel, mTempInsets); setFrame(mTmpFrame); } catch (RemoteException e) { mAdded = false; mView = null; mAttachInfo.mRootView = null; - mInputChannel = null; + inputChannel = null; mFallbackEventHandler.setView(null); unscheduleTraversals(); setAccessibilityFocus(null, null); @@ -980,12 +980,12 @@ public final class ViewRootImpl implements ViewParent, mInputQueueCallback = ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue(); } - if (mInputChannel != null) { + if (inputChannel != null) { if (mInputQueueCallback != null) { mInputQueue = new InputQueue(); mInputQueueCallback.onInputQueueCreated(mInputQueue); } - mInputEventReceiver = new WindowInputEventReceiver(mInputChannel, + mInputEventReceiver = new WindowInputEventReceiver(inputChannel, Looper.myLooper()); } @@ -4388,13 +4388,6 @@ public final class ViewRootImpl implements ViewParent, } catch (RemoteException e) { } - // Dispose the input channel after removing the window so the Window Manager - // doesn't interpret the input channel being closed as an abnormal termination. - if (mInputChannel != null) { - mInputChannel.dispose(); - mInputChannel = null; - } - mDisplayManager.unregisterDisplayListener(mDisplayListener); unscheduleTraversals();