Detailed explanation of keyguard unlocking screen mechanism developed by Android 4.0

This article describes the unlocking screen mechanism of Android 4.0 keyguard. Share with you for your reference, as follows:

The keyguard unlocking screen is an essential module in the Android system. After the user starts up or clicks the power button, the screen that the user first sees is the interface corresponding to the unlocking screen module. The function of keyguard module is relatively simple:

First: intuitively display the current key information of the mobile phone: such as battery information, operator information, date information, call SMS information, etc.

Second: enhance the security performance of the mobile phone: for security, users can set different security policies such as password, pattern and account in the secure option in setting to prevent illegal users from accessing the mobile phone system.

However, from the perspective of code implementation, the logic of the module is still complex. It not only needs to monitor a variety of events in the system, such as sim, telephone, battery and carrier, but also correctly reflect and display the different states of the screen. In order to have a clearer understanding of the processing logic of the keyguard module, the framework of the keyguard unlocking screen module is briefly introduced from the perspective of system composition, and then the important processing logic in the unlocking screen module is introduced in detail.

@H_ 403_ 11 @ I. system introduction

The framework class diagram of keyguard unlocking screen module is as follows:

The figure only lists the important classes involved in the keyguard unlocking screen module. These classes realize the main functions of the keyguard module. Their functions are as follows:

Phonewindowmanager is the interface for external interaction of unlocking screen module. External modules such as window management service and power management service access the internal functions of keyguard through phonewindowmanager. The keyguardviewmediator class is the mediator of the unlocking screen module. It handles the change of keyguard status as an intermediary, handles requests such as event, power management and phonewindowmanager notification, and serves as a callback object for other classes of the unlocking screen module. The keyguard updatemonitor class is the listener of the unlocking screen module. It is responsible for monitoring the changes of time, SIM card, operator information, battery information, telephone information and other status, and notifying the keyguard view module to update the display. The keyguardviewmanager class is the manager of the unlock screen view module, which manages the creation, display, hiding and reset of the unlock screen interface. The lockpatternkeyguardview class is the view interface of the unlock screen module and the host view of all unlock screen interfaces. Different unlocking screen interfaces are displayed according to the set security policy. Six unlocking screen interfaces are implemented in Google native code:

1) Lockscreen: used to display the screen locking status 2) patternunlockscreen: realize the pattern unlocking mode 3) simpukunlockscreen: realize the SIM PUK code unlocking mode on the screen 4) simunlockscreen: realize the SIM pin code unlocking mode 5) accountunlockscreen: realize the Google account unlocking 6) passwordunlockscreen: realize the user-defined password unlocking mode

@H_ 403_ 11 @ II. Main logic

1. Keyguard module startup and display logic

That is, the process from starting the mobile phone to entering the system to the lock screen interface display. During the startup of the mobile phone system, the keyguard unlocking screen module will be automatically started. The creation of this module begins with the windowmanagerservice class. The sequence diagram is as follows:

1) When windowmanagerservice starts, it instantiates the phonewindowmanager object mpolicy and initializes it in the window management policy thread policythread. The code is as follows:

From the code, we can see that phonewindowmanager processes message events in independent threads and looper message queue. The looper object is also used by the unlocking screen module to process all handler messages. 2) The mpolicy function init creates the mediator of the unlocking screen module - the keyguardviewmediator object. 3) Create lockpatternkeyguardviewproperties, keyguardupdatemonitor, keyguardviewmanager and other important objects in the constructor of keyguardviewmediator:

The keyguardviewmediator records objects such as phonewindowmanager and PowerManager, as well as important objects in modules such as lockpatternkeyguardviewproperties, keyguardypdatemonitor and keyguardviewmanager. In this way, this class plays an important role in the interaction of the keyguard module with diplomatic interaction and internal objects as an intermediary.

4) Create mhandler in the keyguardoupdatemonitor constructor to respond to the change of the state of each event listening to this class, and notify the listening objects saved by mininfocallbacks and msimstatecallbacks in the handle processing function. The listening events have action_ TIME_ TICK、ACTION_ TIME_ CHANGED、ACTION_ BATTERY_ CHANGED、ACTION_ TIMEZONE_ CHANGED、ACTION_ SIM_ STATE_ CHANGED、 ACTION_ PHONE_ STATE_ CHANGED、RINGER_ MODE_ CHANGED_ ACTION

