灭屏指纹监听流程

Updated on 2020.03.27

# 核心源码(Android_10)

关键类 路径
PowerManager.java frameworks/base/core/java/android/os/PowerManager.java
PowerManagerService.java frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
PhoneWindowManager.java frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
Notifier.java frameworks/base/services/core/java/com/android/server/power/Notifier.java
FingerprintManager.java frameworks/base/core/java/android/hardware/fingerprint/FingerprintManager.java
FingerprintService.java frameworks/base/services/core/java/com/android/server/biometrics/fingerprint/FingerprintService.java
KeyguardUpdateMonitor.java frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
KeyguardViewMediator.java frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
KeyguardServiceDelegate.java frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
KeyguardServiceWrapper.java frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java
BiometricServiceBase.java frameworks/base/services/core/java/com/android/server/biometrics/BiometricServiceBase.java

# 前言

最近发现一个很有趣的现象,Android 10 设置了指纹后,灭屏瞬间触摸指纹传感器,是无法立即识别指纹从而解锁手机的(我拿了 Google 亲儿子 pixel 做了实验,发现也是这种情况),但是 Android P 是可以的,在灭屏瞬间触摸指纹传感器,急速解锁,丝般顺滑。

由于自己所做的手机项目本身就低配卡顿存在各种无解 Bug 的情况下,灭屏指纹还不能瞬间触摸解锁生效,着实让人无法忍受!

所以就有了这篇文章,深入分析为什么 Android 升级到 10 的版本后会出现这种现象,能不能也像 Android P 一样的顺滑?

这里提前说下延迟原因:

Android P 灭屏后,启动指纹监听的关键方法:FingerprintService: startAuthentication() 很快就会得到执行;

Android 10 灭屏后,启动指纹监听的关键方法:BiometricServiceBase: startAuthentication(),要延迟 1~1.5 秒!

接下来我们通过源码跟踪研究:按下 Power 键灭屏后指纹监听是什么时候被调起来的,到底是什么原因导致了这种延迟(确定是原生现象,是否 Google 有意为之)?

一、灭屏流程(阶段一)

我们就先从灭屏的流程开始分析,当按 power 键灭屏时,首先会调用到 PhoneWindowManagerinterceptKeyBeforeQueueing() 方法。

1.1 PhoneWindowManager.interceptKeyBeforeQueueing

执行 interceptKeyBeforeQueueing() 方法:

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
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

@Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {

// Handle special keys.
switch (keyCode) {
... ...

case KeyEvent.KEYCODE_POWER: {
EventLogTags.writeInterceptPower(KeyEvent.actionToString(event.getAction()),
mPowerKeyHandled ? 1 : 0, mPowerKeyPressCounter);
// Any activity on the power button stops the accessibility shortcut
cancelPendingAccessibilityShortcutAction();
result &= ~ACTION_PASS_TO_USER;
isWakeKey = false; // wake-up will be handled separately
if (down) {
interceptPowerKeyDown(event, interactive); // 处理按下事件
} else {
Log.d("marco", "01. PhoneWindowManager -- interceptKeyBeforeQueueing -- interceptPowerKeyUp");
interceptPowerKeyUp(event, interactive, canceled); // 处理抬起事件,包括灭屏事件
}
break;
}
... ...

}

}

}

1.2 PhoneWindowManager.interceptPowerKeyUp

执行 interceptPowerKeyUp() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

private void interceptPowerKeyUp(KeyEvent event, boolean interactive, boolean canceled) {
final boolean handled = canceled || mPowerKeyHandled;
mScreenshotChordPowerKeyTriggered = false;
cancelPendingScreenshotChordAction();
cancelPendingPowerKeyAction();

if (!handled) {
... ...

// No other actions. Handle it immediately.
Log.d("marco", "02. PhoneWindowManager -- interceptPowerKeyUp -- powerPress");
powerPress(eventTime, interactive, mPowerKeyPressCounter); // 开始灭屏流程
}

// Done. Reset our state.
finishPowerKeyPress();
}

}

1.3 PhoneWindowManager.powerPress

执行 powerPress() 方法:

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
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

private void powerPress(long eventTime, boolean interactive, int count) {
... ...

if (count == 2) {
powerMultiPressAction(eventTime, interactive, mDoublePressOnPowerBehavior);
} else if (count == 3) {
powerMultiPressAction(eventTime, interactive, mTriplePressOnPowerBehavior);
} else if (interactive && !mBeganFromNonInteractive) {
switch (mShortPressOnPowerBehavior) {
case SHORT_PRESS_POWER_NOTHING:
break;
case SHORT_PRESS_POWER_GO_TO_SLEEP: // 灭屏
goToSleepFromPowerButton(eventTime, 0);
break;
case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP: // 灭屏,直接跳过 Doze 状态
goToSleepFromPowerButton(eventTime, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
break;
... ...

}
}
}

}

1.4 PhoneWindowManager.goToSleepFromPowerButton

执行 goToSleepFromPowerButton() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

private boolean goToSleepFromPowerButton(long eventTime, int flags) {
... ...

Log.d("marco", "03. PhoneWindowManager -- goToSleepFromPowerButton -- goToSleep");
goToSleep(eventTime, PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, flags); // 调用 goToSleep 方法
return true;
}

}

1.5 PhoneWindowManager.goToSleep

执行 goToSleep() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

PowerManager mPowerManager;

private void goToSleep(long eventTime, int reason, int flags) {
Log.d("marco", "04. PhoneWindowManager -- goToSleep");
mRequestedOrGoingToSleep = true;
mPowerManager.goToSleep(eventTime, reason, flags); // 调用了 PowerManager 的 goToSleep() 灭屏
}

}

1.6 PowerManager.goToSleep

这时候调转到 PowerManager,执行 goToSleep() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// frameworks/base/core/java/android/os/PowerManager.java

