第七篇 窗口系统机制之KEYGUARD机制

        ANDROID的KEYGUARD相关类提供了屏幕锁屏等相关功能,虽然功能简单,但是相关的逻辑还是挺复杂的,要处理屏幕处于不同状态的情况,要监控各种事件(时间,电池,Sim状态,电话状态),因此GOOGLE还是对屏幕功能进行了细致的设计,架构上采用了中介模式、MVC模式等设计模式。系统类图如下

       第七篇 窗口系统机制之KEYGUARD机制_第1张图片

      PhoneWindowManager、PowerManager、KeyguardViewMediator、KeyguardUpdateMonitor、KeyguardViewManager、KeyguardViewHost、LockPatternKeyguardView、KeyguardScreen相关类之间构成中介模式,KeyguardViewMediator对象处于模式的核心,起到中介者的角色,其它对象都通过KeyguardViewMediator对象相互交互。

      KeyguardViewMediator对象是PhoneWindowManager成员,因此PhoneWindowManage可以直接访问KeyguardViewMediator对象。KeyguardViewMediator也通过PowerManager服务创建了三个不计数WakeLock锁实现与PowerManager服务的通讯,达到锁屏操作时电源状态管理的目的(mWakeLock锁用于保证在 keyguard显示时能够显示一段时间后屏幕才能睡眠;mShowKeyguardWakeLock用于在 keyguard显示要打开时能够保持设备唤醒;mWakeAndHandOff由PhoneWindowManager截获事件进行触发,用于在用户按键或鼠标移动时唤醒设备便于对keyguard进行操作)。KeyguardUpdateMonitor、KeyguardViewManager对象作为KeyguardViewMediator对象的成员又都是由KeyguardViewMediator实例化的,KeyguardViewMediator可以直接操作KeyguardUpdateMonitor、KeyguardViewManager对象。

      而KeyguardViewHost、LockPatternKeyguardView作为视图对象通过调用KeyguardViewManager对象的show函数创建并作为视图添加到窗口上, KeyguardViewHost作为窗口得主视图, LockPatternKeyguardView是KeyguardViewHost的子视图。KeyguardScreen类是各类锁屏、解锁屏幕的接口,各类锁屏、解锁屏幕都是KeyguardScreen类的视图实现类,在调用LockPatternKeyguardView的updateScreen函数时根据不同的模式作为LockPatternKeyguardView的子视图添加到LockPatternKeyguardView视图上显示。

      KeyguardUpdateMonitor、KeyguardViewHost、LockPatternKeyguardView、KeyguardScreen接口的对象都通过回调与KeyguardViewMediator反向调用。

      KeyguardUpdateMonitor 类实现时间,电池,Sim状态,电话状态的监视,KeyguardViewMediator类是KeyguardUpdateMonitor类内部回调接口InfoCallback、SimStateCallbac的实现类,因此KeyguardViewMediator对象可以作为KeyguardUpdateMonitor对象的回调对象,通过KeyguardUpdateMonitor的registerInfoCallback和registerSimStateCallback函数把KeyguardViewMediator对象添加到KeyguardUpdateMonitor对象的回调对象列表中,当KeyguardViewMediator对象检测到时间,电池,Sim状态,电话状态变化时通过回调调用KeyguardViewMediator对象的相关接口。

      KeyguardViewMediator类也是KeyguardViewCallback接口的实现,因此在KeyguardViewManager实例化时KeyguardViewMediator对象也作为KeyguardViewCallback类型的参数传进KeyguardViewManager作为KeyguardViewManager的内部成员mCallback。然后mCallback又作为参数在KeyguardViewHost对象实例化时传进去作为KeyguardViewHost对象的成员,并通过LockPatternKeyguardView的函数setCallback设置LockPatternKeyguardView对象的回调对象,因此KeyguardViewHost、LockPatternKeyguardView都可以通过KeyguardViewCallback类型的回调对象与KeyguardViewMediator对象通讯。

      LockPatternKeyguardView对象创建了六个实现了KeyguardScreen接口的各个锁屏、解锁屏幕对象,LockScreen用于显示屏幕加锁状态,另外根据不同的unlockMode模式创建了不同的解锁屏幕:PatternUnlockScreen实现图案解锁模式,SimPukUnlockScreen屏幕实现SIM PUK码解锁模式,SimUnlockScreen实现 Sim PIN码解锁模式,AccountUnlockScreen实现 GOOGLE帐户解锁,PasswordUnlockScreen实现口令解锁模式。

      每个锁屏、解锁屏幕对象在创建时都传进去一个实现了KeyguardScreenCallback接口的回调对象mKeyguardScreenCallback(LockPatternKeyguardView的一个成员),KeyguardScreenCallback接口派生自KeyguardViewCallback接口。在mKeyguardScreenCallback回调对象中实现了KeyguardViewCallback类型的回调对象mCallback的包装(装饰模式的采用),因此各个锁屏、解锁屏幕对象可以通过KeyguardScreenCallback接口与KeyguardViewMediator对象通讯。

      另外KeyguardUpdateMonitor、KeyguardViewManager、LockPatternKeyguardView、KeyguardScreen类还构成了MVC模式。KeyguardUpdateMonitor实现了MVC模式中的模式角色,用来提供模式状态数据。KeyguardViewManager担当MVC模式中的控制器角色,通过KeyguardViewManager生成视图,生成和控制视图的表现。LockPatternKeyguardView、KeyguardScreen接口类对象担当视图角色,模式可以直接向视图传送状态(通过把LockPatternKeyguardView和锁屏、解锁屏幕的内部对象作为回调对象登记到KeyguardUpdateMonitor对象的回调对象列表中),视图也可以直接读取KeyguardUpdateMonitor模式的状态数据,因此LockPatternKeyguardView、KeyguardScree类对象视图中都有一个KeyguardUpdateMonitor对象的引用。

      LockPatternKeyguardView对象由LockPatternKeyguardViewProperties对象采用了工厂方法进行实例化。

      各个对象的实例化代码片断:

         PhoneWindowManager对象WindowManagerService.java中实例化,并在WindowManagerService的PolicyThread线程运行时初始化。

 


   final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();

        public PolicyThread(WindowManagerPolicy policy,

                WindowManagerService service, Context context,

                PowerManagerService pm) {

            super("WindowManagerPolicy");

            mPolicy = policy;

            mService = service;

            mContext = context;

            mPM = pm;

        }

        public void run() {

            Looper.prepare();

            mPolicy.init(mContext, mService, mService, mPM);

 

            synchronized (this) {

    

            }

            Looper.loop();

        }

    }

        final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager(); public PolicyThread(WindowManagerPolicy policy, WindowManagerService service, Context context, PowerManagerService pm) { super("WindowManagerPolicy"); mPolicy = policy; mService = service; mContext = context; mPM = pm; } public void run() { Looper.prepare(); mPolicy.init(mContext, mService, mService, mPM); synchronized (this) { } Looper.loop(); } }

         在PhoneWindowManager对象init函数调用时实例化KeyguardMediator对象

  

