blob: 25436a9ad45b04e7d94dd623e1033ed1d82f3d41 [file] [log] [blame]
Adrian Roos12c1ef52014-06-04 13:54:08 +02001/*
2 * Copyright (C) 2014 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
17package com.android.systemui.statusbar;
18
Beverly8c785892018-01-31 17:25:52 -050019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010021import android.app.admin.DevicePolicyManager;
Adrian Roos12c1ef52014-06-04 13:54:08 +020022import android.content.Context;
Jason Chang2386a372018-04-24 16:05:30 +080023import android.content.res.ColorStateList;
Lucas Dupinff6628d2018-10-15 10:12:37 -070024import android.content.res.Resources;
Jorim Jaggi27c9b742015-04-09 10:34:49 -070025import android.graphics.Color;
Lucas Dupinff6628d2018-10-15 10:12:37 -070026import android.hardware.biometrics.BiometricSourceType;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020027import android.hardware.face.FaceManager;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070028import android.hardware.fingerprint.FingerprintManager;
Adrian Roos12c1ef52014-06-04 13:54:08 +020029import android.os.BatteryManager;
30import android.os.BatteryStats;
31import android.os.Handler;
32import android.os.Message;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -060035import android.os.UserManager;
Adrian Roos12c1ef52014-06-04 13:54:08 +020036import android.text.TextUtils;
37import android.text.format.Formatter;
38import android.util.Log;
39import android.view.View;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010040import android.view.ViewGroup;
Adrian Roos12c1ef52014-06-04 13:54:08 +020041
Adrian Roosc1b50322017-02-27 21:07:58 +010042import com.android.internal.annotations.VisibleForTesting;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070043import com.android.internal.app.IBatteryStats;
44import com.android.keyguard.KeyguardUpdateMonitor;
45import com.android.keyguard.KeyguardUpdateMonitorCallback;
Jason Monk58be7a62017-02-01 20:17:51 -050046import com.android.settingslib.Utils;
Jason Monk9c7844c2017-01-18 15:21:53 -050047import com.android.systemui.Dependency;
Beverly8c785892018-01-31 17:25:52 -050048import com.android.systemui.Interpolators;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070049import com.android.systemui.R;
Beverly8fdb5332019-02-04 14:29:49 -050050import com.android.systemui.plugins.statusbar.StatusBarStateController;
51import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070052import com.android.systemui.statusbar.phone.KeyguardIndicationTextView;
53import com.android.systemui.statusbar.phone.LockIcon;
54import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
Zachary Iqbalf50284c2017-01-22 18:54:46 -080055import com.android.systemui.statusbar.policy.UserInfoController;
Adrian Roosc1b50322017-02-27 21:07:58 +010056import com.android.systemui.util.wakelock.SettableWakeLock;
57import com.android.systemui.util.wakelock.WakeLock;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070058
Lucas Dupin3fcdd472018-01-19 19:06:45 -080059import java.io.FileDescriptor;
60import java.io.PrintWriter;
Lucas Dupin4272f442018-01-13 22:00:35 -080061import java.text.NumberFormat;
Lucas Dupin8d595d22018-03-08 10:34:58 -080062import java.util.IllegalFormatConversionException;
Lucas Dupin4272f442018-01-13 22:00:35 -080063
Adrian Roos12c1ef52014-06-04 13:54:08 +020064/**
Selim Cinekcfafe4e2015-08-11 14:58:44 -070065 * Controls the indications and error messages shown on the Keyguard
Adrian Roos12c1ef52014-06-04 13:54:08 +020066 */
Evan Laird878c8532018-10-15 15:54:29 -040067public class KeyguardIndicationController implements StateListener {
Adrian Roos12c1ef52014-06-04 13:54:08 +020068
Adrian Roos0c859ae2015-11-23 16:47:50 -080069 private static final String TAG = "KeyguardIndication";
70 private static final boolean DEBUG_CHARGING_SPEED = false;
Adrian Roos12c1ef52014-06-04 13:54:08 +020071
72 private static final int MSG_HIDE_TRANSIENT = 1;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020073 private static final int MSG_CLEAR_BIOMETRIC_MSG = 2;
74 private static final long TRANSIENT_BIOMETRIC_ERROR_TIMEOUT = 1300;
Adrian Roos12c1ef52014-06-04 13:54:08 +020075
76 private final Context mContext;
Lucas Dupin987f1932017-05-13 21:02:52 -070077 private ViewGroup mIndicationArea;
78 private KeyguardIndicationTextView mTextView;
79 private KeyguardIndicationTextView mDisclosure;
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -060080 private final UserManager mUserManager;
Adrian Roos12c1ef52014-06-04 13:54:08 +020081 private final IBatteryStats mBatteryInfo;
Adrian Roosc1b50322017-02-27 21:07:58 +010082 private final SettableWakeLock mWakeLock;
Adrian Roos12c1ef52014-06-04 13:54:08 +020083
Adrian Roos7b043112015-07-10 13:00:33 -070084 private final int mSlowThreshold;
85 private final int mFastThreshold;
Lucas Dupin05904652019-04-09 16:16:15 -070086 private final LockIcon mLockIcon;
Selim Cinekcfafe4e2015-08-11 14:58:44 -070087 private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
Adrian Roos7b043112015-07-10 13:00:33 -070088
Adrian Roos12c1ef52014-06-04 13:54:08 +020089 private String mRestingIndication;
Lucas Dupinef886542018-01-03 16:03:07 -080090 private CharSequence mTransientIndication;
Jason Chang2386a372018-04-24 16:05:30 +080091 private ColorStateList mTransientTextColorState;
92 private ColorStateList mInitialTextColorState;
Adrian Roos12c1ef52014-06-04 13:54:08 +020093 private boolean mVisible;
94
95 private boolean mPowerPluggedIn;
Beverly2034c832018-03-19 11:18:51 -040096 private boolean mPowerPluggedInWired;
Adrian Roos12c1ef52014-06-04 13:54:08 +020097 private boolean mPowerCharged;
Adrian Roos7b043112015-07-10 13:00:33 -070098 private int mChargingSpeed;
Adrian Roos0c859ae2015-11-23 16:47:50 -080099 private int mChargingWattage;
Lucas Dupin4272f442018-01-13 22:00:35 -0800100 private int mBatteryLevel;
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700101 private String mMessageToShowOnScreenOn;
Adrian Roos12c1ef52014-06-04 13:54:08 +0200102
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700103 private KeyguardUpdateMonitorCallback mUpdateMonitorCallback;
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800104
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100105 private final DevicePolicyManager mDevicePolicyManager;
Adrian Roos91ba3072017-02-14 16:50:46 +0100106 private boolean mDozing;
Lucas Dupinff6628d2018-10-15 10:12:37 -0700107 private float mDarkAmount;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100108
Adrian Roosaf45b602017-03-14 13:10:25 -0700109 /**
110 * Creates a new KeyguardIndicationController and registers callbacks.
111 */
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100112 public KeyguardIndicationController(Context context, ViewGroup indicationArea,
113 LockIcon lockIcon) {
Adrian Roosc1b50322017-02-27 21:07:58 +0100114 this(context, indicationArea, lockIcon,
115 WakeLock.createPartial(context, "Doze:KeyguardIndication"));
Adrian Roosaf45b602017-03-14 13:10:25 -0700116
117 registerCallbacks(KeyguardUpdateMonitor.getInstance(context));
Adrian Roosc1b50322017-02-27 21:07:58 +0100118 }
119
Adrian Roosaf45b602017-03-14 13:10:25 -0700120 /**
121 * Creates a new KeyguardIndicationController for testing. Does *not* register callbacks.
122 */
Adrian Roosc1b50322017-02-27 21:07:58 +0100123 @VisibleForTesting
124 KeyguardIndicationController(Context context, ViewGroup indicationArea, LockIcon lockIcon,
125 WakeLock wakeLock) {
Adrian Roos12c1ef52014-06-04 13:54:08 +0200126 mContext = context;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100127 mIndicationArea = indicationArea;
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800128 mTextView = indicationArea.findViewById(R.id.keyguard_indication_text);
Jason Chang2386a372018-04-24 16:05:30 +0800129 mInitialTextColorState = mTextView != null ?
130 mTextView.getTextColors() : ColorStateList.valueOf(Color.WHITE);
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800131 mDisclosure = indicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700132 mLockIcon = lockIcon;
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800133 mWakeLock = new SettableWakeLock(wakeLock, TAG);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200134
Adrian Roos7b043112015-07-10 13:00:33 -0700135 Resources res = context.getResources();
136 mSlowThreshold = res.getInteger(R.integer.config_chargingSlowlyThreshold);
137 mFastThreshold = res.getInteger(R.integer.config_chargingFastThreshold);
138
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600139 mUserManager = context.getSystemService(UserManager.class);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200140 mBatteryInfo = IBatteryStats.Stub.asInterface(
141 ServiceManager.getService(BatteryStats.SERVICE_NAME));
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600142
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100143 mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(
144 Context.DEVICE_POLICY_SERVICE);
145
Adrian Roosaf45b602017-03-14 13:10:25 -0700146 updateDisclosure();
147 }
148
149 private void registerCallbacks(KeyguardUpdateMonitor monitor) {
150 monitor.registerCallback(getKeyguardCallback());
151
Lucas Dupin7e171e22018-12-20 11:29:35 -0800152 KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mTickReceiver);
Jason Monkaf08c152018-12-04 11:12:39 -0500153 Dependency.get(StatusBarStateController.class).addCallback(this);
Evan Laird878c8532018-10-15 15:54:29 -0400154 }
155
156 /**
157 * Used by {@link com.android.systemui.statusbar.phone.StatusBar} to give the indication
158 * controller a chance to unregister itself as a receiver.
159 *
160 * //TODO: This can probably be converted to a fragment and not have to be manually recreated
161 */
162 public void destroy() {
Lucas Dupin7e171e22018-12-20 11:29:35 -0800163 KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mTickReceiver);
Jason Monkaf08c152018-12-04 11:12:39 -0500164 Dependency.get(StatusBarStateController.class).removeCallback(this);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100165 }
166
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800167 /**
168 * Gets the {@link KeyguardUpdateMonitorCallback} instance associated with this
169 * {@link KeyguardIndicationController}.
170 *
171 * <p>Subclasses may override this method to extend or change the callback behavior by extending
172 * the {@link BaseKeyguardCallback}.
173 *
174 * @return A KeyguardUpdateMonitorCallback. Multiple calls to this method <b>must</b> return the
175 * same instance.
176 */
177 protected KeyguardUpdateMonitorCallback getKeyguardCallback() {
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700178 if (mUpdateMonitorCallback == null) {
179 mUpdateMonitorCallback = new BaseKeyguardCallback();
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800180 }
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700181 return mUpdateMonitorCallback;
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800182 }
183
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100184 private void updateDisclosure() {
185 if (mDevicePolicyManager == null) {
186 return;
187 }
188
Adrian Roos91ba3072017-02-14 16:50:46 +0100189 if (!mDozing && mDevicePolicyManager.isDeviceManaged()) {
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100190 final CharSequence organizationName =
191 mDevicePolicyManager.getDeviceOwnerOrganizationName();
192 if (organizationName != null) {
193 mDisclosure.switchIndication(mContext.getResources().getString(
194 R.string.do_disclosure_with_name, organizationName));
195 } else {
196 mDisclosure.switchIndication(R.string.do_disclosure_generic);
197 }
198 mDisclosure.setVisibility(View.VISIBLE);
199 } else {
200 mDisclosure.setVisibility(View.GONE);
201 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200202 }
203
204 public void setVisible(boolean visible) {
205 mVisible = visible;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100206 mIndicationArea.setVisibility(visible ? View.VISIBLE : View.GONE);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200207 if (visible) {
Kevin Chyn4c4001c2017-08-25 14:23:36 -0700208 // If this is called after an error message was already shown, we should not clear it.
209 // Otherwise the error message won't be shown
210 if (!mHandler.hasMessages(MSG_HIDE_TRANSIENT)) {
211 hideTransientIndication();
212 }
Beverly8c785892018-01-31 17:25:52 -0500213 updateIndication(false);
Kevin Chyn4c4001c2017-08-25 14:23:36 -0700214 } else if (!visible) {
215 // If we unlock and return to keyguard quickly, previous error should not be shown
216 hideTransientIndication();
Adrian Roos12c1ef52014-06-04 13:54:08 +0200217 }
218 }
219
220 /**
221 * Sets the indication that is shown if nothing else is showing.
222 */
223 public void setRestingIndication(String restingIndication) {
224 mRestingIndication = restingIndication;
Beverly8c785892018-01-31 17:25:52 -0500225 updateIndication(false);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200226 }
227
228 /**
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800229 * Sets the active controller managing changes and callbacks to user information.
230 */
231 public void setUserInfoController(UserInfoController userInfoController) {
232 }
233
234 /**
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700235 * Returns the indication text indicating that trust has been granted.
236 *
237 * @return {@code null} or an empty string if a trust indication text should not be shown.
238 */
Lucas Dupin859f2c82019-04-17 18:30:00 -0700239 private String getTrustGrantedIndication() {
240 return mContext.getString(R.string.keyguard_indication_trust_unlocked);
Zachary Iqbaldc05aa02017-05-17 18:52:49 -0700241 }
242
243 /**
244 * Returns the indication text indicating that trust is currently being managed.
245 *
246 * @return {@code null} or an empty string if a trust managed text should not be shown.
247 */
Lucas Dupin859f2c82019-04-17 18:30:00 -0700248 private String getTrustManagedIndication() {
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700249 return null;
250 }
251
252 /**
Adrian Roos12c1ef52014-06-04 13:54:08 +0200253 * Hides transient indication in {@param delayMs}.
254 */
255 public void hideTransientIndicationDelayed(long delayMs) {
256 mHandler.sendMessageDelayed(
257 mHandler.obtainMessage(MSG_HIDE_TRANSIENT), delayMs);
258 }
259
260 /**
261 * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}.
262 */
263 public void showTransientIndication(int transientIndication) {
264 showTransientIndication(mContext.getResources().getString(transientIndication));
265 }
266
267 /**
268 * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}.
269 */
Lucas Dupinef886542018-01-03 16:03:07 -0800270 public void showTransientIndication(CharSequence transientIndication) {
Jason Chang2386a372018-04-24 16:05:30 +0800271 showTransientIndication(transientIndication, mInitialTextColorState);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700272 }
273
274 /**
275 * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}.
276 */
Jason Chang2386a372018-04-24 16:05:30 +0800277 public void showTransientIndication(CharSequence transientIndication,
278 ColorStateList textColorState) {
Adrian Roos12c1ef52014-06-04 13:54:08 +0200279 mTransientIndication = transientIndication;
Jason Chang2386a372018-04-24 16:05:30 +0800280 mTransientTextColorState = textColorState;
Adrian Roos12c1ef52014-06-04 13:54:08 +0200281 mHandler.removeMessages(MSG_HIDE_TRANSIENT);
Adrian Roosc1b50322017-02-27 21:07:58 +0100282 if (mDozing && !TextUtils.isEmpty(mTransientIndication)) {
283 // Make sure this doesn't get stuck and burns in. Acquire wakelock until its cleared.
284 mWakeLock.setAcquired(true);
285 hideTransientIndicationDelayed(BaseKeyguardCallback.HIDE_DELAY_MS);
286 }
Beverly8c785892018-01-31 17:25:52 -0500287
288 updateIndication(false);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200289 }
290
291 /**
292 * Hides transient indication.
293 */
294 public void hideTransientIndication() {
295 if (mTransientIndication != null) {
296 mTransientIndication = null;
297 mHandler.removeMessages(MSG_HIDE_TRANSIENT);
Beverly8c785892018-01-31 17:25:52 -0500298 updateIndication(false);
Adrian Roos12c1ef52014-06-04 13:54:08 +0200299 }
300 }
301
Beverly8c785892018-01-31 17:25:52 -0500302 protected final void updateIndication(boolean animate) {
Adrian Roosc1b50322017-02-27 21:07:58 +0100303 if (TextUtils.isEmpty(mTransientIndication)) {
304 mWakeLock.setAcquired(false);
305 }
306
Adrian Roos12c1ef52014-06-04 13:54:08 +0200307 if (mVisible) {
Lucas Dupin53d50622017-05-13 15:54:14 -0700308 // Walk down a precedence-ordered list of what indication
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600309 // should be shown based on user or device state
Lucas Dupinff6628d2018-10-15 10:12:37 -0700310 if (mDozing) {
Lucas Dupin07ed3df2019-03-26 21:23:59 -0700311 // When dozing we ignore any text color and use white instead, because
312 // colors can be hard to read in low brightness.
313 mTextView.setTextColor(Color.WHITE);
Lucas Dupinff6628d2018-10-15 10:12:37 -0700314 if (!TextUtils.isEmpty(mTransientIndication)) {
Lucas Dupinff6628d2018-10-15 10:12:37 -0700315 mTextView.switchIndication(mTransientIndication);
Lucas Dupin07ed3df2019-03-26 21:23:59 -0700316 } else if (mPowerPluggedIn) {
317 String indication = computePowerIndication();
318 if (animate) {
319 animateText(mTextView, indication);
320 } else {
321 mTextView.switchIndication(indication);
322 }
323 } else {
324 String percentage = NumberFormat.getPercentInstance()
325 .format(mBatteryLevel / 100f);
326 mTextView.switchIndication(percentage);
Lucas Dupinff6628d2018-10-15 10:12:37 -0700327 }
Lucas Dupinff6628d2018-10-15 10:12:37 -0700328 return;
329 }
330
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700331 KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Jorim Jaggifabc7432017-05-15 02:40:05 +0200332 int userId = KeyguardUpdateMonitor.getCurrentUser();
Zachary Iqbaldc05aa02017-05-17 18:52:49 -0700333 String trustGrantedIndication = getTrustGrantedIndication();
334 String trustManagedIndication = getTrustManagedIndication();
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700335 if (!mUserManager.isUserUnlocked(userId)) {
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600336 mTextView.switchIndication(com.android.internal.R.string.lockscreen_storage_locked);
Jason Chang2386a372018-04-24 16:05:30 +0800337 mTextView.setTextColor(mInitialTextColorState);
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600338 } else if (!TextUtils.isEmpty(mTransientIndication)) {
339 mTextView.switchIndication(mTransientIndication);
Jason Chang2386a372018-04-24 16:05:30 +0800340 mTextView.setTextColor(mTransientTextColorState);
Zachary Iqbaldc05aa02017-05-17 18:52:49 -0700341 } else if (!TextUtils.isEmpty(trustGrantedIndication)
Zachary Iqbal8f4c2422017-04-20 17:56:42 -0700342 && updateMonitor.getUserHasTrust(userId)) {
Zachary Iqbaldc05aa02017-05-17 18:52:49 -0700343 mTextView.switchIndication(trustGrantedIndication);
Jason Chang2386a372018-04-24 16:05:30 +0800344 mTextView.setTextColor(mInitialTextColorState);
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600345 } else if (mPowerPluggedIn) {
346 String indication = computePowerIndication();
347 if (DEBUG_CHARGING_SPEED) {
348 indication += ", " + (mChargingWattage / 1000) + " mW";
349 }
Jason Chang2386a372018-04-24 16:05:30 +0800350 mTextView.setTextColor(mInitialTextColorState);
Beverly85499d92018-02-14 15:55:16 -0500351 if (animate) {
352 animateText(mTextView, indication);
353 } else {
354 mTextView.switchIndication(indication);
355 }
Zachary Iqbaldc05aa02017-05-17 18:52:49 -0700356 } else if (!TextUtils.isEmpty(trustManagedIndication)
357 && updateMonitor.getUserTrustIsManaged(userId)
358 && !updateMonitor.getUserHasTrust(userId)) {
359 mTextView.switchIndication(trustManagedIndication);
Jason Chang2386a372018-04-24 16:05:30 +0800360 mTextView.setTextColor(mInitialTextColorState);
Jeff Sharkeyb6edaa92016-07-27 15:51:31 -0600361 } else {
362 mTextView.switchIndication(mRestingIndication);
Jason Chang2386a372018-04-24 16:05:30 +0800363 mTextView.setTextColor(mInitialTextColorState);
Adrian Roos7b043112015-07-10 13:00:33 -0700364 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200365 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200366 }
367
Beverly85499d92018-02-14 15:55:16 -0500368 // animates textView - textView moves up and bounces down
369 private void animateText(KeyguardIndicationTextView textView, String indication) {
370 int yTranslation = mContext.getResources().getInteger(
371 R.integer.wired_charging_keyguard_text_animation_distance);
372 int animateUpDuration = mContext.getResources().getInteger(
373 R.integer.wired_charging_keyguard_text_animation_duration_up);
374 int animateDownDuration = mContext.getResources().getInteger(
375 R.integer.wired_charging_keyguard_text_animation_duration_down);
Lucas Dupindef43692018-07-02 15:22:58 -0700376 textView.animate().cancel();
377 float translation = textView.getTranslationY();
Beverly85499d92018-02-14 15:55:16 -0500378 textView.animate()
379 .translationYBy(yTranslation)
380 .setInterpolator(Interpolators.LINEAR)
381 .setDuration(animateUpDuration)
382 .setListener(new AnimatorListenerAdapter() {
Lucas Dupindef43692018-07-02 15:22:58 -0700383 private boolean mCancelled;
384
Beverly85499d92018-02-14 15:55:16 -0500385 @Override
386 public void onAnimationStart(Animator animation) {
387 textView.switchIndication(indication);
388 }
Lucas Dupindef43692018-07-02 15:22:58 -0700389
390 @Override
391 public void onAnimationCancel(Animator animation) {
392 textView.setTranslationY(translation);
393 mCancelled = true;
394 }
395
Beverly85499d92018-02-14 15:55:16 -0500396 @Override
397 public void onAnimationEnd(Animator animation) {
Lucas Dupindef43692018-07-02 15:22:58 -0700398 if (mCancelled) {
399 return;
400 }
Beverly85499d92018-02-14 15:55:16 -0500401 textView.animate()
402 .setDuration(animateDownDuration)
403 .setInterpolator(Interpolators.BOUNCE)
Lucas Dupindef43692018-07-02 15:22:58 -0700404 .translationY(translation)
405 .setListener(new AnimatorListenerAdapter() {
406 @Override
407 public void onAnimationCancel(Animator animation) {
408 textView.setTranslationY(translation);
409 }
410 });
Beverly85499d92018-02-14 15:55:16 -0500411 }
412 });
413 }
414
Adrian Roos12c1ef52014-06-04 13:54:08 +0200415 private String computePowerIndication() {
416 if (mPowerCharged) {
417 return mContext.getResources().getString(R.string.keyguard_charged);
418 }
419
420 // Try fetching charging time from battery stats.
Adrian Roos7e39e592015-09-23 17:03:47 -0700421 long chargingTimeRemaining = 0;
Adrian Roos12c1ef52014-06-04 13:54:08 +0200422 try {
Adrian Roos7e39e592015-09-23 17:03:47 -0700423 chargingTimeRemaining = mBatteryInfo.computeChargeTimeRemaining();
424
Adrian Roos12c1ef52014-06-04 13:54:08 +0200425 } catch (RemoteException e) {
426 Log.e(TAG, "Error calling IBatteryStats: ", e);
427 }
Adrian Roos7e39e592015-09-23 17:03:47 -0700428 final boolean hasChargingTime = chargingTimeRemaining > 0;
Adrian Roos12c1ef52014-06-04 13:54:08 +0200429
Adrian Roos7b043112015-07-10 13:00:33 -0700430 int chargingId;
Beverly7d7f6992019-02-11 13:58:27 -0500431 if (mPowerPluggedInWired) {
432 switch (mChargingSpeed) {
433 case KeyguardUpdateMonitor.BatteryStatus.CHARGING_FAST:
434 chargingId = hasChargingTime
435 ? R.string.keyguard_indication_charging_time_fast
436 : R.string.keyguard_plugged_in_charging_fast;
437 break;
438 case KeyguardUpdateMonitor.BatteryStatus.CHARGING_SLOWLY:
439 chargingId = hasChargingTime
440 ? R.string.keyguard_indication_charging_time_slowly
441 : R.string.keyguard_plugged_in_charging_slowly;
442 break;
443 default:
444 chargingId = hasChargingTime
445 ? R.string.keyguard_indication_charging_time
446 : R.string.keyguard_plugged_in;
447 break;
448 }
449 } else {
450 chargingId = hasChargingTime
451 ? R.string.keyguard_indication_charging_time_wireless
452 : R.string.keyguard_plugged_in_wireless;
Adrian Roos7b043112015-07-10 13:00:33 -0700453 }
Adrian Roos7e39e592015-09-23 17:03:47 -0700454
Lucas Dupin8d595d22018-03-08 10:34:58 -0800455 String percentage = NumberFormat.getPercentInstance()
456 .format(mBatteryLevel / 100f);
Adrian Roos7e39e592015-09-23 17:03:47 -0700457 if (hasChargingTime) {
Lucas Dupin8d595d22018-03-08 10:34:58 -0800458 // We now have battery percentage in these strings and it's expected that all
459 // locales will also have it in the future. For now, we still have to support the old
460 // format until all languages get the new translations.
Adrian Roos7e39e592015-09-23 17:03:47 -0700461 String chargingTimeFormatted = Formatter.formatShortElapsedTimeRoundingUpToMinutes(
462 mContext, chargingTimeRemaining);
Lucas Dupin8d595d22018-03-08 10:34:58 -0800463 try {
464 return mContext.getResources().getString(chargingId, chargingTimeFormatted,
465 percentage);
466 } catch (IllegalFormatConversionException e) {
467 return mContext.getResources().getString(chargingId, chargingTimeFormatted);
468 }
Adrian Roos7e39e592015-09-23 17:03:47 -0700469 } else {
Lucas Dupin8d595d22018-03-08 10:34:58 -0800470 // Same as above
471 try {
472 return mContext.getResources().getString(chargingId, percentage);
473 } catch (IllegalFormatConversionException e) {
474 return mContext.getResources().getString(chargingId);
475 }
Adrian Roos7e39e592015-09-23 17:03:47 -0700476 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200477 }
478
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800479 public void setStatusBarKeyguardViewManager(
480 StatusBarKeyguardViewManager statusBarKeyguardViewManager) {
481 mStatusBarKeyguardViewManager = statusBarKeyguardViewManager;
482 }
483
Lucas Dupin7e171e22018-12-20 11:29:35 -0800484 private final KeyguardUpdateMonitorCallback mTickReceiver =
485 new KeyguardUpdateMonitorCallback() {
486 @Override
487 public void onTimeChanged() {
488 if (mVisible) {
489 updateIndication(false /* animate */);
490 }
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800491 }
Lucas Dupin7e171e22018-12-20 11:29:35 -0800492 };
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800493
494 private final Handler mHandler = new Handler() {
495 @Override
496 public void handleMessage(Message msg) {
Adrian Roosc1b50322017-02-27 21:07:58 +0100497 if (msg.what == MSG_HIDE_TRANSIENT) {
498 hideTransientIndication();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200499 } else if (msg.what == MSG_CLEAR_BIOMETRIC_MSG) {
Lucas Dupinc9e5d762019-01-28 09:34:30 -0800500 mLockIcon.setTransientBiometricsError(false);
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800501 }
502 }
503 };
504
Adrian Roos91ba3072017-02-14 16:50:46 +0100505 public void setDozing(boolean dozing) {
Jorim Jaggifabc7432017-05-15 02:40:05 +0200506 if (mDozing == dozing) {
507 return;
508 }
Adrian Roos91ba3072017-02-14 16:50:46 +0100509 mDozing = dozing;
Beverly8c785892018-01-31 17:25:52 -0500510 updateIndication(false);
Adrian Roos91ba3072017-02-14 16:50:46 +0100511 updateDisclosure();
512 }
513
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800514 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
515 pw.println("KeyguardIndicationController:");
Jason Chang2386a372018-04-24 16:05:30 +0800516 pw.println(" mTransientTextColorState: " + mTransientTextColorState);
517 pw.println(" mInitialTextColorState: " + mInitialTextColorState);
Beverly2034c832018-03-19 11:18:51 -0400518 pw.println(" mPowerPluggedInWired: " + mPowerPluggedInWired);
Lucas Dupin3fcdd472018-01-19 19:06:45 -0800519 pw.println(" mPowerPluggedIn: " + mPowerPluggedIn);
520 pw.println(" mPowerCharged: " + mPowerCharged);
521 pw.println(" mChargingSpeed: " + mChargingSpeed);
522 pw.println(" mChargingWattage: " + mChargingWattage);
523 pw.println(" mMessageToShowOnScreenOn: " + mMessageToShowOnScreenOn);
524 pw.println(" mDozing: " + mDozing);
525 pw.println(" mBatteryLevel: " + mBatteryLevel);
526 pw.println(" mTextView.getText(): " + (mTextView == null ? null : mTextView.getText()));
527 pw.println(" computePowerIndication(): " + computePowerIndication());
528 }
529
Evan Laird878c8532018-10-15 15:54:29 -0400530 @Override
531 public void onStateChanged(int newState) {
532 // don't care
533 }
534
535 @Override
536 public void onDozingChanged(boolean isDozing) {
537 setDozing(isDozing);
538 }
539
Zachary Iqbalf50284c2017-01-22 18:54:46 -0800540 protected class BaseKeyguardCallback extends KeyguardUpdateMonitorCallback {
Adrian Roos56021892017-02-27 20:25:09 +0100541 public static final int HIDE_DELAY_MS = 5000;
Selim Cinek3e451942016-07-14 18:07:53 -0700542
Adrian Roos12c1ef52014-06-04 13:54:08 +0200543 @Override
544 public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) {
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100545 boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STATUS_CHARGING
Adrian Roos12c1ef52014-06-04 13:54:08 +0200546 || status.status == BatteryManager.BATTERY_STATUS_FULL;
Adrian Roos56021892017-02-27 20:25:09 +0100547 boolean wasPluggedIn = mPowerPluggedIn;
Beverly2034c832018-03-19 11:18:51 -0400548 mPowerPluggedInWired = status.isPluggedInWired() && isChargingOrFull;
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100549 mPowerPluggedIn = status.isPluggedIn() && isChargingOrFull;
Adrian Roos12c1ef52014-06-04 13:54:08 +0200550 mPowerCharged = status.isCharged();
Adrian Roos0c859ae2015-11-23 16:47:50 -0800551 mChargingWattage = status.maxChargingWattage;
Adrian Roos7b043112015-07-10 13:00:33 -0700552 mChargingSpeed = status.getChargingSpeed(mSlowThreshold, mFastThreshold);
Lucas Dupin4272f442018-01-13 22:00:35 -0800553 mBatteryLevel = status.level;
Beverly2034c832018-03-19 11:18:51 -0400554 updateIndication(!wasPluggedIn && mPowerPluggedInWired);
Adrian Roosc1b50322017-02-27 21:07:58 +0100555 if (mDozing) {
556 if (!wasPluggedIn && mPowerPluggedIn) {
557 showTransientIndication(computePowerIndication());
558 hideTransientIndicationDelayed(HIDE_DELAY_MS);
559 } else if (wasPluggedIn && !mPowerPluggedIn) {
560 hideTransientIndication();
561 }
Adrian Roos56021892017-02-27 20:25:09 +0100562 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200563 }
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700564
565 @Override
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100566 public void onKeyguardVisibilityChanged(boolean showing) {
567 if (showing) {
568 updateDisclosure();
569 }
570 }
571
572 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200573 public void onBiometricHelp(int msgId, String helpString,
574 BiometricSourceType biometricSourceType) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700575 KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200576 if (!updateMonitor.isUnlockingWithBiometricAllowed()) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700577 return;
578 }
Lucas Dupine54ad1d2019-04-09 17:08:46 -0700579 animatePadlockError();
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700580 if (mStatusBarKeyguardViewManager.isBouncerShowing()) {
Jason Chang2386a372018-04-24 16:05:30 +0800581 mStatusBarKeyguardViewManager.showBouncerMessage(helpString,
Lucas Dupinc2d11b32019-03-06 16:02:18 -0800582 mInitialTextColorState);
Kevin Chyn4c4001c2017-08-25 14:23:36 -0700583 } else if (updateMonitor.isScreenOn()) {
Lucas Dupinc2d11b32019-03-06 16:02:18 -0800584 showTransientIndication(helpString);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200585 hideTransientIndicationDelayed(TRANSIENT_BIOMETRIC_ERROR_TIMEOUT);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700586 }
587 }
588
589 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200590 public void onBiometricError(int msgId, String errString,
591 BiometricSourceType biometricSourceType) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700592 KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200593 if (shouldSuppressBiometricError(msgId, biometricSourceType, updateMonitor)) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700594 return;
595 }
Lucas Dupine54ad1d2019-04-09 17:08:46 -0700596 animatePadlockError();
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700597 if (mStatusBarKeyguardViewManager.isBouncerShowing()) {
Lucas Dupine54ad1d2019-04-09 17:08:46 -0700598 mStatusBarKeyguardViewManager.showBouncerMessage(errString, mInitialTextColorState);
Kevin Chyn4c4001c2017-08-25 14:23:36 -0700599 } else if (updateMonitor.isScreenOn()) {
Lucas Dupinc2d11b32019-03-06 16:02:18 -0800600 showTransientIndication(errString);
Selim Cinek3e451942016-07-14 18:07:53 -0700601 // We want to keep this message around in case the screen was off
Adrian Roos56021892017-02-27 20:25:09 +0100602 hideTransientIndicationDelayed(HIDE_DELAY_MS);
Selim Cinek3e451942016-07-14 18:07:53 -0700603 } else {
604 mMessageToShowOnScreenOn = errString;
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700605 }
Lucas Dupine54ad1d2019-04-09 17:08:46 -0700606 }
607
608 private void animatePadlockError() {
609 mLockIcon.setTransientBiometricsError(true);
610 mHandler.removeMessages(MSG_CLEAR_BIOMETRIC_MSG);
611 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CLEAR_BIOMETRIC_MSG),
612 TRANSIENT_BIOMETRIC_ERROR_TIMEOUT);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700613 }
614
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200615 private boolean shouldSuppressBiometricError(int msgId,
616 BiometricSourceType biometricSourceType, KeyguardUpdateMonitor updateMonitor) {
617 if (biometricSourceType == BiometricSourceType.FINGERPRINT)
618 return shouldSuppressFingerprintError(msgId, updateMonitor);
619 if (biometricSourceType == BiometricSourceType.FACE)
620 return shouldSuppressFaceError(msgId, updateMonitor);
621 return false;
622 }
623
624 private boolean shouldSuppressFingerprintError(int msgId,
625 KeyguardUpdateMonitor updateMonitor) {
626 return ((!updateMonitor.isUnlockingWithBiometricAllowed()
627 && msgId != FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT)
628 || msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED);
629 }
630
631 private boolean shouldSuppressFaceError(int msgId, KeyguardUpdateMonitor updateMonitor) {
632 return ((!updateMonitor.isUnlockingWithBiometricAllowed()
633 && msgId != FaceManager.FACE_ERROR_LOCKOUT_PERMANENT)
634 || msgId == FaceManager.FACE_ERROR_CANCELED);
635 }
636
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700637 @Override
Lucas Dupinef886542018-01-03 16:03:07 -0800638 public void onTrustAgentErrorMessage(CharSequence message) {
Jason Chang2386a372018-04-24 16:05:30 +0800639 showTransientIndication(message, Utils.getColorError(mContext));
Lucas Dupinef886542018-01-03 16:03:07 -0800640 }
641
642 @Override
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700643 public void onScreenTurnedOn() {
644 if (mMessageToShowOnScreenOn != null) {
Jason Chang2386a372018-04-24 16:05:30 +0800645 showTransientIndication(mMessageToShowOnScreenOn, Utils.getColorError(mContext));
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700646 // We want to keep this message around in case the screen was off
Adrian Roos56021892017-02-27 20:25:09 +0100647 hideTransientIndicationDelayed(HIDE_DELAY_MS);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700648 mMessageToShowOnScreenOn = null;
649 }
650 }
651
652 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200653 public void onBiometricRunningStateChanged(boolean running,
654 BiometricSourceType biometricSourceType) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700655 if (running) {
Lucas Dupin8f3faac2019-03-12 15:28:49 -0700656 // Let's hide any previous messages when authentication starts, otherwise
657 // multiple auth attempts would overlap.
658 hideTransientIndication();
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700659 mMessageToShowOnScreenOn = null;
660 }
661 }
Selim Cinek3e451942016-07-14 18:07:53 -0700662
663 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200664 public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType) {
665 super.onBiometricAuthenticated(userId, biometricSourceType);
Kevin Chyn6d793bd2019-03-27 15:36:13 -0700666 mHandler.sendEmptyMessage(MSG_HIDE_TRANSIENT);
Selim Cinek3e451942016-07-14 18:07:53 -0700667 }
668
669 @Override
Jorim Jaggidadafd42016-09-30 07:20:25 -0700670 public void onUserUnlocked() {
671 if (mVisible) {
Beverly8c785892018-01-31 17:25:52 -0500672 updateIndication(false);
Jorim Jaggidadafd42016-09-30 07:20:25 -0700673 }
674 }
Lucas Dupin05904652019-04-09 16:16:15 -0700675
676 @Override
677 public void onKeyguardBouncerChanged(boolean bouncer) {
678 mLockIcon.setBouncerVisible(bouncer);
679 }
Adrian Roos12c1ef52014-06-04 13:54:08 +0200680 };
Adrian Roos12c1ef52014-06-04 13:54:08 +0200681}