Fix screenshot swipe dismiss logs

Currently we log a "swipe dismiss" event whenever we animate to the
side, even if it wasn't caused by a swipe -- e.g. if the UI times
out, we log both a "timeout" and a "swipe dismiss" event. This
change fixes the logging logic so that we only log swipe dismiss
if it was caused by a swipe/fling, as well as adding a couple
of missing dismissal logs in edge cases.

Bug: 252823661
Test: atest DraggableConstraintLayoutTest

Change-Id: I437de637e606f4d2fe806ca6dd6a397b4ca372c6
This commit is contained in:
Miranda Kephart
2022-09-21 17:18:12 -04:00
parent 354ad8b465
commit 3c7ecee13c
4 changed files with 112 additions and 30 deletions

View File

@@ -49,7 +49,6 @@ public class DraggableConstraintLayout extends ConstraintLayout
private final SwipeDismissHandler mSwipeDismissHandler;
private final GestureDetector mSwipeDetector;
private View mActionsContainer;
private View mActionsContainerBackground;
private SwipeDismissCallbacks mCallbacks;
private final DisplayMetrics mDisplayMetrics;
@@ -111,6 +110,9 @@ public class DraggableConstraintLayout extends ConstraintLayout
}
});
mSwipeDetector.setIsLongpressEnabled(false);
mCallbacks = new SwipeDismissCallbacks() {
}; // default to unimplemented callbacks
}
public void setCallbacks(SwipeDismissCallbacks callbacks) {
@@ -119,16 +121,13 @@ public class DraggableConstraintLayout extends ConstraintLayout
@Override
public boolean onInterceptHoverEvent(MotionEvent event) {
if (mCallbacks != null) {
mCallbacks.onInteraction();
}
mCallbacks.onInteraction();
return super.onInterceptHoverEvent(event);
}
@Override // View
protected void onFinishInflate() {
mActionsContainer = findViewById(R.id.actions_container);
mActionsContainerBackground = findViewById(R.id.actions_container_background);
}
@Override
@@ -186,6 +185,13 @@ public class DraggableConstraintLayout extends ConstraintLayout
inoutInfo.touchableRegion.set(r);
}
private int getBackgroundRight() {
// background expected to be null in testing.
// animation may have unexpected behavior if view is not present
View background = findViewById(R.id.actions_container_background);
return background == null ? 0 : background.getRight();
}
/**
* Allows a view to be swipe-dismissed, or returned to its location if distance threshold is not
* met
@@ -213,8 +219,6 @@ public class DraggableConstraintLayout extends ConstraintLayout
mGestureDetector = new GestureDetector(context, gestureListener);
mDisplayMetrics = new DisplayMetrics();
context.getDisplay().getRealMetrics(mDisplayMetrics);
mCallbacks = new SwipeDismissCallbacks() {
}; // default to unimplemented callbacks
}
@Override
@@ -230,7 +234,9 @@ public class DraggableConstraintLayout extends ConstraintLayout
return true;
}
if (isPastDismissThreshold()) {
dismiss();
ValueAnimator anim = createSwipeDismissAnimation();
mCallbacks.onSwipeDismissInitiated(anim);
dismiss(anim);
} else {
// if we've moved, but not past the threshold, start the return animation
if (DEBUG_DISMISS) {
@@ -295,10 +301,7 @@ public class DraggableConstraintLayout extends ConstraintLayout
}
void dismiss() {
float velocityPxPerMs = FloatingWindowUtil.dpToPx(mDisplayMetrics, VELOCITY_DP_PER_MS);
ValueAnimator anim = createSwipeDismissAnimation(velocityPxPerMs);
mCallbacks.onSwipeDismissInitiated(anim);
dismiss(anim);
dismiss(createSwipeDismissAnimation());
}
private void dismiss(ValueAnimator animator) {
@@ -323,6 +326,11 @@ public class DraggableConstraintLayout extends ConstraintLayout
mDismissAnimation.start();
}
private ValueAnimator createSwipeDismissAnimation() {
float velocityPxPerMs = FloatingWindowUtil.dpToPx(mDisplayMetrics, VELOCITY_DP_PER_MS);
return createSwipeDismissAnimation(velocityPxPerMs);
}
private ValueAnimator createSwipeDismissAnimation(float velocity) {
// velocity is measured in pixels per millisecond
velocity = Math.min(3, Math.max(1, velocity));
@@ -337,7 +345,7 @@ public class DraggableConstraintLayout extends ConstraintLayout
if (startX > 0 || (startX == 0 && layoutDir == LAYOUT_DIRECTION_RTL)) {
finalX = mDisplayMetrics.widthPixels;
} else {
finalX = -1 * mActionsContainerBackground.getRight();
finalX = -1 * getBackgroundRight();
}
float distance = Math.abs(finalX - startX);

View File

@@ -28,6 +28,7 @@ import static com.android.systemui.screenshot.LogConfig.DEBUG_UI;
import static com.android.systemui.screenshot.LogConfig.DEBUG_WINDOW;
import static com.android.systemui.screenshot.LogConfig.logTag;
import static com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_DISMISSED_OTHER;
import static com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_INTERACTION_TIMEOUT;
import static java.util.Objects.requireNonNull;
@@ -322,9 +323,7 @@ public class ScreenshotController {
if (DEBUG_UI) {
Log.d(TAG, "Corner timeout hit");
}
mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_INTERACTION_TIMEOUT, 0,
mPackageName);
ScreenshotController.this.dismissScreenshot(false);
dismissScreenshot(SCREENSHOT_INTERACTION_TIMEOUT);
});
mDisplayManager = requireNonNull(context.getSystemService(DisplayManager.class));
@@ -351,8 +350,7 @@ public class ScreenshotController {
@Override
public void onReceive(Context context, Intent intent) {
if (ClipboardOverlayController.COPY_OVERLAY_ACTION.equals(intent.getAction())) {
mUiEventLogger.log(SCREENSHOT_DISMISSED_OTHER);
dismissScreenshot(false);
dismissScreenshot(SCREENSHOT_DISMISSED_OTHER);
}
}
};
@@ -419,24 +417,20 @@ public class ScreenshotController {
/**
* Clears current screenshot
*/
void dismissScreenshot(boolean immediate) {
void dismissScreenshot(ScreenshotEvent event) {
if (DEBUG_DISMISS) {
Log.d(TAG, "dismissScreenshot(immediate=" + immediate + ")");
Log.d(TAG, "dismissScreenshot");
}
// If we're already animating out, don't restart the animation
// (but do obey an immediate dismissal)
if (!immediate && mScreenshotView.isDismissing()) {
if (mScreenshotView.isDismissing()) {
if (DEBUG_DISMISS) {
Log.v(TAG, "Already dismissing, ignoring duplicate command");
}
return;
}
mUiEventLogger.log(event, 0, mPackageName);
mScreenshotHandler.cancelTimeout();
if (immediate) {
finishDismiss();
} else {
mScreenshotView.animateDismissal();
}
mScreenshotView.animateDismissal();
}
boolean isPendingSharedTransition() {
@@ -509,7 +503,7 @@ public class ScreenshotController {
if (DEBUG_INPUT) {
Log.d(TAG, "onKeyEvent: KeyEvent.KEYCODE_BACK");
}
dismissScreenshot(false);
dismissScreenshot(SCREENSHOT_DISMISSED_OTHER);
return true;
}
return false;

View File

@@ -89,8 +89,7 @@ public class TakeScreenshotService extends Service {
Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS");
}
if (!mScreenshot.isPendingSharedTransition()) {
mUiEventLogger.log(SCREENSHOT_DISMISSED_OTHER);
mScreenshot.dismissScreenshot(false);
mScreenshot.dismissScreenshot(SCREENSHOT_DISMISSED_OTHER);
}
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.screenshot;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.view.MotionEvent;
import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@SmallTest
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
public class DraggableConstraintLayoutTest extends SysuiTestCase {
@Mock
DraggableConstraintLayout.SwipeDismissCallbacks mCallbacks;
private DraggableConstraintLayout mDraggableConstraintLayout;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mDraggableConstraintLayout = new DraggableConstraintLayout(mContext, null, 0);
}
@Test
public void test_dismissDoesNotCallSwipeInitiated() {
mDraggableConstraintLayout.setCallbacks(mCallbacks);
mDraggableConstraintLayout.dismiss();
verify(mCallbacks, never()).onSwipeDismissInitiated(any());
}
@Test
public void test_onTouchCallsOnInteraction() {
mDraggableConstraintLayout.setCallbacks(mCallbacks);
mDraggableConstraintLayout.onInterceptTouchEvent(
MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0));
verify(mCallbacks).onInteraction();
}
@Test
public void test_callbacksNotSet() {
// just test that it doesn't throw an NPE
mDraggableConstraintLayout.onInterceptTouchEvent(
MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0));
mDraggableConstraintLayout.onInterceptHoverEvent(
MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_ENTER, 0, 0, 0));
mDraggableConstraintLayout.dismiss();
}
}