Prevent data usage chart sweeps from crossing.

Add rules to clamp sweeps, since it doesn't make sense to have a
limit below warning.

Bug: 4598462
Change-Id: I3323c7bca7fabe3e3f1e9c89906c3a921103579c
diff --git a/src/com/android/settings/widget/ChartSweepView.java b/src/com/android/settings/widget/ChartSweepView.java
index b0d00bb..4e37657 100644
--- a/src/com/android/settings/widget/ChartSweepView.java
+++ b/src/com/android/settings/widget/ChartSweepView.java
@@ -61,6 +61,9 @@
     private ChartAxis mAxis;
     private long mValue;
 
+    private ChartSweepView mClampAfter;
+    private ChartSweepView mClampBefore;
+
     public static final int HORIZONTAL = 0;
     public static final int VERTICAL = 1;
 
@@ -256,6 +259,14 @@
         }
     }
 
+    public void setClampAfter(ChartSweepView clampAfter) {
+        mClampAfter = clampAfter;
+    }
+
+    public void setClampBefore(ChartSweepView clampBefore) {
+        mClampBefore = clampBefore;
+    }
+
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         if (!isEnabled()) return false;
@@ -286,13 +297,14 @@
                 final Rect parentContent = new Rect(parent.getPaddingLeft(), parent.getPaddingTop(),
                         parent.getWidth() - parent.getPaddingRight(),
                         parent.getHeight() - parent.getPaddingBottom());
+                final Rect clampRect = computeClampRect(parentContent);
 
                 if (mFollowAxis == VERTICAL) {
                     final float currentTargetY = getTop() - mMargins.top;
                     final float requestedTargetY = currentTargetY
                             + (event.getRawY() - mTracking.getRawY());
                     final float clampedTargetY = MathUtils.constrain(
-                            requestedTargetY, parentContent.top, parentContent.bottom);
+                            requestedTargetY, clampRect.top, clampRect.bottom);
                     setTranslationY(clampedTargetY - currentTargetY);
 
                     setValue(mAxis.convertToValue(clampedTargetY - parentContent.top));
@@ -301,7 +313,7 @@
                     final float requestedTargetX = currentTargetX
                             + (event.getRawX() - mTracking.getRawX());
                     final float clampedTargetX = MathUtils.constrain(
-                            requestedTargetX, parentContent.left, parentContent.right);
+                            requestedTargetX, clampRect.left, clampRect.right);
                     setTranslationX(clampedTargetX - currentTargetX);
 
                     setValue(mAxis.convertToValue(clampedTargetX - parentContent.left));
@@ -324,6 +336,35 @@
         }
     }
 
+    /**
+     * Compute {@link Rect} in {@link #getParent()} coordinates that we should
+     * be clamped inside of, usually from {@link #setClampAfter(ChartSweepView)}
+     * style rules.
+     */
+    private Rect computeClampRect(Rect parentContent) {
+        final Rect clampRect = new Rect(parentContent);
+
+        final ChartSweepView after = mClampAfter;
+        final ChartSweepView before = mClampBefore;
+
+        if (mFollowAxis == VERTICAL) {
+            if (after != null) {
+                clampRect.top += after.getPoint();
+            }
+            if (before != null) {
+                clampRect.bottom -= clampRect.height() - before.getPoint();
+            }
+        } else {
+            if (after != null) {
+                clampRect.left += after.getPoint();
+            }
+            if (before != null) {
+                clampRect.right -= clampRect.width() - before.getPoint();
+            }
+        }
+        return clampRect;
+    }
+
     @Override
     protected void drawableStateChanged() {
         super.drawableStateChanged();
diff --git a/src/com/android/settings/widget/DataUsageChartView.java b/src/com/android/settings/widget/DataUsageChartView.java
index 89caef1..88f0a19 100644
--- a/src/com/android/settings/widget/DataUsageChartView.java
+++ b/src/com/android/settings/widget/DataUsageChartView.java
@@ -87,6 +87,12 @@
         mSweepLimit = (ChartSweepView) findViewById(R.id.sweep_limit);
         mSweepWarning = (ChartSweepView) findViewById(R.id.sweep_warning);
 
+        // prevent sweeps from crossing each other
+        mSweepLeft.setClampBefore(mSweepRight);
+        mSweepRight.setClampAfter(mSweepLeft);
+        mSweepLimit.setClampBefore(mSweepWarning);
+        mSweepWarning.setClampAfter(mSweepLimit);
+
         mSweepLeft.addOnSweepListener(mSweepListener);
         mSweepRight.addOnSweepListener(mSweepListener);
         mSweepWarning.addOnSweepListener(mWarningListener);