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 android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.Paint; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 import com.android.settings.R; 28 29 import java.util.Collection; 30 31 /** 32 * Draws a horizontal bar chart with colored slices, each represented by 33 * {@link Entry}. 34 */ 35 public class PercentageBarChart extends View { 36 private final Paint mEmptyPaint = new Paint(); 37 38 private Collection<Entry> mEntries; 39 40 private int mMinTickWidth = 1; 41 42 public static class Entry implements Comparable<Entry> { 43 public final int order; 44 public final float percentage; 45 public final Paint paint; 46 Entry(int order, float percentage, Paint paint)47 protected Entry(int order, float percentage, Paint paint) { 48 this.order = order; 49 this.percentage = percentage; 50 this.paint = paint; 51 } 52 53 @Override compareTo(Entry another)54 public int compareTo(Entry another) { 55 return order - another.order; 56 } 57 } 58 PercentageBarChart(Context context, AttributeSet attrs)59 public PercentageBarChart(Context context, AttributeSet attrs) { 60 super(context, attrs); 61 62 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PercentageBarChart); 63 mMinTickWidth = a.getDimensionPixelSize(R.styleable.PercentageBarChart_minTickWidth, 1); 64 int emptyColor = a.getColor(R.styleable.PercentageBarChart_emptyColor, Color.BLACK); 65 a.recycle(); 66 67 mEmptyPaint.setColor(emptyColor); 68 mEmptyPaint.setStyle(Paint.Style.FILL); 69 } 70 71 @Override onDraw(Canvas canvas)72 protected void onDraw(Canvas canvas) { 73 super.onDraw(canvas); 74 75 final int left = getPaddingLeft(); 76 final int right = getWidth() - getPaddingRight(); 77 final int top = getPaddingTop(); 78 final int bottom = getHeight() - getPaddingBottom(); 79 80 final int width = right - left; 81 82 final boolean isLayoutRtl = isLayoutRtl(); 83 if (isLayoutRtl) { 84 float nextX = right; 85 86 if (mEntries != null) { 87 for (final Entry e : mEntries) { 88 final float entryWidth; 89 if (e.percentage == 0.0f) { 90 entryWidth = 0.0f; 91 } else { 92 entryWidth = Math.max(mMinTickWidth, width * e.percentage); 93 } 94 95 final float lastX = nextX - entryWidth; 96 if (lastX < left) { 97 canvas.drawRect(left, top, nextX, bottom, e.paint); 98 return; 99 } 100 101 canvas.drawRect(lastX, top, nextX, bottom, e.paint); 102 nextX = lastX; 103 } 104 } 105 106 canvas.drawRect(left, top, nextX, bottom, mEmptyPaint); 107 } else { 108 float lastX = left; 109 110 if (mEntries != null) { 111 for (final Entry e : mEntries) { 112 final float entryWidth; 113 if (e.percentage == 0.0f) { 114 entryWidth = 0.0f; 115 } else { 116 entryWidth = Math.max(mMinTickWidth, width * e.percentage); 117 } 118 119 final float nextX = lastX + entryWidth; 120 if (nextX > right) { 121 canvas.drawRect(lastX, top, right, bottom, e.paint); 122 return; 123 } 124 125 canvas.drawRect(lastX, top, nextX, bottom, e.paint); 126 lastX = nextX; 127 } 128 } 129 130 canvas.drawRect(lastX, top, right, bottom, mEmptyPaint); 131 } 132 } 133 134 /** 135 * Sets the background for this chart. Callers are responsible for later 136 * calling {@link #invalidate()}. 137 */ 138 @Override setBackgroundColor(int color)139 public void setBackgroundColor(int color) { 140 mEmptyPaint.setColor(color); 141 } 142 143 /** 144 * Adds a new slice to the percentage bar chart. Callers are responsible for 145 * later calling {@link #invalidate()}. 146 * 147 * @param percentage the total width that 148 * @param color the color to draw the entry 149 */ createEntry(int order, float percentage, int color)150 public static Entry createEntry(int order, float percentage, int color) { 151 final Paint p = new Paint(); 152 p.setColor(color); 153 p.setStyle(Paint.Style.FILL); 154 return new Entry(order, percentage, p); 155 } 156 setEntries(Collection<Entry> entries)157 public void setEntries(Collection<Entry> entries) { 158 mEntries = entries; 159 } 160 } 161