From 4ec6dfc389f0ad529cbf371b785b9698fae32333 Mon Sep 17 00:00:00 2001 From: Steven Terrell Date: Fri, 13 May 2022 16:10:32 +0000 Subject: [PATCH 1/2] 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; From d2acc476d07529ad847f41c79af896d9a6f40d80 Mon Sep 17 00:00:00 2001 From: Steven Terrell Date: Wed, 14 Sep 2022 18:22:42 +0000 Subject: [PATCH 2/2] Add Ability to Check if Cursor is Blinking adds a method for returning whether the cursor is blinking based on the current value of the mCancelled property in the Blink class. This method is only inteded to be used for testing purposes. Bug: 228354442 Test: manual, method is called in CtsWidgetTestCases:EditTextTest# testSuspendAndResumeBlinkingCursor, testCursorBlinkingOnNewActivity_WithFocus, testCursorNotBlinkingOnNewActivity_WithoutFocus Change-Id: I02849f6e105cbfe7af78dfb34ffbf1ebbb4a82ae --- core/java/android/widget/Editor.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 1cfe11610e475..fadad99f08853 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -2869,6 +2869,17 @@ public class Editor { } } + /** + * + * @return whether the Blink runnable is blinking or not, if null return false. + * @hide + */ + @VisibleForTesting + public boolean isBlinking() { + if (mBlink == null) return false; + return !mBlink.mCancelled; + } + private class Blink implements Runnable { private boolean mCancelled;