2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
15 package com.android.settingslib.graph;
17 import android.content.Context;
18 import android.content.res.TypedArray;
19 import android.util.AttributeSet;
20 import android.util.SparseIntArray;
21 import android.view.Gravity;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.FrameLayout;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import com.android.settingslib.R;
29 public class UsageView extends FrameLayout {
31 private final UsageGraph mUsageGraph;
32 private final TextView[] mLabels;
33 private final TextView[] mBottomLabels;
35 public UsageView(Context context, AttributeSet attrs) {
36 super(context, attrs);
37 LayoutInflater.from(context).inflate(R.layout.usage_view, this);
38 mUsageGraph = (UsageGraph) findViewById(R.id.usage_graph);
39 mLabels = new TextView[] {
40 (TextView) findViewById(R.id.label_bottom),
41 (TextView) findViewById(R.id.label_middle),
42 (TextView) findViewById(R.id.label_top),
44 mBottomLabels = new TextView[] {
45 (TextView) findViewById(R.id.label_start),
46 (TextView) findViewById(R.id.label_end),
48 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UsageView, 0, 0);
49 if (a.hasValue(R.styleable.UsageView_sideLabels)) {
50 setSideLabels(a.getTextArray(R.styleable.UsageView_sideLabels));
52 if (a.hasValue(R.styleable.UsageView_bottomLabels)) {
53 setBottomLabels(a.getTextArray(R.styleable.UsageView_bottomLabels));
55 if (a.hasValue(R.styleable.UsageView_textColor)) {
56 int color = a.getColor(R.styleable.UsageView_textColor, 0);
57 for (TextView v : mLabels) {
58 v.setTextColor(color);
60 for (TextView v : mBottomLabels) {
61 v.setTextColor(color);
64 if (a.hasValue(R.styleable.UsageView_android_gravity)) {
65 int gravity = a.getInt(R.styleable.UsageView_android_gravity, 0);
66 if (gravity == Gravity.END) {
67 LinearLayout layout = (LinearLayout) findViewById(R.id.graph_label_group);
68 LinearLayout labels = (LinearLayout) findViewById(R.id.label_group);
69 // Swap the children order.
70 layout.removeView(labels);
71 layout.addView(labels);
73 labels.setGravity(Gravity.END);
74 // Swap the bottom label padding
75 LinearLayout bottomLabels = (LinearLayout) findViewById(R.id.bottom_label_group);
76 bottomLabels.setPadding(bottomLabels.getPaddingRight(), bottomLabels.getPaddingTop(),
77 bottomLabels.getPaddingLeft(), bottomLabels.getPaddingBottom());
78 } else if (gravity != Gravity.START) {
79 throw new IllegalArgumentException("Unsupported gravity " + gravity);
82 mUsageGraph.setAccentColor(a.getColor(R.styleable.UsageView_android_colorAccent, 0));
85 public void clearPaths() {
86 mUsageGraph.clearPaths();
89 public void addPath(SparseIntArray points) {
90 mUsageGraph.addPath(points);
93 public void configureGraph(int maxX, int maxY, boolean showProjection, boolean projectUp) {
94 mUsageGraph.setMax(maxX, maxY);
95 mUsageGraph.setShowProjection(showProjection, projectUp);
98 public void setAccentColor(int color) {
99 mUsageGraph.setAccentColor(color);
102 public void setDividerLoc(int dividerLoc) {
103 mUsageGraph.setDividerLoc(dividerLoc);
106 public void setDividerColors(int middleColor, int topColor) {
107 mUsageGraph.setDividerColors(middleColor, topColor);
110 public void setSideLabelWeights(float before, float after) {
111 setWeight(R.id.space1, before);
112 setWeight(R.id.space2, after);
115 private void setWeight(int id, float weight) {
116 View v = findViewById(id);
117 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
118 params.weight = weight;
119 v.setLayoutParams(params);
122 public void setSideLabels(CharSequence[] labels) {
123 if (labels.length != mLabels.length) {
124 throw new IllegalArgumentException("Invalid number of labels");
126 for (int i = 0; i < mLabels.length; i++) {
127 mLabels[i].setText(labels[i]);
131 public void setBottomLabels(CharSequence[] labels) {
132 if (labels.length != mBottomLabels.length) {
133 throw new IllegalArgumentException("Invalid number of labels");
135 for (int i = 0; i < mBottomLabels.length; i++) {
136 mBottomLabels[i].setText(labels[i]);