blob: 38449b1e03f5b14636197f95aea4cfe3c5cf662a [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;
24import com.android.settings.applications.LinearColorBar;
25
26/**
27 * Provides a summary of a setting page in a preference. Such as memory or data usage.
28 */
29public class SummaryPreference extends Preference {
30
31 private static final String TAG = "SummaryPreference";
32 private String mAmount;
33 private String mUnits;
34
35 private int mLeft, mMiddle, mRight;
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070036 private boolean mColorsSet = false;
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
46 public void setAmount(String amount) {
47 mAmount = amount;
48 if (mAmount != null && mUnits != null) {
49 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
50 mAmount, mUnits));
51 }
52 }
53
54 public void setUnits(String units) {
55 mUnits = units;
56 if (mAmount != null && mUnits != null) {
57 setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
58 mAmount, mUnits));
59 }
60 }
61
62 public void setLabels(String start, String end) {
63 mStartLabel = start;
64 mEndLabel = end;
65 notifyChanged();
66 }
67
68 public void setRatios(float left, float middle, float right) {
69 mLeftRatio = left;
70 mMiddleRatio = middle;
71 mRightRatio = right;
72 notifyChanged();
73 }
74
75 public void setColors(int left, int middle, int right) {
76 mLeft = left;
77 mMiddle = middle;
78 mRight = right;
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070079 mColorsSet = true;
Jason Monkb37e2882016-01-11 14:27:20 -050080 notifyChanged();
81 }
82
83 @Override
84 public void onBindViewHolder(PreferenceViewHolder holder) {
85 super.onBindViewHolder(holder);
86
87 LinearColorBar colorBar = (LinearColorBar) holder.itemView.findViewById(R.id.color_bar);
88 colorBar.setRatios(mLeftRatio, mMiddleRatio, mRightRatio);
Andrew Sappersteinb40b0d22016-05-20 18:38:01 -070089 if (mColorsSet) {
90 colorBar.setColors(mLeft, mMiddle, mRight);
91 }
Jason Monkb37e2882016-01-11 14:27:20 -050092
93 if (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel)) {
94 holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE);
95 ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel);
96 ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel);
97 } else {
98 holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
99 }
100 }
101}