Clean up user activity handling

- Only call userActivity in down touch event
- Use normal timeout when QS is open
- Clean up old code regarding user activity in Keyguard

Change-Id: I968eeed33fef44def37b68eb1ddc63f1a531ab35
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
index 85f58881..0681ff4 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
@@ -29,7 +29,6 @@
 import android.content.IntentFilter;
 import android.media.AudioManager;
 import android.media.SoundPool;
-import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
@@ -45,8 +44,8 @@
 import android.util.EventLog;
 import android.util.Log;
 import android.util.Slog;
-import android.view.ViewGroup;
 import android.view.IWindowManager;
+import android.view.ViewGroup;
 import android.view.WindowManagerGlobal;
 import android.view.WindowManagerPolicy;
 
@@ -65,8 +64,6 @@
 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
 import com.android.systemui.statusbar.phone.StatusBarWindowManager;
 
-import java.io.File;
-
 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
 
 
@@ -406,10 +403,6 @@
             KeyguardViewMediator.this.userActivity();
         }
 
-        public void userActivity(long holdMs) {
-            KeyguardViewMediator.this.userActivity(holdMs);
-        }
-
         public void keyguardDone(boolean authenticated) {
             KeyguardViewMediator.this.keyguardDone(authenticated, true);
         }
@@ -439,13 +432,7 @@
         }
     };
 
-    private void userActivity() {
-        userActivity(AWAKE_INTERVAL_DEFAULT_MS);
-    }
-
-    public void userActivity(long holdMs) {
-        // We ignore the hold time.  Eventually we should remove it.
-        // Instead, the keyguard window has an explicit user activity timeout set on it.
+    public void userActivity() {
         mPM.userActivity(SystemClock.uptimeMillis(), false);
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardPageSwipeHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardPageSwipeHelper.java
index e312d58..d5f9619 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardPageSwipeHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardPageSwipeHelper.java
@@ -59,7 +59,6 @@
     private int mMinTranslationAmount;
     private int mMinFlingVelocity;
     private int mHintDistance;
-    private PowerManager mPowerManager;
     private final View mLeftIcon;
     private final View mCenterIcon;
     private final View mRightIcon;
@@ -77,7 +76,6 @@
         updateIcon(mLeftIcon, 1.0f, SWIPE_RESTING_ALPHA_AMOUNT, false);
         updateIcon(mCenterIcon, 1.0f, SWIPE_RESTING_ALPHA_AMOUNT, false);
         updateIcon(mRightIcon, 1.0f, SWIPE_RESTING_ALPHA_AMOUNT, false);
-        mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         initDimens();
     }
 
@@ -145,7 +143,6 @@
                 }
                 if (mSwipingInProgress) {
                     setTranslation(mTranslationOnDown + x - mInitialTouchX, false);
-                    onUserActivity(event.getEventTime());
                 }
                 break;
 
@@ -254,10 +251,6 @@
         }
     }
 
-    private void onUserActivity(long when) {
-        mPowerManager.userActivity(when, false);
-    }
-
     private void cancelAnimations() {
         ArrayList<View> targetViews = mCallback.getTranslationViews();
         for (View target : targetViews) {
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 0c6ea50..82e082b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -618,6 +618,7 @@
             updateQsState();
             requestPanelHeightUpdate();
             mNotificationStackScroller.setInterceptDelegateEnabled(expanded);
+            mStatusBar.setQsExpanded(expanded);
         }
     }
 
@@ -657,7 +658,6 @@
         setQsTranslation(height);
         requestScrollerTopPaddingUpdate(false /* animate */);
         updateNotificationScrim(height);
-        mStatusBar.userActivity();
     }
 
     private void updateNotificationScrim(float height) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
