Add % for battery in expanded QS; make it clickable

Also removes the "xx minutes until full" label in expanded QS.

Bug: 15392950
Bug: 15863237
Change-Id: I41e85a9ffeb3cba31b58769f8faee9147a3b031e
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 c9606ca..bea5d49 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -147,7 +147,7 @@
     protected void onFinishInflate() {
         super.onFinishInflate();
         mHeader = (StatusBarHeaderView) findViewById(R.id.header);
-        mHeader.getBackgroundView().setOnClickListener(this);
+        mHeader.setOnClickListener(this);
         mHeader.setOverlayParent(this);
         mKeyguardStatusView = findViewById(R.id.keyguard_status_view);
         mQsContainer = findViewById(R.id.quick_settings_container);
@@ -484,6 +484,9 @@
             mInitialTouchY = event.getX();
             mInitialTouchX = event.getY();
         }
+        if (mExpandedHeight != 0) {
+            handleQsDown(event);
+        }
         if (mQsTracking || mQsExpanded) {
             onQsTouch(event);
             if (!mConflictingQsExpansionGesture) {
@@ -498,6 +501,17 @@
         return true;
     }
 
+    private void handleQsDown(MotionEvent event) {
+        if (event.getActionMasked() == MotionEvent.ACTION_DOWN
+                && shouldQuickSettingsIntercept(event.getX(), event.getY(), 0)) {
+            mQsTracking = true;
+            onQsExpansionStarted();
+            mInitialHeightOnTouch = mQsExpansionHeight;
+            mInitialTouchY = event.getX();
+            mInitialTouchX = event.getY();
+        }
+    }
+
     @Override
     protected boolean flingExpands(float vel, float vectorVel) {
         boolean expands = super.flingExpands(vel, vectorVel);
@@ -1120,7 +1134,7 @@
 
     @Override
     public void onClick(View v) {
-        if (v == mHeader.getBackgroundView()) {
+        if (v == mHeader) {
             onQsExpansionStarted();
             if (mQsExpanded) {
                 flingSettings(0 /* vel */, false /* expand */);
@@ -1251,4 +1265,9 @@
     public boolean isQsExpanded() {
         return mQsExpanded;
     }
+
+    @Override
+    public boolean shouldDelayChildPressedState() {
+        return true;
+    }
 }
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 a6b2110..6532b71 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -538,15 +538,6 @@
                 R.id.notification_panel);
         mNotificationPanel.setStatusBar(this);
 
-        // make the header non-responsive to clicks
-        mNotificationPanel.findViewById(R.id.header).setOnTouchListener(
-                new View.OnTouchListener() {
-                    @Override
-                    public boolean onTouch(View v, MotionEvent event) {
-                        return true; // e eats everything
-                    }
-                });
-
         if (!ActivityManager.isHighEndGfx()) {
             mStatusBarWindow.setBackground(null);
             mNotificationPanel.setBackground(new FastColorDrawable(context.getResources().getColor(
@@ -700,7 +691,6 @@
 //                    updateCarrierLabelVisibility(false);
         }
 
-        mBatteryController.setStatusBarHeaderView(mHeader);
         mFlashlightController = new FlashlightController(mContext);
 
         // Set up the quick settings tile panel
@@ -720,6 +710,8 @@
         mHeader.setUserInfoController(mUserInfoController);
         mUserInfoController.reloadUserInfo();
 
+        mHeader.setBatteryController(mBatteryController);
+
         PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         mBroadcastReceiver.onReceive(mContext,
                 new Intent(pm.isScreenOn() ? Intent.ACTION_SCREEN_ON : Intent.ACTION_SCREEN_OFF));
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java
index 2119316..70a664f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java
@@ -22,6 +22,7 @@
 import android.content.Intent;
 import android.graphics.Outline;
 import android.graphics.Rect;
+import android.provider.Settings;
 import android.util.AttributeSet;
 import android.view.View;
 import android.view.ViewGroup;
@@ -36,24 +37,20 @@
 import com.android.systemui.qs.QSTile;
 import com.android.systemui.settings.BrightnessController;
 import com.android.systemui.settings.ToggleSlider;
+import com.android.systemui.statusbar.policy.BatteryController;
 import com.android.systemui.statusbar.policy.UserInfoController;
 
 /**
  * The view to manage the header area in the expanded status bar.
  */
-public class StatusBarHeaderView extends RelativeLayout implements View.OnClickListener {
-
-    /**
-     * How much the header expansion gets rubberbanded while expanding the panel.
-     */
-    private static final float EXPANSION_RUBBERBAND_FACTOR = 0.35f;
+public class StatusBarHeaderView extends RelativeLayout implements View.OnClickListener,
+        BatteryController.BatteryStateChangeCallback {
 
     private boolean mExpanded;
     private boolean mListening;
     private boolean mOverscrolled;
     private boolean mKeyguardShowing;
 
-    private View mBackground;
     private ViewGroup mSystemIconsContainer;
     private View mSystemIconsSuperContainer;
     private View mDateTime;
@@ -66,10 +63,9 @@
     private View mBrightnessContainer;
     private View mQsDetailHeader;
     private View mEmergencyCallsOnly;
-    private TextView mChargingInfo;
+    private TextView mBatteryLevel;
 
     private boolean mShowEmergencyCallsOnly;
-    private boolean mShowChargingInfo;
     private boolean mKeyguardUserSwitcherShowing;
 
     private int mCollapsedHeight;
@@ -84,6 +80,7 @@
 
     private ActivityStarter mActivityStarter;
     private BrightnessController mBrightnessController;
+    private BatteryController mBatteryController;
     private QSPanel mQSPanel;
 
     private final Rect mClipBounds = new Rect();
@@ -96,9 +93,9 @@
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
-        mBackground = findViewById(R.id.background);
         mSystemIconsSuperContainer = findViewById(R.id.system_icons_super_container);
         mSystemIconsContainer = (ViewGroup) findViewById(R.id.system_icons_container);
+        mSystemIconsSuperContainer.setOnClickListener(this);
         mDateTime = findViewById(R.id.datetime);
         mKeyguardCarrierText = findViewById(R.id.keyguard_carrier_text);
         mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
@@ -112,7 +109,7 @@
         mQsDetailHeader = findViewById(R.id.qs_detail_header);
         mQsDetailHeader.setAlpha(0);
         mEmergencyCallsOnly = findViewById(R.id.header_emergency_calls_only);
-        mChargingInfo = (TextView) findViewById(R.id.header_charging_info);
+        mBatteryLevel = (TextView) findViewById(R.id.battery_level);
         loadDimens();
         updateVisibilities();
         addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@@ -145,6 +142,10 @@
         mActivityStarter = activityStarter;
     }
 
+    public void setBatteryController(BatteryController batteryController) {
+        mBatteryController = batteryController;
+    }
+
     public int getCollapsedHeight() {
         return mKeyguardShowing ? mKeyguardHeight : mCollapsedHeight;
     }
@@ -159,6 +160,7 @@
         }
         mListening = listening;
         updateBrightnessControllerState();
+        updateBatteryListening();
     }
 
     public void setExpanded(boolean expanded, boolean overscrolled) {
@@ -220,7 +222,11 @@
 
     private void updateVisibilities() {
         boolean onKeyguardAndCollapsed = mKeyguardShowing && !mExpanded;
-        mBackground.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
+        if (onKeyguardAndCollapsed) {
+            setBackground(null);
+        } else {
+            setBackgroundResource(R.drawable.notification_header_bg);
+        }
         mDateTime.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
         mKeyguardCarrierText.setVisibility(onKeyguardAndCollapsed ? View.VISIBLE : View.GONE);
         mDate.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
@@ -235,38 +241,23 @@
         }
         mEmergencyCallsOnly.setVisibility(mExpanded && !mOverscrolled && mShowEmergencyCallsOnly
                 ? VISIBLE : GONE);
-        mChargingInfo.setVisibility(mExpanded && !mOverscrolled && mShowChargingInfo
-                && !mShowEmergencyCallsOnly ? VISIBLE : GONE);
         mMultiUserSwitch.setVisibility(mExpanded || !mKeyguardUserSwitcherShowing
                 ? VISIBLE : GONE);
+        mBatteryLevel.setVisibility(mExpanded && !mOverscrolled ? View.VISIBLE : View.GONE);
     }
 
     private void updateSystemIconsLayoutParams() {
         RelativeLayout.LayoutParams lp = (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
-        boolean systemIconsAboveClock = mExpanded && !mOverscrolled
-                && mShowChargingInfo && !mShowEmergencyCallsOnly;
-        lp.setMarginEnd(0);
-        if (systemIconsAboveClock) {
-            lp.addRule(ALIGN_PARENT_START);
-            lp.removeRule(START_OF);
+        lp.addRule(RelativeLayout.START_OF, mExpanded
+                ? mSettingsButton.getId()
+                : mMultiUserSwitch.getId());
+        lp.removeRule(ALIGN_PARENT_START);
+        if (mMultiUserSwitch.getVisibility() == GONE) {
+            lp.setMarginEnd(mSystemIconsSwitcherHiddenExpandedMargin);
         } else {
-            lp.addRule(RelativeLayout.START_OF, mExpanded
-                    ? mSettingsButton.getId()
-                    : mMultiUserSwitch.getId());
-            lp.removeRule(ALIGN_PARENT_START);
-            if (mMultiUserSwitch.getVisibility() == GONE) {
-                lp.setMarginEnd(mSystemIconsSwitcherHiddenExpandedMargin);
-            }
+            lp.setMarginEnd(0);
         }
         mSystemIconsSuperContainer.setLayoutParams(lp);
-
-        RelativeLayout.LayoutParams clockLp = (LayoutParams) mDateTime.getLayoutParams();
-        if (systemIconsAboveClock) {
-            clockLp.addRule(BELOW, mChargingInfo.getId());
-        } else {
-            clockLp.addRule(BELOW, mEmergencyCallsOnly.getId());
-        }
-        mDateTime.setLayoutParams(clockLp);
     }
 
     private void updateBrightnessControllerState() {
@@ -277,9 +268,24 @@
         }
     }
 
+    private void updateBatteryListening() {
+        if (mListening) {
+            mBatteryController.addStateChangedCallback(this);
+        } else {
+            mBatteryController.removeStateChangedCallback(this);
+        }
+    }
+
+    @Override
+    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
+        mBatteryLevel.setText(getResources().getString(R.string.battery_level_template, level));
+    }
+
     private void updateClickTargets() {
+        setClickable(!mKeyguardShowing || mExpanded);
         mDateTime.setClickable(mExpanded);
         mMultiUserSwitch.setClickable(mExpanded);
+        mSystemIconsSuperContainer.setClickable(mExpanded);
     }
 
     private void updateZTranslation() {
@@ -326,10 +332,6 @@
         setOutline(mOutline);
     }
 
-    public View getBackgroundView() {
-        return mBackground;
-    }
-
     public void attachSystemIcons(LinearLayout systemIcons) {
         mSystemIconsContainer.addView(systemIcons);
         mStatusIcons = systemIcons.findViewById(R.id.statusIcons);
@@ -355,6 +357,7 @@
         updateZTranslation();
         updatePadding();
         updateMultiUserSwitch();
+        updateClickTargets();
     }
 
     public void setUserInfoController(UserInfoController userInfoController) {
@@ -369,6 +372,8 @@
     public void onClick(View v) {
         if (v == mSettingsButton) {
             startSettingsActivity();
+        } else if (v == mSystemIconsSuperContainer) {
+            startBatteryActivity();
         }
     }
 
@@ -376,6 +381,10 @@
         mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
     }
 
+    private void startBatteryActivity() {
+        mActivityStarter.startActivity(new Intent(Intent.ACTION_POWER_USAGE_SUMMARY));
+    }
+
     public void setQSPanel(QSPanel qsp) {
         mQSPanel = qsp;
         if (mQSPanel != null) {
@@ -392,22 +401,9 @@
         mShowEmergencyCallsOnly = show;
         if (mExpanded) {
             updateVisibilities();
-            updateSystemIconsLayoutParams();
         }
     }
 
-    public void setShowChargingInfo(boolean showChargingInfo) {
-        mShowChargingInfo = showChargingInfo;
-        if (mExpanded) {
-            updateVisibilities();
-            updateSystemIconsLayoutParams();
-        }
-    }
-
-    public void setChargingInfo(CharSequence chargingInfo) {
-        mChargingInfo.setText(chargingInfo);
-    }
-
     public void setKeyguardUserSwitcherShowing(boolean showing) {
         // STOPSHIP: NOT CALLED PROPERLY WHEN GOING TO FULL SHADE AND RETURNING!?!
         mKeyguardUserSwitcherShowing = showing;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java
index 4cf66a3..ccfe18d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java
@@ -36,29 +36,18 @@
 public class BatteryController extends BroadcastReceiver {
     private static final String TAG = "StatusBar.BatteryController";
 
-    private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
-            new ArrayList<BatteryStateChangeCallback>();
-
-    private Context mContext;
-    private StatusBarHeaderView mStatusBarHeaderView;
-    private IBatteryStats mBatteryInfo;
+    private ArrayList<BatteryStateChangeCallback> mChangeCallbacks = new ArrayList<>();
 
     private int mLevel;
     private boolean mPluggedIn;
     private boolean mCharging;
     private boolean mCharged;
 
-
     public interface BatteryStateChangeCallback {
         public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging);
     }
 
     public BatteryController(Context context) {
-        mContext = context;
-
-        mBatteryInfo = IBatteryStats.Stub.asInterface(
-                ServiceManager.getService(BatteryStats.SERVICE_NAME));
-
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
         context.registerReceiver(this, filter);
@@ -66,17 +55,19 @@
 
     public void addStateChangedCallback(BatteryStateChangeCallback cb) {
         mChangeCallbacks.add(cb);
+        cb.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
     }
 
-    public void setStatusBarHeaderView(StatusBarHeaderView statusBarHeaderView) {
-        mStatusBarHeaderView = statusBarHeaderView;
-        updateStatusBarHeaderView();
+    public void removeStateChangedCallback(BatteryStateChangeCallback cb) {
+        mChangeCallbacks.remove(cb);
     }
 
     public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
         if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
-            mLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
+            mLevel = (int)(100f
+                    * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
+                    / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
             mPluggedIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
 
             final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
@@ -84,43 +75,9 @@
             mCharged = status == BatteryManager.BATTERY_STATUS_FULL;
             mCharging = mCharged || status == BatteryManager.BATTERY_STATUS_CHARGING;
 
-            updateStatusBarHeaderView();
             for (BatteryStateChangeCallback cb : mChangeCallbacks) {
                 cb.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
             }
         }
     }
-
-    private void updateStatusBarHeaderView() {
-        if (mStatusBarHeaderView != null) {
-            mStatusBarHeaderView.setShowChargingInfo(mPluggedIn);
-            mStatusBarHeaderView.setChargingInfo(computeChargingInfo());
-        }
-    }
-
-    private String computeChargingInfo() {
-        if (!mPluggedIn || !mCharged && !mCharging) {
-            return mContext.getResources().getString(R.string.expanded_header_battery_not_charging);
-        }
-
-        if (mCharged) {
-            return mContext.getResources().getString(R.string.expanded_header_battery_charged);
-        }
-
-        // Try fetching charging time from battery stats.
-        try {
-            long chargingTimeRemaining = mBatteryInfo.computeChargeTimeRemaining();
-            if (chargingTimeRemaining > 0) {
-                String chargingTimeFormatted =
-                        Formatter.formatShortElapsedTime(mContext, chargingTimeRemaining);
-                return mContext.getResources().getString(
-                        R.string.expanded_header_battery_charging_with_time, chargingTimeFormatted);
-            }
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error calling IBatteryStats: ", e);
-        }
-
-        // Fall back to simple charging label.
-        return mContext.getResources().getString(R.string.expanded_header_battery_charging);
-    }
 }