So far, important class objects in the keyguard unlocking screen module have been instantiated, but the creation and display of the unlocking screen view interface have not been involved.

5) After the system is started, the first display of the unlock screen interface starts with the systemready function of windowmanagerservice to notify phonewindowmanager that the system is ready. The code is as follows:

6) The systemready function of phonewindowmanager notifies the mediator of the unlocking screen module that the keyguardviewmediator object system is ready

7) Handling system readiness in the mediator keyguardviewmediator class: call the dokeyguardlocked function to display the unlock screen interface:

Send a show message in the showlocked function to asynchronously process the request to unlock the display of the screen interface.

8) handle the message request of interface display in handleShow, and call the KeyguardViewManager function show in the function to realize the real display of the unlocking screen interface.

This function mainly creates two important objects in the keyguard display view: mkeyguardhost and mkeyguardview, which inherit from FrameLayout and are the root view of the unlock screen view.

9) When creating the object mkeyguardview, create the unlocking screen interface according to the unlocking screen mode:

10) Create a specific lock or unlock view interface in the function createlockscreen or createunlockscreenfor, and call the show function to display it

So far, the processing logic of keyguard unlocking screen module from system startup to interface display has been introduced.

2. Press the power button twice, and the screen is on - > dark - > on. During the process, the screen locking module processes logic

Press the power button twice in succession, and the sequence diagram of processing logic of unlocking screen module in the process of screen on - > dark - > on is as follows:

1) In the function powermanagerservice: setpowerstate, respond to the pressing of the power button. The code is as follows:

According to the above code logic, the function screenofffinishedanimatinglocked is called when the screen is about to darken, and the function sendnotificationlocked is called when the screen is about to lighten.

2) The function sendnotificationlocked sends the notification task thread to the handler and asynchronously executes the notification unlocking screen module to update the status:

The above thread function run deals with the screen darkening and brightening respectively. When the power button is pressed, the function screenturnedoff is called when the screen darkens. Why is the reason for the darkening, and the value here is off_ BECAUSE_ OF_ USER。

3) In keyguardviewmediator, screen dimming events are handled according to the causes of screen dimming:

4) Call dokeyguardlocked to re display the lock screen interface. The display logic of the subsequent lock screen interface is the same as step 8 ~ 10 in the startup display of keyguard module, which will not be repeated.

5) Press the power button, and the screen will be re executed by the code processing logic from dark - > light. In the second step, the function called when the screen is light is phonewindowmanager: screenturningon.

6) function onScreenTurnedOn in the function screenTurningOn calling the intermediary KeyguardViewMediator, which directly calls the screen to brighten the asynchronous notification function KeyguardViewMediator:notifyScreenOnLocked, and tells the unlocking screen module that the screen will soon become brighter.

7) The function handlenotifyscreenon responds to a notification that the screen is lit

8) The program executes the lockpatternkeyguardview: onscreen turnedon function, and calls the show function to display the unlock screen interface. The code is as follows:

At this point, the logical processing is completed.

3. Custom password unlocking logic

The custom password unlocking starts with passwordunlockscreen. The sequence diagram is as follows:

1) Enter the password in the unlocking screen interface and click OK to respond in the function oneditoraction:

2) Judge the entered password in the function verifypasswordandunlock. If the input is correct, call keyguarddone to respond to the unlocking operation. The call to mcallback.keyguarddone (true) is a function that must be called after all unlocking screen mode situations are unlocked successfully. The subsequent processing logic is the same for different unlocking screen interfaces.

3) Call back the keyguardscreen callback and keyguardviewmediator functions keyguarddone. In the keyguarddone function of the latter, send the keydone event asynchronously:

4) The function keyguardviewmediator: handlekeyguarddone asynchronously handles the keyguarddone event and calls handlehide to hide the lock screen interface.

5) in the KeyguardViewManager.hide function, the destruction letter LockPatternKeyguardView:cleanUp number is called to hide the destruction interface.

At this point, the unlocking is completed.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android operation skills of SQLite database, summary of Android operation skills of JSON format data, summary of Android resource operation skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>