1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
13  */
14 
15 package com.android.settingslib.graph;
16 
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;
28 
29 public class UsageView extends FrameLayout {
30 
31     private final UsageGraph mUsageGraph;
32     private final TextView[] mLabels;
33     private final TextView[] mBottomLabels;
34 
UsageView(Context context, AttributeSet attrs)35     public UsageView(Context context, AttributeSet attrs) {
36         super(context, attrs);
37         LayoutInflater.from(context).inflate(R.layout.usage_view, this);
38         mUsageGraph = findViewById(R.id.usage_graph);
39         mLabels = new TextView[] {
40                 findViewById(R.id.label_bottom),
41                 findViewById(R.id.label_middle),
42                 findViewById(R.id.label_top),
43         };
44         mBottomLabels = new TextView[] {
45                 findViewById(R.id.label_start),
46                 findViewById(R.id.label_end),
47         };
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));
51         }
52         if (a.hasValue(R.styleable.UsageView_bottomLabels)) {
53             setBottomLabels(a.getTextArray(R.styleable.UsageView_bottomLabels));
54         }
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);
59             }
60             for (TextView v : mBottomLabels) {
61                 v.setTextColor(color);
62             }
63         }
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 = findViewById(R.id.graph_label_group);
68                 LinearLayout labels = findViewById(R.id.label_group);
69                 // Swap the children order.
70                 layout.removeView(labels);
71                 layout.addView(labels);
72                 // Set gravity.
73                 labels.setGravity(Gravity.END);
74                 // Swap the bottom space order.
75                 LinearLayout bottomLabels = findViewById(R.id.bottom_label_group);
76                 View bottomSpace = bottomLabels.findViewById(R.id.bottom_label_space);
77                 bottomLabels.removeView(bottomSpace);
78                 bottomLabels.addView(bottomSpace);
79             } else if (gravity != Gravity.START) {
80                 throw new IllegalArgumentException("Unsupported gravity " + gravity);
81             }
82         }
83         mUsageGraph.setAccentColor(a.getColor(R.styleable.UsageView_android_colorAccent, 0));
84     }
85 
clearPaths()86     public void clearPaths() {
87         mUsageGraph.clearPaths();
88     }
89 
addPath(SparseIntArray points)90     public void addPath(SparseIntArray points) {
91         mUsageGraph.addPath(points);
92     }
93 
configureGraph(int maxX, int maxY, boolean showProjection, boolean projectUp)94     public void configureGraph(int maxX, int maxY, boolean showProjection, boolean projectUp) {
95         mUsageGraph.setMax(maxX, maxY);
96         mUsageGraph.setShowProjection(showProjection, projectUp);
97     }
98 
setAccentColor(int color)99     public void setAccentColor(int color) {
100         mUsageGraph.setAccentColor(color);
101     }
102 
setDividerLoc(int dividerLoc)103     public void setDividerLoc(int dividerLoc) {
104         mUsageGraph.setDividerLoc(dividerLoc);
105     }
106 
setDividerColors(int middleColor, int topColor)107     public void setDividerColors(int middleColor, int topColor) {
108         mUsageGraph.setDividerColors(middleColor, topColor);
109     }
110 
setSideLabelWeights(float before, float after)111     public void setSideLabelWeights(float before, float after) {
112         setWeight(R.id.space1, before);
113         setWeight(R.id.space2, after);
114     }
115 
setWeight(int id, float weight)116     private void setWeight(int id, float weight) {
117         View v = findViewById(id);
118         LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
119         params.weight = weight;
120         v.setLayoutParams(params);
121     }
122 
setSideLabels(CharSequence[] labels)123     public void setSideLabels(CharSequence[] labels) {
124         if (labels.length != mLabels.length) {
125             throw new IllegalArgumentException("Invalid number of labels");
126         }
127         for (int i = 0; i < mLabels.length; i++) {
128             mLabels[i].setText(labels[i]);
129         }
130     }
131 
setBottomLabels(CharSequence[] labels)132     public void setBottomLabels(CharSequence[] labels) {
133         if (labels.length != mBottomLabels.length) {
134             throw new IllegalArgumentException("Invalid number of labels");
135         }
136         for (int i = 0; i < mBottomLabels.length; i++) {
137             mBottomLabels[i].setText(labels[i]);
138         }
139     }
140 
141 }
142