public final class PowerManager {

final IPowerManager mService;

@UnsupportedAppUsage
public void goToSleep(long time, int reason, int flags) {
Log.d("marco", "05. PowerManager -- goToSleep");
try {
mService.goToSleep(time, reason, flags); // 跳转到 PowerManagerService 中
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}

}

1.7 PowerManagerService.goToSleep

这时候调转到 PowerManagerService,执行 goToSleep() 方法:

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
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

@VisibleForTesting
final class BinderService extends IPowerManager.Stub {
... ...

@Override // Binder call
public void goToSleep(long eventTime, int reason, int flags) {
Log.d("marco", "06. PowerManagerService -- goToSleep");
if (eventTime > SystemClock.uptimeMillis()) {
throw new IllegalArgumentException("event time must not be in the future");
}

// 检查权限
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DEVICE_POWER, null);

final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
try {
goToSleepInternal(eventTime, reason, flags, uid); // 调用 gotToSleepInternal 方法
} finally {
Binder.restoreCallingIdentity(ident);
}
}
... ..

}

}

1.8 PowerManagerService.goToSleepInternal

执行 goToSleepInternal() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

private void goToSleepInternal(long eventTime, int reason, int flags, int uid) {
Log.d("marco", "07. PowerManagerService -- goToSleep");
synchronized (mLock) {
// 出现了分支,两个核心方法都干了些什么,拆开解析,一步步来
if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) { // 方法 1:goToSleepNoUpdateLocked
Log.d("marco", "U-01. PowerManagerService -- goToSleepInternal -- updatePowerStateLocked");
updatePowerStateLocked(); // 方法 2:updatePowerStateLocked
}
}
}

}

二、灭屏流程(阶段二)

接下来需要分析的代码你我都会很头疼,所以我们需要静下心来一步步对照 Log 分析,仔细的抽丝剥茧,抓住主干、研究细节!

2.1 PowerManagerService.goToSleepNoUpdateLocked

首先看看 goToSleepNoUpdateLocked 方法内部都做了些什么:

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
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) {
Log.d("marco", "G-01. PowerManagerService -- goToSleepInternal -- goToSleepNoUpdateLocked");
... ...

Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep");
try {
reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX,
Math.max(reason, PowerManager.GO_TO_SLEEP_REASON_MIN));
Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason) + " (uid " + uid + ")...");

mLastSleepTime = eventTime; // 标记最后一次灭屏时间
mLastSleepReason = reason;
mSandmanSummoned = true; // 用于判定是否进入屏保
mDozeStartInProgress = true;
// 设置 wakefulness 值为 WAKEFULNESS_DOZING,因此先进 Doze 状态
setWakefulnessLocked(WAKEFULNESS_DOZING, reason, eventTime);

// 灭屏时,将清除以下三种使得屏幕保持亮屏的 wakelock 锁,numWakeLocksCleared 统计下个数
int numWakeLocksCleared = 0;
final int numWakeLocks = mWakeLocks.size();
for (int i = 0; i < numWakeLocks; i++) {
final WakeLock wakeLock = mWakeLocks.get(i);
switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {
case PowerManager.FULL_WAKE_LOCK:
case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
case PowerManager.SCREEN_DIM_WAKE_LOCK:
numWakeLocksCleared += 1;
break;
}
}
EventLogTags.writePowerSleepRequested(numWakeLocksCleared);

// Skip dozing if requested.
// 如果带有 PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE 的 flag,则直接进入 Sleep 状态,不再进入 Doze 状态
if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) {
reallyGoToSleepNoUpdateLocked(eventTime, uid);
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_POWER);
}
return true; // 返回 true
}

}

2.2 PowerManagerService.setWakefulnessLocked

执行 setWakefulnessLocked() 方法:

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
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

private Notifier mNotifier;

@VisibleForTesting
void setWakefulnessLocked(int wakefulness, int reason, long eventTime) {
Log.d("marco", "G-02. PowerManagerService -- setWakefulnessLocked");
if (mWakefulness != wakefulness) {
mWakefulness = wakefulness; // 设置 mWakefulness
mWakefulnessChanging = true;
mDirty |= DIRTY_WAKEFULNESS;

// This is only valid while we are in wakefulness dozing. Set to false otherwise.
mDozeStartInProgress &= (mWakefulness == WAKEFULNESS_DOZING);

if (mNotifier != null) {
// 调用 Notifier 中的方法,做 wakefulness 改变开始时的工作
mNotifier.onWakefulnessChangeStarted(wakefulness, reason, eventTime);
}
mAttentionDetector.onWakefulnessChangeStarted(wakefulness);
}
}

}

2.3 Notifier.onWakefulnessChangeStarted

Notifier 是 PackageManagerService 模块中用于进行 通知 的一个组件类,比如 发送亮灭屏广播就是它来负责

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
// frameworks/base/services/core/java/com/android/server/power/Notifier.java

@VisibleForTesting
public class Notifier {

public void onWakefulnessChangeStarted(final int wakefulness, int reason, long eventTime) {
Log.d("marco", "G-03. Notifier -- onWakefulnessChangeStarted");
final boolean interactive = PowerManagerInternal.isInteractive(wakefulness);
if (DEBUG) {
Slog.d(TAG, "onWakefulnessChangeStarted: wakefulness=" + wakefulness
+ ", reason=" + reason + ", interactive=" + interactive);
}

mHandler.post(new Runnable() {
@Override
public void run() {
mActivityManagerInternal.onWakefulnessChanged(wakefulness);
}
});

if (mInteractive != interactive) {
// Finish up late behaviors if needed.
if (mInteractiveChanging) {
Log.d("marco", "G-test. Notifier -- onWakefulnessChangeStarted");
handleLateInteractiveChange(); // 原本我以为启动指纹监听最后走的逻辑是这边,后来发现并不是
}

// Start input as soon as we start waking up or going to sleep.
mInputManagerInternal.setInteractive(interactive);
mInputMethodManagerInternal.setInteractive(interactive);

// Notify battery stats.
try {
mBatteryStats.noteInteractive(interactive);
} catch (RemoteException ex) { }
StatsLog.write(StatsLog.INTERACTIVE_STATE_CHANGED,
interactive ? StatsLog.INTERACTIVE_STATE_CHANGED__STATE__ON :
StatsLog.INTERACTIVE_STATE_CHANGED__STATE__OFF);

// Handle early behaviors.
mInteractive = interactive;
mInteractiveChangeReason = reason;
mInteractiveChangeStartTime = eventTime;
mInteractiveChanging = true;
// 会先执行 handleEarlyInteractiveChange 方法
handleEarlyInteractiveChange(); // 处理早期工作
}
}

}

