DataUsageGraph.java revision b98f747c714ff8252471ca0a3295c2cb9ccb4f3a
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.qs;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.RectF;
23import android.util.AttributeSet;
24import android.view.View;
25
26import com.android.systemui.R;
27
28public class DataUsageGraph extends View {
29
30    private final int mBackgroundColor;
31    private final int mUsageColor;
32    private final RectF mTmpRect = new RectF();
33    private final Paint mTmpPaint = new Paint();
34
35    private long mMaxLevel = 1;
36    private long mLimitLevel;
37    private long mWarningLevel;
38    private long mUsageLevel;
39
40    public DataUsageGraph(Context context, AttributeSet attrs) {
41        super(context, attrs);
42        mBackgroundColor = context.getResources().getColor(R.color.data_usage_graph_track);
43        mUsageColor = context.getResources().getColor(R.color.system_accent_color);
44    }
45
46    public void setLevels(long maxLevel, long limitLevel, long warningLevel, long usageLevel) {
47        mMaxLevel = Math.max(maxLevel, 1);
48        mLimitLevel = limitLevel;
49        mWarningLevel = warningLevel;
50        mUsageLevel = usageLevel;
51        postInvalidate();
52    }
53
54    @Override
55    protected void onDraw(Canvas canvas) {
56        super.onDraw(canvas);
57
58        final RectF r = mTmpRect;
59        final Paint p = mTmpPaint;
60        final int w = getWidth();
61        final int h = getHeight();
62
63        // draw background
64        r.set(0, 0, w, h);
65        p.setColor(mBackgroundColor);
66        canvas.drawRect(r, p);
67
68        // draw usage
69        r.set(0, 0, w * mUsageLevel / (float) mMaxLevel, h);
70        p.setColor(mUsageColor);
71        canvas.drawRect(r, p);
72    }
73}
74