Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c93b57ba authored by bquezada's avatar bquezada Committed by Brian Quezada
Browse files

Fix Screen Off logging for Flip to Screen Off.

Test: Locally & atest FaceDownDetector
Bug: 183621058
Change-Id: I00baf9a0292a3f45cc3c453381e19320576c0272
parent cc260245
Loading
Loading
Loading
Loading
+17 −11
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.SystemClock;
import android.provider.DeviceConfig;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.FrameworkStatsLog;

import java.io.PrintWriter;
@@ -59,6 +60,8 @@ public class FaceDownDetector implements SensorEventListener {
            FrameworkStatsLog.FACE_DOWN_REPORTED__FACE_DOWN_RESPONSE__USER_INTERACTION;
    private static final int UNFLIP =
            FrameworkStatsLog.FACE_DOWN_REPORTED__FACE_DOWN_RESPONSE__UNFLIP;
    private static final int UNKNOWN =
            FrameworkStatsLog.FACE_DOWN_REPORTED__FACE_DOWN_RESPONSE__UNKNOWN;

    /**
     * Used by the ExponentialMovingAverage accelerations, this determines how quickly the
@@ -130,7 +133,7 @@ public class FaceDownDetector implements SensorEventListener {

    /** Values we store for logging purposes. */
    private long mLastFlipTime = 0L;
    public int mPreviousResultType = 0;
    public int mPreviousResultType = UNKNOWN;
    public long mPreviousResultTime = 0L;
    private long mMillisSaved = 0L;

@@ -151,7 +154,8 @@ public class FaceDownDetector implements SensorEventListener {

    private final Handler mHandler;
    private final Runnable mUserActivityRunnable;
    private final BroadcastReceiver mScreenReceiver;
    @VisibleForTesting
    final BroadcastReceiver mScreenReceiver;

    private Context mContext;

@@ -203,6 +207,10 @@ public class FaceDownDetector implements SensorEventListener {
                    logScreenOff();
                }
            } else {
                if (mFaceDown && !mInteractive) {
                    mPreviousResultType = SCREEN_OFF_RESULT;
                    mPreviousResultTime = currentTime;
                }
                mSensorManager.unregisterListener(this);
                mFaceDown = false;
                mOnFlip.accept(false);
@@ -311,15 +319,13 @@ public class FaceDownDetector implements SensorEventListener {
    }

    private void logScreenOff() {
        if (mPreviousResultType == SCREEN_OFF_RESULT) {
        final long currentTime = SystemClock.uptimeMillis();
        FrameworkStatsLog.write(FrameworkStatsLog.FACE_DOWN_REPORTED,
                    mPreviousResultType,
                SCREEN_OFF_RESULT,
                /* millis_since_flip= */ mPreviousResultTime  - mLastFlipTime,
                mMillisSaved,
                /* millis_until_next_screen_on= */ currentTime - mPreviousResultTime);
            mPreviousResultType = -1;
        }
        mPreviousResultType = UNKNOWN;
    }

    private boolean isEnabled() {
+43 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
@@ -129,10 +130,7 @@ public class FaceDownDetectorTest {
        triggerFaceDown();

        // Phone flips
        for (int i = 0; i < 10; i++) {
            advanceTime(Duration.ofMillis(5));
            mFaceDownDetector.onSensorChanged(createTestEvent(0.5f, 1.0f, 0.0f));
        }
        triggerUnflip();

        assertThat(mOnFaceDownCalls).isEqualTo(1);
        assertThat(mOnFaceDownExitCalls).isEqualTo(1);
@@ -184,6 +182,47 @@ public class FaceDownDetectorTest {
        assertThat(mOnFaceDownCalls).isEqualTo(1);
    }

    @Test
    public void faceDownToScreenOff_followedByScreenOnAndUserInteraction_doesNotDisable()
            throws Exception {
        mFaceDownDetector.systemReady(sContext);
        // Face down to screen off
        triggerFaceDown();
        mFaceDownDetector.mScreenReceiver.onReceive(sContext, new Intent(Intent.ACTION_SCREEN_OFF));

        // Screen on
        mFaceDownDetector.mScreenReceiver.onReceive(sContext, new Intent(Intent.ACTION_SCREEN_ON));

        // User interaction
        mFaceDownDetector.userActivity(PowerManager.USER_ACTIVITY_EVENT_TOUCH);
        waitForListenerToHandle();

        // Attempt another face down to see if disabled
        triggerFaceDown();

        assertThat(mOnFaceDownCalls).isEqualTo(2);
    }

    @Test
    public void faceDownUserInteraction_disablesDetector()  throws Exception {
        mFaceDownDetector.systemReady(sContext);
        triggerFaceDown();
        mFaceDownDetector.userActivity(PowerManager.USER_ACTIVITY_EVENT_TOUCH);
        waitForListenerToHandle();

        triggerUnflip();
        triggerFaceDown();

        assertThat(mOnFaceDownCalls).isEqualTo(1);
    }

    private void triggerUnflip() throws Exception {
        for (int i = 0; i < 10; i++) {
            advanceTime(Duration.ofMillis(5));
            mFaceDownDetector.onSensorChanged(createTestEvent(0.5f, 1.0f, 0.0f));
        }
    }

    private void triggerFaceDown() throws Exception {
        // Face up
        // Using 0.5 on x to simulate constant acceleration, such as a sloped surface.