Fix Screen Off logging for Flip to Screen Off.

Test: Locally & atest FaceDownDetector
Bug: 183621058
Change-Id: I00baf9a0292a3f45cc3c453381e19320576c0272
This commit is contained in:
bquezada
2021-03-24 17:43:31 +00:00
committed by Brian Quezada
parent cc2602452b
commit c93b57ba29
2 changed files with 60 additions and 15 deletions

View File

@@ -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,
/* millis_since_flip= */ mPreviousResultTime - mLastFlipTime,
mMillisSaved,
/* millis_until_next_screen_on= */ currentTime - mPreviousResultTime);
mPreviousResultType = -1;
}
final long currentTime = SystemClock.uptimeMillis();
FrameworkStatsLog.write(FrameworkStatsLog.FACE_DOWN_REPORTED,
SCREEN_OFF_RESULT,
/* millis_since_flip= */ mPreviousResultTime - mLastFlipTime,
mMillisSaved,
/* millis_until_next_screen_on= */ currentTime - mPreviousResultTime);
mPreviousResultType = UNKNOWN;
}
private boolean isEnabled() {

View File

@@ -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.