From 5f79ba7dcbd3c29046c0ffbdc9994303294e1d5e Mon Sep 17 00:00:00 2001 From: Wilson Wu Date: Fri, 8 Jul 2022 17:32:44 +0800 Subject: [PATCH] Clean up setInputChannelLocked -. Rename setInputChannelLocked and add a static method for checking same InputChannel. Suppose we need to handle the flush pending events when the input channel is modified based on CL[1], CL[2] and CL[3]. This is a mechanical refactor and it should no user visible change. [1]: Ibe26311edd0060cdcae80194f1753482e635786f [2]: I7e0b6a3c43cbe72f8762991f5d36560feebd214b [3]: If8317941dd1c6d5db77f1239b9a9f45d49997df9 Bug: 236920321 Test: atest CtsInputMethodTestCases Change-Id: I4f0d2404705a8f93de345571136690dd408d1132 --- .../view/inputmethod/InputMethodManager.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 8f6a42f7a43ae..36600ab402ebf 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -921,7 +921,7 @@ public final class InputMethodManager { mRequestUpdateCursorAnchorInfoMonitorMode = REQUEST_UPDATE_CURSOR_ANCHOR_INFO_NONE; - setInputChannelLocked(res.channel); + updateInputChannelLocked(res.channel); mCurMethod = res.method; // for @UnsupportedAppUsage mCurBindState = new BindState(res); mCurId = res.id; // for @UnsupportedAppUsage @@ -1623,7 +1623,7 @@ public final class InputMethodManager { void clearBindingLocked() { if (DEBUG) Log.v(TAG, "Clearing binding!"); clearConnectionLocked(); - setInputChannelLocked(null); + updateInputChannelLocked(null); mCurId = null; // for @UnsupportedAppUsage mCurMethod = null; // for @UnsupportedAppUsage // We only reset sequence number for input method, but not accessibility. @@ -1648,28 +1648,38 @@ public final class InputMethodManager { mAccessibilityInputMethodSession.clear(); } - void setInputChannelLocked(InputChannel channel) { - if (mCurChannel == channel) { - return; - } - if (mCurChannel != null && channel != null - && mCurChannel.getToken() == channel.getToken()) { - // channel is a dupe of 'mCurChannel', because they have the same token, and represent - // the same connection. Ignore the incoming channel and keep using 'mCurChannel' to - // avoid confusing the InputEventReceiver. + @GuardedBy("mH") + private void updateInputChannelLocked(InputChannel channel) { + if (areSameInputChannel(mCurChannel, channel)) { return; } + // TODO(b/238720598) : Requirements when design a new protocol for InputChannel + // channel is a dupe of 'mCurChannel', because they have the same token, and represent + // the same connection. Ignore the incoming channel and keep using 'mCurChannel' to + // avoid confusing the InputEventReceiver. if (mCurSender != null) { flushPendingEventsLocked(); mCurSender.dispose(); mCurSender = null; } + if (mCurChannel != null) { mCurChannel.dispose(); } mCurChannel = channel; } + private static boolean areSameInputChannel(@Nullable InputChannel lhs, + @Nullable InputChannel rhs) { + if (lhs == rhs) { + return true; + } + if (lhs == null || rhs == null) { + return false; + } + return lhs.getToken() == rhs.getToken(); + } + /** * Reset all of the state associated with a served view being connected * to an input method @@ -2384,7 +2394,7 @@ public final class InputMethodManager { } mVirtualDisplayToScreenMatrix = res.getVirtualDisplayToScreenMatrix(); if (res.id != null) { - setInputChannelLocked(res.channel); + updateInputChannelLocked(res.channel); mCurMethod = res.method; // for @UnsupportedAppUsage mCurBindState = new BindState(res); mAccessibilityInputMethodSession.clear();