From d245501a5c9972b6ee948361aaa1eb743ed9e840 Mon Sep 17 00:00:00 2001 From: ryanlwlin Date: Wed, 9 Dec 2020 20:29:37 +0800 Subject: Fix button is gone after dragging over 5 secs The pending fading out animation is not canceled while dragging. It should be canceled when the users touchs the button. If it is fading out when the user touchs it, we should cancel the animation and set the alpha value to 1. Bug: 175194712 Test: atest MagnificationModeSwitchTest manually test Change-Id: I3dec8520e90db7aff1050924be4e3bab1e27f23d --- .../accessibility/MagnificationModeSwitch.java | 13 +++-- .../accessibility/MagnificationModeSwitchTest.java | 58 +++++++++++++++++----- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/accessibility/MagnificationModeSwitch.java b/packages/SystemUI/src/com/android/systemui/accessibility/MagnificationModeSwitch.java index e40185c279a8..c3d437d329ca 100644 --- a/packages/SystemUI/src/com/android/systemui/accessibility/MagnificationModeSwitch.java +++ b/packages/SystemUI/src/com/android/systemui/accessibility/MagnificationModeSwitch.java @@ -146,7 +146,7 @@ class MagnificationModeSwitch { } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - mImageView.animate().cancel(); + stopFadeOutAnimation(); mLastDown.set(event.getRawX(), event.getRawY()); mLastDrag.set(event.getRawX(), event.getRawY()); return true; @@ -212,13 +212,18 @@ class MagnificationModeSwitch { AccessibilityManager.FLAG_CONTENT_ICONS | AccessibilityManager.FLAG_CONTENT_CONTROLS); } + // Refresh the time slot of the fade-out task whenever this method is called. + stopFadeOutAnimation(); + mImageView.postOnAnimationDelayed(mFadeOutAnimationTask, mUiTimeout); + } + + private void stopFadeOutAnimation() { + mImageView.removeCallbacks(mFadeOutAnimationTask); if (mIsFadeOutAnimating) { mImageView.animate().cancel(); mImageView.setAlpha(1f); + mIsFadeOutAnimating = false; } - // Refresh the time slot of the fade-out task whenever this method is called. - mImageView.removeCallbacks(mFadeOutAnimationTask); - mImageView.postOnAnimationDelayed(mFadeOutAnimationTask, mUiTimeout); } void onConfigurationChanged(int configDiff) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/accessibility/MagnificationModeSwitchTest.java b/packages/SystemUI/tests/src/com/android/systemui/accessibility/MagnificationModeSwitchTest.java index 11150432f757..d13fd86818f4 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/accessibility/MagnificationModeSwitchTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/accessibility/MagnificationModeSwitchTest.java @@ -33,6 +33,7 @@ import static junit.framework.Assert.assertNotNull; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; @@ -91,6 +92,7 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { private MagnificationModeSwitch mMagnificationModeSwitch; private View.OnTouchListener mTouchListener; private List mMotionEvents = new ArrayList<>(); + private Runnable mFadeOutAnimation; @Before public void setUp() throws Exception { @@ -119,6 +121,7 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { event.recycle(); } mMotionEvents.clear(); + mFadeOutAnimation = null; } @Test @@ -157,15 +160,9 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { } @Test - public void showMagnificationButton_windowMode_verifyAnimationEndAction() { - // Execute the runnable immediately to run the animation. - doAnswer((invocation) -> { - final Runnable action = invocation.getArgument(0); - action.run(); - return null; - }).when(mSpyImageView).postOnAnimationDelayed(any(Runnable.class), anyLong()); - + public void showMagnificationButton_windowModeAndFadingOut_verifyAnimationEndAction() { mMagnificationModeSwitch.showButton(ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW); + executeFadeOutAnimation(); // Verify the end action after fade-out. final ArgumentCaptor endActionCaptor = ArgumentCaptor.forClass(Runnable.class); @@ -197,9 +194,6 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { final long downTime = SystemClock.uptimeMillis(); mTouchListener.onTouch(mSpyImageView, obtainMotionEvent(downTime, 0, ACTION_DOWN, 100, 100)); - - verify(mViewPropertyAnimator).cancel(); - resetAndStubMockImageViewAndAnimator(); mTouchListener.onTouch(mSpyImageView, obtainMotionEvent(downTime, downTime, ACTION_UP, 100, 100)); @@ -207,6 +201,31 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { verifyTapAction(ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW); } + @Test + public void sendDownEvent_fullscreenMode_fadeOutAnimationIsNull() { + mMagnificationModeSwitch.showButton(ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN); + resetAndStubMockImageViewAndAnimator(); + + final long downTime = SystemClock.uptimeMillis(); + mTouchListener.onTouch(mSpyImageView, + obtainMotionEvent(downTime, 0, ACTION_DOWN, 100, 100)); + + assertNull(mFadeOutAnimation); + } + + @Test + public void sendDownEvent_fullscreenModeAndFadingOut_cancelAnimation() { + mMagnificationModeSwitch.showButton(ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN); + executeFadeOutAnimation(); + resetAndStubMockImageViewAndAnimator(); + + final long downTime = SystemClock.uptimeMillis(); + mTouchListener.onTouch(mSpyImageView, + obtainMotionEvent(downTime, 0, ACTION_DOWN, 100, 100)); + + verify(mViewPropertyAnimator).cancel(); + } + @Test public void performDragging_showMagnificationButton_updateViewLayout() { mMagnificationModeSwitch.showButton(ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN); @@ -219,7 +238,6 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { final long downTime = SystemClock.uptimeMillis(); mTouchListener.onTouch(mSpyImageView, obtainMotionEvent( downTime, 0, ACTION_DOWN, 100, 100)); - verify(mViewPropertyAnimator).cancel(); mTouchListener.onTouch(mSpyImageView, obtainMotionEvent(downTime, downTime, ACTION_MOVE, 100 + offset, @@ -369,6 +387,16 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { resetAndStubMockAnimator(); Mockito.reset(mSpyImageView); doReturn(mViewPropertyAnimator).when(mSpyImageView).animate(); + doAnswer((invocation) -> { + mFadeOutAnimation = invocation.getArgument(0); + return null; + }).when(mSpyImageView).postOnAnimationDelayed(any(Runnable.class), anyLong()); + doAnswer((invocation) -> { + if (mFadeOutAnimation == invocation.getArgument(0)) { + mFadeOutAnimation = null; + } + return null; + }).when(mSpyImageView).removeCallbacks(any(Runnable.class)); } private void resetAndStubMockAnimator() { @@ -397,4 +425,10 @@ public class MagnificationModeSwitchTest extends SysuiTestCase { mMotionEvents.add(event); return event; } + + private void executeFadeOutAnimation() { + assertNotNull(mFadeOutAnimation); + mFadeOutAnimation.run(); + mFadeOutAnimation = null; + } } -- cgit v1.2.3-59-g8ed1b