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
This commit is contained in:
Jeff Sharkey
2011-07-10 12:23:55 -07:00
parent 9fab0da184
commit 4ec71e79ef
2 changed files with 49 additions and 2 deletions

View File

@@ -61,6 +61,9 @@ public class ChartSweepView extends FrameLayout {
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 class ChartSweepView extends FrameLayout {
}
}
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 @@ public class ChartSweepView extends FrameLayout {
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 @@ public class ChartSweepView extends FrameLayout {
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 @@ public class ChartSweepView extends FrameLayout {
}
}
/**
* 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();

View File

@@ -87,6 +87,12 @@ public class DataUsageChartView extends ChartView {
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);