am 1a3af711: Merge "Doze: Break out proxcheck stats by pulse reason." into lmp-mr1-dev

* commit '1a3af711ab6f811a6bc4e9211cb255721fb1847d':
  Doze: Break out proxcheck stats by pulse reason.
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeHost.java b/packages/SystemUI/src/com/android/systemui/doze/DozeHost.java
index e92f988..6ea5c70 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeHost.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeHost.java
@@ -25,7 +25,7 @@
     void addCallback(@NonNull Callback callback);
     void removeCallback(@NonNull Callback callback);
     void startDozing(@NonNull Runnable ready);
-    void pulseWhileDozing(@NonNull PulseCallback callback);
+    void pulseWhileDozing(@NonNull PulseCallback callback, int reason);
     void stopDozing();
     boolean isPowerSaveActive();
 
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
index fcdbfc1..7eb9c6e 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
@@ -35,6 +35,13 @@
     private static final int SIZE = Build.IS_DEBUGGABLE ? 400 : 50;
     private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
 
+    private static final int PULSE_REASONS = 4;
+
+    public static final int PULSE_REASON_INTENT = 0;
+    public static final int PULSE_REASON_NOTIFICATION = 1;
+    public static final int PULSE_REASON_SENSOR_SIGMOTION = 2;
+    public static final int PULSE_REASON_SENSOR_PICKUP = 3;
+
     private static long[] sTimes;
     private static String[] sMessages;
     private static int sPosition;
@@ -48,8 +55,7 @@
     private static SummaryStats sScreenOnPulsingStats;
     private static SummaryStats sScreenOnNotPulsingStats;
     private static SummaryStats sEmergencyCallStats;
-    private static SummaryStats sProxNearStats;
-    private static SummaryStats sProxFarStats;
+    private static SummaryStats[][] sProxStats; // [reason][near/far]
 
     public static void tracePickupPulse(boolean withinVibrationThreshold) {
         if (!ENABLED) return;
@@ -58,10 +64,10 @@
                 : sPickupPulseNotNearVibrationStats).append();
     }
 
-    public static void tracePulseStart() {
+    public static void tracePulseStart(int reason) {
         if (!ENABLED) return;
         sPulsing = true;
-        log("pulseStart");
+        log("pulseStart reason=" + pulseReasonToString(reason));
     }
 
     public static void tracePulseFinish() {
@@ -90,8 +96,11 @@
                 sScreenOnPulsingStats = new SummaryStats();
                 sScreenOnNotPulsingStats = new SummaryStats();
                 sEmergencyCallStats = new SummaryStats();
-                sProxNearStats = new SummaryStats();
-                sProxFarStats = new SummaryStats();
+                sProxStats = new SummaryStats[PULSE_REASONS][2];
+                for (int i = 0; i < PULSE_REASONS; i++) {
+                    sProxStats[i][0] = new SummaryStats();
+                    sProxStats[i][1] = new SummaryStats();
+                }
                 log("init");
                 KeyguardUpdateMonitor.getInstance(context).registerCallback(sKeyguardCallback);
             }
@@ -137,10 +146,21 @@
         }
     }
 
-    public static void traceProximityResult(boolean near, long millis) {
+    public static void traceProximityResult(boolean near, long millis, int pulseReason) {
         if (!ENABLED) return;
-        log("proximityResult near=" + near + " millis=" + millis);
-        (near ? sProxNearStats : sProxFarStats).append();
+        log("proximityResult reason=" + pulseReasonToString(pulseReason) + " near=" + near
+                + " millis=" + millis);
+        sProxStats[pulseReason][near ? 0 : 1].append();
+    }
+
+    private static String pulseReasonToString(int pulseReason) {
+        switch (pulseReason) {
+            case PULSE_REASON_INTENT: return "intent";
+            case PULSE_REASON_NOTIFICATION: return "notification";
+            case PULSE_REASON_SENSOR_SIGMOTION: return "sigmotion";
+            case PULSE_REASON_SENSOR_PICKUP: return "pickup";
+            default: throw new IllegalArgumentException("bad reason: " + pulseReason);
+        }
     }
 
     public static void dump(PrintWriter pw) {
@@ -164,8 +184,11 @@
             sScreenOnPulsingStats.dump(pw, "Screen on (pulsing)");
             sScreenOnNotPulsingStats.dump(pw, "Screen on (not pulsing)");
             sEmergencyCallStats.dump(pw, "Emergency call");
-            sProxNearStats.dump(pw, "Proximity (near)");
-            sProxFarStats.dump(pw, "Proximity (far)");
+            for (int i = 0; i < PULSE_REASONS; i++) {
+                final String reason = pulseReasonToString(i);
+                sProxStats[i][0].dump(pw, "Proximity near (" + reason + ")");
+                sProxStats[i][1].dump(pw, "Proximity far (" + reason + ")");
+            }
         }
     }
 
