1 /* 2 * Copyright (C) 2016 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.widget; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 import android.util.SparseIntArray; 23 import android.view.Gravity; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.widget.FrameLayout; 27 import android.widget.LinearLayout; 28 import android.widget.TextView; 29 30 import com.android.settingslib.R; 31 32 public class UsageView extends FrameLayout { 33 34 private final UsageGraph mUsageGraph; 35 private final TextView[] mLabels; 36 private final TextView[] mBottomLabels; 37 UsageView(Context context, AttributeSet attrs)38 public UsageView(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 LayoutInflater.from(context).inflate(R.layout.usage_view, this); 41 mUsageGraph = findViewById(R.id.usage_graph); 42 mLabels = new TextView[] { 43 findViewById(R.id.label_bottom), 44 findViewById(R.id.label_middle), 45 findViewById(R.id.label_top), 46 }; 47 mBottomLabels = new TextView[] { 48 findViewById(R.id.label_start), 49 findViewById(R.id.label_end), 50 }; 51 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UsageView, 0, 0); 52 if (a.hasValue(R.styleable.UsageView_sideLabels)) { 53 setSideLabels(a.getTextArray(R.styleable.UsageView_sideLabels)); 54 } 55 if (a.hasValue(R.styleable.UsageView_bottomLabels)) { 56 setBottomLabels(a.getTextArray(R.styleable.UsageView_bottomLabels)); 57 } 58 if (a.hasValue(R.styleable.UsageView_textColor)) { 59 int color = a.getColor(R.styleable.UsageView_textColor, 0); 60 for (TextView v : mLabels) { 61 v.setTextColor(color); 62 } 63 for (TextView v : mBottomLabels) { 64 v.setTextColor(color); 65 } 66 } 67 if (a.hasValue(R.styleable.UsageView_android_gravity)) { 68 int gravity = a.getInt(R.styleable.UsageView_android_gravity, 0); 69 if (gravity == Gravity.END) { 70 LinearLayout layout = findViewById(R.id.graph_label_group); 71 LinearLayout labels = findViewById(R.id.label_group); 72 // Swap the children order. 73 layout.removeView(labels); 74 layout.addView(labels); 75 // Set gravity. 76 labels.setGravity(Gravity.END); 77 // Swap the bottom space order. 78 LinearLayout bottomLabels = findViewById(R.id.bottom_label_group); 79 View bottomSpace = bottomLabels.findViewById(R.id.bottom_label_space); 80 bottomLabels.removeView(bottomSpace); 81 bottomLabels.addView(bottomSpace); 82 } else if (gravity != Gravity.START) { 83 throw new IllegalArgumentException("Unsupported gravity " + gravity); 84 } 85 } 86 mUsageGraph.setAccentColor(a.getColor(R.styleable.UsageView_android_colorAccent, 0)); 87 a.recycle(); 88 } 89 clearPaths()90 public void clearPaths() { 91 mUsageGraph.clearPaths(); 92 } 93 addPath(SparseIntArray points)94 public void addPath(SparseIntArray points) { 95 mUsageGraph.addPath(points); 96 } 97 addProjectedPath(SparseIntArray points)98 public void addProjectedPath(SparseIntArray points) { 99 mUsageGraph.addProjectedPath(points); 100 } 101 configureGraph(int maxX, int maxY)102 public void configureGraph(int maxX, int maxY) { 103 mUsageGraph.setMax(maxX, maxY); 104 } 105 setAccentColor(int color)106 public void setAccentColor(int color) { 107 mUsageGraph.setAccentColor(color); 108 } 109 setDividerLoc(int dividerLoc)110 public void setDividerLoc(int dividerLoc) { 111 mUsageGraph.setDividerLoc(dividerLoc); 112 } 113 setDividerColors(int middleColor, int topColor)114 public void setDividerColors(int middleColor, int topColor) { 115 mUsageGraph.setDividerColors(middleColor, topColor); 116 } 117 setSideLabelWeights(float before, float after)118 public void setSideLabelWeights(float before, float after) { 119 setWeight(R.id.space1, before); 120 setWeight(R.id.space2, after); 121 } 122 setWeight(int id, float weight)123 private void setWeight(int id, float weight) { 124 View v = findViewById(id); 125 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams(); 126 params.weight = weight; 127 v.setLayoutParams(params); 128 } 129 setSideLabels(CharSequence[] labels)130 public void setSideLabels(CharSequence[] labels) { 131 if (labels.length != mLabels.length) { 132 throw new IllegalArgumentException("Invalid number of labels"); 133 } 134 for (int i = 0; i < mLabels.length; i++) { 135 mLabels[i].setText(labels[i]); 136 } 137 } 138 setBottomLabels(CharSequence[] labels)139 public void setBottomLabels(CharSequence[] labels) { 140 if (labels.length != mBottomLabels.length) { 141 throw new IllegalArgumentException("Invalid number of labels"); 142 } 143 for (int i = 0; i < mBottomLabels.length; i++) { 144 mBottomLabels[i].setText(labels[i]); 145 } 146 } 147 148 } 149