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