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; |
Aurimas Liutikas | e0069d3 | 2018-04-17 11:22:43 -0700 | [diff] [blame^] | 18 | import androidx.preference.Preference; |
| 19 | import androidx.preference.PreferenceViewHolder; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 20 | import android.text.TextUtils; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
Andrew Sapperstein | 38bf192 | 2018-03-18 15:53:09 -0700 | [diff] [blame] | 23 | import android.widget.ProgressBar; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 24 | import android.widget.TextView; |
Fan Zhang | 7a6726e | 2018-01-17 10:38:49 -0800 | [diff] [blame] | 25 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 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 | |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 35 | private boolean mChartEnabled = true; |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 36 | private float mLeftRatio, mMiddleRatio, mRightRatio; |
| 37 | private String mStartLabel; |
| 38 | private String mEndLabel; |
| 39 | |
| 40 | public SummaryPreference(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
| 42 | setLayoutResource(R.layout.settings_summary_preference); |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 45 | public void setChartEnabled(boolean enabled) { |
| 46 | if (mChartEnabled != enabled) { |
| 47 | mChartEnabled = enabled; |
| 48 | notifyChanged(); |
| 49 | } |
| 50 | } |
| 51 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 52 | public void setAmount(String amount) { |
| 53 | mAmount = amount; |
| 54 | if (mAmount != null && mUnits != null) { |
| 55 | setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large), |
| 56 | mAmount, mUnits)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void setUnits(String units) { |
| 61 | mUnits = units; |
| 62 | if (mAmount != null && mUnits != null) { |
| 63 | setTitle(TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large), |
| 64 | mAmount, mUnits)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public void setLabels(String start, String end) { |
| 69 | mStartLabel = start; |
| 70 | mEndLabel = end; |
| 71 | notifyChanged(); |
| 72 | } |
| 73 | |
| 74 | public void setRatios(float left, float middle, float right) { |
| 75 | mLeftRatio = left; |
| 76 | mMiddleRatio = middle; |
| 77 | mRightRatio = right; |
| 78 | notifyChanged(); |
| 79 | } |
| 80 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 81 | @Override |
| 82 | public void onBindViewHolder(PreferenceViewHolder holder) { |
| 83 | super.onBindViewHolder(holder); |
| 84 | |
Andrew Sapperstein | 38bf192 | 2018-03-18 15:53:09 -0700 | [diff] [blame] | 85 | final ProgressBar colorBar = holder.itemView.findViewById(R.id.color_bar); |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 86 | |
| 87 | if (mChartEnabled) { |
| 88 | colorBar.setVisibility(View.VISIBLE); |
Andrew Sapperstein | 38bf192 | 2018-03-18 15:53:09 -0700 | [diff] [blame] | 89 | int progress = (int) (mLeftRatio * 100); |
| 90 | colorBar.setProgress(progress); |
| 91 | colorBar.setSecondaryProgress(progress + (int) (mMiddleRatio * 100)); |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 92 | } else { |
| 93 | colorBar.setVisibility(View.GONE); |
Andrew Sapperstein | b40b0d2 | 2016-05-20 18:38:01 -0700 | [diff] [blame] | 94 | } |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 95 | |
Fan Zhang | 7bd19b8 | 2016-09-12 15:00:53 -0700 | [diff] [blame] | 96 | if (mChartEnabled && (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel))) { |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 97 | holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE); |
| 98 | ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel); |
| 99 | ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel); |
| 100 | } else { |
| 101 | holder.findViewById(R.id.label_bar).setVisibility(View.GONE); |
| 102 | } |
| 103 | } |
| 104 | } |