2.5 Notifier.handleEarlyInteractiveChange

接下来执行 handleEarlyInteractiveChange 方法:

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
// frameworks/base/services/core/java/com/android/server/power/Notifier.java

@VisibleForTesting
public class Notifier {

private final WindowManagerPolicy mPolicy;

private void handleEarlyInteractiveChange() {
Log.d("marco", "G-04. Notifier -- handleEarlyInteractiveChange");
synchronized (mLock) {
if (mInteractive) {
// Waking up...
mHandler.post(new Runnable() {
@Override
public void run() {
final int why = translateOnReason(mInteractiveChangeReason);
mPolicy.startedWakingUp(why);
}
});

// Send interactive broadcast.
mPendingInteractiveState = INTERACTIVE_STATE_AWAKE;
mPendingWakeUpBroadcast = true;
updatePendingBroadcastLocked();
} else {
// Going to sleep...
// Tell the policy that we started going to sleep.
final int why = translateOffReason(mInteractiveChangeReason);
mHandler.post(new Runnable() {
@Override
public void run() {
mPolicy.startedGoingToSleep(why); // 通过 PhoneWindowManager 设置锁屏
}
});
}
}
}

}

2.6 PhoneWindowManager.startedGoingToSleep

WindowManagerPolicy 的 finishedGoingToSleep() 方法由 PhoneWindowManager 实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

KeyguardServiceDelegate mKeyguardDelegate;

// Called on the PowerManager's Notifier thread.
@Override
public void startedGoingToSleep(int why) {
Log.d("marco", "G-05. PhoneWindowManager -- startedGoingToSleep");
if (DEBUG_WAKEUP) {
Slog.i(TAG, "Started going to sleep... (why=" + WindowManagerPolicyConstants.offReasonToString(why) + ")");
}

mGoingToSleep = true;
mRequestedOrGoingToSleep = true;

if (mKeyguardDelegate != null) {
mKeyguardDelegate.onStartedGoingToSleep(why);
}
}

}

2.7 KeyguardServiceDelegate.onStartedGoingToSleep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java

public class KeyguardServiceDelegate {

protected KeyguardServiceWrapper mKeyguardService;

public void onStartedGoingToSleep(int why) {
Log.d("marco", "G-06. KeyguardServiceDelegate -- onStartedGoingToSleep");
if (mKeyguardService != null) {
// 调用 KeyguardServiceWrapper 的 onStartedGoingToSleep 方法
mKeyguardService.onStartedGoingToSleep(why);
}
mKeyguardState.offReason = why;
mKeyguardState.interactiveState = INTERACTIVE_STATE_GOING_TO_SLEEP;
}

}

2.8 KeyguardServiceWrapper.onStartedGoingToSleep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java

public class KeyguardServiceWrapper implements IKeyguardService {

private IKeyguardService mService;

@Override
public void onStartedGoingToSleep(int reason) {
Log.d("marco", "G-07. KeyguardServiceWrapper -- onStartedGoingToSleep");
try {
mService.onStartedGoingToSleep(reason); // 调用 KeyguardService 的 onStartedGoingToSleep 方法
} catch (RemoteException e) {
Slog.w(TAG , "Remote Exception", e);
}
}

}

2.9 KeyguardService.onStartedGoingToSleep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java

public class KeyguardService extends Service {

private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {

@Override // Binder interface
public void onStartedGoingToSleep(int reason) {
Log.d("marco", "G-08. KeyguardService -- onStartedGoingToSleep");
checkPermission();
// 调用 KeyguardViewMediator 的 onStartedGoingToSleep 方法
mKeyguardViewMediator.onStartedGoingToSleep(reason);
mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.STARTED_GOING_TO_SLEEP);
}

}

}

2.10 KeyguardViewMediator.onStartedGoingToSleep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

public class KeyguardViewMediator extends SystemUI {

public void onStartedGoingToSleep(int why) {
Log.d("marco", "G-09. KeyguardViewMediator -- onStartedGoingToSleep");
synchronized (this) {
... ...

}
// 调用 KeyguardUpdateMonitor 的 dispatchStartedGoingToSleep() 方法
KeyguardUpdateMonitor.getInstance(mContext).dispatchStartedGoingToSleep(why);
notifyStartedGoingToSleep();
}

}

2.11 KeyguardUpdateMonitor.dispatchStartedGoingToSleep

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
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
... ...

case MSG_STARTED_GOING_TO_SLEEP:
// 调用 KeyguardUpdateMonitor 的 handleStartedGoingToSleep 方法
handleStartedGoingToSleep(msg.arg1);
break;

... ...
}
}
}

public void dispatchStartedGoingToSleep(int why) {
Log.d("marco", "G-10. KeyguardUpdateMonitor -- dispatchStartedGoingToSleep");
mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
}

}

2.12 KeyguardUpdateMonitor.handleStartedGoingToSleep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

