Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0193a871 authored by Jason Monk's avatar Jason Monk
Browse files

Support updates to the data usage graph

Also make the graph a little bit more smooth.

Bug: 27278313
Change-Id: Ia9ff8a69691671c57cfaf6eb43e373a8386469ea
parent be3696d4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@
    <LinearLayout
        android:id="@+id/graph_label_group"
        android:layout_width="match_parent"
        android:layout_height="@dimen/usage_graph_area_height"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:clipChildren="false"
        android:clipToPadding="false">
@@ -37,6 +38,7 @@
                layout="@layout/usage_side_label" />

            <Space
                android:id="@+id/space1"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1" />
@@ -45,6 +47,7 @@
                layout="@layout/usage_side_label" />

            <Space
                android:id="@+id/space2"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1" />
+37 −9
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ public class UsageGraph extends View {
    private final Paint mDottedPaint;

    private final Drawable mDivider;
    private final Drawable mTintedDivider;
    private final int mDividerSize;

    private final Path mPath = new Path();
@@ -51,6 +52,7 @@ public class UsageGraph extends View {
    private final SparseIntArray mPaths = new SparseIntArray();
    // Paths in local coordinates for drawing.
    private final SparseIntArray mLocalPaths = new SparseIntArray();
    private final int mCornerRadius;

    private int mAccentColor;
    private boolean mShowProjection;
@@ -59,6 +61,10 @@ public class UsageGraph extends View {
    private float mMaxX = 100;
    private float mMaxY = 100;

    private float mMiddleDividerLoc = .5f;
    private int mMiddleDividerTint = -1;
    private int mTopDividerTint = -1;

    public UsageGraph(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        final Resources resources = context.getResources();
@@ -68,8 +74,8 @@ public class UsageGraph extends View {
        mLinePaint.setStrokeCap(Cap.ROUND);
        mLinePaint.setStrokeJoin(Join.ROUND);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setPathEffect(new CornerPathEffect(resources.getDimensionPixelSize(
                R.dimen.usage_graph_line_corner_radius)));
        mCornerRadius = resources.getDimensionPixelSize(R.dimen.usage_graph_line_corner_radius);
        mLinePaint.setPathEffect(new CornerPathEffect(mCornerRadius));
        mLinePaint.setStrokeWidth(resources.getDimensionPixelSize(R.dimen.usage_graph_line_width));

        mFillPaint = new Paint(mLinePaint);
@@ -86,6 +92,7 @@ public class UsageGraph extends View {
        TypedValue v = new TypedValue();
        context.getTheme().resolveAttribute(com.android.internal.R.attr.listDivider, v, true);
        mDivider = context.getDrawable(v.resourceId);
        mTintedDivider = context.getDrawable(v.resourceId);
        mDividerSize = resources.getDimensionPixelSize(R.dimen.usage_graph_divider_size);
    }

@@ -98,6 +105,15 @@ public class UsageGraph extends View {
        mMaxY = maxY;
    }

    void setDividerLoc(int height) {
        mMiddleDividerLoc = 1 - height / mMaxY;
    }

    void setDividerColors(int middleColor, int topColor) {
        mMiddleDividerTint = middleColor;
        mTopDividerTint = topColor;
    }

    public void addPath(SparseIntArray points) {
        for (int i = 0; i < points.size(); i++) {
            mPaths.put(points.keyAt(i), points.valueAt(i));
@@ -150,7 +166,7 @@ public class UsageGraph extends View {
                if (mLocalPaths.size() > 0) {
                    int lastX = mLocalPaths.keyAt(mLocalPaths.size() - 1);
                    int lastY = mLocalPaths.valueAt(mLocalPaths.size() - 1);
                    if (lastY != PATH_DELIM && (lastX == lx || lastY == ly)) {
                    if (lastY != PATH_DELIM && !hasDiff(lastX, lx) && !hasDiff(lastY, ly)) {
                        pendingYLoc = ly;
                        continue;
                    }
@@ -160,6 +176,10 @@ public class UsageGraph extends View {
        }
    }

    private boolean hasDiff(int x1, int x2) {
        return Math.abs(x2 - x1) >= mCornerRadius;
    }

    private int getX(float x) {
        return (int) (x / mMaxX * getWidth());
    }
@@ -180,9 +200,12 @@ public class UsageGraph extends View {
    @Override
    protected void onDraw(Canvas canvas) {
        // Draw lines across the top, middle, and bottom.
        drawDivider(0, canvas);
        drawDivider((canvas.getHeight() - mDividerSize) / 2, canvas);
        drawDivider(canvas.getHeight() - mDividerSize, canvas);
        if (mMiddleDividerLoc != 0) {
            drawDivider(0, canvas, mTopDividerTint);
        }
        drawDivider((int) ((canvas.getHeight() - mDividerSize) * mMiddleDividerLoc), canvas,
                mMiddleDividerTint);
        drawDivider(canvas.getHeight() - mDividerSize, canvas, -1);

        if (mLocalPaths.size() == 0) {
            return;
@@ -242,8 +265,13 @@ public class UsageGraph extends View {
        canvas.drawPath(mPath, mFillPaint);
    }

    private void drawDivider(int y, Canvas canvas) {
        mDivider.setBounds(0, y, canvas.getWidth(), y + mDividerSize);
        mDivider.draw(canvas);
    private void drawDivider(int y, Canvas canvas, int tintColor) {
        Drawable d = mDivider;
        if (tintColor != -1) {
            mTintedDivider.setTint(tintColor);
            d = mTintedDivider;
        }
        d.setBounds(0, y, canvas.getWidth(), y + mDividerSize);
        d.draw(canvas);
    }
}
+21 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
@@ -98,6 +99,26 @@ public class UsageView extends FrameLayout {
        mUsageGraph.setAccentColor(color);
    }

    public void setDividerLoc(int dividerLoc) {
        mUsageGraph.setDividerLoc(dividerLoc);
    }

    public void setDividerColors(int middleColor, int topColor) {
        mUsageGraph.setDividerColors(middleColor, topColor);
    }

    public void setSideLabelWeights(float before, float after) {
        setWeight(R.id.space1, before);
        setWeight(R.id.space2, after);
    }

    private void setWeight(int id, float weight) {
        View v = findViewById(id);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        params.weight = weight;
        v.setLayoutParams(params);
    }

    public void setSideLabels(CharSequence[] labels) {
        if (labels.length != mLabels.length) {
            throw new IllegalArgumentException("Invalid number of labels");
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@
    <com.android.settingslib.graph.UsageView
        android:id="@+id/battery_usage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_height="141dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="24dp"
        systemui:sideLabels="@array/battery_labels"