From 4ec6dfc389f0ad529cbf371b785b9698fae32333 Mon Sep 17 00:00:00 2001 From: Steven Terrell Date: Fri, 13 May 2022 16:10:32 +0000 Subject: [PATCH] Stop cursor from blinking when not visible Cursor continues to blink in a text field even when the text field is not currently visible. This change checks the visibility of the window that houses the text field in addition to checking if the view is in focus. Bug: 228354442 Test: Added logging to the shouldBlink() method ran following steps: 1: open contacts app 2: tap add new contact, tap in an edit field 3: move back to home screen 4: lock screen, then unlock screen 5: observed logs written out to logcat console when app not visible. after change logs no longer printed to console, cursor still blinks correctly after app is re-opened or when new field becomes in focus. Test: CtsWidgetTestCases:EditTextTest# testCursorNotBlinkingOnNewActivity_WithoutFocus() testCursorBlinkingOnNewActivity_WithFocus() testSuspendAndResumeBlinkingCursor() Change-Id: Iad4396e5f7fd810344c711320d65c57ea25eda70 Merged-In: Iad4396e5f7fd810344c711320d65c57ea25eda70 --- core/java/android/widget/Editor.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 285a40779b900..1cfe11610e475 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -709,7 +709,7 @@ public class Editor { } getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true); - resumeBlink(); + makeBlink(); } void onDetachedFromWindow() { @@ -1685,17 +1685,12 @@ public class Editor { void onWindowFocusChanged(boolean hasWindowFocus) { if (hasWindowFocus) { - if (mBlink != null) { - mBlink.uncancel(); - makeBlink(); - } + resumeBlink(); if (mTextView.hasSelection() && !extractedTextModeWillBeStarted()) { refreshTextActionMode(); } } else { - if (mBlink != null) { - mBlink.cancel(); - } + suspendBlink(); if (mInputContentType != null) { mInputContentType.enterDown = false; } @@ -2851,7 +2846,8 @@ public class Editor { * @return True when the TextView isFocused and has a valid zero-length selection (cursor). */ private boolean shouldBlink() { - if (!isCursorVisible() || !mTextView.isFocused()) return false; + if (!isCursorVisible() || !mTextView.isFocused() + || mTextView.getWindowVisibility() != mTextView.VISIBLE) return false; final int start = mTextView.getSelectionStart(); if (start < 0) return false;