protected void handleStartedGoingToSleep(int arg1) {
Log.d("marco", "G-11. KeyguardUpdateMonitor -- handleStartedGoingToSleep");
mLockIconPressed = false;
clearBiometricRecognized();
final int count = mCallbacks.size();
for (int i = 0; i < count; i++) {
KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
if (cb != null) {
cb.onStartedGoingToSleep(arg1);
}
}
mGoingToSleep = true;
updateBiometricListeningState(); // 调用 updateBiometricListeningState 方法
}

}

2.13 KeyguardUpdateMonitor.updateBiometricListeningState

1
2
3
4
5
6
7
8
9
10
11
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void updateBiometricListeningState() {
Log.d("marco", "G-12. KeyguardUpdateMonitor -- updateBiometricListeningState");
updateFingerprintListeningState(); // 更新指纹监听状态,一个是指纹
updateFaceListeningState(); // 更新面部监听状态,一个是面部识别
}

}

2.14 KeyguardUpdateMonitor.updateFingerprintListeningState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void updateFingerprintListeningState() {
Log.d("marco", "G-13. KeyguardUpdateMonitor -- updateFingerprintListeningState");
// If this message exists, we should not authenticate again until this message is
// consumed by the handler
if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
return;
}
mHandler.removeCallbacks(mRetryFingerprintAuthentication);
boolean shouldListenForFingerprint = shouldListenForFingerprint();
boolean runningOrRestarting = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING
|| mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING;
if (runningOrRestarting && !shouldListenForFingerprint) {
stopListeningForFingerprint();
} else if (!runningOrRestarting && shouldListenForFingerprint) {
startListeningForFingerprint(); // 开始指纹监听
}
}

}

2.15 KeyguardUpdateMonitor.startListeningForFingerprint

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
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void startListeningForFingerprint() {
Log.d("marco", "G-14. KeyguardUpdateMonitor -- startListeningForFingerprint");
if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING) {
setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
return;
}
if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
// Waiting for restart via handleFingerprintError().
return;
}
if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
int userId = getCurrentUser();
if (isUnlockWithFingerprintPossible(userId)) {
if (mFingerprintCancelSignal != null) {
mFingerprintCancelSignal.cancel();
}
mFingerprintCancelSignal = new CancellationSignal();
// 调用 FingerprintManager 的 authenticate() 方法
mFpm.authenticate(null, mFingerprintCancelSignal, 0, mFingerprintAuthenticationCallback, null, userId);
setFingerprintRunningState(BIOMETRIC_STATE_RUNNING);
}
}

}

Log 验证:

03-26 14:24:22.865 1195 2221 D marco : 01. PhoneWindowManager – interceptKeyBeforeQueueing – interceptPowerKeyUp
03-26 14:24:22.872 1195 2221 D marco : 02. PhoneWindowManager – interceptPowerKeyUp – powerPress
03-26 14:24:22.873 1195 2221 D marco : 03. PhoneWindowManager – goToSleepFromPowerButton – goToSleep
03-26 14:24:22.873 1195 2221 D marco : 04. PhoneWindowManager – goToSleep
03-26 14:24:22.873 1195 2221 D marco : 06. PowerManagerService – goToSleep
03-26 14:24:22.873 1195 2221 D marco : 07. PowerManagerService – goToSleep
03-26 14:24:22.873 1195 2221 D marco : G-01. PowerManagerService – goToSleepInternal – goToSleepNoUpdateLocked
03-26 14:24:22.874 1195 2221 D marco : G-02. PowerManagerService – setWakefulnessLocked
03-26 14:24:22.874 1195 2221 D marco : G-03. Notifier – onWakefulnessChangeStarted
03-26 14:24:22.880 1195 2221 D marco : G-04. Notifier – handleEarlyInteractiveChange

// updatePowerStateLocked 被执行了
03-26 14:24:22.881 1195 2221 D marco : U-01. PowerManagerService – goToSleepInternal – updatePowerStateLocked

03-26 14:24:22.928 1195 1195 D marco : G-05. PhoneWindowManager – startedGoingToSleep
03-26 14:24:22.928 1195 1195 D marco : G-06. KeyguardServiceDelegate – onStartedGoingToSleep
03-26 14:24:22.929 1195 1195 D marco : G-07. KeyguardServiceWrapper – onStartedGoingToSleep
03-26 14:24:22.929 2299 2960 D marco : G-08. KeyguardService – onStartedGoingToSleep
03-26 14:24:22.930 2299 2960 D marco : G-09. KeyguardViewMediator – onStartedGoingToSleep
03-26 14:24:22.958 2299 2960 D marco : G-10. KeyguardUpdateMonitor – dispatchStartedGoingToSleep
03-26 14:24:23.005 2299 2299 D marco : G-11. KeyguardUpdateMonitor – handleStartedGoingToSleep
03-26 14:24:23.008 2299 2299 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:24:23.008 2299 2299 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState

03-26 14:24:23.020 2299 2299 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:24:23.020 2299 2299 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState

// … … 耗时操作区(14:24:22.865 ~ 14:24:24.641,耗时超过 1.5s)

03-26 14:24:24.594 2299 2299 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:24:24.594 2299 2299 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState

03-26 14:24:24.595 2299 2299 D marco : G-14. KeyguardUpdateMonitor – startListeningForFingerprint

03-26 14:24:24.641 1195 1195 D marco : startAuthentication(com.android.systemui) // 指纹监听生效

三、灭屏流程(阶段三)

我们从上面的 log 中发现 updatePowerStateLocked 被执行了,我们先跟下流程,然后再解释上面这一段让人很觉得很乱的 Log 信息。

3.1 PowerManagerService.updatePowerStateLocked

执行 updatePowerStateLocked 方法:

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
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

