From 699fc515e02390d8ca63391fd865993cd5216fb1 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Mon, 17 Oct 2022 20:18:44 -0700 Subject: Use WeakReference#refersTo() when possible For a given WeakReference ref, ref.refersTo(obj) is more efficient than ref.get() == obj. This CL applies the above rule to RemoteInputConnectionImpl for a better performance. This is mechanical refactoring. There must be no observable behavior change. Bug: 182259171 Test: presubmit Change-Id: Iddf73b0fcb16a14f6c024da4a7db136d4fea4900 --- .../android/view/inputmethod/InputMethodManager.java | 2 +- .../view/inputmethod/RemoteInputConnectionImpl.java | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 74ed348d663f..72a886b479ae 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -994,7 +994,7 @@ public final class InputMethodManager { return mServedInputConnection != null && mServedInputConnection.isActive() - && mServedInputConnection.getServedView() == view; + && mServedInputConnection.isAssociatedWith(view); } } diff --git a/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java b/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java index 937687815200..3f9d5dc158ea 100644 --- a/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java +++ b/core/java/android/view/inputmethod/RemoteInputConnectionImpl.java @@ -218,10 +218,26 @@ final class RemoteInputConnectionImpl extends IRemoteInputConnection.Stub { return mParentInputMethodManager.isActive() && !isFinished(); } - public View getServedView() { + private View getServedView() { return mServedView.get(); } + /** + * Queries if the given {@link View} is associated with this {@link RemoteInputConnectionImpl} + * or not. + * + * @param view {@link View}. + * @return {@code true} if the given {@link View} is not null and is associated with this + * {@link RemoteInputConnectionImpl}. + */ + @AnyThread + public boolean isAssociatedWith(@Nullable View view) { + if (view == null) { + return false; + } + return mServedView.refersTo(view); + } + /** * Gets and resets {@link #mHasPendingImmediateCursorAnchorInfoUpdate}. * -- cgit v1.2.3-59-g8ed1b