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