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