1c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza/*
2c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * Copyright (C) 2016 The Android Open Source Project
3c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza *
4c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * except in compliance with the License. You may obtain a copy of the License at
6c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza *
7c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza *      http://www.apache.org/licenses/LICENSE-2.0
8c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza *
9c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * Unless required by applicable law or agreed to in writing, software distributed under the
10c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * KIND, either express or implied. See the License for the specific language governing
12c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza * permissions and limitations under the License.
13c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza */
14c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
15c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszapackage com.android.settings.graph;
16c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
17c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.content.Context;
18c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.content.res.TypedArray;
19c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.util.AttributeSet;
20c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.util.SparseIntArray;
21c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.view.Gravity;
22c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.view.LayoutInflater;
23c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.view.View;
24c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.widget.FrameLayout;
25c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.widget.LinearLayout;
26c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport android.widget.TextView;
27c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszaimport com.android.settingslib.R;
28c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
29c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kuleszapublic class UsageView extends FrameLayout {
30c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
31c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    private final UsageGraph mUsageGraph;
32c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    private final TextView[] mLabels;
33c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    private final TextView[] mBottomLabels;
34c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
35c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public UsageView(Context context, AttributeSet attrs) {
36c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        super(context, attrs);
37c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        LayoutInflater.from(context).inflate(R.layout.usage_view, this);
38c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph = findViewById(R.id.usage_graph);
39c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mLabels = new TextView[] {
40c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                findViewById(R.id.label_bottom),
41c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                findViewById(R.id.label_middle),
42c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                findViewById(R.id.label_top),
43c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        };
44c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mBottomLabels = new TextView[] {
45c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                findViewById(R.id.label_start),
46c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                findViewById(R.id.label_end),
47c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        };
48c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UsageView, 0, 0);
49c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (a.hasValue(R.styleable.UsageView_sideLabels)) {
50c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            setSideLabels(a.getTextArray(R.styleable.UsageView_sideLabels));
51c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
52c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (a.hasValue(R.styleable.UsageView_bottomLabels)) {
53c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            setBottomLabels(a.getTextArray(R.styleable.UsageView_bottomLabels));
54c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
55c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (a.hasValue(R.styleable.UsageView_textColor)) {
56c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            int color = a.getColor(R.styleable.UsageView_textColor, 0);
57c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            for (TextView v : mLabels) {
58c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                v.setTextColor(color);
59c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            }
60c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            for (TextView v : mBottomLabels) {
61c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                v.setTextColor(color);
62c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            }
63c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
64c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (a.hasValue(R.styleable.UsageView_android_gravity)) {
65c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            int gravity = a.getInt(R.styleable.UsageView_android_gravity, 0);
66c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            if (gravity == Gravity.END) {
67c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                LinearLayout layout = findViewById(R.id.graph_label_group);
68c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                LinearLayout labels = findViewById(R.id.label_group);
69c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                // Swap the children order.
70c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                layout.removeView(labels);
71c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                layout.addView(labels);
72c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                // Set gravity.
73c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                labels.setGravity(Gravity.END);
74c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                // Swap the bottom space order.
75c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                LinearLayout bottomLabels = findViewById(R.id.bottom_label_group);
76c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                View bottomSpace = bottomLabels.findViewById(R.id.bottom_label_space);
77c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                bottomLabels.removeView(bottomSpace);
78c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                bottomLabels.addView(bottomSpace);
79c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            } else if (gravity != Gravity.START) {
80c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza                throw new IllegalArgumentException("Unsupported gravity " + gravity);
81c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            }
82c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
83c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.setAccentColor(a.getColor(R.styleable.UsageView_android_colorAccent, 0));
849d854ef36f06c53ffcc74a24e05e596000725a76Rajeev Kumar        a.recycle();
85c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
86c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
87c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void clearPaths() {
88c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.clearPaths();
89c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
90c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
91c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void addPath(SparseIntArray points) {
92c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.addPath(points);
93c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
94c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
95c57ceaaa8cfc033bb397eab7af0b4359befbef52Alex Kulesza    public void addProjectedPath(SparseIntArray points) {
96c57ceaaa8cfc033bb397eab7af0b4359befbef52Alex Kulesza        mUsageGraph.addProjectedPath(points);
97c57ceaaa8cfc033bb397eab7af0b4359befbef52Alex Kulesza    }
98c57ceaaa8cfc033bb397eab7af0b4359befbef52Alex Kulesza
99c57ceaaa8cfc033bb397eab7af0b4359befbef52Alex Kulesza    public void configureGraph(int maxX, int maxY) {
100c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.setMax(maxX, maxY);
101c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
102c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
103c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setAccentColor(int color) {
104c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.setAccentColor(color);
105c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
106c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
107c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setDividerLoc(int dividerLoc) {
108c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.setDividerLoc(dividerLoc);
109c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
110c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
111c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setDividerColors(int middleColor, int topColor) {
112c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        mUsageGraph.setDividerColors(middleColor, topColor);
113c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
114c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
115c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setSideLabelWeights(float before, float after) {
116c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        setWeight(R.id.space1, before);
117c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        setWeight(R.id.space2, after);
118c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
119c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
120c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    private void setWeight(int id, float weight) {
121c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        View v = findViewById(id);
122c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
123c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        params.weight = weight;
124c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        v.setLayoutParams(params);
125c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
126c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
127c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setSideLabels(CharSequence[] labels) {
128c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (labels.length != mLabels.length) {
129c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            throw new IllegalArgumentException("Invalid number of labels");
130c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
131c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        for (int i = 0; i < mLabels.length; i++) {
132c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            mLabels[i].setText(labels[i]);
133c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
134c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
135c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
136c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    public void setBottomLabels(CharSequence[] labels) {
137c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        if (labels.length != mBottomLabels.length) {
138c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            throw new IllegalArgumentException("Invalid number of labels");
139c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
140c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        for (int i = 0; i < mBottomLabels.length; i++) {
141c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza            mBottomLabels[i].setText(labels[i]);
142c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza        }
143c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza    }
144c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza
145c661098ab8e278c57f1ad1a9adb8f1bbd80a81a5Alex Kulesza}
146