From 9624ac3129d879320bd3dc927c835601b3d48948 Mon Sep 17 00:00:00 2001 From: Wilson Wu Date: Fri, 2 Sep 2022 14:54:18 +0800 Subject: [PATCH] Improve lock annotation for InputMethodManager (1/N) In InputMethodManager, we used the "mH" as a lock to protect some fields and methods for thread safe. Some of them are already protected with this lock but they didn't have the annotation yet. Add the annotation for those fields to prevent unexpected behavior change and increase the readability. Bug: b/236937383 Test: presubmit Change-Id: I3b345da25841b328358d16695e3a29ee65ae8d83 --- .../view/inputmethod/InputMethodManager.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 21b39a8e42ca0..b0075fa1132b9 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -439,6 +439,7 @@ public final class InputMethodManager { /** * True if this input method client is active, initially false. */ + @GuardedBy("mH") private boolean mActive = false; /** @@ -450,6 +451,7 @@ public final class InputMethodManager { /** * As reported by IME through InputConnection. */ + @GuardedBy("mH") private boolean mFullscreenMode; // ----------------------------------------------------------- @@ -460,16 +462,20 @@ public final class InputMethodManager { */ @GuardedBy("mH") ViewRootImpl mCurRootView; + /** * This is set when we are in the process of connecting, to determine * when we have actually finished. */ + @GuardedBy("mH") private boolean mServedConnecting; + /** * This is non-null when we have connected the served view; it holds * the attributes that were last retrieved from the served view and given * to the input connection. */ + @GuardedBy("mH") private EditorInfo mCurrentEditorInfo; @GuardedBy("mH") @@ -479,10 +485,13 @@ public final class InputMethodManager { /** * The InputConnection that was last retrieved from the served view. */ + @GuardedBy("mH") private RemoteInputConnectionImpl mServedInputConnection; + /** * The completions that were last provided by the served view. */ + @GuardedBy("mH") private CompletionInfo[] mCompletions; // Cursor position on the screen. @@ -490,21 +499,30 @@ public final class InputMethodManager { Rect mTmpCursorRect = new Rect(); @UnsupportedAppUsage Rect mCursorRect = new Rect(); + + @GuardedBy("mH") private int mCursorSelStart; + @GuardedBy("mH") private int mCursorSelEnd; + @GuardedBy("mH") private int mCursorCandStart; + @GuardedBy("mH") private int mCursorCandEnd; + @GuardedBy("mH") private int mInitialSelStart; + @GuardedBy("mH") private int mInitialSelEnd; /** * Handler for {@link RemoteInputConnectionImpl#getInputConnection()}. */ + @GuardedBy("mH") private Handler mServedInputConnectionHandler; /** * The instance that has previously been sent to the input method. */ + @GuardedBy("mH") private CursorAnchorInfo mCursorAnchorInfo = null; /** @@ -556,7 +574,9 @@ public final class InputMethodManager { private final SparseArray mAccessibilityInputMethodSession = new SparseArray<>(); + @GuardedBy("mH") private InputChannel mCurChannel; + @GuardedBy("mH") private ImeInputEventSender mCurSender; private static final int REQUEST_UPDATE_CURSOR_ANCHOR_INFO_NONE = 0x0; @@ -571,6 +591,7 @@ public final class InputMethodManager { /** * Applies the IME visibility and listens for other state changes. */ + @GuardedBy("mH") private ImeInsetsSourceConsumer mImeInsetsConsumer; private final Pool mPendingEventPool = new SimplePool<>(20); @@ -785,8 +806,10 @@ public final class InputMethodManager { */ @Override public void finishComposingText() { - if (mServedInputConnection != null) { - mServedInputConnection.finishComposingTextFromImm(); + synchronized (mH) { + if (mServedInputConnection != null) { + mServedInputConnection.finishComposingTextFromImm(); + } } } @@ -3154,6 +3177,7 @@ public final class InputMethodManager { } // Must be called on the main looper + @GuardedBy("mH") private int sendInputEventOnMainLooperLocked(PendingEvent p) { if (mCurChannel != null) { if (mCurSender == null) {