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:
@@ -61,6 +61,9 @@ public class ChartSweepView extends FrameLayout {
|
|||||||
private ChartAxis mAxis;
|
private ChartAxis mAxis;
|
||||||
private long mValue;
|
private long mValue;
|
||||||
|
|
||||||
|
private ChartSweepView mClampAfter;
|
||||||
|
private ChartSweepView mClampBefore;
|
||||||
|
|
||||||
public static final int HORIZONTAL = 0;
|
public static final int HORIZONTAL = 0;
|
||||||
public static final int VERTICAL = 1;
|
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
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
if (!isEnabled()) return false;
|
if (!isEnabled()) return false;
|
||||||
@@ -286,13 +297,14 @@ public class ChartSweepView extends FrameLayout {
|
|||||||
final Rect parentContent = new Rect(parent.getPaddingLeft(), parent.getPaddingTop(),
|
final Rect parentContent = new Rect(parent.getPaddingLeft(), parent.getPaddingTop(),
|
||||||
parent.getWidth() - parent.getPaddingRight(),
|
parent.getWidth() - parent.getPaddingRight(),
|
||||||
parent.getHeight() - parent.getPaddingBottom());
|
parent.getHeight() - parent.getPaddingBottom());
|
||||||
|
final Rect clampRect = computeClampRect(parentContent);
|
||||||
|
|
||||||
if (mFollowAxis == VERTICAL) {
|
if (mFollowAxis == VERTICAL) {
|
||||||
final float currentTargetY = getTop() - mMargins.top;
|
final float currentTargetY = getTop() - mMargins.top;
|
||||||
final float requestedTargetY = currentTargetY
|
final float requestedTargetY = currentTargetY
|
||||||
+ (event.getRawY() - mTracking.getRawY());
|
+ (event.getRawY() - mTracking.getRawY());
|
||||||
final float clampedTargetY = MathUtils.constrain(
|
final float clampedTargetY = MathUtils.constrain(
|
||||||
requestedTargetY, parentContent.top, parentContent.bottom);
|
requestedTargetY, clampRect.top, clampRect.bottom);
|
||||||
setTranslationY(clampedTargetY - currentTargetY);
|
setTranslationY(clampedTargetY - currentTargetY);
|
||||||
|
|
||||||
setValue(mAxis.convertToValue(clampedTargetY - parentContent.top));
|
setValue(mAxis.convertToValue(clampedTargetY - parentContent.top));
|
||||||
@@ -301,7 +313,7 @@ public class ChartSweepView extends FrameLayout {
|
|||||||
final float requestedTargetX = currentTargetX
|
final float requestedTargetX = currentTargetX
|
||||||
+ (event.getRawX() - mTracking.getRawX());
|
+ (event.getRawX() - mTracking.getRawX());
|
||||||
final float clampedTargetX = MathUtils.constrain(
|
final float clampedTargetX = MathUtils.constrain(
|
||||||
requestedTargetX, parentContent.left, parentContent.right);
|
requestedTargetX, clampRect.left, clampRect.right);
|
||||||
setTranslationX(clampedTargetX - currentTargetX);
|
setTranslationX(clampedTargetX - currentTargetX);
|
||||||
|
|
||||||
setValue(mAxis.convertToValue(clampedTargetX - parentContent.left));
|
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
|
@Override
|
||||||
protected void drawableStateChanged() {
|
protected void drawableStateChanged() {
|
||||||
super.drawableStateChanged();
|
super.drawableStateChanged();
|
||||||
|
@@ -87,6 +87,12 @@ public class DataUsageChartView extends ChartView {
|
|||||||
mSweepLimit = (ChartSweepView) findViewById(R.id.sweep_limit);
|
mSweepLimit = (ChartSweepView) findViewById(R.id.sweep_limit);
|
||||||
mSweepWarning = (ChartSweepView) findViewById(R.id.sweep_warning);
|
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);
|
mSweepLeft.addOnSweepListener(mSweepListener);
|
||||||
mSweepRight.addOnSweepListener(mSweepListener);
|
mSweepRight.addOnSweepListener(mSweepListener);
|
||||||
mSweepWarning.addOnSweepListener(mWarningListener);
|
mSweepWarning.addOnSweepListener(mWarningListener);
|
||||||
|
Reference in New Issue
Block a user