More data usage chart iteration, app details.

Moved app details back into single Fragment to support animations and
template tabs.  Show the network in background behind app details
chart series to match designs.

Clamping sweeps at axis boundaries.

Bug: 4813014, 4598460, 4818029
Change-Id: I72c0b21ee1d595e4da31d293ae0dab9e801041f3
diff --git a/src/com/android/settings/widget/ChartNetworkSeriesView.java b/src/com/android/settings/widget/ChartNetworkSeriesView.java
index 0a34565..83c10cd 100644
--- a/src/com/android/settings/widget/ChartNetworkSeriesView.java
+++ b/src/com/android/settings/widget/ChartNetworkSeriesView.java
@@ -75,6 +75,7 @@
                 R.styleable.ChartNetworkSeriesView_fillColorSecondary, Color.RED);
 
         setChartColor(stroke, fill, fillSecondary);
+        setWillNotDraw(false);
 
         a.recycle();
 
@@ -110,8 +111,13 @@
 
         mPathStroke.reset();
         mPathFill.reset();
+        invalidate();
     }
 
+    /**
+     * Set the range to paint with {@link #mPaintFill}, leaving the remaining
+     * area to be painted with {@link #mPaintFillSecondary}.
+     */
     public void setPrimaryRange(long left, long right) {
         mPrimaryLeft = left;
         mPrimaryRight = right;
@@ -190,18 +196,21 @@
     protected void onDraw(Canvas canvas) {
         int save;
 
+        final float primaryLeftPoint = mHoriz.convertToPoint(mPrimaryLeft);
+        final float primaryRightPoint = mHoriz.convertToPoint(mPrimaryRight);
+
         save = canvas.save();
-        canvas.clipRect(0, 0, mPrimaryLeft, getHeight());
+        canvas.clipRect(0, 0, primaryLeftPoint, getHeight());
         canvas.drawPath(mPathFill, mPaintFillSecondary);
         canvas.restoreToCount(save);
 
         save = canvas.save();
-        canvas.clipRect(mPrimaryRight, 0, getWidth(), getHeight());
+        canvas.clipRect(primaryRightPoint, 0, getWidth(), getHeight());
         canvas.drawPath(mPathFill, mPaintFillSecondary);
         canvas.restoreToCount(save);
 
         save = canvas.save();
-        canvas.clipRect(mPrimaryLeft, 0, mPrimaryRight, getHeight());
+        canvas.clipRect(primaryLeftPoint, 0, primaryRightPoint, getHeight());
         canvas.drawPath(mPathFill, mPaintFill);
         canvas.drawPath(mPathStroke, mPaintStroke);
         canvas.restoreToCount(save);
diff --git a/src/com/android/settings/widget/ChartSweepView.java b/src/com/android/settings/widget/ChartSweepView.java
index 881fde4..d8344d5 100644
--- a/src/com/android/settings/widget/ChartSweepView.java
+++ b/src/com/android/settings/widget/ChartSweepView.java
@@ -39,6 +39,8 @@
     // TODO: paint label when requested
 
     private Drawable mSweep;
+    private Rect mSweepMargins = new Rect();
+
     private int mFollowAxis;
     private boolean mShowLabel;
 
@@ -88,8 +90,28 @@
         return mFollowAxis;
     }
 
-    public void getExtraMargins(Rect rect) {
-        mSweep.getPadding(rect);
+    /**
+     * Return margins of {@link #setSweepDrawable(Drawable)}, indicating how the
+     * sweep should be displayed around a content region.
+     */
+    public Rect getSweepMargins() {
+        return mSweepMargins;
+    }
+
+    /**
+     * Return the number of pixels that the "target" area is inset from the
+     * {@link View} edge, along the current {@link #setFollowAxis(int)}.
+     */
+    public float getTargetInset() {
+        if (mFollowAxis == VERTICAL) {
+            final float targetHeight = mSweep.getIntrinsicHeight() - mSweepMargins.top
+                    - mSweepMargins.bottom;
+            return mSweepMargins.top + (targetHeight / 2);
+        } else {
+            final float targetWidth = mSweep.getIntrinsicWidth() - mSweepMargins.left
+                    - mSweepMargins.right;
+            return mSweepMargins.left + (targetWidth / 2);
+        }
     }
 
     public void addOnSweepListener(OnSweepListener listener) {
@@ -115,6 +137,7 @@
             }
             sweep.setVisible(getVisibility() == VISIBLE, false);
             mSweep = sweep;
+            sweep.getPadding(mSweepMargins);
         } else {
             mSweep = null;
         }
@@ -175,33 +198,51 @@
         final View parent = (View) getParent();
         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN: {
-                mTracking = event.copy();
-                return true;
+
+                // only start tracking when in sweet spot
+                final boolean accept;
+                if (mFollowAxis == VERTICAL) {
+                    accept = event.getX() > getWidth() - (mSweepMargins.right * 2);
+                } else {
+                    accept = event.getY() > getHeight() - (mSweepMargins.bottom * 2);
+                }
+
+                if (accept) {
+                    mTracking = event.copy();
+                    return true;
+                } else {
+                    return false;
+                }
             }
             case MotionEvent.ACTION_MOVE: {
                 getParent().requestDisallowInterceptTouchEvent(true);
 
+                final Rect sweepMargins = mSweepMargins;
+
+                // content area of parent
+                final Rect parentContent = new Rect(parent.getPaddingLeft(), parent.getPaddingTop(),
+                        parent.getWidth() - parent.getPaddingRight(),
+                        parent.getHeight() - parent.getPaddingBottom());
+
                 if (mFollowAxis == VERTICAL) {
-                    final float chartHeight = parent.getHeight() - parent.getPaddingTop()
-                            - parent.getPaddingBottom();
-                    final float translationY = MathUtils.constrain(
-                            event.getRawY() - mTracking.getRawY(), -getTop(),
-                            chartHeight - getTop());
-                    setTranslationY(translationY);
-                    final float point = (getTop() + getTranslationY() + (getHeight() / 2))
-                            - parent.getPaddingTop();
-                    mValue = mAxis.convertToValue(point);
+                    final float currentTargetY = getTop() + getTargetInset();
+                    final float requestedTargetY = currentTargetY
+                            + (event.getRawY() - mTracking.getRawY());
+                    final float clampedTargetY = MathUtils.constrain(
+                            requestedTargetY, parentContent.top, parentContent.bottom);
+                    setTranslationY(clampedTargetY - currentTargetY);
+
+                    mValue = mAxis.convertToValue(clampedTargetY - parentContent.top);
                     dispatchOnSweep(false);
                 } else {
-                    final float chartWidth = parent.getWidth() - parent.getPaddingLeft()
-                            - parent.getPaddingRight();
-                    final float translationX = MathUtils.constrain(
-                            event.getRawX() - mTracking.getRawX(), -getLeft(),
-                            chartWidth - getLeft());
-                    setTranslationX(translationX);
-                    final float point = (getLeft() + getTranslationX() + (getWidth() / 2))
-                            - parent.getPaddingLeft();
-                    mValue = mAxis.convertToValue(point);
+                    final float currentTargetX = getLeft() + getTargetInset();
+                    final float requestedTargetX = currentTargetX
+                            + (event.getRawX() - mTracking.getRawX());
+                    final float clampedTargetX = MathUtils.constrain(
+                            requestedTargetX, parentContent.left, parentContent.right);
+                    setTranslationX(clampedTargetX - currentTargetX);
+
+                    mValue = mAxis.convertToValue(clampedTargetX - parentContent.left);
                     dispatchOnSweep(false);
                 }
                 return true;
diff --git a/src/com/android/settings/widget/ChartView.java b/src/com/android/settings/widget/ChartView.java
index d762631..bf5616d 100644
--- a/src/com/android/settings/widget/ChartView.java
+++ b/src/com/android/settings/widget/ChartView.java
@@ -36,6 +36,8 @@
 
     // TODO: extend something that supports two-dimensional scrolling
 
+    private static final int SWEEP_GRAVITY = Gravity.TOP | Gravity.LEFT;
+
     ChartAxis mHoriz;
     ChartAxis mVert;
 
@@ -74,7 +76,6 @@
 
         final Rect parentRect = new Rect();
         final Rect childRect = new Rect();
-        final Rect extraMargins = new Rect();
 
         for (int i = 0; i < getChildCount(); i++) {
             final View child = getChildAt(i);
@@ -91,21 +92,23 @@
             } else if (child instanceof ChartSweepView) {
                 // sweep is always placed along specific dimension
                 final ChartSweepView sweep = (ChartSweepView) child;
+                final Rect sweepMargins = sweep.getSweepMargins();
                 final float point = sweep.getPoint();
-                sweep.getExtraMargins(extraMargins);
 
                 if (sweep.getFollowAxis() == ChartSweepView.HORIZONTAL) {
-                    parentRect.left = parentRect.right = (int) point + getPaddingLeft();
-                    parentRect.top -= extraMargins.top;
-                    parentRect.bottom += extraMargins.bottom;
-                    Gravity.apply(params.gravity, child.getMeasuredWidth(), parentRect.height(),
+                    parentRect.left = parentRect.right =
+                            (int) (sweep.getPoint() - sweep.getTargetInset()) + getPaddingLeft();
+                    parentRect.top -= sweepMargins.top;
+                    parentRect.bottom += sweepMargins.bottom;
+                    Gravity.apply(SWEEP_GRAVITY, child.getMeasuredWidth(), parentRect.height(),
                             parentRect, childRect);
 
                 } else {
-                    parentRect.top = parentRect.bottom = (int) point + getPaddingTop();
-                    parentRect.left -= extraMargins.left;
-                    parentRect.right += extraMargins.right;
-                    Gravity.apply(params.gravity, parentRect.width(), child.getMeasuredHeight(),
+                    parentRect.top = parentRect.bottom =
+                            (int) (sweep.getPoint() - sweep.getTargetInset()) + getPaddingTop();
+                    parentRect.left -= sweepMargins.left;
+                    parentRect.right += sweepMargins.right;
+                    Gravity.apply(SWEEP_GRAVITY, parentRect.width(), child.getMeasuredHeight(),
                             parentRect, childRect);
                 }
             }
diff --git a/src/com/android/settings/widget/DataUsageChartView.java b/src/com/android/settings/widget/DataUsageChartView.java
index 1c76291..6fe4042 100644
--- a/src/com/android/settings/widget/DataUsageChartView.java
+++ b/src/com/android/settings/widget/DataUsageChartView.java
@@ -38,10 +38,10 @@
     private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
 
     // TODO: enforce that sweeps cant cross each other
-    // TODO: limit sweeps at graph boundaries
 
     private ChartGridView mGrid;
     private ChartNetworkSeriesView mSeries;
+    private ChartNetworkSeriesView mDetailSeries;
 
     private ChartSweepView mSweepLeft;
     private ChartSweepView mSweepRight;
@@ -75,6 +75,8 @@
 
         mGrid = (ChartGridView) findViewById(R.id.grid);
         mSeries = (ChartNetworkSeriesView) findViewById(R.id.series);
+        mDetailSeries = (ChartNetworkSeriesView) findViewById(R.id.detail_series);
+        mDetailSeries.setVisibility(View.GONE);
 
         mSweepLeft = (ChartSweepView) findViewById(R.id.sweep_left);
         mSweepRight = (ChartSweepView) findViewById(R.id.sweep_right);
@@ -89,6 +91,7 @@
         // tell everyone about our axis
         mGrid.init(mHoriz, mVert);
         mSeries.init(mHoriz, mVert);
+        mDetailSeries.init(mHoriz, mVert);
         mSweepLeft.init(mHoriz);
         mSweepRight.init(mHoriz);
         mSweepWarning.init(mVert);
@@ -97,27 +100,21 @@
         setActivated(false);
     }
 
-    @Override
-    public void setActivated(boolean activated) {
-        super.setActivated(activated);
-
-        mSweepLeft.setEnabled(activated);
-        mSweepRight.setEnabled(activated);
-        mSweepWarning.setEnabled(activated);
-        mSweepLimit.setEnabled(activated);
-    }
-
-    @Deprecated
-    public void setChartColor(int stroke, int fill, int disabled) {
-        mSeries.setChartColor(stroke, fill, disabled);
-    }
-
     public void setListener(DataUsageChartListener listener) {
         mListener = listener;
     }
 
     public void bindNetworkStats(NetworkStatsHistory stats) {
         mSeries.bindNetworkStats(stats);
+        updatePrimaryRange();
+        requestLayout();
+    }
+
+    public void bindDetailNetworkStats(NetworkStatsHistory stats) {
+        mDetailSeries.bindNetworkStats(stats);
+        mDetailSeries.setVisibility(stats != null ? View.VISIBLE : View.GONE);
+        updatePrimaryRange();
+        requestLayout();
     }
 
     public void bindNetworkPolicy(NetworkPolicy policy) {
@@ -146,22 +143,11 @@
         }
 
         requestLayout();
-
-        // TODO: eventually remove this; was to work around lack of sweep clamping
-        if (policy.limitBytes < -1 || policy.limitBytes > 5 * GB_IN_BYTES) {
-            policy.limitBytes = 5 * GB_IN_BYTES;
-            mLimitListener.onSweep(mSweepLimit, true);
-        }
-        if (policy.warningBytes < -1 || policy.warningBytes > 5 * GB_IN_BYTES) {
-            policy.warningBytes = 4 * GB_IN_BYTES;
-            mWarningListener.onSweep(mSweepWarning, true);
-        }
-
     }
 
     private OnSweepListener mSweepListener = new OnSweepListener() {
         public void onSweep(ChartSweepView sweep, boolean sweepDone) {
-            mSeries.setPrimaryRange(mSweepLeft.getValue(), mSweepRight.getValue());
+            updatePrimaryRange();
 
             // update detail list only when done sweeping
             if (sweepDone && mListener != null) {
@@ -236,13 +222,26 @@
 
         mSweepLeft.setValue(sweepMin);
         mSweepRight.setValue(sweepMax);
-        mSeries.setPrimaryRange(sweepMin, sweepMax);
+        updatePrimaryRange();
 
         requestLayout();
         mSeries.generatePath();
         mSeries.invalidate();
     }
 
+    private void updatePrimaryRange() {
+        final long left = mSweepLeft.getValue();
+        final long right = mSweepRight.getValue();
+
+        // prefer showing primary range on detail series, when available
+        if (mDetailSeries.getVisibility() == View.VISIBLE) {
+            mDetailSeries.setPrimaryRange(left, right);
+            mSeries.setPrimaryRange(0, 0);
+        } else {
+            mSeries.setPrimaryRange(left, right);
+        }
+    }
+
     public static class TimeAxis implements ChartAxis {
         private static final long TICK_INTERVAL = DateUtils.DAY_IN_MILLIS * 7;