1 /*
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 
15 package com.android.settings;
16 
17 import android.content.Context;
18 import android.support.v7.preference.Preference;
19 import android.support.v7.preference.PreferenceViewHolder;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.TextView;
24 import 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  */
29 public 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;
36     private float mLeftRatio, mMiddleRatio, mRightRatio;
37     private String mStartLabel;
38     private String mEndLabel;
39 
SummaryPreference(Context context, AttributeSet attrs)40     public SummaryPreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         setLayoutResource(R.layout.settings_summary_preference);
43         mLeft = context.getColor(R.color.summary_default_start);
44         mRight = context.getColor(R.color.summary_default_end);
45     }
46 
setAmount(String amount)47     public void setAmount(String amount) {
48         mAmount = amount;
49         if (mAmount != null && mUnits != null) {
50             setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
51                     mAmount, mUnits));
52         }
53     }
54 
setUnits(String units)55     public void setUnits(String units) {
56         mUnits = units;
57         if (mAmount != null && mUnits != null) {
58             setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
59                     mAmount, mUnits));
60         }
61     }
62 
setLabels(String start, String end)63     public void setLabels(String start, String end) {
64         mStartLabel = start;
65         mEndLabel = end;
66         notifyChanged();
67     }
68 
setRatios(float left, float middle, float right)69     public void setRatios(float left, float middle, float right) {
70         mLeftRatio = left;
71         mMiddleRatio = middle;
72         mRightRatio = right;
73         notifyChanged();
74     }
75 
setColors(int left, int middle, int right)76     public void setColors(int left, int middle, int right) {
77         mLeft = left;
78         mMiddle = middle;
79         mRight = right;
80         notifyChanged();
81     }
82 
83     @Override
onBindViewHolder(PreferenceViewHolder holder)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);
89         colorBar.setColors(mLeft, mMiddle, mRight);
90 
91         if (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel)) {
92             holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE);
93             ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel);
94             ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel);
95         } else {
96             holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
97         }
98     }
99 }
100