1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge.batteryusage; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.text.TextUtils; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.TextView; 26 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 import com.android.settings.Utils; 31 import com.android.settingslib.widget.AppPreference; 32 33 /** 34 * Custom preference for displaying battery usage info as a bar and an icon on the left for the 35 * subsystem/app type. 36 * 37 * <p>The battery usage info could be usage percentage or usage time. The preference won't show any 38 * icon if it is null. 39 */ 40 public class PowerGaugePreference extends AppPreference { 41 42 // Please see go/battery-usage-app-list-alpha 43 private static final float SELECTABLE_ALPHA = 1f; 44 private static final float UNSELECTABLE_ALPHA_LIGHT_MODE = 0.65f; 45 private static final float UNSELECTABLE_ALPHA_DARK_MODE = 0.65f; 46 47 private final int mTitleColorNormal; 48 49 private BatteryEntry mInfo; 50 private BatteryDiffEntry mBatteryDiffEntry; 51 private CharSequence mContentDescription; 52 private CharSequence mProgress; 53 private CharSequence mProgressContentDescription; 54 private boolean mShowAnomalyIcon; 55 PowerGaugePreference( Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info)56 public PowerGaugePreference( 57 Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info) { 58 this(context, null, icon, contentDescription, info); 59 } 60 PowerGaugePreference(Context context)61 public PowerGaugePreference(Context context) { 62 this(context, null, null, null, null); 63 } 64 PowerGaugePreference(Context context, AttributeSet attrs)65 public PowerGaugePreference(Context context, AttributeSet attrs) { 66 this(context, attrs, null, null, null); 67 } 68 PowerGaugePreference( Context context, AttributeSet attrs, Drawable icon, CharSequence contentDescription, BatteryEntry info)69 private PowerGaugePreference( 70 Context context, 71 AttributeSet attrs, 72 Drawable icon, 73 CharSequence contentDescription, 74 BatteryEntry info) { 75 super(context, attrs); 76 if (icon != null) { 77 setIcon(icon); 78 } 79 setWidgetLayoutResource(R.layout.preference_widget_summary); 80 mInfo = info; 81 mContentDescription = contentDescription; 82 mShowAnomalyIcon = false; 83 mTitleColorNormal = 84 Utils.getColorAttrDefaultColor(context, android.R.attr.textColorPrimary); 85 } 86 87 /** Sets the content description. */ setContentDescription(String name)88 public void setContentDescription(String name) { 89 mContentDescription = name; 90 notifyChanged(); 91 } 92 93 /** Sets the percentage to show. */ setPercentage(CharSequence percentage)94 public void setPercentage(CharSequence percentage) { 95 mProgress = percentage; 96 mProgressContentDescription = percentage; 97 notifyChanged(); 98 } 99 100 /** Sets the content description of the percentage. */ setPercentageContentDescription(CharSequence contentDescription)101 public void setPercentageContentDescription(CharSequence contentDescription) { 102 mProgressContentDescription = contentDescription; 103 notifyChanged(); 104 } 105 106 /** Gets the percentage to show. */ getPercentage()107 public String getPercentage() { 108 return mProgress.toString(); 109 } 110 111 /** Sets whether to show anomaly icon */ shouldShowAnomalyIcon(boolean showAnomalyIcon)112 public void shouldShowAnomalyIcon(boolean showAnomalyIcon) { 113 mShowAnomalyIcon = showAnomalyIcon; 114 notifyChanged(); 115 } 116 117 /** Gets whether to show anomaly icon */ showAnomalyIcon()118 public boolean showAnomalyIcon() { 119 return mShowAnomalyIcon; 120 } 121 setBatteryDiffEntry(BatteryDiffEntry entry)122 public void setBatteryDiffEntry(BatteryDiffEntry entry) { 123 mBatteryDiffEntry = entry; 124 } 125 getInfo()126 BatteryEntry getInfo() { 127 return mInfo; 128 } 129 getBatteryDiffEntry()130 BatteryDiffEntry getBatteryDiffEntry() { 131 return mBatteryDiffEntry; 132 } 133 134 @Override onBindViewHolder(PreferenceViewHolder view)135 public void onBindViewHolder(PreferenceViewHolder view) { 136 super.onBindViewHolder(view); 137 138 final boolean isNightMode = Utils.isNightMode(getContext()); 139 final float alpha = 140 isSelectable() 141 ? SELECTABLE_ALPHA 142 : (isNightMode 143 ? UNSELECTABLE_ALPHA_DARK_MODE 144 : UNSELECTABLE_ALPHA_LIGHT_MODE); 145 setViewAlpha(view.itemView, alpha); 146 147 final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary); 148 subtitle.setText(mProgress); 149 if (!TextUtils.isEmpty(mProgressContentDescription)) { 150 subtitle.setContentDescription(mProgressContentDescription); 151 } 152 if (mShowAnomalyIcon) { 153 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds( 154 R.drawable.ic_warning_24dp, 0, 0, 0); 155 } else { 156 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0); 157 } 158 if (mContentDescription != null) { 159 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 160 titleView.setContentDescription(mContentDescription); 161 } 162 163 if (!isSelectable()) { 164 // Set colors consistently to meet contrast requirements for non-selectable items 165 ((TextView) view.findViewById(android.R.id.title)).setTextColor(mTitleColorNormal); 166 ((TextView) view.findViewById(android.R.id.summary)).setTextColor(mTitleColorNormal); 167 subtitle.setTextColor(mTitleColorNormal); 168 } 169 } 170 setViewAlpha(View view, float alpha)171 private static void setViewAlpha(View view, float alpha) { 172 if (view instanceof ViewGroup) { 173 final ViewGroup viewGroup = (ViewGroup) view; 174 for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) { 175 setViewAlpha(viewGroup.getChildAt(i), alpha); 176 } 177 } else { 178 view.setAlpha(alpha); 179 } 180 } 181 } 182