private void updatePowerStateLocked() {
if (!mSystemReady || mDirty == 0) {
return;
}
if (!Thread.holdsLock(mLock)) {
Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked");
}

Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState");
try {
// Phase 0: Basic state updates.
updateIsPoweredLocked(mDirty);
updateStayOnLocked(mDirty);
updateScreenBrightnessBoostLocked(mDirty);

// Phase 1: Update wakefulness.
// Loop because the wake lock and user activity computations are influenced
// by changes in wakefulness.
final long now = SystemClock.uptimeMillis();
int dirtyPhase2 = 0;
for (;;) {
int dirtyPhase1 = mDirty;
dirtyPhase2 |= dirtyPhase1;
mDirty = 0;

updateWakeLockSummaryLocked(dirtyPhase1);
updateUserActivitySummaryLocked(now, dirtyPhase1);
if (!updateWakefulnessLocked(dirtyPhase1)) {
break;
}
}

// Phase 2: Lock profiles that became inactive/not kept awake.
updateProfilesLocked(now);

// Phase 3: Update display power state.
final boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2);

// Phase 4: Update dream state (depends on display ready signal).
updateDreamLocked(dirtyPhase2, displayBecameReady);

// Phase 5: Send notifications, if needed.
// 会调用到 finishWakefulnessChangeIfNeededLocked 方法
finishWakefulnessChangeIfNeededLocked();

// Phase 6: Update suspend blocker.
// Because we might release the last suspend blocker here, we need to make sure
// we finished everything else first!
updateSuspendBlockerLocked();
} finally {
Trace.traceEnd(Trace.TRACE_TAG_POWER);
}
}

}

3.2 PowerManagerService.finishWakefulnessChangeIfNeededLocked

执行 finishWakefulnessChangeIfNeededLocked 方法:

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
// frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

public final class PowerManagerService extends SystemService implements Watchdog.Monitor {

private void finishWakefulnessChangeIfNeededLocked() {
if (mWakefulnessChanging && mDisplayReady) {
if (mWakefulness == WAKEFULNESS_DOZING && (mWakeLockSummary & WAKE_LOCK_DOZE) == 0) {
return; // wait until dream has enabled dozing
} else {
// Doze wakelock acquired (doze started) or device is no longer dozing.
mDozeStartInProgress = false;
}
if (mWakefulness == WAKEFULNESS_DOZING || mWakefulness == WAKEFULNESS_ASLEEP) {
logSleepTimeoutRecapturedLocked();
}
if (mWakefulness == WAKEFULNESS_AWAKE) {
Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, TRACE_SCREEN_ON, 0);
final int latencyMs = (int) (SystemClock.uptimeMillis() - mLastWakeTime);
if (latencyMs >= SCREEN_ON_LATENCY_WARNING_MS) {
Slog.w(TAG, "Screen on took " + latencyMs + " ms");
}
}
mWakefulnessChanging = false;
mNotifier.onWakefulnessChangeFinished();
}
}

}

3.3 Notifier.onWakefulnessChangeFinished

执行 onWakefulnessChangeFinished 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// frameworks/base/services/core/java/com/android/server/power/Notifier.java

public class Notifier {

/**
* Notifies that the device has finished changing wakefulness.
*/
public void onWakefulnessChangeFinished() {
if (DEBUG) {
Slog.d(TAG, "onWakefulnessChangeFinished");
}

if (mInteractiveChanging) {
mInteractiveChanging = false;
handleLateInteractiveChange();
}
}

}

3.4 Notifier.handleLateInteractiveChange

执行 handleLateInteractiveChange 方法:

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
// frameworks/base/services/core/java/com/android/server/power/Notifier.java

public class Notifier {

private void handleLateInteractiveChange() {
Log.d("marco", "U-02. Notifier -- handleLateInteractiveChange");
synchronized (mLock) {
final int interactiveChangeLatency =
(int) (SystemClock.uptimeMillis() - mInteractiveChangeStartTime);
if (mInteractive) {
... ...
} else {
// Cancel pending user activity.
if (mUserActivityPending) {
mUserActivityPending = false;
mHandler.removeMessages(MSG_USER_ACTIVITY);
}

// Tell the policy we finished going to sleep.
final int why = translateOffReason(mInteractiveChangeReason);
mHandler.post(new Runnable() {
@Override
public void run() {
LogMaker log = new LogMaker(MetricsEvent.SCREEN);
log.setType(MetricsEvent.TYPE_CLOSE);
log.setSubtype(why);
log.setLatency(interactiveChangeLatency);
log.addTaggedData(MetricsEvent.FIELD_SCREEN_SLEEP_REASON, mInteractiveChangeReason);
MetricsLogger.action(log);
EventLogTags.writePowerScreenState(0, why, 0, 0, interactiveChangeLatency);
// 调用 PhoneWindowManager 的 finishedGoingToSleep 方法
mPolicy.finishedGoingToSleep(why);
}
});

// Send non-interactive broadcast.
mPendingInteractiveState = INTERACTIVE_STATE_ASLEEP;
mPendingGoToSleepBroadcast = true;
updatePendingBroadcastLocked();
}
}
}

}

3.5 PhoneWindowManager.finishedGoingToSleep

执行 finishedGoingToSleep 方法:

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
// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

public class PhoneWindowManager implements WindowManagerPolicy {

// Called on the PowerManager's Notifier thread.
@Override
public void finishedGoingToSleep(int why) {
Log.d("marco", "U-03. PhoneWindowManager -- finishedGoingToSleep");
EventLogTags.writeScreenToggled(0);
if (DEBUG_WAKEUP) {
Slog.i(TAG, "Finished going to sleep... (why="
+ WindowManagerPolicyConstants.offReasonToString(why) + ")");
}
MetricsLogger.histogram(mContext, "screen_timeout", mLockScreenTimeout / 1000);

mGoingToSleep = false;
mRequestedOrGoingToSleep = false;
mDefaultDisplayPolicy.setAwake(false);

// We must get this work done here because the power manager will drop
// the wake lock and let the system suspend once this function returns.
synchronized (mLock) {
updateWakeGestureListenerLp();
updateLockScreenTimeout();
}
mDefaultDisplayRotation.updateOrientationListener();

if (mKeyguardDelegate != null) {
// 调用 KeyguardServiceDelegate 的 onFinishedGoingToSleep 方法
mKeyguardDelegate.onFinishedGoingToSleep(why, mCameraGestureTriggeredDuringGoingToSleep);
}
if (mDisplayFoldController != null) {
mDisplayFoldController.finishedGoingToSleep();
}
mCameraGestureTriggeredDuringGoingToSleep = false;
}

}

