Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 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; |
Andrew Sapperstein | b40b0d2 | 2016-05-20 18:38:01 -0700 | [diff] [blame] | 36 | private boolean mColorsSet = false; |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 37 | private boolean mChartEnabled = true; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 38 | private float mLeftRatio, mMiddleRatio, mRightRatio; |
| 39 | private String mStartLabel; |
| 40 | private String mEndLabel; |
| 41 | |
| 42 | public SummaryPreference(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
| 44 | setLayoutResource(R.layout.settings_summary_preference); |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 47 | public void setChartEnabled(boolean enabled) { |
| 48 | if (mChartEnabled != enabled) { |
| 49 | mChartEnabled = enabled; |
| 50 | notifyChanged(); |
| 51 | } |
| 52 | } |
| 53 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 54 | public void setAmount(String amount) { |
| 55 | mAmount = amount; |
| 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 setUnits(String units) { |
| 63 | mUnits = units; |
| 64 | if (mAmount != null && mUnits != null) { |
| 65 | setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large), |
| 66 | mAmount, mUnits)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public void setLabels(String start, String end) { |
| 71 | mStartLabel = start; |
| 72 | mEndLabel = end; |
| 73 | notifyChanged(); |
| 74 | } |
| 75 | |
| 76 | public void setRatios(float left, float middle, float right) { |
| 77 | mLeftRatio = left; |
| 78 | mMiddleRatio = middle; |
| 79 | mRightRatio = right; |
| 80 | notifyChanged(); |
| 81 | } |
| 82 | |
| 83 | public void setColors(int left, int middle, int right) { |
| 84 | mLeft = left; |
| 85 | mMiddle = middle; |
| 86 | mRight = right; |
Andrew Sapperstein | b40b0d2 | 2016-05-20 18:38:01 -0700 | [diff] [blame] | 87 | mColorsSet = true; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 88 | notifyChanged(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onBindViewHolder(PreferenceViewHolder holder) { |
| 93 | super.onBindViewHolder(holder); |
| 94 | |
| 95 | LinearColorBar colorBar = (LinearColorBar) holder.itemView.findViewById(R.id.color_bar); |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 96 | |
| 97 | if (mChartEnabled) { |
| 98 | colorBar.setVisibility(View.VISIBLE); |
| 99 | colorBar.setRatios(mLeftRatio, mMiddleRatio, mRightRatio); |
| 100 | if (mColorsSet) { |
| 101 | colorBar.setColors(mLeft, mMiddle, mRight); |
| 102 | } |
| 103 | } else { |
| 104 | colorBar.setVisibility(View.GONE); |
Andrew Sapperstein | b40b0d2 | 2016-05-20 18:38:01 -0700 | [diff] [blame] | 105 | } |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 106 | |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 107 | if (mChartEnabled && (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel))) { |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 108 | holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE); |
| 109 | ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel); |
| 110 | ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel); |
| 111 | } else { |
| 112 | holder.findViewById(R.id.label_bar).setVisibility(View.GONE); |
| 113 | } |
| 114 | } |
| 115 | } |