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 = (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),
43        };
44        mBottomLabels = new TextView[] {
45                (TextView) findViewById(R.id.label_start),
46                (TextView) 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 = (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);
72                // Set gravity.
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);
80            }
81        }
82        mUsageGraph.setAccentColor(a.getColor(R.styleable.UsageView_android_colorAccent, 0));
83    }
84
85    public void clearPaths() {
86        mUsageGraph.clearPaths();
87    }
88
89    public void addPath(SparseIntArray points) {
90        mUsageGraph.addPath(points);
91    }
92
93    public void configureGraph(int maxX, int maxY, boolean showProjection, boolean projectUp) {
94        mUsageGraph.setMax(maxX, maxY);
95        mUsageGraph.setShowProjection(showProjection, projectUp);
96    }
97
98    public void setAccentColor(int color) {
99        mUsageGraph.setAccentColor(color);
100    }
101
102    public void setDividerLoc(int dividerLoc) {
103        mUsageGraph.setDividerLoc(dividerLoc);
104    }
105
106    public void setDividerColors(int middleColor, int topColor) {
107        mUsageGraph.setDividerColors(middleColor, topColor);
108    }
109
110    public void setSideLabelWeights(float before, float after) {
111        setWeight(R.id.space1, before);
112        setWeight(R.id.space2, after);
113    }
114
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);
120    }
121
122    public void setSideLabels(CharSequence[] labels) {
123        if (labels.length != mLabels.length) {
124            throw new IllegalArgumentException("Invalid number of labels");
125        }
126        for (int i = 0; i < mLabels.length; i++) {
127            mLabels[i].setText(labels[i]);
128        }
129    }
130
131    public void setBottomLabels(CharSequence[] labels) {
132        if (labels.length != mBottomLabels.length) {
133            throw new IllegalArgumentException("Invalid number of labels");
134        }
135        for (int i = 0; i < mBottomLabels.length; i++) {
136            mBottomLabels[i].setText(labels[i]);
137        }
138    }
139
140}
141