public void init(Context context, IWindowManager windowManager,

            WindowManagerFuncs windowManagerFuncs,

            LocalPowerManager powerManager) {    

 …

   mKeyguardMediator = new KeyguardViewMediator(context, this, powerManager);

}

       KeyguardViewMediator构造函数中实例化PowerManager的三个锁及KeyguardUpdateMonitor、LockPatternKeyguardViewProperties、KeyguardViewManager对象。

    

 public KeyguardViewMediator(Context context, PhoneWindowManager callback,

            LocalPowerManager powerManager) {

        mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        mWakeLock = mPM.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "keyguard");

        mWakeLock.setReferenceCounted(false);

        mShowKeyguardWakeLock=mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");

        mShowKeyguardWakeLock.setReferenceCounted(false);

 

        mWakeAndHandOff = mPM.newWakeLock(

                PowerManager.PARTIAL_WAKE_LOCK,

                "keyguardWakeAndHandOff");

        mWakeAndHandOff.setReferenceCounted(false);

 

        mUpdateMonitor = new KeyguardUpdateMonitor(context);

        mUpdateMonitor.registerInfoCallback(this);

        mUpdateMonitor.registerSimStateCallback(this);、

 

        mKeyguardViewProperties=new LockPatternKeyguardViewProperties(mLockPatternUtils, mUpdateMonitor);

 

        mKeyguardViewManager = new KeyguardViewManager(

                context, WindowManagerImpl.getDefault(), this,

                mKeyguardViewProperties, mUpdateMonitor);

 

    }

      在KeyguardViewManagershow中实例化KeyguardViewHostLockPatternKeyguardView对象。

     

 public synchronized void show() {

           if (mKeyguardHost == null) {

                mKeyguardHost = new KeyguardViewHost(mContext, mCallback);

                WindowManager.LayoutParams lp = new WindowManager.LayoutParams(

                    stretch, stretch, WindowManager.LayoutParams.TYPE_KEYGUARD,

                    flags, PixelFormat.TRANSLUCENT);

  

                mViewManager.addView(mKeyguardHost, lp);

        }

 

        if (mKeyguardView == null) {

            mKeyguardView = mKeyguardViewProperties.createKeyguardView(mContext, mUpdateMonitor, this);

            mKeyguardView.setCallback(mCallback);

 

            final ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams(

                    ViewGroup.LayoutParams.MATCH_PARENT,

                    ViewGroup.LayoutParams.MATCH_PARENT);

 

            mKeyguardHost.addView(mKeyguardView, lp);

    }

   在LockPatternKeyguardViewcreateLockScreencreateUnlockScreenFor函数中创建加锁和解锁屏幕。

       

View createLockScreen() {

        View lockView = new LockScreen(

                mContext,

                mConfiguration,

                mLockPatternUtils,

                mUpdateMonitor,

                mKeyguardScreenCallback);

        return lockView;

    }

 

    View createUnlockScreenFor(UnlockMode unlockMode) {

        if (unlockMode == UnlockMode.Pattern) {

            PatternUnlockScreen view = new PatternUnlockScreen(

                    mContext,

                    mConfiguration,

                    mLockPatternUtils,

                    mUpdateMonitor,

                    mKeyguardScreenCallback,

                    mUpdateMonitor.getFailedAttempts());

        } else if (unlockMode == UnlockMode.SimPuk) {

            unlockView = new SimPukUnlockScreen(

                    mContext,

                    mConfiguration,

                    mUpdateMonitor,

                    mKeyguardScreenCallback,

                    mLockPatternUtils);

        } else if (unlockMode == UnlockMode.SimPin) {

            unlockView = new SimUnlockScreen(

                    mContext,

                    mConfiguration,

                    mUpdateMonitor,

                    mKeyguardScreenCallback,

                    mLockPatternUtils);

        } else if (unlockMode == UnlockMode.Account) {

   

                unlockView = new AccountUnlockScreen(

                        mContext,

                        mConfiguration,

                        mUpdateMonitor,

                        mKeyguardScreenCallback,

                        mLockPatternUtils);

           

        } else if (unlockMode == UnlockMode.Password) {

            unlockView = new PasswordUnlockScreen(

                    mContext,

                    mConfiguration,

                    mLockPatternUtils,

                    mUpdateMonitor,

                    mKeyguardScreenCallback);

        }

        return unlockView;

    }

   

 上一篇                                   版权所有,转载时请尊重原创显要处注明链接,谢谢!

下一篇

你可能感兴趣的:(MVC模式,Keyguard,中介模式,屏幕锁屏)