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
This commit is contained in:
ryanlwlin
2020-12-09 20:29:37 +08:00
parent 7bd311cf7a
commit d245501a5c
2 changed files with 55 additions and 16 deletions

View File

@@ -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) {

View File

@@ -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<MotionEvent> 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<Runnable> 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;
}
}