Android 7.1.1 锁屏界面启动流程
前几天遇到一个低概率复现锁屏界面不显示,只显示状态栏的问题,跟了下锁屏界面启动显示的流程,在这分享下,也方便以后自己查看。前面简单介绍了下Zygote启动流程, Zygote进程启动后会首先创建一个SystemServer进程,SystemServer进程在调用startOtherServices同时也会调用WindowManagerService的systemReady()方法
- //frameworks/base/services/java/com/android/server/SystemServer.java
- private void startOtherServices() {
- ...
- wm = WindowManagerService.main(context, inputManager,
- mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
- !mFirstBoot, mOnlyCore);
- ...
- try {
- wm.systemReady();
- Slog.i("jason11", "SystemServer wm.systemReady");
- } catch (Throwable e) {
- reportWtf("making Window Manager Service ready", e);
- }
- ...
- }
在WindowManagerService中直接调用了PhoneWindowManager里的systemReady()
- //frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
- //final WindowManagerPolicy mPolicy = new PhoneWindowManager();
- public void systemReady() {
- mPolicy.systemReady();
- }
在 PhoneWindowManager的systemReady()会根据一个Boolean值bindKeyguardNow来决定是否绑定keyguard service
- //frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
- /** {@inheritDoc} */
- @Override
- public void systemReady() {
- mKeyguardDelegate = new KeyguardServiceDelegate(mContext);
- mKeyguardDelegate.onSystemReady();
- readCameraLensCoverState();
- updateUiMode();
- boolean bindKeyguardNow;
- synchronized (mLock) {
- updateOrientationListenerLp();
- mSystemReady = true;
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- updateSettings();
- }
- });
- bindKeyguardNow = mDeferBindKeyguard;
- if (bindKeyguardNow) {
- // systemBooted ran but wasn't able to bind to the Keyguard, we'll do it now.
- mDeferBindKeyguard = false;
- }
- }
- if (bindKeyguardNow) {
- mKeyguardDelegate.bindService(mContext);
- mKeyguardDelegate.onBootCompleted();
- }
- mSystemGestures.systemReady();
- }
看到这里,可能会想到如果bindKeyguardNow为false就会不绑定,后面通过继续跟踪发现在PhoneWindowManager的systemBooted()里也会去绑定keyguard service,如果在systemBooted里绑定了就不在systemReady里再去绑定,自己测试的时候是在systemBooted绑定的
- //frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
- /** {@inheritDoc} */
- @Override
- public void systemBooted() {
- boolean bindKeyguardNow = false;
- synchronized (mLock) {
- // Time to bind Keyguard; take care to only bind it once, either here if ready or
- // in systemReady if not.
- if (mKeyguardDelegate != null) {
- bindKeyguardNow = true;
- } else {
- // Because mKeyguardDelegate is null, we know that the synchronized block in
- // systemReady didn't run yet and setting this will actually have an effect.
- mDeferBindKeyguard = true;
- }
- }
- if (bindKeyguardNow) {
- mKeyguardDelegate.bindService(mContext);
- mKeyguardDelegate.onBootCompleted();
- }
- synchronized (mLock) {
- mSystemBooted = true;
- }
- startedWakingUp();
- screenTurningOn(null);
- screenTurnedOn();
- }
下面就通过如下的时序图看看是如何调用到systemBooted的,就不在一步步跟了
通过上面的分析知道,无论是在systemReady或systemBooted,都调用了KeyguardServiceDelegate对象的bindService方法,下面就以这个方法开始,看看锁屏界面是怎么显示出来的,先看看下面的时序图,再来分步讲解
1、先来看看在KeyguardServiceDelegate如何绑定KeyguardService的
- //frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
- public class KeyguardServiceDelegate {
- ...
- public void bindService(Context context) {
- Intent intent = new Intent();
- final Resources resources = context.getApplicationContext().getResources();
- final ComponentName keyguardComponent = ComponentName.unflattenFromString(
- resources.getString(com.android.internal.R.string.config_keyguardComponent));
- intent.setComponent(keyguardComponent);
- if (!context.bindServiceAsUser(intent, mKeyguardConnection,
- Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
- Log.v(TAG, "*** Keyguard: can't bind to " + keyguardComponent);
- mKeyguardState.showing = false;
- mKeyguardState.showingAndNotOccluded = false;
- mKeyguardState.secure = false;
- synchronized (mKeyguardState) {
- // TODO: Fix synchronisation model in this class. The other state in this class
- // is at least self-healing but a race condition here can lead to the scrim being
- // stuck on keyguard-less devices.
- mKeyguardState.deviceHasKeyguard = false;
- hideScrim();
- }
- } else {
- if (DEBUG) Log.v(TAG, "*** Keyguard started");
- }
- }
- ...
- }
在bindService中调用了bindServiceAsUser绑定指定intent的service,config_keyguardComponent的定义如下
- //frameworks/base/core/res/res/values/config.xml
- <!-- Keyguard component -->
- <string name="config_keyguardComponent" translatable="false">com.android.systemui/com.android.systemui.keyguard.KeyguardService</string>
当绑定成功后会调用mKeyguardConnection里的onServiceConnected方法
- //frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
- public class KeyguardServiceDelegate {
- ...
- private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- if (DEBUG) Log.v(TAG, "*** Keyguard connected (yay!)");
- mKeyguardService = new KeyguardServiceWrapper(mContext,
- IKeyguardService.Stub.asInterface(service));
- if (mKeyguardState.systemIsReady) {
- // If the system is ready, it means keyguard crashed and restarted.
- mKeyguardService.onSystemReady();
- // This is used to hide the scrim once keyguard displays.
- if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE) {
- mKeyguardService.onStartedWakingUp();
- }
- 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);
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- if (DEBUG) Log.v(TAG, "*** Keyguard disconnected (boo!)");
- mKeyguardService = null;
- }
- };
- ...
- }
当mKeyguardState.systemIsReady为true是,就会通过KeyguardServiceWrapper的实例mKeyguardService调用onSystemReady方法,在 KeyguardServiceWrapper的onSystemReady里调用了上面刚刚绑定成功的KeyguardService的onSystemReady方法
- //frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java
- public class KeyguardServiceWrapper implements IKeyguardService {
- ...
- @Override // Binder interface
- public void onSystemReady() {
- try {
- mService.onSystemReady();
- } catch (RemoteException e) {
- Slog.w(TAG , "Remote Exception", e);
- }
- }
- ...
- }
在KeyguardService的onSystemReady里调用了KeyguardViewMediator里的onSystemReady,在这里就不贴这个代码了,直接看看KeyguardViewMediator.onSystemReady这个里面干啥了
2、KeyguardViewMediator.onSystemReady
- //frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
- public class KeyguardViewMediator extends SystemUI {
- ...
- public void onSystemReady() {
- mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
- synchronized (this) {
- if (DEBUG) Log.d(TAG, "onSystemReady");
- mSystemReady = true;
- doKeyguardLocked(null);
- mUpdateMonitor.registerCallback(mUpdateCallback);
- }
- // Most services aren't available until the system reaches the ready state, so we
- // send it here when the device first boots.
- maybeSendUserPresentBroadcast();
- }
- ...
- }
在这个方法里主要调用了doKeyguardLocked和注册了KeyguardUpdateMonitorCallback
3、通过调用doKeyguardLocked显示锁屏界面
- //frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
- public class KeyguardViewMediator extends SystemUI {
- ...
- private void doKeyguardLocked(Bundle options) {
- // if another app is disabling us, don't show
- if (!mExternallyEnabled) {
- if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
- // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
- // for an occasional ugly flicker in this situation:
- // 1) receive a call with the screen on (no keyguard) or make a call
- // 2) screen times out
- // 3) user hits key to turn screen back on
- // instead, we reenable the keyguard when we know the screen is off and the call
- // ends (see the broadcast receiver below)
- // TODO: clean this up when we have better support at the window manager level
- // for apps that wish to be on top of the keyguard
- return;
- }
- // if the keyguard is already showing, don't bother
- if (mStatusBarKeyguardViewManager.isShowing()) {
- if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
- resetStateLocked();
- return;
- }
- // if the setup wizard hasn't run yet, don't show
- final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim", false);
- final boolean absent = SubscriptionManager.isValidSubscriptionId(
- mUpdateMonitor.getNextSubIdForState(IccCardConstants.State.ABSENT));
- final boolean disabled = SubscriptionManager.isValidSubscriptionId(
- mUpdateMonitor.getNextSubIdForState(IccCardConstants.State.PERM_DISABLED));
- final boolean lockedOrMissing = mUpdateMonitor.isSimPinSecure()
- || ((absent || disabled) && requireSim);
- if (!lockedOrMissing && shouldWaitForProvisioning()) {
- if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
- + " and the sim is not locked or missing");
- return;
- }
- if (mLockPatternUtils.isLockScreenDisabled(KeyguardUpdateMonitor.getCurrentUser())
- && !lockedOrMissing) {
- if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
- return;
- }
- if (mLockPatternUtils.checkVoldPassword(KeyguardUpdateMonitor.getCurrentUser())) {
- if (DEBUG) Log.d(TAG, "Not showing lock screen since just decrypted");
- // Without this, settings is not enabled until the lock screen first appears
- setShowingLocked(false);
- hideLocked();
- mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
- return;
- }
- if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
- showLocked(options);
- }
- ...
- }
这段代码主要是在是否要显示锁屏之前做了5个判断:1.如果启用第三方锁屏界面,不显示原生界面;2.锁屏界面已经显示了话,重新更新下状态;3.如果第一次开机引导界面setup wizard 还没有运行,也先不显示;4.屏幕没有亮不显示;5.当前正在解密界面不显示。如果这几个条件都不满足,则调用showLocked显示锁屏界面。在 showLocked通过mHandler发送Message,在handleMessage里“case SHOW:”时调用handleShow
4、在handleShow里设置一些锁屏状态和显示锁屏界面
- //frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
- public class KeyguardViewMediator extends SystemUI {
- ...
- private void handleShow(Bundle options) {
- 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");
- }
- setShowingLocked(true);
- mStatusBarKeyguardViewManager.show(options);
- mHiding = false;
- mWakeAndUnlocking = false;
- resetKeyguardDonePendingLocked();
- mHideAnimationRun = false;
- updateActivityLockScreenState();
- adjustStatusBarLocked();
- userActivity();
- mShowKeyguardWakeLock.release();
- }
- mKeyguardDisplayManager.show();
- }
- ...
- }
5、通过调用StatusBarKeyguardViewManager的show重置当前状态显示keyguard
- //frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
- public class StatusBarKeyguardViewManager {
- ...
- public void show(Bundle options) {
- mShowing = true;
- mStatusBarWindowManager.setKeyguardShowing(true);
- mScrimController.abortKeyguardFadingOut();
- reset();
- }
- ...
- }
在reset里调用本类的showBouncerOrKeyguard,在这个方法里通过KeyguardBouncer的实例mBouncer调用prepare(),在 prepare里调用了KeyguardHostView的showPrimarySecurityScreen
6、KeyguardSecurityContainer.showPrimarySecurityScreen
在KeyguardHostView的showPrimarySecurityScreen里调用KeyguardSecurityContainer的showPrimarySecurityScreen方法,如下
- //frameworks/base/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityContainer.java
- public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView {
- ...
- void showPrimarySecurityScreen(boolean turningOff) {
- SecurityMode securityMode = mSecurityModel.getSecurityMode();
- if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")");
- showSecurityScreen(securityMode);
- }
- ...
- }
在这个方法里调用了showSecurityScreen,根据mSecurityModel.getSecurityMode()获取的SecurityMode来显示不同界面,SecurityMode定义如下
- //frameworks/base/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityModel.java
- public class KeyguardSecurityModel {
- public enum SecurityMode {
- Invalid, // NULL state
- None, // No security enabled
- Pattern, // Unlock by drawing a pattern.
- Password, // Unlock by entering an alphanumeric password
- PIN, // Strictly numeric password
- SimPin, // Unlock by entering a sim pin.
- SimPuk // Unlock by entering a sim puk
- }
- ...
- }
showSecurityScreen方法如下:
- //frameworks/base/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityContainer.java
- public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView {
- ...
- private void showSecurityScreen(SecurityMode securityMode) {
- if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")");
- if (securityMode == mCurrentSecuritySelection) return;
- KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection);
- KeyguardSecurityView newView = getSecurityView(securityMode);//根据securityMode获取对应的view
- // Emulate Activity life cycle
- if (oldView != null) {
- oldView.onPause();
- oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view
- }
- if (securityMode != SecurityMode.None) {
- newView.onResume(KeyguardSecurityView.VIEW_REVEALED);
- newView.setKeyguardCallback(mCallback);
- }
- // Find and show this child.
- final int childCount = mSecurityViewFlipper.getChildCount();
- final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
- for (int i = 0; i < childCount; i++) {
- if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) {
- mSecurityViewFlipper.setDisplayedChild(i);
- break;
- }
- }
- mCurrentSecuritySelection = securityMode;
- mSecurityCallback.onSecurityModeChanged(securityMode,
- securityMode != SecurityMode.None && newView.needsInput());
- }
- ...
- }
到这里锁屏就启动完成了,这里简单总结一下:
1. 在KeyguardServiceDelegate里绑定KeyguardService,并调用onSystemReady方法。
2. KeyguardViewMediator里调用doKeyguardLocked来决定是否需要显示锁屏界面;如果显示则调用StatusBarKeyguardViewManager的show,最后调用到KeyguardHostView的showPrimarySecurityScreen()。
3. 在KeyguardSecurityContainer的showPrimarySecurityScreen利用mSecurityModel.getSecurityMode()获取当前的securityMode,传入showSecurityScreen来显示不同锁屏界面。
原文地址: http://blog.csdn.net/yin1031468524/article/details/56284449
Android 7.1.1 锁屏界面启动流程相关推荐
- Android 9.0 去除锁屏界面及SystemUI无sim卡拨打紧急电话控件显示功能实现
1.1概述 在9.0的系统rom定制化开发中,关于SystemUI的定制化功能也是比较多的,在SystemUI的锁屏页面和状态栏提示无sim卡拨打紧急电话控件显示等相关提示 的功能中,在有些syste ...
- android开发笔记之锁屏界面未读短信未接来电提醒(android 4.4)
客户需求 最近在做一个项目,android 4.4系统,客户要求在锁屏界面有未读短信未接来电的提醒功能.而平台没有此功能,要自己实现.并且时间非常紧,---.(其实软件工程师基本上都是这样,坑,坑,坑 ...
- Android 10.0去除锁屏界面及SystemUI无sim卡拨打紧急电话控件显示
在开发定制化wifi版平板时,需要去掉所有紧急拨打电话的功能,而紧急拨打电话在锁屏界面 和SystemUI 的下拉快捷里面有这些功能 所以就从这两个地方入手,屏蔽到紧急拨打电话功能 1.SystemU ...
- Android 9.0 SystemUI 锁屏界面禁止状态栏下拉
目录 1.概述 2.SystemUI 锁屏界面禁止状态栏下拉的核心类 3.SystemUI 锁屏界面禁止状
- android锁屏时间大小,Android 4.4.4 锁屏界面时间大小修改
源码所在目录: frameworks\base\packages\Keyguard\res\values\dimens.xml 80dp 其中的80dp即代表时间字体的大小,修改这个值,即可改变字体的 ...
- android锁屏界面快捷键,Funtouch新特性 锁屏快捷键可以自定义
凤凰数码讯 5月1日 vivo的Funtouch OS系统日前曝出新特性,用户可以自定义锁屏快捷键,大大提升了锁屏界面的易用性. 中关村在线消息,由于设计风格华丽.UI特色鲜明,vivo基于Andro ...
- 《SystemUI》修改SystemUI锁屏界面时间格式
要求:修改SystemUI锁屏界面时间格式 Android P 上Sysyemui锁屏界面上的日期显示不在DateView处理了,使用KeyguardSliceProvider来处理,继承Conten ...
- android 锁屏界面处理
锁屏相关的文件在android对应package:com.android.internal.policy.impl. phoneWindowManager.java可以派发各种物理key,如Power ...
- android 游戏 锁屏界面开发,android 锁屏程序开发
参考http://blog.csdn.net/wdaming1986/article/details/8837023 锁屏程序的步骤如下: 1.替换系统锁屏 2.屏蔽Home键,back键.menu键 ...
最新文章
- 自动驾驶真的会来得那么快吗:关于自动驾驶的7个疑问
- 浅析网站流量出现异常情况应怎样解决?
- python运用实例视频_python爬视频实例
- Day 4 - PB级规模数据的Elasticsearch分库分表实践
- matlab都有什么接口,介绍MATLAB与C++的几种接口方式
- 转:【Java集合源码剖析】LinkedHashmap源码剖析
- Linux学习笔记5
- jsoup爬虫简书首页数据做个小Demo
- java 里面怎么截取倒数第几个字_Java反射是什么
- 超越Hadoop的大数据分析之图形处理尺寸
- node.js 与 nvm安装教程
- 中国大学生在线官方微博入围全国十大中央机构微博、全国十大教育微博
- 转载:分配器、切换器、同屏器、分屏器 区别
- 最新的ESD(ElectroStatic Discharge)静电介绍及其标准下载(见文尾)
- 莫队算法完整总结(普通莫队、带修莫队、树上莫队、回滚莫队)
- 某网站登录接口password参数还原
- 查看pcie总线上的设备ID以及带宽计算
- Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8
- html网页页面制作用到了什么技术,技术干货|常用的HTML5网页制作软件,这些你有在用吗?...
- 12月份参加工作的年假怎么休_请问我的年假可以在12月份休吗?单位说可以12月1日就不用上班了,我可以上到12月28日吗?...
热门文章
- Uber新功能:隐藏司机乘客们的手机号码
- SharePoint 2013 Error - TypeError: Unable to get property 'replace' of undefined or null reference
- 《JavaScript语言精髓与编程实践》读书笔记二
- Unity3d webplayer发布的问题和100%自适应浏览器
- Python学习笔记:错误和异常
- Git bash:初步入门(1)
- Matlab 基于svm的图像物体分类
- 一个操作系统的实现(1):分析linux下如何运行一个执行文件
- 科大星云诗社动态20210905
- 荣耀X8碎屏2020-05-14