@@ -188,6 +211,7 @@
         }
 
         public void dump(PrintWriter pw, String type) {
+            if (mCount == 0) return;
             pw.print("    ");
             pw.print(type);
             pw.print(": n=");
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
index 89b3e5b..1e29476 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
@@ -118,9 +118,11 @@
 
         mSensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
         mSigMotionSensor = new TriggerSensor(Sensor.TYPE_SIGNIFICANT_MOTION,
-                mDozeParameters.getPulseOnSigMotion(), mDozeParameters.getVibrateOnSigMotion());
+                mDozeParameters.getPulseOnSigMotion(), mDozeParameters.getVibrateOnSigMotion(),
+                DozeLog.PULSE_REASON_SENSOR_SIGMOTION);
         mPickupSensor = new TriggerSensor(Sensor.TYPE_PICK_UP_GESTURE,
-                mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup());
+                mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup(),
+                DozeLog.PULSE_REASON_SENSOR_PICKUP);
         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag);
         mWakeLock.setReferenceCounted(true);
@@ -195,7 +197,7 @@
         mHost.stopDozing();
     }
 
-    private void requestPulse() {
+    private void requestPulse(final int reason) {
         if (mHost != null && mDreaming && !mPulsing) {
             // Let the host know we want to pulse.  Wait for it to be ready, then
             // turn the screen on.  When finished, turn the screen off again.
@@ -204,7 +206,7 @@
             mPulsing = true;
             if (!mDozeParameters.getProxCheckBeforePulse()) {
                 // skip proximity check
-                continuePulsing();
+                continuePulsing(reason);
                 return;
             }
             // perform a proximity check before pulsing
@@ -214,7 +216,8 @@
                 public void onProximityResult(int result) {
                     // avoid pulsing in pockets
                     final boolean isNear = result == RESULT_NEAR;
-                    DozeLog.traceProximityResult(isNear, SystemClock.uptimeMillis() - start);
+                    final long end = SystemClock.uptimeMillis();
+                    DozeLog.traceProximityResult(isNear, end - start, reason);
                     if (isNear) {
                         mPulsing = false;
                         mWakeLock.release();
@@ -222,13 +225,13 @@
                     }
 
                     // not in-pocket, continue pulsing
-                    continuePulsing();
+                    continuePulsing(reason);
                 }
             }.check();
         }
     }
 