3.6 KeyguardServiceDelegate.onFinishedGoingToSleep

执行 onFinishedGoingToSleep 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
// frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java

public class KeyguardServiceDelegate {

public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) {
Log.d("marco", "U-04. KeyguardServiceDelegate -- onFinishedGoingToSleep");
if (mKeyguardService != null) {
mKeyguardService.onFinishedGoingToSleep(why, cameraGestureTriggered);
}
mKeyguardState.interactiveState = INTERACTIVE_STATE_SLEEP;
}

}

3.7 KeyguardServiceWrapper.onFinishedGoingToSleep

执行 onFinishedGoingToSleep 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java

public class KeyguardServiceWrapper implements IKeyguardService {

private IKeyguardService mService;

@Override
public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) {
Log.d("marco", "U-05. KeyguardServiceWrapper -- onFinishedGoingToSleep");
try {
mService.onFinishedGoingToSleep(reason, cameraGestureTriggered);
} catch (RemoteException e) {
Slog.w(TAG , "Remote Exception", e);
}
}

}

3.8 KeyguardService.onFinishedGoingToSleep

执行 onFinishedGoingToSleep 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java

public class KeyguardService extends Service {

private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {

@Override // Binder interface
public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) {
Log.d("marco", "U-06. KeyguardService -- onFinishedGoingToSleep");
checkPermission();
mKeyguardViewMediator.onFinishedGoingToSleep(reason, cameraGestureTriggered);
mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.FINISHED_GOING_TO_SLEEP);
}

}

}

3.9 KeyguardViewMediator.onFinishedGoingToSleep

执行 onFinishedGoingToSleep 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

public class KeyguardViewMediator extends SystemUI {

public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) {
Log.d("marco", "U-07. KeyguardViewMediator -- onFinishedGoingToSleep");
if (DEBUG) Log.d(TAG, "onFinishedGoingToSleep(" + why + ")");
synchronized (this) {
... ...

if (mPendingLock) {
doKeyguardLocked(null); // 执行 doKeyguardLocked 方法
mPendingLock = false;
}

if (!mLockLater && !cameraGestureTriggered) {
doKeyguardForChildProfilesLocked();
}

}
KeyguardUpdateMonitor.getInstance(mContext).dispatchFinishedGoingToSleep(why);
}

}

3.10 KeyguardViewMediator.doKeyguardLocked

执行 doKeyguardLocked 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

public class KeyguardViewMediator extends SystemUI {

private void doKeyguardLocked(Bundle options) {
Log.d("marco", "U-08. KeyguardViewMediator -- doKeyguardLocked");
... ...

if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
showLocked(options);
}

}

3.11 KeyguardViewMediator.showLocked

执行 showLocked 方法:

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
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

public class KeyguardViewMediator extends SystemUI {

private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW:
handleShow((Bundle) msg.obj); // 调用 handleShow 方法
break;
... ...
}
}
}

private void showLocked(Bundle options) {
Log.d("marco", "U-09. KeyguardViewMediator -- showLocked");
Trace.beginSection("KeyguardViewMediator#showLocked aqcuiring mShowKeyguardWakeLock");
if (DEBUG) Log.d(TAG, "showLocked");
// ensure we stay awake until we are finished displaying the keyguard
mShowKeyguardWakeLock.acquire();
Message msg = mHandler.obtainMessage(SHOW, options);
mHandler.sendMessage(msg);
Trace.endSection();
}

}

3.12 KeyguardViewMediator.handleShow

执行 handleShow 方法:

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
// frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

public class KeyguardViewMediator extends SystemUI {

private void handleShow(Bundle options) {
Log.d("marco", "U-10. KeyguardViewMediator -- handleShow");
Trace.beginSection("KeyguardViewMediator#handleShow");
final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
if (mLockPatternUtils.isSecure(currentUser)) {
mLockPatternUtils.getDevicePolicyManager().reportKeyguardSecured(currentUser);
}
synchronized (KeyguardViewMediator.this) {
if (!mSystemReady) {
if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
return;
} else {
if (DEBUG) Log.d(TAG, "handleShow");
}
... ...

// 调用 StatusBarKeyguardViewManager 的 show 方法
mStatusBarKeyguardViewManager.show(options);
... ...

}
mKeyguardDisplayManager.show();
Trace.endSection();
}

}

3.13 StatusBarKeyguardViewManager.show

执行 show 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java

public class StatusBarKeyguardViewManager implements RemoteInputController.Callback, ... {

public void show(Bundle options) {
Log.d("marco", "U-11. StatusBarKeyguardViewManager -- show");
mShowing = true;
mStatusBarWindowController.setKeyguardShowing(true);
mKeyguardMonitor.notifyKeyguardState(
mShowing, mKeyguardMonitor.isSecure(), mKeyguardMonitor.isOccluded());
reset(true /* hideBouncerWhenShowing */); // 调用 reset 方法
StatsLog.write(StatsLog.KEYGUARD_STATE_CHANGED, StatsLog.KEYGUARD_STATE_CHANGED__STATE__SHOWN);
}

}

3.14 StatusBarKeyguardViewManager.reset

执行 reset 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java

