blob: ced5cd75621f104e832a38e1249a7b758a20cc30 [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;
Jason Monkb37e2882016-01-11 14:27:20 -050018import android.text.TextUtils;
19import android.util.AttributeSet;
20import android.view.View;
Andrew Sapperstein38bf1922018-03-18 15:53:09 -070021import android.widget.ProgressBar;
Jason Monkb37e2882016-01-11 14:27:20 -050022import android.widget.TextView;
Fan Zhang7a6726e2018-01-17 10:38:49 -080023
Fan Zhangc7162cd2018-06-18 15:21:41 -070024import androidx.preference.Preference;
25import androidx.preference.PreferenceViewHolder;
26
Jason Monkb37e2882016-01-11 14:27:20 -050027/**
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
Fan Zhang7bd19b82016-09-12 15:00:53 -070036 private boolean mChartEnabled = true;
Jason Monkb37e2882016-01-11 14:27:20 -050037 private float mLeftRatio, mMiddleRatio, mRightRatio;
38 private String mStartLabel;
39 private String mEndLabel;
40
41 public SummaryPreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
43 setLayoutResource(R.layout.settings_summary_preference);
Jason Monkb37e2882016-01-11 14:27:20 -050044 }
45
Fan Zhang7bd19b82016-09-12 15:00:53 -070046 public void setChartEnabled(boolean enabled) {
47 if (mChartEnabled != enabled) {
48 mChartEnabled = enabled;
49 notifyChanged();
50 }
51 }
52
Jason Monkb37e2882016-01-11 14:27:20 -050053 public void setAmount(String amount) {
54 mAmount = amount;
55 if (mAmount != null && mUnits != null) {
56 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
57 mAmount, mUnits));
58 }
59 }
60
61 public void setUnits(String units) {
62 mUnits = units;
63 if (mAmount != null && mUnits != null) {
64 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
65 mAmount, mUnits));
66 }
67 }
68
69 public void setLabels(String start, String end) {
70 mStartLabel = start;
71 mEndLabel = end;
72 notifyChanged();
73 }
74
75 public void setRatios(float left, float middle, float right) {
76 mLeftRatio = left;
77 mMiddleRatio = middle;
78 mRightRatio = right;
79 notifyChanged();
80 }
81
Jason Monkb37e2882016-01-11 14:27:20 -050082 @Override
83 public void onBindViewHolder(PreferenceViewHolder holder) {
84 super.onBindViewHolder(holder);
85
Andrew Sapperstein38bf1922018-03-18 15:53:09 -070086 final ProgressBar colorBar = holder.itemView.findViewById(R.id.color_bar);
Fan Zhang7bd19b82016-09-12 15:00:53 -070087
88 if (mChartEnabled) {
89 colorBar.setVisibility(View.VISIBLE);
Andrew Sapperstein38bf1922018-03-18 15:53:09 -070090 int progress = (int) (mLeftRatio * 100);
91 colorBar.setProgress(progress);
92 colorBar.setSecondaryProgress(progress + (int) (mMiddleRatio * 100));
Fan Zhang7bd19b82016-09-12 15:00:53 -070093 } else {
94 colorBar.setVisibility(View.GONE);
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070095 }
Jason Monkb37e2882016-01-11 14:27:20 -050096
Fan Zhang7bd19b82016-09-12 15:00:53 -070097 if (mChartEnabled && (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel))) {
Jason Monkb37e2882016-01-11 14:27:20 -050098 holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE);
99 ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel);
100 ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel);
101 } else {
102 holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
103 }
104 }
105}