1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.statusbar.policy;
import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.biometrics.BiometricSourceType;
import android.os.Build;
import android.os.SystemProperties;
import android.os.Trace;
import androidx.annotation.VisibleForTesting;
import com.android.internal.widget.LockPatternUtils;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
import com.android.keyguard.logging.KeyguardUpdateMonitorLogger;
import com.android.systemui.Dumpable;
import com.android.systemui.R;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dump.DumpManager;
import com.android.systemui.keyguard.KeyguardUnlockAnimationController;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Objects;
import javax.inject.Inject;
import dagger.Lazy;
/**
*/
@SysUISingleton
public class KeyguardStateControllerImpl implements KeyguardStateController, Dumpable {
private static final boolean DEBUG_AUTH_WITH_ADB = false;
private static final String AUTH_BROADCAST_KEY = "debug_trigger_auth";
private final ArrayList<Callback> mCallbacks = new ArrayList<>();
private final Context mContext;
private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
private final LockPatternUtils mLockPatternUtils;
private final KeyguardUpdateMonitorCallback mKeyguardUpdateMonitorCallback =
new UpdateMonitorCallback();
private final Lazy<KeyguardUnlockAnimationController> mUnlockAnimationControllerLazy;
private final KeyguardUpdateMonitorLogger mLogger;
private boolean mCanDismissLockScreen;
private boolean mShowing;
private boolean mPrimaryBouncerShowing;
private boolean mSecure;
private boolean mOccluded;
private boolean mKeyguardFadingAway;
private long mKeyguardFadingAwayDelay;
private long mKeyguardFadingAwayDuration;
private boolean mKeyguardGoingAway;
private boolean mLaunchTransitionFadingAway;
private boolean mTrustManaged;
private boolean mTrusted;
private boolean mDebugUnlocked = false;
private boolean mFaceAuthEnabled;
private float mDismissAmount = 0f;
private boolean mDismissingFromTouch = false;
/**
* Whether the panel is currently flinging to a collapsed state, which means we're dismissing
* the keyguard.
*/
private boolean mFlingingToDismissKeyguard = false;
/**
* Whether the panel is currently flinging to a collapsed state, which means we're dismissing
* the keyguard, and the fling started during a swipe gesture. This means that we need to take
* over the gesture and animate the rest of the way dismissed.
*/
private boolean mFlingingToDismissKeyguardDuringSwipeGesture = false;
/**
* Whether the panel is currently flinging to an expanded state, which means we cancelled the
* dismiss gesture and are snapping back to the keyguard state.
*/
private boolean mSnappingKeyguardBackAfterSwipe = false;
/**
*/
@Inject
public KeyguardStateControllerImpl(
Context context,
KeyguardUpdateMonitor keyguardUpdateMonitor,
LockPatternUtils lockPatternUtils,
Lazy<KeyguardUnlockAnimationController> keyguardUnlockAnimationController,
KeyguardUpdateMonitorLogger logger,
DumpManager dumpManager) {
mContext = context;
mLogger = logger;
mKeyguardUpdateMonitor = keyguardUpdateMonitor;
mLockPatternUtils = lockPatternUtils;
mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateMonitorCallback);
mUnlockAnimationControllerLazy = keyguardUnlockAnimationController;
dumpManager.registerDumpable(getClass().getSimpleName(), this);
update(true /* updateAlways */);
if (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB) {
// Watch for interesting updates
final IntentFilter filter = new IntentFilter();
filter.addAction(AUTH_BROADCAST_KEY);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG_AUTH_WITH_ADB && AUTH_BROADCAST_KEY.equals(intent.getAction())) {
mDebugUnlocked = !mDebugUnlocked;
update(true /* updateAlways */);
}
}
}, filter, null, null, Context.RECEIVER_EXPORTED_UNAUDITED);
}
}
@Override
public void addCallback(@NonNull Callback callback) {
Objects.requireNonNull(callback, "Callback must not be null. b/128895449");
if (!mCallbacks.contains(callback)) {
mCallbacks.add(callback);
}
}
@Override
public void removeCallback(@NonNull Callback callback) {
Objects.requireNonNull(callback, "Callback must not be null. b/128895449");
mCallbacks.remove(callback);
}
@Override
public boolean isShowing() {
return mShowing;
}
@Override
public boolean isPrimaryBouncerShowing() {
return mPrimaryBouncerShowing;
}
@Override
public boolean isMethodSecure() {
return mSecure;
}
@Override
public boolean isOccluded() {
return mOccluded;
}
@Override
public boolean isTrusted() {
return mTrusted;
}
@Override
public void notifyKeyguardState(boolean showing, boolean occluded) {
if (mShowing == showing && mOccluded == occluded) return;
mShowing = showing;
mOccluded = occluded;
mKeyguardUpdateMonitor.setKeyguardShowing(showing, occluded);
Trace.instantForTrack(Trace.TRACE_TAG_APP, "UI Events",
"Keyguard showing: " + showing + " occluded: " + occluded);
notifyKeyguardChanged();
// Update the dismiss amount to the full 0f/1f if we explicitly show or hide the keyguard.
// Otherwise, the dismiss amount could be left at a random value if we show/hide during a
// dismiss gesture, canceling the gesture.
notifyKeyguardDismissAmountChanged(showing ? 0f : 1f, false);
}
private void notifyKeyguardChanged() {
Trace.beginSection("KeyguardStateController#notifyKeyguardChanged");
// Copy the list to allow removal during callback.
new ArrayList<>(mCallbacks).forEach(Callback::onKeyguardShowingChanged);
Trace.endSection();
}
private void notifyUnlockedChanged() {
Trace.beginSection("KeyguardStateController#notifyUnlockedChanged");
// Copy the list to allow removal during callback.
new ArrayList<>(mCallbacks).forEach(Callback::onUnlockedChanged);
Trace.endSection();
}
@Override
public void notifyKeyguardFadingAway(long delay, long fadeoutDuration) {
mKeyguardFadingAwayDelay = delay;
mKeyguardFadingAwayDuration = fadeoutDuration;
setKeyguardFadingAway(true);
}
private void setKeyguardFadingAway(boolean keyguardFadingAway) {
if (mKeyguardFadingAway != keyguardFadingAway) {
Trace.traceCounter(Trace.TRACE_TAG_APP, "keyguardFadingAway",
keyguardFadingAway ? 1 : 0);
mKeyguardFadingAway = keyguardFadingAway;
ArrayList<Callback> callbacks = new ArrayList<>(mCallbacks);
for (int i = 0; i < callbacks.size(); i++) {
callbacks.get(i).onKeyguardFadingAwayChanged();
}
}
}
@Override
public void notifyKeyguardDoneFading() {
notifyKeyguardGoingAway(false);
setKeyguardFadingAway(false);
}
@VisibleForTesting
void update(boolean updateAlways) {
Trace.beginSection("KeyguardStateController#update");
int user = KeyguardUpdateMonitor.getCurrentUser();
boolean secure = mLockPatternUtils.isSecure(user);
boolean canDismissLockScreen = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)
|| (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB && mDebugUnlocked);
boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
boolean faceAuthEnabled = mKeyguardUpdateMonitor.isFaceAuthEnabledForUser(user);
boolean changed = secure != mSecure || canDismissLockScreen != mCanDismissLockScreen
|| trustManaged != mTrustManaged || mTrusted != trusted
|| mFaceAuthEnabled != faceAuthEnabled;
if (changed || updateAlways) {
mSecure = secure;
mCanDismissLockScreen = canDismissLockScreen;
mTrusted = trusted;
mTrustManaged = trustManaged;
mFaceAuthEnabled = faceAuthEnabled;
mLogger.logKeyguardStateUpdate(
mSecure, mCanDismissLockScreen, mTrusted, mTrustManaged);
notifyUnlockedChanged();
}
Trace.endSection();
}
@Override
public boolean canDismissLockScreen() {
return mCanDismissLockScreen;
}
@Override
public boolean isKeyguardScreenRotationAllowed() {
return SystemProperties.getBoolean("lockscreen.rot_override", false)
&& mContext.getResources().getBoolean(R.bool.config_enableLockScreenRotation);
}
@Override
public boolean isFaceAuthEnabled() {
return mFaceAuthEnabled;
}
@Override
public boolean isKeyguardFadingAway() {
return mKeyguardFadingAway;
}
@Override
public boolean isKeyguardGoingAway() {
return mKeyguardGoingAway;
}
@Override
public boolean isAnimatingBetweenKeyguardAndSurfaceBehind() {
return mUnlockAnimationControllerLazy.get().isAnimatingBetweenKeyguardAndSurfaceBehind();
}
@Override
public long getKeyguardFadingAwayDelay() {
return mKeyguardFadingAwayDelay;
}
@Override
public long getKeyguardFadingAwayDuration() {
return mKeyguardFadingAwayDuration;
}
@Override
public long calculateGoingToFullShadeDelay() {
return mKeyguardFadingAwayDelay + mKeyguardFadingAwayDuration;
}
@Override
public boolean isFlingingToDismissKeyguard() {
return mFlingingToDismissKeyguard;
}
@Override
public boolean isFlingingToDismissKeyguardDuringSwipeGesture() {
return mFlingingToDismissKeyguardDuringSwipeGesture;
}
@Override
public boolean isSnappingKeyguardBackAfterSwipe() {
return mSnappingKeyguardBackAfterSwipe;
}
@Override
public float getDismissAmount() {
return mDismissAmount;
}
@Override
public boolean isDismissingFromSwipe() {
return mDismissingFromTouch;
}
@Override
public void notifyKeyguardGoingAway(boolean keyguardGoingAway) {
if (mKeyguardGoingAway != keyguardGoingAway) {
Trace.traceCounter(Trace.TRACE_TAG_APP, "keyguardGoingAway",
keyguardGoingAway ? 1 : 0);
mKeyguardGoingAway = keyguardGoingAway;
new ArrayList<>(mCallbacks).forEach(Callback::onKeyguardGoingAwayChanged);
}
}
@Override
public void notifyPrimaryBouncerShowing(boolean showing) {
if (mPrimaryBouncerShowing != showing) {
mPrimaryBouncerShowing = showing;
new ArrayList<>(mCallbacks).forEach(Callback::onPrimaryBouncerShowingChanged);
}
}
@Override
public void notifyPanelFlingEnd() {
mFlingingToDismissKeyguard = false;
mFlingingToDismissKeyguardDuringSwipeGesture = false;
mSnappingKeyguardBackAfterSwipe = false;
}
@Override
public void notifyPanelFlingStart(boolean flingToDismiss) {
mFlingingToDismissKeyguard = flingToDismiss;
mFlingingToDismissKeyguardDuringSwipeGesture =
flingToDismiss && mDismissingFromTouch;
mSnappingKeyguardBackAfterSwipe = !flingToDismiss;
}
@Override
public void notifyKeyguardDismissAmountChanged(float dismissAmount,
boolean dismissingFromTouch) {
mDismissAmount = dismissAmount;
mDismissingFromTouch = dismissingFromTouch;
new ArrayList<>(mCallbacks).forEach(Callback::onKeyguardDismissAmountChanged);
}
@Override
public void setLaunchTransitionFadingAway(boolean fadingAway) {
mLaunchTransitionFadingAway = fadingAway;
new ArrayList<>(mCallbacks).forEach(Callback::onLaunchTransitionFadingAwayChanged);
}
@Override
public boolean isLaunchTransitionFadingAway() {
return mLaunchTransitionFadingAway;
}
/**
* Dumps internal state for debugging.
* @param pw Where to dump.
*/
@Override
public void dump(PrintWriter pw, String[] args) {
pw.println("KeyguardStateController:");
pw.println(" mShowing: " + mShowing);
pw.println(" mOccluded: " + mOccluded);
pw.println(" mSecure: " + mSecure);
pw.println(" mCanDismissLockScreen: " + mCanDismissLockScreen);
pw.println(" mTrustManaged: " + mTrustManaged);
pw.println(" mTrusted: " + mTrusted);
pw.println(" mDebugUnlocked: " + mDebugUnlocked);
pw.println(" mFaceAuthEnabled: " + mFaceAuthEnabled);
pw.println(" isKeyguardFadingAway: " + isKeyguardFadingAway());
pw.println(" isKeyguardGoingAway: " + isKeyguardGoingAway());
pw.println(" isLaunchTransitionFadingAway: " + isLaunchTransitionFadingAway());
}
private class UpdateMonitorCallback extends KeyguardUpdateMonitorCallback {
@Override
public void onUserSwitchComplete(int userId) {
update(false /* updateAlways */);
}
@Override
public void onTrustChanged(int userId) {
update(false /* updateAlways */);
notifyKeyguardChanged();
}
@Override
public void onTrustManagedChanged(int userId) {
update(false /* updateAlways */);
}
@Override
public void onStartedWakingUp() {
update(false /* updateAlways */);
}
@Override
public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType,
boolean isStrongBiometric) {
Trace.beginSection("KeyguardUpdateMonitorCallback#onBiometricAuthenticated");
if (mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed(isStrongBiometric)) {
update(false /* updateAlways */);
}
Trace.endSection();
}
@Override
public void onStrongAuthStateChanged(int userId) {
update(false /* updateAlways */);
}
@Override
public void onLockedOutStateChanged(BiometricSourceType biometricSourceType) {
update(false /* updateAlways */);
}
@Override
public void onKeyguardVisibilityChanged(boolean visible) {
update(false /* updateAlways */);
}
@Override
public void onBiometricsCleared() {
update(false /* alwaysUpdate */);
}
}
}
|