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.settings.fuelgauge.WarningFramePreference; 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 WarningFramePreference { 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 PowerGaugePreference( Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info)55 public PowerGaugePreference( 56 Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info) { 57 this(context, null, icon, contentDescription, info); 58 } 59 PowerGaugePreference(Context context)60 public PowerGaugePreference(Context context) { 61 this(context, null, null, null, null); 62 } 63 PowerGaugePreference(Context context, AttributeSet attrs)64 public PowerGaugePreference(Context context, AttributeSet attrs) { 65 this(context, attrs, null, null, null); 66 } 67 PowerGaugePreference( Context context, AttributeSet attrs, Drawable icon, CharSequence contentDescription, BatteryEntry info)68 private PowerGaugePreference( 69 Context context, 70 AttributeSet attrs, 71 Drawable icon, 72 CharSequence contentDescription, 73 BatteryEntry info) { 74 super(context, attrs); 75 if (icon != null) { 76 setIcon(icon); 77 } 78 setWidgetLayoutResource(R.layout.preference_widget_summary); 79 mInfo = info; 80 mContentDescription = contentDescription; 81 mTitleColorNormal = 82 Utils.getColorAttrDefaultColor(context, android.R.attr.textColorPrimary); 83 } 84 85 /** Sets the content description. */ setContentDescription(String name)86 public void setContentDescription(String name) { 87 mContentDescription = name; 88 notifyChanged(); 89 } 90 91 /** Sets the percentage to show. */ setPercentage(CharSequence percentage)92 public void setPercentage(CharSequence percentage) { 93 mProgress = percentage; 94 mProgressContentDescription = percentage; 95 notifyChanged(); 96 } 97 98 /** Sets the content description of the percentage. */ setPercentageContentDescription(CharSequence contentDescription)99 public void setPercentageContentDescription(CharSequence contentDescription) { 100 mProgressContentDescription = contentDescription; 101 notifyChanged(); 102 } 103 104 /** Gets the percentage to show. */ getPercentage()105 public String getPercentage() { 106 return mProgress.toString(); 107 } 108 setBatteryDiffEntry(BatteryDiffEntry entry)109 public void setBatteryDiffEntry(BatteryDiffEntry entry) { 110 mBatteryDiffEntry = entry; 111 } 112 getInfo()113 BatteryEntry getInfo() { 114 return mInfo; 115 } 116 getBatteryDiffEntry()117 BatteryDiffEntry getBatteryDiffEntry() { 118 return mBatteryDiffEntry; 119 } 120 121 @Override onBindViewHolder(PreferenceViewHolder view)122 public void onBindViewHolder(PreferenceViewHolder view) { 123 super.onBindViewHolder(view); 124 125 final boolean isNightMode = Utils.isNightMode(getContext()); 126 final float alpha = 127 isSelectable() 128 ? SELECTABLE_ALPHA 129 : (isNightMode 130 ? UNSELECTABLE_ALPHA_DARK_MODE 131 : UNSELECTABLE_ALPHA_LIGHT_MODE); 132 setViewAlpha(view.itemView, alpha); 133 134 final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary); 135 subtitle.setText(mProgress); 136 if (!TextUtils.isEmpty(mProgressContentDescription)) { 137 subtitle.setContentDescription(mProgressContentDescription); 138 } 139 if (mContentDescription != null) { 140 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 141 titleView.setContentDescription(mContentDescription); 142 } 143 144 if (!isSelectable()) { 145 // Set colors consistently to meet contrast requirements for non-selectable items 146 ((TextView) view.findViewById(android.R.id.title)).setTextColor(mTitleColorNormal); 147 ((TextView) view.findViewById(android.R.id.summary)).setTextColor(mTitleColorNormal); 148 subtitle.setTextColor(mTitleColorNormal); 149 } 150 } 151 setViewAlpha(View view, float alpha)152 private static void setViewAlpha(View view, float alpha) { 153 if (view instanceof ViewGroup) { 154 final ViewGroup viewGroup = (ViewGroup) view; 155 for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) { 156 setViewAlpha(viewGroup.getChildAt(i), alpha); 157 } 158 } else { 159 view.setAlpha(alpha); 160 } 161 } 162 } 163