blob: 23965ee3833515000934328786d76c8aba1979e3 [file] [log] [blame]
Jason Monkb37e2882016-01-11 14:27:20 -05001/*
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.settings;
16
17import android.content.Context;
18import android.support.v7.preference.Preference;
19import android.support.v7.preference.PreferenceViewHolder;
20import android.text.TextUtils;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.TextView;
Fan Zhang7a6726e2018-01-17 10:38:49 -080024
25import com.android.settings.widget.LinearColorBar;
Jason Monkb37e2882016-01-11 14:27:20 -050026
27/**
28 * Provides a summary of a setting page in a preference. Such as memory or data usage.
29 */
30public class SummaryPreference extends Preference {
31
32 private static final String TAG = "SummaryPreference";
33 private String mAmount;
34 private String mUnits;
35
36 private int mLeft, mMiddle, mRight;
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070037 private boolean mColorsSet = false;
Fan Zhang7bd19b82016-09-12 15:00:53 -070038 private boolean mChartEnabled = true;
Jason Monkb37e2882016-01-11 14:27:20 -050039 private float mLeftRatio, mMiddleRatio, mRightRatio;
40 private String mStartLabel;
41 private String mEndLabel;
42
43 public SummaryPreference(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 setLayoutResource(R.layout.settings_summary_preference);
Jason Monkb37e2882016-01-11 14:27:20 -050046 }
47
Fan Zhang7bd19b82016-09-12 15:00:53 -070048 public void setChartEnabled(boolean enabled) {
49 if (mChartEnabled != enabled) {
50 mChartEnabled = enabled;
51 notifyChanged();
52 }
53 }
54
Jason Monkb37e2882016-01-11 14:27:20 -050055 public void setAmount(String amount) {
56 mAmount = amount;
57 if (mAmount != null && mUnits != null) {
58 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
59 mAmount, mUnits));
60 }
61 }
62
63 public void setUnits(String units) {
64 mUnits = units;
65 if (mAmount != null && mUnits != null) {
66 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
67 mAmount, mUnits));
68 }
69 }
70
71 public void setLabels(String start, String end) {
72 mStartLabel = start;
73 mEndLabel = end;
74 notifyChanged();
75 }
76
77 public void setRatios(float left, float middle, float right) {
78 mLeftRatio = left;
79 mMiddleRatio = middle;
80 mRightRatio = right;
81 notifyChanged();
82 }
83
84 public void setColors(int left, int middle, int right) {
85 mLeft = left;
86 mMiddle = middle;
87 mRight = right;
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070088 mColorsSet = true;
Jason Monkb37e2882016-01-11 14:27:20 -050089 notifyChanged();
90 }
91
92 @Override
93 public void onBindViewHolder(PreferenceViewHolder holder) {
94 super.onBindViewHolder(holder);
95
Fan Zhang7a6726e2018-01-17 10:38:49 -080096 final LinearColorBar colorBar = holder.itemView.findViewById(R.id.color_bar);
Fan Zhang7bd19b82016-09-12 15:00:53 -070097
98 if (mChartEnabled) {
99 colorBar.setVisibility(View.VISIBLE);
100 colorBar.setRatios(mLeftRatio, mMiddleRatio, mRightRatio);
101 if (mColorsSet) {
102 colorBar.setColors(mLeft, mMiddle, mRight);
103 }
104 } else {
105 colorBar.setVisibility(View.GONE);
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -0700106 }
Jason Monkb37e2882016-01-11 14:27:20 -0500107
Fan Zhang7bd19b82016-09-12 15:00:53 -0700108 if (mChartEnabled && (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel))) {
Jason Monkb37e2882016-01-11 14:27:20 -0500109 holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE);
110 ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel);
111 ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel);
112 } else {
113 holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
114 }
115 }
116}