public class StatusBarKeyguardViewManager implements RemoteInputController.Callback, ... {

public void reset(boolean hideBouncerWhenShowing) {
Log.d("marco", "U-12. StatusBarKeyguardViewManager -- reset");
if (mShowing) {
if (mOccluded && !mDozing) {
mStatusBar.hideKeyguard();
if (hideBouncerWhenShowing || mBouncer.needsFullscreenBouncer()) {
hideBouncer(false /* destroyView */);
}
} else {
showBouncerOrKeyguard(hideBouncerWhenShowing); // 调用 showBouncerOrKeyguard 方法
}
KeyguardUpdateMonitor.getInstance(mContext).sendKeyguardReset();
updateStates();
}
}

}

3.15 StatusBarKeyguardViewManager.showBouncerOrKeyguard

执行 showBouncerOrKeyguard 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java

public class StatusBarKeyguardViewManager implements RemoteInputController.Callback, ... {

protected void showBouncerOrKeyguard(boolean hideBouncerWhenShowing) {
Log.d("marco", "U-13. StatusBarKeyguardViewManager -- showBouncerOrKeyguard");
if (mBouncer.needsFullscreenBouncer() && !mDozing) {
// The keyguard might be showing (already). So we need to hide it.
mStatusBar.hideKeyguard();
mBouncer.show(true /* resetSecuritySelection */);
} else {
mStatusBar.showKeyguard();
if (hideBouncerWhenShowing) {
hideBouncer(shouldDestroyViewOnReset() /* destroyView */);
mBouncer.prepare();
}
}
updateStates(); // 调用 updateStates 方法
}

}

3.16 StatusBarKeyguardViewManager.updateStates

执行 updateStates 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java

public class StatusBarKeyguardViewManager implements RemoteInputController.Callback, ... {

protected void updateStates() {
Log.d("marco", "U-14. StatusBarKeyguardViewManager -- updateStates");
... ...

KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
if ((showing && !occluded) != (mLastShowing && !mLastOccluded) || mFirstUpdate) {
// 调用 KeyguardUpdateMonitor 的 onKeyguardVisibilityChanged 方法
updateMonitor.onKeyguardVisibilityChanged(showing && !occluded);
}
if (bouncerShowing != mLastBouncerShowing || mFirstUpdate) {
updateMonitor.sendKeyguardBouncerChanged(bouncerShowing);
}
... ...

}

}

3.17 KeyguardUpdateMonitor.onKeyguardVisibilityChanged

执行 onKeyguardVisibilityChanged 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

public void onKeyguardVisibilityChanged(boolean showing) {
Log.d("marco", "U-15. KeyguardUpdateMonitor -- onKeyguardVisibilityChanged");
checkIsHandlerThread();
Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
mKeyguardIsVisible = showing;

if (showing) {
mSecureCameraLaunched = false;
}

for (int i = 0; i < mCallbacks.size(); i++) {
KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
if (cb != null) {
cb.onKeyguardVisibilityChangedRaw(showing);
}
}
updateBiometricListeningState(); // 调用 updateBiometricListeningState 方法
}

}

3.18 KeyguardUpdateMonitor.updateBiometricListeningState

执行 updateBiometricListeningState 方法:

1
2
3
4
5
6
7
8
9
10
11
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void updateBiometricListeningState() {
Log.d("marco", "U-16. KeyguardUpdateMonitor -- updateBiometricListeningState");
updateFingerprintListeningState(); // 调用 updateFingerprintListeningState 方法
updateFaceListeningState();
}

}

3.19 KeyguardUpdateMonitor.updateFingerprintListeningState

执行 updateFingerprintListeningState 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void updateFingerprintListeningState() {
Log.d("marco", "U-17. KeyguardUpdateMonitor -- updateFingerprintListeningState");
// If this message exists, we should not authenticate again until this message is
// consumed by the handler
if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
return;
}
mHandler.removeCallbacks(mRetryFingerprintAuthentication);
boolean shouldListenForFingerprint = shouldListenForFingerprint();
boolean runningOrRestarting = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING
|| mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING;
if (runningOrRestarting && !shouldListenForFingerprint) {
stopListeningForFingerprint();
} else if (!runningOrRestarting && shouldListenForFingerprint) {
startListeningForFingerprint(); // 调用 startListeningForFingerprint 方法
}
}

}

3.20 KeyguardUpdateMonitor.startListeningForFingerprint

执行 startListeningForFingerprint 方法:

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
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void startListeningForFingerprint() {
Log.d("marco", "U-18. KeyguardUpdateMonitor -- startListeningForFingerprint");
if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING) {
setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
return;
}
if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
// Waiting for restart via handleFingerprintError().
return;
}
if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
int userId = getCurrentUser();
if (isUnlockWithFingerprintPossible(userId)) {
if (mFingerprintCancelSignal != null) {
mFingerprintCancelSignal.cancel();
}
mFingerprintCancelSignal = new CancellationSignal();
mFpm.authenticate(null, mFingerprintCancelSignal, 0, mFingerprintAuthenticationCallback, null, userId);
setFingerprintRunningState(BIOMETRIC_STATE_RUNNING);
}
}

}

Log 验证:

03-26 14:27:54.124 1200 2217 D marco : 01. PhoneWindowManager – interceptKeyBeforeQueueing – interceptPowerKeyUp
03-26 14:27:54.124 1200 2217 D marco : 02. PhoneWindowManager – interceptPowerKeyUp – powerPress
03-26 14:27:54.125 1200 2217 D marco : 03. PhoneWindowManager – goToSleepFromPowerButton – goToSleep
03-26 14:27:54.125 1200 2217 D marco : 04. PhoneWindowManager – goToSleep
03-26 14:27:54.125 1200 2217 D marco : 06. PowerManagerService – goToSleep
03-26 14:27:54.125 1200 2217 D marco : 07. PowerManagerService – goToSleep
03-26 14:27:54.126 1200 2217 D marco : G-01. PowerManagerService – goToSleepInternal – goToSleepNoUpdateLocked
03-26 14:27:54.126 1200 2217 D marco : G-02. PowerManagerService – setWakefulnessLocked
03-26 14:27:54.126 1200 2217 D marco : G-03. Notifier – onWakefulnessChangeStarted
03-26 14:27:54.127 1200 2217 D marco : G-04. Notifier – handleEarlyInteractiveChange
03-26 14:27:54.127 1200 2217 D marco : U-01. PowerManagerService – goToSleepInternal – updatePowerStateLocked

