Only doze when not occluded

This fixes a bug where an activity could have been started on top of
the lockscreen while dozing already. In that case, we would execute a
pulse and reveal the fully colored activity that is occluding the
Keyguard

Bug: 19465785
Change-Id: I9533390e13f11424a20a107005d60050a460e333
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
index 1f3a830..3f72125 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
@@ -82,11 +82,9 @@
         sNotificationPulseStats.append();
     }
 
-    public static void traceDozing(Context context, boolean dozing) {
-        if (!ENABLED) return;
-        sPulsing = false;
+    private static void init(Context context) {
         synchronized (DozeLog.class) {
-            if (dozing && sMessages == null) {
+            if (sMessages == null) {
                 sTimes = new long[SIZE];
                 sMessages = new String[SIZE];
                 sSince = System.currentTimeMillis();
@@ -105,6 +103,12 @@
                 KeyguardUpdateMonitor.getInstance(context).registerCallback(sKeyguardCallback);
             }
         }
+    }
+
+    public static void traceDozing(Context context, boolean dozing) {
+        if (!ENABLED) return;
+        sPulsing = false;
+        init(context);
         log("dozing " + dozing);
     }
 
@@ -146,10 +150,12 @@
         }
     }
 
-    public static void traceProximityResult(boolean near, long millis, int pulseReason) {
+    public static void traceProximityResult(Context context, boolean near, long millis,
+            int pulseReason) {
         if (!ENABLED) return;
         log("proximityResult reason=" + pulseReasonToString(pulseReason) + " near=" + near
                 + " millis=" + millis);
+        init(context);
         sProxStats[pulseReason][near ? 0 : 1].append();
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
index 8d27450..5d46712 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
@@ -235,7 +235,7 @@
                 public void onProximityResult(int result) {
                     final boolean isNear = result == RESULT_NEAR;
                     final long end = SystemClock.uptimeMillis();
-                    DozeLog.traceProximityResult(isNear, end - start, reason);
+                    DozeLog.traceProximityResult(mContext, isNear, end - start, reason);
                     if (nonBlocking) {
                         // we already continued
                         return;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
index 39dc480..8c2c543 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -2112,7 +2112,9 @@
     public void setDozing(boolean dozing, boolean animate) {
         if (dozing == mDozing) return;
         mDozing = dozing;
-        updateDozingVisibilities(animate);
+        if (mStatusBarState == StatusBarState.KEYGUARD) {
+            updateDozingVisibilities(animate);
+        }
     }
 
     private void updateDozingVisibilities(boolean animate) {
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 f1e51a52..516174b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -32,7 +32,6 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.IPackageManager;
-import android.content.pm.PackageManager;
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.database.ContentObserver;
@@ -412,6 +411,7 @@
 
     private boolean mWaitingForKeyguardExit;
     private boolean mDozing;
+    private boolean mDozingRequested;
     private boolean mScrimSrcModeEnabled;
 
     private Interpolator mLinearInterpolator = new LinearInterpolator();
@@ -3478,9 +3478,6 @@
     }
 
     private void updateDozingState() {
-        if (mState != StatusBarState.KEYGUARD && !mNotificationPanel.isDozing()) {
-            return;
-        }
         boolean animate = !mDozing && mDozeScrimController.isPulsing();
         mNotificationPanel.setDozing(mDozing, animate);
         mStackScroller.setDark(mDozing, animate, mScreenOnTouchLocation);
@@ -3597,6 +3594,7 @@
         mState = state;
         mGroupManager.setStatusBarState(state);
         mStatusBarWindowManager.setStatusBarState(state);
+        updateDozing();
     }
 
     @Override
@@ -3912,6 +3910,11 @@
         }
     }
 
+    private void updateDozing() {
+        mDozing = mDozingRequested && mState == StatusBarState.KEYGUARD;
+        updateDozingState();
+    }
+
     private final class ShadeUpdates {
         private final ArraySet<String> mVisibleNotifications = new ArraySet<String>();
         private final ArraySet<String> mNewVisibleNotifications = new ArraySet<String>();
@@ -4017,10 +4020,10 @@
         }
 
         private void handleStartDozing(@NonNull Runnable ready) {
-            if (!mDozing) {
-                mDozing = true;
+            if (!mDozingRequested) {
+                mDozingRequested = true;
                 DozeLog.traceDozing(mContext, mDozing);
-                updateDozingState();
+                updateDozing();
             }
             ready.run();
         }
@@ -4030,10 +4033,10 @@
         }
 
         private void handleStopDozing() {
-            if (mDozing) {
-                mDozing = false;
+            if (mDozingRequested) {
+                mDozingRequested = false;
                 DozeLog.traceDozing(mContext, mDozing);
-                updateDozingState();
+                updateDozing();
             }
         }