From 3d479ed2220a80ab7b5697856f106637ef9a03aa Mon Sep 17 00:00:00 2001 From: Steven Terrell Date: Thu, 20 Apr 2023 02:46:08 +0000 Subject: [PATCH] Fix cursor not blinking when view re-added to layout This fixes a bug where if the EditText view has or had focus and then is removed from the layout and re-added the cursor does not resume blinking. There was a change several months ago that prevented the cursor from blinking when the window is no longer visible. That change added a check in shouldBlink to verify that the window is visible. This created a situation where the window might not be visible when makeBlink is called from onFocusChanged and as a result mBlink is never instantiated. This resulted in the cursor not blinking after an app starts. The fix was to call makeBlink again when onAttachToWindow is called. The Window is visible at this point and the cursor blinks as expected. The reason the code change below is needed is in the case where a view is removed and added back after the mBlink object has been instantiated. A call to unCancel is needed to ensure that the cursor resumes blinking as expected. This was identified in this change aosp/2540031. The change also guards against the window not visible case as outlined above. Bug: 278907680 Test: Added following CTS test: testCursorResumeBlinking_AfterFocusedView_DynamicallyRemovedAdded testCursorBlinking_ViewDynamically_RemovedAdded_NeverHadFocus testCursorSuspendBlinking_ViewDynamicallyRemoved testCursorNotBlinking_ViewDynamicallyAdded_NoFocus testCursorBlinking_ViewDynamicallyAdded_WithFocus (cherry picked from https://android-review.googlesource.com/q/commit:61d982884fa0e14dc5f58e872f182bb5c1b04afd) Merged-In: I07afc7ef1a707549240479015a00a66db814afb7 Change-Id: I07afc7ef1a707549240479015a00a66db814afb7 Cherrypick from AOSP master to udc-dev. --- core/java/android/widget/Editor.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 7931d1a06d393..2dbff581fe84b 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -724,7 +724,10 @@ public class Editor { } getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true); - makeBlink(); + // Call resumeBlink here instead of makeBlink to ensure that if mBlink is not null the + // Blink object is uncancelled. This ensures when a view is removed and added back the + // cursor will resume blinking. + resumeBlink(); } void onDetachedFromWindow() { @@ -1094,8 +1097,10 @@ public class Editor { private void resumeBlink() { if (mBlink != null) { mBlink.uncancel(); - makeBlink(); } + // Moving makeBlink outside of the null check block ensures that mBlink object gets + // instantiated when the view is added to the window if mBlink is still null. + makeBlink(); } void adjustInputType(boolean password, boolean passwordInputType, @@ -2921,6 +2926,9 @@ public class Editor { if (shouldBlink()) { mShowCursor = SystemClock.uptimeMillis(); if (mBlink == null) mBlink = new Blink(); + // Call uncancel as mBlink could have previously been cancelled and cursor will not + // resume blinking unless uncancelled. + mBlink.uncancel(); mTextView.removeCallbacks(mBlink); mTextView.postDelayed(mBlink, BLINK); } else {