KeyguardService作为锁屏的服务供系统进程调用。KeyguardService在systemui进程,在system进程KeyguardServiceWrapper是其代理类。系统有相关Keyguard相关的事情要做,都是让KeyguardServiceWrapper去通知KeygaurdService做的。
那么KeyguardServiceWrapper是做到代理KeyguardService呢?
通过追踪代码发现在,当系统SystemServer 的 startOtherServices() 方法中完成各种Service的初始化后,会调用AMS的SystemReady()方法:
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
....
mActivityManagerService.systemReady(() -> {
....
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_ACTIVITY_MANAGER_READY);
....
try {
startSystemUi(context, windowManagerF); //启动SystemUI进程
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
....
}, t);
....
}
AMS的SystemReady()方法如下。SystemServer 的startSystemUi()方法启动 com.android.internal.R.string.config_systemUIServiceComponent定义的组件,启动SystemUI进程。
public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
....
synchronized(this) {
if (mSystemReady) {
if (goingCallback != null) {
goingCallback.run(); //执行SystemServer的startOtherServices() 方法传入的线程,见前面的代码
}
return;
}
....
}
}
SystemServer 的 startSystemUi() 方法:
private static void startSystemUi(Context context, WindowManagerService windowManager) {
PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
Intent intent = new Intent();
intent.setComponent(pm.getSystemUiServiceComponent());//指向com.android.systemui/com.android.systemui.SystemUIService服务
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
context.startServiceAsUser(intent, UserHandle.SYSTEM);
windowManager.onSystemUiStarted();
}
启动SystemUIService会启动systemui进程,然后执行 WindowManagerService的onSystemUiStarted()方法。方法中mPolicy实际上指向的是PhoneWindowManager
public void onSystemUiStarted() {
mPolicy.onSystemUiStarted();
}
再来看WindowManagerService的mPolicy是怎么与PhoneWindowManager关联上的?
原来在SystemServer 的 startOtherServices() 方法中,调用WindowManagerService的main()方法创建WindowManagerService的时候,new 了一个PhoneWindowManager
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
....
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager()/*WindowManagerService中的mPolicy*/, mActivityManagerService.mActivityTaskManager);
....
}
WindowManagerService的公有方法main()如下,第二个main()方方法构造了WindowManagerService
public static WindowManagerService main(final Context context, final InputManagerService im,
final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,
ActivityTaskManagerService atm) {
return main(context, im, showBootMsgs, onlyCore, policy/*指向PhoneWindowManager*/, atm,
SurfaceControl.Transaction::new, Surface::new, SurfaceControl.Builder::new);
}
public static WindowManagerService main(final Context context, final InputManagerService im,
final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,
ActivityTaskManagerService atm, Supplier<SurfaceControl.Transaction> transactionFactory,
Supplier<Surface> surfaceFactory,
Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {
DisplayThread.getHandler().runWithScissors(() ->
sInstance = new WindowManagerService(context, im, showBootMsgs, onlyCore, policy/*指向PhoneWindowManager*/,
atm, transactionFactory, surfaceFactory, surfaceControlFactory), 0);
return sInstance;
}
在WindowManagerService的构造方法中,成员mPolicy最终指向PhoneWindowManager
private WindowManagerService(Context context, InputManagerService inputManager,
boolean showBootMsgs, boolean onlyCore, WindowManagerPolicy policy,
ActivityTaskManagerService atm, Supplier<SurfaceControl.Transaction> transactionFactory,
Supplier<Surface> surfaceFactory,
Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {
....
mPolicy = policy;/*指向PhoneWindowManager*/
....
}
接着看PhoneWindowManager的onSystemUiStarted方法,最后会调用KeyguardServiceDelegate的bindService()方法
public void onSystemUiStarted() {
bindKeyguard();
}
private void bindKeyguard() {
synchronized (mLock) {
if (mKeyguardBound) {
return;
}
mKeyguardBound = true;
}
mKeyguardDelegate.bindService(mContext);
}
KeyguardServiceDelegate的bindService()方法会真正去bind SystemUI的KeyguardService
public void bindService(Context context) {
....
//指向com.android.systemui/com.android.systemui.keyguard.KeyguardService
final ComponentName keyguardComponent = ComponentName.unflattenFromString(
resources.getString(com.android.internal.R.string.config_keyguardComponent));
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
intent.setComponent(keyguardComponent);
//bind KeyguardService
if (!context.bindServiceAsUser(intent, mKeyguardConnection,
Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM)) {
....
}
....
}
服务绑定建立之后, new KeyguardServiceWrapper(),让KeyguardServiceWrapper最为KeygaurdService在System进程的代理。以后系统有相关Keyguard相关的事情要做,那就让KeyguardServiceWrapper去通知KeygaurdService做就OK了。从后面的代码调用全部是mKeyguardService相关的调用就可以看出来。
private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//KeyguardServiceWrapper最为KeygaurdService在System进程的代理。
mKeyguardService = new KeyguardServiceWrapper(mContext,
IKeyguardService.Stub.asInterface(service), mCallback);
if (mKeyguardState.systemIsReady) {
mKeyguardService.onSystemReady();
if (mKeyguardState.currentUser != UserHandle.USER_NULL) {
mKeyguardService.setCurrentUser(mKeyguardState.currentUser);
}
if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE
|| mKeyguardState.interactiveState == INTERACTIVE_STATE_WAKING) {
mKeyguardService.onStartedWakingUp();
}
if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE) {
mKeyguardService.onFinishedWakingUp();
}
if (mKeyguardState.screenState == SCREEN_STATE_ON
|| mKeyguardState.screenState == SCREEN_STATE_TURNING_ON) {
mKeyguardService.onScreenTurningOn(
new KeyguardShowDelegate(mDrawnListenerWhenConnect));
}
if (mKeyguardState.screenState == SCREEN_STATE_ON) {
mKeyguardService.onScreenTurnedOn();
}
mDrawnListenerWhenConnect = null;
}
if (mKeyguardState.bootCompleted) {
mKeyguardService.onBootCompleted();
}
if (mKeyguardState.occluded) {
mKeyguardService.setOccluded(mKeyguardState.occluded, false /* animate */);
}
if (!mKeyguardState.enabled) {
mKeyguardService.setKeyguardEnabled(mKeyguardState.enabled);
}
}
....
};
从前面的分析可知:
1.KeyguardServiceWrapper是 KeyguardService 在system进程的代理, KeyguardServiceWrapper 其实只是一个空壳子,什么事情都是KeyguardService 去做。
2.在system进程在绑定 KeyguardService之前,会在SystemServer 的 startOtherServices() 方法中先拉起SystemUIService服务,然后再绑定KeyguardService.