// … … 耗时操作开始(14:27:54.127 ~ 14:27:55.945,耗时 1.8s)

03-26 14:27:54.137 1200 1200 D marco : G-05. PhoneWindowManager – startedGoingToSleep
03-26 14:27:54.137 1200 1200 D marco : G-06. KeyguardServiceDelegate – onStartedGoingToSleep
03-26 14:27:54.138 1200 1200 D marco : G-07. KeyguardServiceWrapper – onStartedGoingToSleep
03-26 14:27:54.140 2302 5304 D marco : G-08. KeyguardService – onStartedGoingToSleep
03-26 14:27:54.155 2302 5304 D marco : G-09. KeyguardViewMediator – onStartedGoingToSleep
03-26 14:27:54.171 2302 5304 D marco : G-10. KeyguardUpdateMonitor – dispatchStartedGoingToSleep
03-26 14:27:54.172 2302 2302 D marco : G-11. KeyguardUpdateMonitor – handleStartedGoingToSleep
03-26 14:27:54.177 2302 2302 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:54.177 2302 2302 D marco : U-16. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:54.177 2302 2302 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:54.177 2302 2302 D marco : U-17. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:55.188 2302 2302 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:55.189 2302 2302 D marco : U-16. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:55.189 2302 2302 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:55.189 2302 2302 D marco : U-17. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:55.254 2302 2302 D marco : U-12. StatusBarKeyguardViewManager – reset
03-26 14:27:55.254 2302 2302 D marco : U-14. StatusBarKeyguardViewManager – updateStates
03-26 14:27:55.287 1200 2075 D marco : U-02. Notifier – handleLateInteractiveChange
03-26 14:27:55.291 1200 1200 D marco : U-03. PhoneWindowManager – finishedGoingToSleep
03-26 14:27:55.306 1200 1200 D marco : U-04. KeyguardServiceDelegate – onFinishedGoingToSleep
03-26 14:27:55.307 1200 1200 D marco : U-05. KeyguardServiceWrapper – onFinishedGoingToSleep
03-26 14:27:55.307 2302 5304 D marco : U-06. KeyguardService – onFinishedGoingToSleep
03-26 14:27:55.307 2302 5304 D marco : U-07. KeyguardViewMediator – onFinishedGoingToSleep
03-26 14:27:55.307 2302 5304 D marco : U-08. KeyguardViewMediator – doKeyguardLocked
03-26 14:27:55.313 2302 5304 D marco : U-09. KeyguardViewMediator – showLocked
03-26 14:27:55.717 2302 2302 D marco : U-10. KeyguardViewMediator – handleShow
03-26 14:27:55.726 2302 2302 D marco : U-11. StatusBarKeyguardViewManager – show
03-26 14:27:55.745 2302 2302 D marco : U-12. StatusBarKeyguardViewManager – reset
03-26 14:27:55.745 2302 2302 D marco : U-13. StatusBarKeyguardViewManager – showBouncerOrKeyguard
03-26 14:27:55.906 2302 2302 D marco : U-14. StatusBarKeyguardViewManager – updateStates
03-26 14:27:55.906 2302 2302 D marco : U-15. KeyguardUpdateMonitor – onKeyguardVisibilityChanged
03-26 14:27:55.914 2302 2302 D marco : G-12. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:55.914 2302 2302 D marco : U-16. KeyguardUpdateMonitor – updateBiometricListeningState
03-26 14:27:55.914 2302 2302 D marco : G-13. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:55.914 2302 2302 D marco : U-17. KeyguardUpdateMonitor – updateFingerprintListeningState
03-26 14:27:55.915 2302 2302 D marco : G-14. KeyguardUpdateMonitor – startListeningForFingerprint
03-26 14:27:55.915 2302 2302 D marco : U-18. KeyguardUpdateMonitor – startListeningForFingerprint

03-26 14:27:55.945 1200 1200 D marco : startAuthentication(com.android.systemui) // 指纹监听生效,耗时 1.8s

针对以上 Log,就产生了一个疑问疑点:
updateFingerprintListeningState 更新生物监听状态(包含面部和指纹)方法被执行了很多遍,很多地方调用到了它?

我们看下 updateFingerprintListeningState 放在在哪里被调用了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

public class KeyguardUpdateMonitor implements TrustManager.TrustListener {

private void updateFingerprintListeningState() {
Log.d("mx_dump", "updateFingerprintListeningState", new Exception());
// If this message exists, we should not authenticate again until this message is
// consumed by the handler
if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
return;
}
mHandler.removeCallbacks(mRetryFingerprintAuthentication);
boolean shouldListenForFingerprint = shouldListenForFingerprint();
boolean runningOrRestarting = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING
|| mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING;
if (runningOrRestarting && !shouldListenForFingerprint) {
stopListeningForFingerprint();
} else if (!runningOrRestarting && shouldListenForFingerprint) {
startListeningForFingerprint();
}
}

}

Log 验证:

handleStartedGoingToSleep 调用:

handleDreamingStateChanged 调用:

onKeyguardVisibilityChanged 调用:(灭屏指纹监听走的流程)

handleFinishedGoingToSleep 调用:

handleKeyguardReset 调用:

setKeyguardGoingAway 调用:

# 灭屏指纹监听流程图

<重新绘制>