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
15package com.android.settingslib.graph;
16
17import android.content.Context;
18import android.content.res.TypedArray;
19import android.util.AttributeSet;
20import android.util.SparseIntArray;
21import android.view.Gravity;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.widget.FrameLayout;
25import android.widget.LinearLayout;
26import android.widget.TextView;
27import com.android.settingslib.R;
28
29public class UsageView extends FrameLayout {
30
31    private final UsageGraph mUsageGraph;
32    private final TextView[] mLabels;
33    private final TextView[] mBottomLabels;
34
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
86    public void clearPaths() {
87        mUsageGraph.clearPaths();
88    }
89
90    public void addPath(SparseIntArray points) {
91        mUsageGraph.addPath(points);
92    }
93
94    public void configureGraph(int maxX, int maxY, boolean showProjection, boolean projectUp) {
95        mUsageGraph.setMax(maxX, maxY);
96        mUsageGraph.setShowProjection(showProjection, projectUp);
97    }
98
99    public void setAccentColor(int color) {
100        mUsageGraph.setAccentColor(color);
101    }
102
103    public void setDividerLoc(int dividerLoc) {
104        mUsageGraph.setDividerLoc(dividerLoc);
105    }
106
107    public void setDividerColors(int middleColor, int topColor) {
108        mUsageGraph.setDividerColors(middleColor, topColor);
109    }
110
111    public void setSideLabelWeights(float before, float after) {
112        setWeight(R.id.space1, before);
113        setWeight(R.id.space2, after);
114    }
115
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
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
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