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 17 package com.android.systemui.qs; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.Canvas; 22 import android.graphics.Paint; 23 import android.graphics.RectF; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 import com.android.settingslib.Utils; 28 import com.android.systemui.R; 29 30 public class DataUsageGraph extends View { 31 32 private final int mTrackColor; 33 private final int mUsageColor; 34 private final int mOverlimitColor; 35 private final int mWarningColor; 36 private final int mMarkerWidth; 37 private final RectF mTmpRect = new RectF(); 38 private final Paint mTmpPaint = new Paint(); 39 40 private long mLimitLevel; 41 private long mWarningLevel; 42 private long mUsageLevel; 43 private long mMaxLevel; 44 DataUsageGraph(Context context, AttributeSet attrs)45 public DataUsageGraph(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 final Resources res = context.getResources(); 48 mTrackColor = context.getColor(R.color.data_usage_graph_track); 49 mUsageColor = Utils.getColorAccent(context); 50 mOverlimitColor = context.getColor(R.color.system_warning_color); 51 mWarningColor = context.getColor(R.color.data_usage_graph_warning); 52 mMarkerWidth = res.getDimensionPixelSize(R.dimen.data_usage_graph_marker_width); 53 } 54 setLevels(long limitLevel, long warningLevel, long usageLevel)55 public void setLevels(long limitLevel, long warningLevel, long usageLevel) { 56 mLimitLevel = Math.max(0, limitLevel); 57 mWarningLevel = Math.max(0, warningLevel); 58 mUsageLevel = Math.max(0, usageLevel); 59 mMaxLevel = Math.max(Math.max(Math.max(mLimitLevel, mWarningLevel), mUsageLevel), 1); 60 postInvalidate(); 61 } 62 63 @Override onDraw(Canvas canvas)64 protected void onDraw(Canvas canvas) { 65 super.onDraw(canvas); 66 67 final RectF r = mTmpRect; 68 final Paint p = mTmpPaint; 69 final int w = getWidth(); 70 final int h = getHeight(); 71 72 final boolean overLimit = mLimitLevel > 0 && mUsageLevel > mLimitLevel; 73 float usageRight = w * (mUsageLevel / (float) mMaxLevel); 74 if (overLimit) { 75 // compute the gap 76 usageRight = w * (mLimitLevel / (float) mMaxLevel) - (mMarkerWidth / 2); 77 usageRight = Math.min(Math.max(usageRight, mMarkerWidth), w - mMarkerWidth * 2); 78 79 // draw overlimit 80 r.set(usageRight + mMarkerWidth, 0, w, h); 81 p.setColor(mOverlimitColor); 82 canvas.drawRect(r, p); 83 } else { 84 // draw track 85 r.set(0, 0, w, h); 86 p.setColor(mTrackColor); 87 canvas.drawRect(r, p); 88 } 89 90 // draw usage 91 r.set(0, 0, usageRight, h); 92 p.setColor(mUsageColor); 93 canvas.drawRect(r, p); 94 95 // draw warning marker 96 float warningLeft = w * (mWarningLevel / (float) mMaxLevel) - mMarkerWidth / 2; 97 warningLeft = Math.min(Math.max(warningLeft, 0), w - mMarkerWidth); 98 r.set(warningLeft, 0, warningLeft + mMarkerWidth, h); 99 p.setColor(mWarningColor); 100 canvas.drawRect(r, p); 101 } 102 } 103