-    private void continuePulsing() {
+    private void continuePulsing(int reason) {
         mHost.pulseWhileDozing(new DozeHost.PulseCallback() {
             @Override
             public void onPulseStarted() {
@@ -245,7 +248,7 @@
                 }
                 mWakeLock.release(); // needs to be unconditional to balance acquire
             }
-        });
+        }, reason);
     }
 
     private void turnDisplayOff() {
@@ -380,13 +383,13 @@
         public void onReceive(Context context, Intent intent) {
             if (PULSE_ACTION.equals(intent.getAction())) {
                 if (DEBUG) Log.d(mTag, "Received pulse intent");
-                requestPulse();
+                requestPulse(DozeLog.PULSE_REASON_INTENT);
             }
             if (NOTIFICATION_PULSE_ACTION.equals(intent.getAction())) {
                 final long instance = intent.getLongExtra(EXTRA_INSTANCE, -1);
                 if (DEBUG) Log.d(mTag, "Received notification pulse intent instance=" + instance);
                 DozeLog.traceNotificationPulse(instance);
-                requestPulse();
+                requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
                 rescheduleNotificationPulse(mNotificationLightOn);
             }
             if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
@@ -434,15 +437,17 @@
         private final Sensor mSensor;
         private final boolean mConfigured;
         private final boolean mDebugVibrate;
+        private final int mPulseReason;
 
         private boolean mRequested;
         private boolean mRegistered;
         private boolean mDisabled;
 
-        public TriggerSensor(int type, boolean configured, boolean debugVibrate) {
+        public TriggerSensor(int type, boolean configured, boolean debugVibrate, int pulseReason) {
             mSensor = mSensors.getDefaultSensor(type);
             mConfigured = configured;
             mDebugVibrate = debugVibrate;
+            mPulseReason = pulseReason;
         }
 
         public void setListening(boolean listen) {
@@ -494,7 +499,7 @@
                     }
                 }
 
-                requestPulse();
+                requestPulse(mPulseReason);
                 mRegistered = false;
                 updateListener();  // reregister, this sensor only fires once
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
index c0d7b9b..022e64e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
@@ -47,6 +47,7 @@
 
     private boolean mDozing;
     private DozeHost.PulseCallback mPulseCallback;
+    private int mPulseReason;
     private Animator mInFrontAnimator;
     private Animator mBehindAnimator;
     private float mInFrontTarget;
@@ -82,7 +83,7 @@
     }
 
     /** When dozing, fade screen contents in and out using the front scrim. */
-    public void pulse(@NonNull DozeHost.PulseCallback callback) {
+    public void pulse(@NonNull DozeHost.PulseCallback callback, int reason) {
         if (callback == null) {
             throw new IllegalArgumentException("callback must not be null");
         }
@@ -96,6 +97,7 @@
         // Begin pulse.  Note that it's very important that the pulse finished callback
         // be invoked when we're done so that the caller can drop the pulse wakelock.
         mPulseCallback = callback;
+        mPulseReason = reason;
         mHandler.post(mPulseIn);
     }
 
@@ -219,7 +221,7 @@
         public void run() {
             if (DEBUG) Log.d(TAG, "Pulse in, mDozing=" + mDozing);
             if (!mDozing) return;
-            DozeLog.tracePulseStart();
+            DozeLog.tracePulseStart(mPulseReason);
             startScrimAnimation(true /* inFront */, 0f, mDozeParameters.getPulseInDuration(),
                     mPulseInInterpolator, mDozeParameters.getPulseInDelay(), mPulseInFinished);
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index 54eb18c..6fecc0f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -4183,8 +4183,8 @@
         }
 
         @Override
-        public void pulseWhileDozing(@NonNull PulseCallback callback) {
-            mHandler.obtainMessage(H.MSG_PULSE_WHILE_DOZING, callback).sendToTarget();
+        public void pulseWhileDozing(@NonNull PulseCallback callback, int reason) {
+            mHandler.obtainMessage(H.MSG_PULSE_WHILE_DOZING, reason, 0, callback).sendToTarget();
         }
 
         @Override
@@ -4206,8 +4206,8 @@
             ready.run();
         }
 
-        private void handlePulseWhileDozing(@NonNull PulseCallback callback) {
-            mDozeScrimController.pulse(callback);
+        private void handlePulseWhileDozing(@NonNull PulseCallback callback, int reason) {
+            mDozeScrimController.pulse(callback, reason);
         }
 
         private void handleStopDozing() {
@@ -4230,7 +4230,7 @@
                         handleStartDozing((Runnable) msg.obj);
                         break;
                     case MSG_PULSE_WHILE_DOZING:
-                        handlePulseWhileDozing((PulseCallback) msg.obj);
+                        handlePulseWhileDozing((PulseCallback) msg.obj, msg.arg1);
                         break;
                     case MSG_STOP_DOZING:
                         handleStopDozing();