diff options
author | 2018-10-14 23:30:05 +0000 | |
---|---|---|
committer | 2018-10-14 23:30:05 +0000 | |
commit | 2d3972e2f1b86c33adeb5d9bee953cebac4b20df (patch) | |
tree | 8eec5cee9e145a237b3f28952d340c875c363e2c | |
parent | 8f799e487fa6bab247c6f4bc8f62034ea0c3660c (diff) | |
parent | 4b173140f30c801ab62e0d480d549ee9efa6ca33 (diff) |
Merge "Get InputMethodManager in View only if needed"
-rw-r--r-- | core/java/android/view/View.java | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index c18187b21200..29d3742ba632 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -7315,17 +7315,16 @@ public class View implements Drawable.Callback, KeyEvent.Callback, // Here we check whether we still need the default focus highlight, and switch it on/off. switchDefaultFocusHighlight(); - InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); if (!gainFocus) { if (isPressed()) { setPressed(false); } - if (imm != null && mAttachInfo != null && mAttachInfo.mHasWindowFocus) { - imm.focusOut(this); + if (mAttachInfo != null && mAttachInfo.mHasWindowFocus) { + notifyFocusChangeToInputMethodManager(false /* hasFocus */); } onFocusLost(); - } else if (imm != null && mAttachInfo != null && mAttachInfo.mHasWindowFocus) { - imm.focusIn(this); + } else if (mAttachInfo != null && mAttachInfo.mHasWindowFocus) { + notifyFocusChangeToInputMethodManager(true /* hasFocus */); } invalidate(true); @@ -7341,6 +7340,26 @@ public class View implements Drawable.Callback, KeyEvent.Callback, notifyEnterOrExitForAutoFillIfNeeded(gainFocus); } + /** + * Notify {@link InputMethodManager} about the focus change of the {@link View}. + * + * <p>Does nothing when {@link InputMethodManager} is not available.</p> + * + * @param hasFocus {@code true} when the {@link View} is being focused. + */ + private void notifyFocusChangeToInputMethodManager(boolean hasFocus) { + final InputMethodManager imm = + getContext().getSystemService(InputMethodManager.class); + if (imm == null) { + return; + } + if (hasFocus) { + imm.focusIn(this); + } else { + imm.focusOut(this); + } + } + /** @hide */ public void notifyEnterOrExitForAutoFillIfNeeded(boolean enter) { if (canNotifyAutofillEnterExitEvent()) { @@ -12485,7 +12504,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH; onFinishTemporaryDetach(); if (hasWindowFocus() && hasFocus()) { - getContext().getSystemService(InputMethodManager.class).focusIn(this); + notifyFocusChangeToInputMethodManager(true /* hasFocus */); } notifyEnterOrExitForAutoFillIfNeeded(true); } @@ -12876,20 +12895,19 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * focus, false otherwise. */ public void onWindowFocusChanged(boolean hasWindowFocus) { - InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); if (!hasWindowFocus) { if (isPressed()) { setPressed(false); } mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN; - if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) { - imm.focusOut(this); + if ((mPrivateFlags & PFLAG_FOCUSED) != 0) { + notifyFocusChangeToInputMethodManager(false /* hasFocus */); } removeLongPressCallback(); removeTapCallback(); onFocusLost(); - } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) { - imm.focusIn(this); + } else if ((mPrivateFlags & PFLAG_FOCUSED) != 0) { + notifyFocusChangeToInputMethodManager(true /* hasFocus */); } refreshDrawableState(); @@ -17981,10 +17999,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, rebuildOutline(); if (isFocused()) { - InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); - if (imm != null) { - imm.focusIn(this); - } + notifyFocusChangeToInputMethodManager(true /* hasFocus */); } } |