Home | History | Annotate | Download | only in deviceinfo
      1 /*
      2  * Copyright (C) 2010 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.settings.deviceinfo;
     18 
     19 import com.android.settings.R;
     20 
     21 import android.content.Context;
     22 import android.content.res.TypedArray;
     23 import android.graphics.Canvas;
     24 import android.graphics.Color;
     25 import android.graphics.Paint;
     26 import android.util.AttributeSet;
     27 import android.view.View;
     28 
     29 import java.util.Collection;
     30 
     31 /**
     32  *
     33  */
     34 public class PercentageBarChart extends View {
     35     private final Paint mEmptyPaint = new Paint();
     36 
     37     private Collection<Entry> mEntries;
     38 
     39     private int mMinTickWidth = 1;
     40 
     41     public static class Entry {
     42         public final float percentage;
     43         public final Paint paint;
     44 
     45         protected Entry(float percentage, Paint paint) {
     46             this.percentage = percentage;
     47             this.paint = paint;
     48         }
     49     }
     50 
     51     public PercentageBarChart(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53 
     54         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PercentageBarChart);
     55         mMinTickWidth = a.getDimensionPixelSize(R.styleable.PercentageBarChart_minTickWidth, 1);
     56         int emptyColor = a.getColor(R.styleable.PercentageBarChart_emptyColor, Color.BLACK);
     57         a.recycle();
     58 
     59         mEmptyPaint.setColor(emptyColor);
     60         mEmptyPaint.setStyle(Paint.Style.FILL);
     61     }
     62 
     63     @Override
     64     protected void onDraw(Canvas canvas) {
     65         super.onDraw(canvas);
     66 
     67         final int left = getPaddingLeft();
     68         final int right = getWidth() - getPaddingRight();
     69         final int top = getPaddingTop();
     70         final int bottom = getHeight() - getPaddingBottom();
     71 
     72         final int width = right - left;
     73 
     74         float lastX = left;
     75 
     76         if (mEntries != null) {
     77             for (final Entry e : mEntries) {
     78                 final float entryWidth;
     79                 if (e.percentage == 0.0f) {
     80                     entryWidth = 0.0f;
     81                 } else {
     82                     entryWidth = Math.max(mMinTickWidth, width * e.percentage);
     83                 }
     84 
     85                 final float nextX = lastX + entryWidth;
     86                 if (nextX > right) {
     87                     canvas.drawRect(lastX, top, right, bottom, e.paint);
     88                     return;
     89                 }
     90 
     91                 canvas.drawRect(lastX, top, nextX, bottom, e.paint);
     92                 lastX = nextX;
     93             }
     94         }
     95 
     96         canvas.drawRect(lastX, top, right, bottom, mEmptyPaint);
     97     }
     98 
     99     /**
    100      * Sets the background for this chart. Callers are responsible for later
    101      * calling {@link #invalidate()}.
    102      */
    103     @Override
    104     public void setBackgroundColor(int color) {
    105         mEmptyPaint.setColor(color);
    106     }
    107 
    108     /**
    109      * Adds a new slice to the percentage bar chart. Callers are responsible for
    110      * later calling {@link #invalidate()}.
    111      *
    112      * @param percentage the total width that
    113      * @param color the color to draw the entry
    114      */
    115     public static Entry createEntry(float percentage, int color) {
    116         final Paint p = new Paint();
    117         p.setColor(color);
    118         p.setStyle(Paint.Style.FILL);
    119 
    120         return new Entry(percentage, p);
    121     }
    122 
    123     public void setEntries(Collection<Entry> entries) {
    124         mEntries = entries;
    125     }
    126 }
    127