Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | */ |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 16 | package com.android.systemui; |
| 17 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 18 | import static android.app.StatusBarManager.DISABLE2_SYSTEM_ICONS; |
| 19 | import static android.app.StatusBarManager.DISABLE_NONE; |
Evan Laird | bcf631d | 2017-03-10 10:56:45 -0500 | [diff] [blame] | 20 | import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; |
| 21 | |
Lucas Dupin | 8968f6a | 2019-08-09 17:41:15 -0700 | [diff] [blame] | 22 | import static com.android.systemui.DejankUtils.whitelistIpcs; |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 23 | import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle; |
| 24 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 25 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 26 | |
shawnlin | f603ea0 | 2019-03-12 15:44:37 +0800 | [diff] [blame] | 27 | import android.animation.LayoutTransition; |
| 28 | import android.animation.ObjectAnimator; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 29 | import android.annotation.IntDef; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 30 | import android.app.ActivityManager; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 31 | import android.content.Context; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 32 | import android.content.res.Resources; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 34 | import android.database.ContentObserver; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 35 | import android.graphics.Rect; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
| 37 | import android.os.Handler; |
| 38 | import android.provider.Settings; |
Salvador Martinez | 7de8929 | 2019-04-23 14:55:36 -0700 | [diff] [blame] | 39 | import android.text.TextUtils; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 40 | import android.util.ArraySet; |
| 41 | import android.util.AttributeSet; |
| 42 | import android.util.TypedValue; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 43 | import android.view.Gravity; |
| 44 | import android.view.LayoutInflater; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 45 | import android.view.ViewGroup; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 46 | import android.widget.ImageView; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 47 | import android.widget.LinearLayout; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 48 | import android.widget.TextView; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 49 | |
Fabian Kozynski | bca09b7 | 2019-04-04 15:57:42 -0400 | [diff] [blame] | 50 | import androidx.annotation.StyleRes; |
| 51 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 52 | import com.android.settingslib.Utils; |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 53 | import com.android.settingslib.graph.ThemedBatteryDrawable; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 54 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Beverly | 1be62f4 | 2018-12-19 17:17:48 -0500 | [diff] [blame] | 55 | import com.android.systemui.plugins.DarkIconDispatcher; |
| 56 | import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 57 | import com.android.systemui.settings.CurrentUserTracker; |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 58 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 59 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 60 | import com.android.systemui.statusbar.policy.BatteryController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 61 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
| 62 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 63 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 64 | import com.android.systemui.tuner.TunerService; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 65 | import com.android.systemui.tuner.TunerService.Tunable; |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 66 | import com.android.systemui.util.Utils.DisableStateTracker; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 67 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
| 69 | import java.io.PrintWriter; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 70 | import java.lang.annotation.Retention; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 71 | import java.text.NumberFormat; |
| 72 | |
| 73 | public class BatteryMeterView extends LinearLayout implements |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 74 | BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 75 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 76 | |
| 77 | @Retention(SOURCE) |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 78 | @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE}) |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 79 | public @interface BatteryPercentMode {} |
| 80 | public static final int MODE_DEFAULT = 0; |
| 81 | public static final int MODE_ON = 1; |
| 82 | public static final int MODE_OFF = 2; |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 83 | public static final int MODE_ESTIMATE = 3; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 84 | |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 85 | private final ThemedBatteryDrawable mDrawable; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 86 | private final String mSlotBattery; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 87 | private final ImageView mBatteryIconView; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 88 | private final CurrentUserTracker mUserTracker; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 89 | private TextView mBatteryPercentView; |
| 90 | |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 91 | private BatteryController mBatteryController; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 92 | private SettingObserver mSettingObserver; |
Fabian Kozynski | bca09b7 | 2019-04-04 15:57:42 -0400 | [diff] [blame] | 93 | private final @StyleRes int mPercentageStyleId; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 94 | private int mTextColor; |
| 95 | private int mLevel; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 96 | private int mShowPercentMode = MODE_DEFAULT; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 97 | private boolean mForceShowPercent; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 98 | private boolean mShowPercentAvailable; |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 99 | // Some places may need to show the battery conditionally, and not obey the tuner |
| 100 | private boolean mIgnoreTunerUpdates; |
| 101 | private boolean mIsSubscribedForTunerUpdates; |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 102 | private boolean mCharging; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 103 | |
Fabian Kozynski | f86df99 | 2019-04-22 14:23:47 -0400 | [diff] [blame] | 104 | private DualToneHandler mDualToneHandler; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 105 | private int mUser; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 106 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings. |
| 109 | */ |
| 110 | private boolean mUseWallpaperTextColors; |
| 111 | |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 112 | private int mNonAdaptedSingleToneColor; |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 113 | private int mNonAdaptedForegroundColor; |
| 114 | private int mNonAdaptedBackgroundColor; |
| 115 | |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 116 | public BatteryMeterView(Context context, AttributeSet attrs) { |
| 117 | this(context, attrs, 0); |
| 118 | } |
| 119 | |
| 120 | public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) { |
| 121 | super(context, attrs, defStyle); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 122 | BroadcastDispatcher broadcastDispatcher = Dependency.get(BroadcastDispatcher.class); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 123 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 124 | setOrientation(LinearLayout.HORIZONTAL); |
| 125 | setGravity(Gravity.CENTER_VERTICAL | Gravity.START); |
| 126 | |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 127 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 128 | defStyle, 0); |
| 129 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Daniel Nishi | 8af93cb | 2017-05-15 14:37:20 -0700 | [diff] [blame] | 130 | context.getColor(R.color.meter_background_color)); |
Fabian Kozynski | bca09b7 | 2019-04-04 15:57:42 -0400 | [diff] [blame] | 131 | mPercentageStyleId = atts.getResourceId(R.styleable.BatteryMeterView_textAppearance, 0); |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 132 | mDrawable = new ThemedBatteryDrawable(context, frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 133 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 134 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 135 | mSettingObserver = new SettingObserver(new Handler(context.getMainLooper())); |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 136 | mShowPercentAvailable = context.getResources().getBoolean( |
| 137 | com.android.internal.R.bool.config_battery_percentage_setting_available); |
| 138 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 139 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 140 | addOnAttachStateChangeListener( |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 141 | new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS, |
| 142 | Dependency.get(CommandQueue.class))); |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 143 | |
shawnlin | f603ea0 | 2019-03-12 15:44:37 +0800 | [diff] [blame] | 144 | setupLayoutTransition(); |
| 145 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 146 | mSlotBattery = context.getString( |
| 147 | com.android.internal.R.string.status_bar_battery); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 148 | mBatteryIconView = new ImageView(context); |
| 149 | mBatteryIconView.setImageDrawable(mDrawable); |
| 150 | final MarginLayoutParams mlp = new MarginLayoutParams( |
| 151 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width), |
| 152 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height)); |
| 153 | mlp.setMargins(0, 0, 0, |
| 154 | getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom)); |
| 155 | addView(mBatteryIconView, mlp); |
| 156 | |
| 157 | updateShowPercent(); |
Fabian Kozynski | f86df99 | 2019-04-22 14:23:47 -0400 | [diff] [blame] | 158 | mDualToneHandler = new DualToneHandler(context); |
Jason Monk | d866b8a | 2017-03-29 15:30:44 -0400 | [diff] [blame] | 159 | // Init to not dark at all. |
| 160 | onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 161 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 162 | mUserTracker = new CurrentUserTracker(broadcastDispatcher) { |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 163 | @Override |
| 164 | public void onUserSwitched(int newUserId) { |
| 165 | mUser = newUserId; |
| 166 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
| 167 | getContext().getContentResolver().registerContentObserver( |
| 168 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, |
| 169 | newUserId); |
Evan Laird | 6ead1e3 | 2018-06-19 13:39:05 -0400 | [diff] [blame] | 170 | updateShowPercent(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 171 | } |
| 172 | }; |
Evan Laird | 39ea810 | 2018-05-18 19:49:07 -0400 | [diff] [blame] | 173 | |
| 174 | setClipChildren(false); |
| 175 | setClipToPadding(false); |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 176 | Dependency.get(ConfigurationController.class).observe(viewAttachLifecycle(this), this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 177 | } |
| 178 | |
shawnlin | f603ea0 | 2019-03-12 15:44:37 +0800 | [diff] [blame] | 179 | private void setupLayoutTransition() { |
| 180 | LayoutTransition transition = new LayoutTransition(); |
| 181 | transition.setDuration(200); |
| 182 | |
| 183 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f); |
| 184 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 185 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 186 | |
| 187 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f); |
| 188 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 189 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 190 | |
| 191 | setLayoutTransition(transition); |
| 192 | } |
| 193 | |
Dan Sandler | df14c20 | 2017-02-21 14:51:11 -0500 | [diff] [blame] | 194 | public void setForceShowPercent(boolean show) { |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 195 | setPercentShowMode(show ? MODE_ON : MODE_DEFAULT); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Force a particular mode of showing percent |
| 200 | * |
| 201 | * 0 - No preference |
| 202 | * 1 - Force on |
| 203 | * 2 - Force off |
| 204 | * @param mode desired mode (none, on, off) |
| 205 | */ |
| 206 | public void setPercentShowMode(@BatteryPercentMode int mode) { |
| 207 | mShowPercentMode = mode; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 208 | updateShowPercent(); |
| 209 | } |
| 210 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 211 | /** |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 212 | * Set {@code true} to turn off BatteryMeterView's subscribing to the tuner for updates, and |
| 213 | * thus avoid it controlling its own visibility |
| 214 | * |
| 215 | * @param ignore whether to ignore the tuner or not |
| 216 | */ |
| 217 | public void setIgnoreTunerUpdates(boolean ignore) { |
| 218 | mIgnoreTunerUpdates = ignore; |
| 219 | updateTunerSubscription(); |
| 220 | } |
| 221 | |
| 222 | private void updateTunerSubscription() { |
| 223 | if (mIgnoreTunerUpdates) { |
| 224 | unsubscribeFromTunerUpdates(); |
| 225 | } else { |
| 226 | subscribeForTunerUpdates(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private void subscribeForTunerUpdates() { |
| 231 | if (mIsSubscribedForTunerUpdates || mIgnoreTunerUpdates) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | Dependency.get(TunerService.class) |
| 236 | .addTunable(this, StatusBarIconController.ICON_BLACKLIST); |
| 237 | mIsSubscribedForTunerUpdates = true; |
| 238 | } |
| 239 | |
| 240 | private void unsubscribeFromTunerUpdates() { |
| 241 | if (!mIsSubscribedForTunerUpdates) { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | Dependency.get(TunerService.class).removeTunable(this); |
| 246 | mIsSubscribedForTunerUpdates = false; |
| 247 | } |
| 248 | |
| 249 | /** |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 250 | * Sets whether the battery meter view uses the wallpaperTextColor. If we're not using it, we'll |
| 251 | * revert back to dark-mode-based/tinted colors. |
| 252 | * |
| 253 | * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for all |
| 254 | * components |
| 255 | */ |
| 256 | public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) { |
| 257 | if (shouldUseWallpaperTextColor == mUseWallpaperTextColors) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | mUseWallpaperTextColors = shouldUseWallpaperTextColor; |
| 262 | |
| 263 | if (mUseWallpaperTextColors) { |
| 264 | updateColors( |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 265 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor), |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 266 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary), |
| 267 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor)); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 268 | } else { |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 269 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor, |
| 270 | mNonAdaptedSingleToneColor); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 274 | public void setColorsFromContext(Context context) { |
| 275 | if (context == null) { |
| 276 | return; |
| 277 | } |
| 278 | |
Fabian Kozynski | f86df99 | 2019-04-22 14:23:47 -0400 | [diff] [blame] | 279 | mDualToneHandler.setColorsFromContext(context); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 282 | @Override |
| 283 | public boolean hasOverlappingRendering() { |
| 284 | return false; |
| 285 | } |
| 286 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 287 | @Override |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 288 | public void onTuningChanged(String key, String newValue) { |
| 289 | if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { |
| 290 | ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 295 | public void onAttachedToWindow() { |
| 296 | super.onAttachedToWindow(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 297 | mBatteryController = Dependency.get(BatteryController.class); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 298 | mBatteryController.addCallback(this); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 299 | mUser = ActivityManager.getCurrentUser(); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 300 | getContext().getContentResolver().registerContentObserver( |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 301 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser); |
Salvador Martinez | 7de8929 | 2019-04-23 14:55:36 -0700 | [diff] [blame] | 302 | getContext().getContentResolver().registerContentObserver( |
| 303 | Settings.Global.getUriFor(Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME), |
| 304 | false, mSettingObserver); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 305 | updateShowPercent(); |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 306 | subscribeForTunerUpdates(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 307 | mUserTracker.startTracking(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 308 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 309 | |
| 310 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 311 | public void onDetachedFromWindow() { |
| 312 | super.onDetachedFromWindow(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 313 | mUserTracker.stopTracking(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 314 | mBatteryController.removeCallback(this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 315 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 316 | unsubscribeFromTunerUpdates(); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 317 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 318 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 319 | @Override |
| 320 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
jackqdyulei | ff5bd94 | 2017-04-04 10:54:21 -0700 | [diff] [blame] | 321 | mDrawable.setCharging(pluggedIn); |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 322 | mDrawable.setBatteryLevel(level); |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 323 | mCharging = pluggedIn; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 324 | mLevel = level; |
| 325 | updatePercentText(); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 328 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 329 | public void onPowerSaveChanged(boolean isPowerSave) { |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 330 | mDrawable.setPowerSaveEnabled(isPowerSave); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 331 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 332 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 333 | private TextView loadPercentView() { |
| 334 | return (TextView) LayoutInflater.from(getContext()) |
| 335 | .inflate(R.layout.battery_percentage_view, null); |
| 336 | } |
| 337 | |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 338 | /** |
| 339 | * Updates percent view by removing old one and reinflating if necessary |
| 340 | */ |
| 341 | public void updatePercentView() { |
| 342 | if (mBatteryPercentView != null) { |
| 343 | removeView(mBatteryPercentView); |
| 344 | mBatteryPercentView = null; |
| 345 | } |
| 346 | updateShowPercent(); |
| 347 | } |
| 348 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 349 | private void updatePercentText() { |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 350 | if (mBatteryController == null) { |
| 351 | return; |
| 352 | } |
| 353 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 354 | if (mBatteryPercentView != null) { |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 355 | if (mShowPercentMode == MODE_ESTIMATE && !mCharging) { |
| 356 | mBatteryController.getEstimatedTimeRemainingString((String estimate) -> { |
Evan Laird | 85ee4a3 | 2019-03-06 18:09:20 -0500 | [diff] [blame] | 357 | if (estimate != null) { |
| 358 | mBatteryPercentView.setText(estimate); |
Amin Shaikh | 0fcc337 | 2019-05-29 14:08:41 -0400 | [diff] [blame] | 359 | setContentDescription(getContext().getString( |
Amin Shaikh | 4438674 | 2019-05-31 08:53:50 -0400 | [diff] [blame] | 360 | R.string.accessibility_battery_level_with_estimate, |
| 361 | mLevel, estimate)); |
Evan Laird | 85ee4a3 | 2019-03-06 18:09:20 -0500 | [diff] [blame] | 362 | } else { |
| 363 | setPercentTextAtCurrentLevel(); |
| 364 | } |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 365 | }); |
| 366 | } else { |
Evan Laird | 85ee4a3 | 2019-03-06 18:09:20 -0500 | [diff] [blame] | 367 | setPercentTextAtCurrentLevel(); |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 368 | } |
Evan Laird | 7839fe8 | 2019-06-14 13:27:54 -0400 | [diff] [blame] | 369 | } else { |
| 370 | setContentDescription( |
| 371 | getContext().getString(mCharging ? R.string.accessibility_battery_level_charging |
| 372 | : R.string.accessibility_battery_level, mLevel)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Evan Laird | 85ee4a3 | 2019-03-06 18:09:20 -0500 | [diff] [blame] | 376 | private void setPercentTextAtCurrentLevel() { |
| 377 | mBatteryPercentView.setText( |
| 378 | NumberFormat.getPercentInstance().format(mLevel / 100f)); |
Amin Shaikh | 0fcc337 | 2019-05-29 14:08:41 -0400 | [diff] [blame] | 379 | setContentDescription( |
| 380 | getContext().getString(mCharging ? R.string.accessibility_battery_level_charging |
| 381 | : R.string.accessibility_battery_level, mLevel)); |
Evan Laird | 85ee4a3 | 2019-03-06 18:09:20 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 384 | private void updateShowPercent() { |
| 385 | final boolean showing = mBatteryPercentView != null; |
Lucas Dupin | 8968f6a | 2019-08-09 17:41:15 -0700 | [diff] [blame] | 386 | // TODO(b/140051051) |
| 387 | final boolean systemSetting = 0 != whitelistIpcs(() -> Settings.System |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 388 | .getIntForUser(getContext().getContentResolver(), |
Lucas Dupin | 8968f6a | 2019-08-09 17:41:15 -0700 | [diff] [blame] | 389 | SHOW_BATTERY_PERCENT, 0, mUser)); |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 390 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 391 | if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF) |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame] | 392 | || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 393 | if (!showing) { |
| 394 | mBatteryPercentView = loadPercentView(); |
Fabian Kozynski | bca09b7 | 2019-04-04 15:57:42 -0400 | [diff] [blame] | 395 | if (mPercentageStyleId != 0) { // Only set if specified as attribute |
| 396 | mBatteryPercentView.setTextAppearance(mPercentageStyleId); |
| 397 | } |
Fabian Kozynski | 7c35d33 | 2019-04-12 09:24:13 -0400 | [diff] [blame] | 398 | if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 399 | updatePercentText(); |
| 400 | addView(mBatteryPercentView, |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 401 | new ViewGroup.LayoutParams( |
| 402 | LayoutParams.WRAP_CONTENT, |
| 403 | LayoutParams.MATCH_PARENT)); |
| 404 | } |
| 405 | } else { |
| 406 | if (showing) { |
| 407 | removeView(mBatteryPercentView); |
| 408 | mBatteryPercentView = null; |
| 409 | } |
| 410 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 413 | @Override |
| 414 | public void onDensityOrFontScaleChanged() { |
| 415 | scaleBatteryMeterViews(); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Looks up the scale factor for status bar icons and scales the battery view by that amount. |
| 420 | */ |
| 421 | private void scaleBatteryMeterViews() { |
| 422 | Resources res = getContext().getResources(); |
| 423 | TypedValue typedValue = new TypedValue(); |
| 424 | |
| 425 | res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 426 | float iconScaleFactor = typedValue.getFloat(); |
| 427 | |
| 428 | int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height); |
| 429 | int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width); |
| 430 | int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom); |
| 431 | |
| 432 | LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams( |
| 433 | (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 434 | scaledLayoutParams.setMargins(0, 0, 0, marginBottom); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 435 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 436 | mBatteryIconView.setLayoutParams(scaledLayoutParams); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 441 | float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0; |
Fabian Kozynski | f86df99 | 2019-04-22 14:23:47 -0400 | [diff] [blame] | 442 | mNonAdaptedSingleToneColor = mDualToneHandler.getSingleColor(intensity); |
| 443 | mNonAdaptedForegroundColor = mDualToneHandler.getFillColor(intensity); |
| 444 | mNonAdaptedBackgroundColor = mDualToneHandler.getBackgroundColor(intensity); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 445 | |
| 446 | if (!mUseWallpaperTextColors) { |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 447 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor, |
| 448 | mNonAdaptedSingleToneColor); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 449 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 452 | private void updateColors(int foregroundColor, int backgroundColor, int singleToneColor) { |
| 453 | mDrawable.setColors(foregroundColor, backgroundColor, singleToneColor); |
| 454 | mTextColor = singleToneColor; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 455 | if (mBatteryPercentView != null) { |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 456 | mBatteryPercentView.setTextColor(singleToneColor); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 457 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 458 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 459 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 460 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Evan Laird | 2259da4 | 2019-02-08 16:48:53 -0500 | [diff] [blame] | 461 | String powerSave = mDrawable == null ? null : mDrawable.getPowerSaveEnabled() + ""; |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 462 | CharSequence percent = mBatteryPercentView == null ? null : mBatteryPercentView.getText(); |
| 463 | pw.println(" BatteryMeterView:"); |
| 464 | pw.println(" mDrawable.getPowerSave: " + powerSave); |
| 465 | pw.println(" mBatteryPercentView.getText(): " + percent); |
| 466 | pw.println(" mTextColor: #" + Integer.toHexString(mTextColor)); |
| 467 | pw.println(" mLevel: " + mLevel); |
| 468 | pw.println(" mForceShowPercent: " + mForceShowPercent); |
| 469 | } |
| 470 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 471 | private final class SettingObserver extends ContentObserver { |
| 472 | public SettingObserver(Handler handler) { |
| 473 | super(handler); |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void onChange(boolean selfChange, Uri uri) { |
| 478 | super.onChange(selfChange, uri); |
| 479 | updateShowPercent(); |
Salvador Martinez | 7de8929 | 2019-04-23 14:55:36 -0700 | [diff] [blame] | 480 | if (TextUtils.equals(uri.getLastPathSegment(), |
| 481 | Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME)) { |
| 482 | // update the text for sure if the estimate in the cache was updated |
| 483 | updatePercentText(); |
| 484 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 487 | } |