Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 19 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 25 | import android.graphics.Color; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 26 | import android.hardware.fingerprint.FingerprintManager; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 27 | import android.os.BatteryManager; |
| 28 | import android.os.BatteryStats; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Message; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.os.UserHandle; |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 34 | import android.os.UserManager; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 35 | import android.text.TextUtils; |
| 36 | import android.text.format.Formatter; |
| 37 | import android.util.Log; |
| 38 | import android.view.View; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 39 | import android.view.ViewGroup; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 40 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 41 | import com.android.internal.annotations.VisibleForTesting; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 42 | import com.android.internal.app.IBatteryStats; |
| 43 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 44 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 45 | import com.android.settingslib.Utils; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 46 | import com.android.systemui.Dependency; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 47 | import com.android.systemui.R; |
| 48 | import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; |
| 49 | import com.android.systemui.statusbar.phone.LockIcon; |
| 50 | import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager; |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 51 | import com.android.systemui.statusbar.policy.UserInfoController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 52 | import com.android.systemui.util.wakelock.SettableWakeLock; |
| 53 | import com.android.systemui.util.wakelock.WakeLock; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 54 | |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 55 | import java.text.NumberFormat; |
| 56 | |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 57 | /** |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 58 | * Controls the indications and error messages shown on the Keyguard |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 59 | */ |
| 60 | public class KeyguardIndicationController { |
| 61 | |
Adrian Roos | 0c859ae | 2015-11-23 16:47:50 -0800 | [diff] [blame] | 62 | private static final String TAG = "KeyguardIndication"; |
| 63 | private static final boolean DEBUG_CHARGING_SPEED = false; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 64 | |
| 65 | private static final int MSG_HIDE_TRANSIENT = 1; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 66 | private static final int MSG_CLEAR_FP_MSG = 2; |
| 67 | private static final long TRANSIENT_FP_ERROR_TIMEOUT = 1300; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 68 | |
| 69 | private final Context mContext; |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 70 | private ViewGroup mIndicationArea; |
| 71 | private KeyguardIndicationTextView mTextView; |
| 72 | private KeyguardIndicationTextView mDisclosure; |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 73 | private final UserManager mUserManager; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 74 | private final IBatteryStats mBatteryInfo; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 75 | private final SettableWakeLock mWakeLock; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 76 | |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 77 | private final int mSlowThreshold; |
| 78 | private final int mFastThreshold; |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 79 | private LockIcon mLockIcon; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 80 | private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 81 | |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 82 | private String mRestingIndication; |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 83 | private CharSequence mTransientIndication; |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 84 | private int mTransientTextColor; |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 85 | private int mInitialTextColor; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 86 | private boolean mVisible; |
| 87 | |
| 88 | private boolean mPowerPluggedIn; |
| 89 | private boolean mPowerCharged; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 90 | private int mChargingSpeed; |
Adrian Roos | 0c859ae | 2015-11-23 16:47:50 -0800 | [diff] [blame] | 91 | private int mChargingWattage; |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 92 | private int mBatteryLevel; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 93 | private String mMessageToShowOnScreenOn; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 94 | |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 95 | private KeyguardUpdateMonitorCallback mUpdateMonitorCallback; |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 96 | |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 97 | private final DevicePolicyManager mDevicePolicyManager; |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 98 | private boolean mDozing; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 99 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Creates a new KeyguardIndicationController and registers callbacks. |
| 102 | */ |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 103 | public KeyguardIndicationController(Context context, ViewGroup indicationArea, |
| 104 | LockIcon lockIcon) { |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 105 | this(context, indicationArea, lockIcon, |
| 106 | WakeLock.createPartial(context, "Doze:KeyguardIndication")); |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 107 | |
| 108 | registerCallbacks(KeyguardUpdateMonitor.getInstance(context)); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Creates a new KeyguardIndicationController for testing. Does *not* register callbacks. |
| 113 | */ |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 114 | @VisibleForTesting |
| 115 | KeyguardIndicationController(Context context, ViewGroup indicationArea, LockIcon lockIcon, |
| 116 | WakeLock wakeLock) { |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 117 | mContext = context; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 118 | mIndicationArea = indicationArea; |
| 119 | mTextView = (KeyguardIndicationTextView) indicationArea.findViewById( |
| 120 | R.id.keyguard_indication_text); |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 121 | mInitialTextColor = mTextView != null ? mTextView.getCurrentTextColor() : Color.WHITE; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 122 | mDisclosure = (KeyguardIndicationTextView) indicationArea.findViewById( |
| 123 | R.id.keyguard_indication_enterprise_disclosure); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 124 | mLockIcon = lockIcon; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 125 | mWakeLock = new SettableWakeLock(wakeLock); |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 126 | |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 127 | Resources res = context.getResources(); |
| 128 | mSlowThreshold = res.getInteger(R.integer.config_chargingSlowlyThreshold); |
| 129 | mFastThreshold = res.getInteger(R.integer.config_chargingFastThreshold); |
| 130 | |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 131 | mUserManager = context.getSystemService(UserManager.class); |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 132 | mBatteryInfo = IBatteryStats.Stub.asInterface( |
| 133 | ServiceManager.getService(BatteryStats.SERVICE_NAME)); |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 134 | |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 135 | mDevicePolicyManager = (DevicePolicyManager) context.getSystemService( |
| 136 | Context.DEVICE_POLICY_SERVICE); |
| 137 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 138 | updateDisclosure(); |
| 139 | } |
| 140 | |
| 141 | private void registerCallbacks(KeyguardUpdateMonitor monitor) { |
| 142 | monitor.registerCallback(getKeyguardCallback()); |
| 143 | |
| 144 | mContext.registerReceiverAsUser(mTickReceiver, UserHandle.SYSTEM, |
Jason Monk | cd26af7 | 2017-01-11 14:32:58 -0500 | [diff] [blame] | 145 | new IntentFilter(Intent.ACTION_TIME_TICK), null, |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 146 | Dependency.get(Dependency.TIME_TICK_HANDLER)); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Gets the {@link KeyguardUpdateMonitorCallback} instance associated with this |
| 151 | * {@link KeyguardIndicationController}. |
| 152 | * |
| 153 | * <p>Subclasses may override this method to extend or change the callback behavior by extending |
| 154 | * the {@link BaseKeyguardCallback}. |
| 155 | * |
| 156 | * @return A KeyguardUpdateMonitorCallback. Multiple calls to this method <b>must</b> return the |
| 157 | * same instance. |
| 158 | */ |
| 159 | protected KeyguardUpdateMonitorCallback getKeyguardCallback() { |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 160 | if (mUpdateMonitorCallback == null) { |
| 161 | mUpdateMonitorCallback = new BaseKeyguardCallback(); |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 162 | } |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 163 | return mUpdateMonitorCallback; |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 166 | private void updateDisclosure() { |
| 167 | if (mDevicePolicyManager == null) { |
| 168 | return; |
| 169 | } |
| 170 | |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 171 | if (!mDozing && mDevicePolicyManager.isDeviceManaged()) { |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 172 | final CharSequence organizationName = |
| 173 | mDevicePolicyManager.getDeviceOwnerOrganizationName(); |
| 174 | if (organizationName != null) { |
| 175 | mDisclosure.switchIndication(mContext.getResources().getString( |
| 176 | R.string.do_disclosure_with_name, organizationName)); |
| 177 | } else { |
| 178 | mDisclosure.switchIndication(R.string.do_disclosure_generic); |
| 179 | } |
| 180 | mDisclosure.setVisibility(View.VISIBLE); |
| 181 | } else { |
| 182 | mDisclosure.setVisibility(View.GONE); |
| 183 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public void setVisible(boolean visible) { |
| 187 | mVisible = visible; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 188 | mIndicationArea.setVisibility(visible ? View.VISIBLE : View.GONE); |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 189 | if (visible) { |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 190 | // If this is called after an error message was already shown, we should not clear it. |
| 191 | // Otherwise the error message won't be shown |
| 192 | if (!mHandler.hasMessages(MSG_HIDE_TRANSIENT)) { |
| 193 | hideTransientIndication(); |
| 194 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 195 | updateIndication(); |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 196 | } else if (!visible) { |
| 197 | // If we unlock and return to keyguard quickly, previous error should not be shown |
| 198 | hideTransientIndication(); |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Sets the indication that is shown if nothing else is showing. |
| 204 | */ |
| 205 | public void setRestingIndication(String restingIndication) { |
| 206 | mRestingIndication = restingIndication; |
| 207 | updateIndication(); |
| 208 | } |
| 209 | |
| 210 | /** |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 211 | * Sets the active controller managing changes and callbacks to user information. |
| 212 | */ |
| 213 | public void setUserInfoController(UserInfoController userInfoController) { |
| 214 | } |
| 215 | |
| 216 | /** |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 217 | * Returns the indication text indicating that trust has been granted. |
| 218 | * |
| 219 | * @return {@code null} or an empty string if a trust indication text should not be shown. |
| 220 | */ |
Zachary Iqbal | dc05aa0 | 2017-05-17 18:52:49 -0700 | [diff] [blame] | 221 | protected String getTrustGrantedIndication() { |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns the indication text indicating that trust is currently being managed. |
| 227 | * |
| 228 | * @return {@code null} or an empty string if a trust managed text should not be shown. |
| 229 | */ |
| 230 | protected String getTrustManagedIndication() { |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 231 | return null; |
| 232 | } |
| 233 | |
| 234 | /** |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 235 | * Hides transient indication in {@param delayMs}. |
| 236 | */ |
| 237 | public void hideTransientIndicationDelayed(long delayMs) { |
| 238 | mHandler.sendMessageDelayed( |
| 239 | mHandler.obtainMessage(MSG_HIDE_TRANSIENT), delayMs); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}. |
| 244 | */ |
| 245 | public void showTransientIndication(int transientIndication) { |
| 246 | showTransientIndication(mContext.getResources().getString(transientIndication)); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}. |
| 251 | */ |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 252 | public void showTransientIndication(CharSequence transientIndication) { |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 253 | showTransientIndication(transientIndication, mInitialTextColor); |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Shows {@param transientIndication} until it is hidden by {@link #hideTransientIndication}. |
| 258 | */ |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 259 | public void showTransientIndication(CharSequence transientIndication, int textColor) { |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 260 | mTransientIndication = transientIndication; |
Jorim Jaggi | 27c9b74 | 2015-04-09 10:34:49 -0700 | [diff] [blame] | 261 | mTransientTextColor = textColor; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 262 | mHandler.removeMessages(MSG_HIDE_TRANSIENT); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 263 | if (mDozing && !TextUtils.isEmpty(mTransientIndication)) { |
| 264 | // Make sure this doesn't get stuck and burns in. Acquire wakelock until its cleared. |
| 265 | mWakeLock.setAcquired(true); |
| 266 | hideTransientIndicationDelayed(BaseKeyguardCallback.HIDE_DELAY_MS); |
| 267 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 268 | updateIndication(); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Hides transient indication. |
| 273 | */ |
| 274 | public void hideTransientIndication() { |
| 275 | if (mTransientIndication != null) { |
| 276 | mTransientIndication = null; |
| 277 | mHandler.removeMessages(MSG_HIDE_TRANSIENT); |
| 278 | updateIndication(); |
| 279 | } |
| 280 | } |
| 281 | |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 282 | protected final void updateIndication() { |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 283 | if (TextUtils.isEmpty(mTransientIndication)) { |
| 284 | mWakeLock.setAcquired(false); |
| 285 | } |
| 286 | |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 287 | if (mVisible) { |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 288 | // Walk down a precedence-ordered list of what indication |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 289 | // should be shown based on user or device state |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 290 | if (mDozing) { |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 291 | mTextView.setTextColor(Color.WHITE); |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 292 | if (!TextUtils.isEmpty(mTransientIndication)) { |
Adrian Roos | 12e112d | 2017-07-25 16:46:23 +0200 | [diff] [blame] | 293 | // When dozing we ignore any text color and use white instead, because |
| 294 | // colors can be hard to read in low brightness. |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 295 | mTextView.switchIndication(mTransientIndication); |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 296 | } else if (mPowerPluggedIn) { |
| 297 | String indication = computePowerIndication(); |
| 298 | mTextView.switchIndication(indication); |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 299 | } else { |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 300 | String percentage = NumberFormat.getPercentInstance() |
| 301 | .format(mBatteryLevel / 100f); |
| 302 | mTextView.switchIndication(percentage); |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 303 | } |
| 304 | return; |
| 305 | } |
| 306 | |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 307 | KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
Jorim Jaggi | fabc743 | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 308 | int userId = KeyguardUpdateMonitor.getCurrentUser(); |
Zachary Iqbal | dc05aa0 | 2017-05-17 18:52:49 -0700 | [diff] [blame] | 309 | String trustGrantedIndication = getTrustGrantedIndication(); |
| 310 | String trustManagedIndication = getTrustManagedIndication(); |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 311 | if (!mUserManager.isUserUnlocked(userId)) { |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 312 | mTextView.switchIndication(com.android.internal.R.string.lockscreen_storage_locked); |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 313 | mTextView.setTextColor(mInitialTextColor); |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 314 | } else if (!TextUtils.isEmpty(mTransientIndication)) { |
| 315 | mTextView.switchIndication(mTransientIndication); |
| 316 | mTextView.setTextColor(mTransientTextColor); |
Zachary Iqbal | dc05aa0 | 2017-05-17 18:52:49 -0700 | [diff] [blame] | 317 | } else if (!TextUtils.isEmpty(trustGrantedIndication) |
Zachary Iqbal | 8f4c242 | 2017-04-20 17:56:42 -0700 | [diff] [blame] | 318 | && updateMonitor.getUserHasTrust(userId)) { |
Zachary Iqbal | dc05aa0 | 2017-05-17 18:52:49 -0700 | [diff] [blame] | 319 | mTextView.switchIndication(trustGrantedIndication); |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 320 | mTextView.setTextColor(mInitialTextColor); |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 321 | } else if (mPowerPluggedIn) { |
| 322 | String indication = computePowerIndication(); |
| 323 | if (DEBUG_CHARGING_SPEED) { |
| 324 | indication += ", " + (mChargingWattage / 1000) + " mW"; |
| 325 | } |
| 326 | mTextView.switchIndication(indication); |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 327 | mTextView.setTextColor(mInitialTextColor); |
Zachary Iqbal | dc05aa0 | 2017-05-17 18:52:49 -0700 | [diff] [blame] | 328 | } else if (!TextUtils.isEmpty(trustManagedIndication) |
| 329 | && updateMonitor.getUserTrustIsManaged(userId) |
| 330 | && !updateMonitor.getUserHasTrust(userId)) { |
| 331 | mTextView.switchIndication(trustManagedIndication); |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 332 | mTextView.setTextColor(mInitialTextColor); |
Jeff Sharkey | b6edaa9 | 2016-07-27 15:51:31 -0600 | [diff] [blame] | 333 | } else { |
| 334 | mTextView.switchIndication(mRestingIndication); |
Lucas Dupin | 53d5062 | 2017-05-13 15:54:14 -0700 | [diff] [blame] | 335 | mTextView.setTextColor(mInitialTextColor); |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 336 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 337 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | private String computePowerIndication() { |
| 341 | if (mPowerCharged) { |
| 342 | return mContext.getResources().getString(R.string.keyguard_charged); |
| 343 | } |
| 344 | |
| 345 | // Try fetching charging time from battery stats. |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 346 | long chargingTimeRemaining = 0; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 347 | try { |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 348 | chargingTimeRemaining = mBatteryInfo.computeChargeTimeRemaining(); |
| 349 | |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 350 | } catch (RemoteException e) { |
| 351 | Log.e(TAG, "Error calling IBatteryStats: ", e); |
| 352 | } |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 353 | final boolean hasChargingTime = chargingTimeRemaining > 0; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 354 | |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 355 | int chargingId; |
| 356 | switch (mChargingSpeed) { |
| 357 | case KeyguardUpdateMonitor.BatteryStatus.CHARGING_FAST: |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 358 | chargingId = hasChargingTime |
Adrian Roos | f142cac | 2015-09-25 15:15:17 -0700 | [diff] [blame] | 359 | ? R.string.keyguard_indication_charging_time_fast |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 360 | : R.string.keyguard_plugged_in_charging_fast; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 361 | break; |
| 362 | case KeyguardUpdateMonitor.BatteryStatus.CHARGING_SLOWLY: |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 363 | chargingId = hasChargingTime |
Adrian Roos | f142cac | 2015-09-25 15:15:17 -0700 | [diff] [blame] | 364 | ? R.string.keyguard_indication_charging_time_slowly |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 365 | : R.string.keyguard_plugged_in_charging_slowly; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 366 | break; |
| 367 | default: |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 368 | chargingId = hasChargingTime |
| 369 | ? R.string.keyguard_indication_charging_time |
| 370 | : R.string.keyguard_plugged_in; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 371 | break; |
| 372 | } |
Adrian Roos | 7e39e59 | 2015-09-23 17:03:47 -0700 | [diff] [blame] | 373 | |
| 374 | if (hasChargingTime) { |
| 375 | String chargingTimeFormatted = Formatter.formatShortElapsedTimeRoundingUpToMinutes( |
| 376 | mContext, chargingTimeRemaining); |
| 377 | return mContext.getResources().getString(chargingId, chargingTimeFormatted); |
| 378 | } else { |
| 379 | return mContext.getResources().getString(chargingId); |
| 380 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 383 | public void setStatusBarKeyguardViewManager( |
| 384 | StatusBarKeyguardViewManager statusBarKeyguardViewManager) { |
| 385 | mStatusBarKeyguardViewManager = statusBarKeyguardViewManager; |
| 386 | } |
| 387 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 388 | private final BroadcastReceiver mTickReceiver = new BroadcastReceiver() { |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 389 | @Override |
| 390 | public void onReceive(Context context, Intent intent) { |
| 391 | mHandler.post(() -> { |
| 392 | if (mVisible) { |
| 393 | updateIndication(); |
| 394 | } |
| 395 | }); |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | private final Handler mHandler = new Handler() { |
| 400 | @Override |
| 401 | public void handleMessage(Message msg) { |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 402 | if (msg.what == MSG_HIDE_TRANSIENT) { |
| 403 | hideTransientIndication(); |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 404 | } else if (msg.what == MSG_CLEAR_FP_MSG) { |
| 405 | mLockIcon.setTransientFpError(false); |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | }; |
| 409 | |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 410 | public void setDozing(boolean dozing) { |
Jorim Jaggi | fabc743 | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 411 | if (mDozing == dozing) { |
| 412 | return; |
| 413 | } |
Adrian Roos | 91ba307 | 2017-02-14 16:50:46 +0100 | [diff] [blame] | 414 | mDozing = dozing; |
| 415 | updateIndication(); |
| 416 | updateDisclosure(); |
| 417 | } |
| 418 | |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 419 | protected class BaseKeyguardCallback extends KeyguardUpdateMonitorCallback { |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 420 | public static final int HIDE_DELAY_MS = 5000; |
Zachary Iqbal | f50284c | 2017-01-22 18:54:46 -0800 | [diff] [blame] | 421 | private int mLastSuccessiveErrorMessage = -1; |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 422 | |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 423 | @Override |
| 424 | public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) { |
Adrian Roos | ad3bc7f | 2014-10-30 18:29:38 +0100 | [diff] [blame] | 425 | boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STATUS_CHARGING |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 426 | || status.status == BatteryManager.BATTERY_STATUS_FULL; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 427 | boolean wasPluggedIn = mPowerPluggedIn; |
Adrian Roos | ad3bc7f | 2014-10-30 18:29:38 +0100 | [diff] [blame] | 428 | mPowerPluggedIn = status.isPluggedIn() && isChargingOrFull; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 429 | mPowerCharged = status.isCharged(); |
Adrian Roos | 0c859ae | 2015-11-23 16:47:50 -0800 | [diff] [blame] | 430 | mChargingWattage = status.maxChargingWattage; |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 431 | mChargingSpeed = status.getChargingSpeed(mSlowThreshold, mFastThreshold); |
Lucas Dupin | 4272f44 | 2018-01-13 22:00:35 -0800 | [diff] [blame^] | 432 | mBatteryLevel = status.level; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 433 | updateIndication(); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 434 | if (mDozing) { |
| 435 | if (!wasPluggedIn && mPowerPluggedIn) { |
| 436 | showTransientIndication(computePowerIndication()); |
| 437 | hideTransientIndicationDelayed(HIDE_DELAY_MS); |
| 438 | } else if (wasPluggedIn && !mPowerPluggedIn) { |
| 439 | hideTransientIndication(); |
| 440 | } |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 441 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 442 | } |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 443 | |
| 444 | @Override |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 445 | public void onKeyguardVisibilityChanged(boolean showing) { |
| 446 | if (showing) { |
| 447 | updateDisclosure(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | @Override |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 452 | public void onFingerprintHelp(int msgId, String helpString) { |
| 453 | KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
| 454 | if (!updateMonitor.isUnlockingWithFingerprintAllowed()) { |
| 455 | return; |
| 456 | } |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 457 | int errorColor = Utils.getColorError(mContext); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 458 | if (mStatusBarKeyguardViewManager.isBouncerShowing()) { |
| 459 | mStatusBarKeyguardViewManager.showBouncerMessage(helpString, errorColor); |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 460 | } else if (updateMonitor.isScreenOn()) { |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 461 | mLockIcon.setTransientFpError(true); |
| 462 | showTransientIndication(helpString, errorColor); |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 463 | hideTransientIndicationDelayed(TRANSIENT_FP_ERROR_TIMEOUT); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 464 | mHandler.removeMessages(MSG_CLEAR_FP_MSG); |
| 465 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CLEAR_FP_MSG), |
| 466 | TRANSIENT_FP_ERROR_TIMEOUT); |
| 467 | } |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 468 | // Help messages indicate that there was actually a try since the last error, so those |
| 469 | // are not two successive error messages anymore. |
| 470 | mLastSuccessiveErrorMessage = -1; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void onFingerprintError(int msgId, String errString) { |
| 475 | KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 476 | if ((!updateMonitor.isUnlockingWithFingerprintAllowed() |
| 477 | && msgId != FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 478 | || msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED) { |
| 479 | return; |
| 480 | } |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 481 | int errorColor = Utils.getColorError(mContext); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 482 | if (mStatusBarKeyguardViewManager.isBouncerShowing()) { |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 483 | // When swiping up right after receiving a fingerprint error, the bouncer calls |
| 484 | // authenticate leading to the same message being shown again on the bouncer. |
| 485 | // We want to avoid this, as it may confuse the user when the message is too |
| 486 | // generic. |
| 487 | if (mLastSuccessiveErrorMessage != msgId) { |
| 488 | mStatusBarKeyguardViewManager.showBouncerMessage(errString, errorColor); |
| 489 | } |
Kevin Chyn | 4c4001c | 2017-08-25 14:23:36 -0700 | [diff] [blame] | 490 | } else if (updateMonitor.isScreenOn()) { |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 491 | showTransientIndication(errString, errorColor); |
| 492 | // We want to keep this message around in case the screen was off |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 493 | hideTransientIndicationDelayed(HIDE_DELAY_MS); |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 494 | } else { |
| 495 | mMessageToShowOnScreenOn = errString; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 496 | } |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 497 | mLastSuccessiveErrorMessage = msgId; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Override |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 501 | public void onTrustAgentErrorMessage(CharSequence message) { |
| 502 | int errorColor = Utils.getColorError(mContext); |
| 503 | showTransientIndication(message, errorColor); |
| 504 | } |
| 505 | |
| 506 | @Override |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 507 | public void onScreenTurnedOn() { |
| 508 | if (mMessageToShowOnScreenOn != null) { |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 509 | int errorColor = Utils.getColorError(mContext); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 510 | showTransientIndication(mMessageToShowOnScreenOn, errorColor); |
| 511 | // We want to keep this message around in case the screen was off |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 512 | hideTransientIndicationDelayed(HIDE_DELAY_MS); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 513 | mMessageToShowOnScreenOn = null; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void onFingerprintRunningStateChanged(boolean running) { |
| 519 | if (running) { |
| 520 | mMessageToShowOnScreenOn = null; |
| 521 | } |
| 522 | } |
Selim Cinek | 3e45194 | 2016-07-14 18:07:53 -0700 | [diff] [blame] | 523 | |
| 524 | @Override |
| 525 | public void onFingerprintAuthenticated(int userId) { |
| 526 | super.onFingerprintAuthenticated(userId); |
| 527 | mLastSuccessiveErrorMessage = -1; |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void onFingerprintAuthFailed() { |
| 532 | super.onFingerprintAuthFailed(); |
| 533 | mLastSuccessiveErrorMessage = -1; |
| 534 | } |
Jorim Jaggi | dadafd4 | 2016-09-30 07:20:25 -0700 | [diff] [blame] | 535 | |
| 536 | @Override |
| 537 | public void onUserUnlocked() { |
| 538 | if (mVisible) { |
| 539 | updateIndication(); |
| 540 | } |
| 541 | } |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 542 | }; |
Adrian Roos | 12c1ef5 | 2014-06-04 13:54:08 +0200 | [diff] [blame] | 543 | } |