最近大体看了一下android源码锁屏模块,顺便把自己的收获在此记录下来,希望对研究锁屏的同行们有所帮助(对于锁屏模块,本人也没什么时间去真正的深究,只是摸清了个大概,若有奇异和错误之处,恳请指出)
好了,废话不多说了。
Android源码模块锁屏大体分为两种:
1.LockScreen: 系统默认的锁屏,就是我们所常见的系统原生波纹解锁(涉及MultiWaveView视图类)。如下图:
2.UnlockScreen: 进入手机的设置----->安全----->屏幕锁定。在列表中将看到的可选择项:图案,PIN,密码等锁屏都归为UnlockScreen。(可选择任意一项切换锁屏)
锁屏相关源码所在路径:
1.锁屏模块的框架源码所在路径为:frameworks\base\policy\src\com\android\internal\policy\impl(本文所涉及的代码都在这个目录里)
2.相关的锁屏自定义View类及与其关联类的源码所在路径为:frameworks\base\core\java\com\android\internal\widget
开机绘制锁屏流程代码分析:
手机开机时,在SystemServer类的init2()方法中会启动线程类ServerThread的run方法如下:
classServerThreadextendsThread
{
@Override
publicvoidrun()
{
WindowManagerService wm = null;
...
try
{
wm.systemReady();
} catch(Throwable e)
{
reportWtf("making Window Manager Service ready", e);
}
...
}
}
------>上述代码中的wm为WindowManagerService的引用,所以,wm.systemReady()为调用WindowManagerService的systemReady()方法,如下代码:
publicclassWindowManagerServiceextendsIWindowManager.StubimplementsWatchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs
{
finalWindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
...
publicvoidsystemReady() {
mPolicy.systemReady();
}
...
}
------>WindowManagerPolicy的实现类为PhoneWindowManager,所以,接着调用到PhoneWindowManager的systemReady,如下:
publicclassPhoneWindowManagerimplementsWindowManagerPolicy
{
KeyguardViewMediator mKeyguardMediator;
...
//手机开机后执行
publicvoidsystemReady() {
// tell the keyguard
mKeyguardMediator.onSystemReady(); //进行待机锁屏及解锁逻辑
android.os.SystemProperties.set("dev.bootcomplete","1");
synchronized(mLock) {
updateOrientationListenerLp();
mSystemReady = true;
mHandler.post(newRunnable() {
publicvoidrun() {
updateSettings();
}
});
}
}
...
}
------>接着,调用到KeyguardViewMediator类的onSystemReady()方法如下:
publicclassKeyguardViewMediatorimplementsKeyguardViewCallback,
KeyguardUpdateMonitor.InfoCallback, KeyguardUpdateMonitor.SimStateCallback
{
...
/**
* Let us know that the system is ready after startup.
*/
//开机显示锁屏入口
publicvoidonSystemReady() {
synchronized(this) {
if(DEBUG) Log.d(TAG,"onSystemReady");
mSystemReady = true;
doKeyguardLocked();
}
}
...
}
------>调用KeyguardViewMediator.doKeyguardLocked方法,在该方法中,先执行一些条件判断,若满足直接返回。若不直接返回,则紧接着调用KeyguardViewMediator. showLocked方法,代码如下:
...
/**
* Send message to keyguard telling it to show itself
* @see #handleShow()
*/
privatevoidshowLocked() {
if(DEBUG) Log.d(TAG,"showLocked");
// ensure we stay awake until we are finished displaying the keyguard
mShowKeyguardWakeLock.acquire(); //确保屏幕处于唤醒状态
Message msg = mHandler.obtainMessage(SHOW);
mHandler.sendMessage(msg);
}
...
----->通过handler发送消息SHOW到handleMessage处理,如下:
...
*
* This handler will be associated with the policy thread, which will also
* be the UI thread of the keyguard. Since the apis of the policy, and therefore
* thisclass, can be called by other threads, any action that directly
* interacts with the keyguard ui should be posted to thishandler, rather
* than called directly.
*/
//Handler对象 , 异步处理
privateHandler mHandler =newHandler() {
@Override
publicvoidhandleMessage(Message msg) {//异步处理
switch(msg.what) {
caseTIMEOUT:
handleTimeout(msg.arg1);
return;
caseSHOW:
handleShow();
return;
caseHIDE:
handleHide();
return;
caseRESET:
handleReset();
return;
caseVERIFY_UNLOCK:
handleVerifyUnlock();
return;
caseNOTIFY_SCREEN_OFF:
handleNotifyScreenOff();
return;
caseNOTIFY_SCREEN_ON:
handleNotifyScreenOn((KeyguardViewManager.ShowListener)msg.obj);
return;
case