index 2c5ece6..c962dde 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
@@ -368,6 +368,7 @@
 
         switch (event.getActionMasked()) {
             case MotionEvent.ACTION_DOWN:
+                mStatusBar.userActivity();
                 if (mHeightAnimator != null && !mHintAnimationRunning ||
                         mPeekPending || mPeekAnimator != null) {
                     if (mHeightAnimator != null) {
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 62e2734..4c34eb1 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -61,6 +61,7 @@
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.SystemClock;
+import android.os.Trace;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
@@ -377,14 +378,6 @@
     private Interpolator mAlphaIn = new PathInterpolator(0f, 0.2f, 1f, 1f);
     private Interpolator mAlphaOut = new PathInterpolator(0f, 0f, 0.8f, 1f);
 
-    private final OnChildLocationsChangedListener mOnChildLocationsChangedListener =
-            new OnChildLocationsChangedListener() {
-        @Override
-        public void onChildLocationsChanged(NotificationStackScrollLayout stackScrollLayout) {
-            userActivity();
-        }
-    };
-
     private int mDisabledUnmodified;
 
     /** Keys of notifications currently visible to the user. */
@@ -608,7 +601,6 @@
         mStackScroller = (NotificationStackScrollLayout) mStatusBarWindow.findViewById(
                 R.id.notification_stack_scroller);
         mStackScroller.setLongPressListener(getNotificationLongClicker());
-        mStackScroller.setChildLocationsChangedListener(mOnChildLocationsChangedListener);
 
         mKeyguardIconOverflowContainer =
                 (NotificationOverflowContainer) LayoutInflater.from(mContext).inflate(
@@ -1553,6 +1545,10 @@
         return mScrimController;
     }
 
+    public void setQsExpanded(boolean expanded) {
+        mStatusBarWindowManager.setQsExpanded(expanded);
+    }
+
     /**
      * All changes to the status bar and notifications funnel through here and are batched.
      */
@@ -2886,8 +2882,9 @@
     }
 
     public void userActivity() {
-        mHandler.removeCallbacks(mUserActivity);
-        mHandler.post(mUserActivity);
+        if (mState == StatusBarState.KEYGUARD) {
+            mKeyguardViewMediatorCallback.userActivity();
+        }
     }
 
     public boolean interceptMediaKey(KeyEvent event) {
@@ -2928,7 +2925,6 @@
 
     @Override
     public void onActivated(ActivatableNotificationView view) {
-        userActivity();
         mKeyguardIndicationController.showTransientIndication(R.string.notification_tap_again);
         ActivatableNotificationView previousView = mStackScroller.getActivatedChild();
         if (previousView != null) {
@@ -3094,15 +3090,6 @@
         }
     }
 
-    private final Runnable mUserActivity = new Runnable() {
-        @Override
-        public void run() {
-            if (mState == StatusBarState.KEYGUARD) {
-                mKeyguardViewMediatorCallback.userActivity();
-            }
-        }
-    };
-
     // Recents
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
index 103a582..6411fb8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
@@ -173,6 +173,5 @@
         super.panelExpansionChanged(panel, frac, expanded);
         mScrimController.setPanelExpansion(frac);
         mBar.updateCarrierLabelVisibility(false);
-        mBar.userActivity();
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
index fe57cef..6cb0f95 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
@@ -139,7 +139,8 @@
 
     private void applyUserActivityTimeout(State state) {
         if (state.isKeyguardShowingAndNotOccluded()
-                && state.statusBarState == StatusBarState.KEYGUARD) {
+                && state.statusBarState == StatusBarState.KEYGUARD
+                && !state.qsExpanded) {
             mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
         } else {
             mLp.userActivityTimeout = -1;
@@ -148,7 +149,8 @@
 
     private void applyInputFeatures(State state) {
         if (state.isKeyguardShowingAndNotOccluded()
-                && state.statusBarState == StatusBarState.KEYGUARD) {
+                && state.statusBarState == StatusBarState.KEYGUARD
+                && !state.qsExpanded) {
             mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
         } else {
             mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
@@ -207,6 +209,11 @@
         apply(mCurrentState);
     }
 
+    public void setQsExpanded(boolean expanded) {
+        mCurrentState.qsExpanded = expanded;
+        apply(mCurrentState);
+    }
+
     /**
      * @param state The {@link StatusBarState} of the status bar.
      */
@@ -224,6 +231,7 @@
         long keyguardUserActivityTimeout;
         boolean bouncerShowing;
         boolean keyguardFadingAway;
+        boolean qsExpanded;
 
         /**
          * The {@link BaseStatusBar} state from the status bar.