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.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 
     45     public DataUsageGraph(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47         final Resources res = context.getResources();
     48         mTrackColor = Utils.getColorStateListDefaultColor(context,
     49                 R.color.data_usage_graph_track);
     50         mWarningColor = Utils.getColorStateListDefaultColor(context,
     51                 R.color.data_usage_graph_warning);
     52         mUsageColor = Utils.getColorAccentDefaultColor(context);
     53         mOverlimitColor = Utils.getColorErrorDefaultColor(context);
     54         mMarkerWidth = res.getDimensionPixelSize(R.dimen.data_usage_graph_marker_width);
     55     }
     56 
     57     public void setLevels(long limitLevel, long warningLevel, long usageLevel) {
     58         mLimitLevel = Math.max(0, limitLevel);
     59         mWarningLevel = Math.max(0, warningLevel);
     60         mUsageLevel = Math.max(0, usageLevel);
     61         mMaxLevel = Math.max(Math.max(Math.max(mLimitLevel, mWarningLevel), mUsageLevel), 1);
     62         postInvalidate();
     63     }
     64 
     65     @Override
     66     protected void onDraw(Canvas canvas) {
     67         super.onDraw(canvas);
     68 
     69         final RectF r = mTmpRect;
     70         final Paint p = mTmpPaint;
     71         final int w = getWidth();
     72         final int h = getHeight();
     73 
     74         final boolean overLimit = mLimitLevel > 0 && mUsageLevel > mLimitLevel;
     75         float usageRight = w * (mUsageLevel / (float) mMaxLevel);
     76         if (overLimit) {
     77             // compute the gap
     78             usageRight = w * (mLimitLevel / (float) mMaxLevel) - (mMarkerWidth / 2);
     79             usageRight = Math.min(Math.max(usageRight, mMarkerWidth), w - mMarkerWidth * 2);
     80 
     81             // draw overlimit
     82             r.set(usageRight + mMarkerWidth, 0, w, h);
     83             p.setColor(mOverlimitColor);
     84             canvas.drawRect(r, p);
     85         } else {
     86             // draw track
     87             r.set(0, 0, w, h);
     88             p.setColor(mTrackColor);
     89             canvas.drawRect(r, p);
     90         }
     91 
     92         // draw usage
     93         r.set(0, 0, usageRight, h);
     94         p.setColor(mUsageColor);
     95         canvas.drawRect(r, p);
     96 
     97         // draw warning marker
     98         float warningLeft = w * (mWarningLevel / (float) mMaxLevel) - mMarkerWidth / 2;
     99         warningLeft = Math.min(Math.max(warningLeft, 0), w - mMarkerWidth);
    100         r.set(warningLeft, 0, warningLeft + mMarkerWidth, h);
    101         p.setColor(mWarningColor);
    102         canvas.drawRect(r, p);
    103     }
    104 }
    105