This article will use the way of code walking, combined with a simple flow chart, and we look at the lock screen module code on Android9.0, other Android versions should be similar, but this document is based on the code on Android9.0.

Before understanding a module, we often have questions to understand, before this, I also have the following questions:

1) Which module is the lock screen interface code included in?

2) What is the display process of the screen lock interface when the screen is on or off?

3) Slide up the lock screen to display the login and password interface process;

4) Why is the lock screen not displayed when you press the Power button to start the machine?

5) I want to modify the contents of the lock screen. I should pay attention to which classes and layouts can be quickly modified.

6) Some problems encountered and analyzed and solved;

It is always good to have questions. This article will explain the above questions step by step.

Writing in the front

On the Android operating system, you can press the Power button to enter the off screen and on screen. If the screen lock mode is set in the Settings and there is a password, when the screen lights up, we will see the lock screen interface as shown in the picture below. Then the lock screen interface slides up, which is the password verification interface. Only after entering the correct password can you enter the system interface.

​ 

First, which module is the lock screen interface code contained in

A: In the SystemUI module.

Ii. What is the display process of lock screen

The lock screen is handled in SystemUI. The following figure is a simple flow chart of lock screen related logic after system startup. Let’s have a general understanding first, at least know which important classes are involved.

KeyguardViewMediator.java

StatusBarKeyguardViewManager.java

StatusBarWindowManager.java

StatusBar.java

KeyguardBouncer.java

KeyguardHostView.java

KeyguardSecurityContainer.java

We have seen a very important class KeyguardViewMediator. Java, yes, we can see the code for this class first.

Before we look at the code, let’s take a look at the first comments of this class, let’s translate the important ones to facilitate the overall understanding (my English level is limited, we can directly see the English understanding).

/** * Mediates requests related to the keyguard. This includes queries about the * state of the keyguard, power management events that effect whether the keyguard * should be shown or reset, callbacks to the phone window manager to notify * it of when the keyguard is showing, And events from the keyguard view itself * that the keyguard was succesfully unlocked. * * //Mediates (mediate) Handle screen lock related requests. These include querying the status of the lock screen, deciding whether to display or reset the lock screen according to Power management events, using a callback to call back the current status of the lock screen to phone Window Manager, and whether the lock screen is successfully unlocked from the view of the lock screen itself. * * * Note that the keyguard view is shown when the screen is off (as appropriate) * so that once the screen comes on, * * // Note that the lock screen is displayed when the screen is off (depending on the situation), so that the lock screen can be quickly displayed when the screen lights up. * * Example external events that translate to keyguard view changes: * - screen turned off -> reset the keyguard, and show it so it will be ready * next time the screen turns on * - keyboard is slid open -> if the keyguard is not Secure, hide it * * // Examples of lock screen changes caused by external events: * Off screen -> Lock screen resets and then displays so that the lock screen is ready the next time it lights up. * * Events from the keyguard view: * -user succesfully unlocked keyGuard -> hide keyguard view, and no longer * restrict input events. * The user successfully unlocked the lock screen -> hide the lock screen view and no longer limit the input events * * Note: in addition to normal power managment events that effect the state of * whether the keyguard should be showing, external apps and services may request * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When * False, this will override all other conditions for turning on the keyguard. External apps or services may use the setKeyguardEnabled(Boolean) interface to set whether or not a lock screen should be displayed. * To standardize event calls from Power Management on whether or not a lock screen should be displayed, When setKeyguardEnabled sets the value of * to false, this will override the other conditions for unlocking the lock screen /Copy the code

According to the description in the above notes, we can go through the process of button off screen and on screen: screen turned off -> reset the keyguard, and show it so it will be ready next time the screen turns on

KeyguardViewMediator. Java -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- light screen / * * * * * / public void onScreenTurnedOn () { Trace.beginSection("KeyguardViewMediator#onScreenTurnedOn"); notifyScreenTurnedOn(); mUpdateMonitor.dispatchScreenTurnedOn(); Trace.endSection(); } /** */ public void onScreenTurnedOff() {notifyScreenTurnedOff(); mUpdateMonitor.dispatchScreenTurnedOff(); }... private void handleNotifyScreenTurnedOff() { synchronized (this) { if (DEBUG) Log.d(TAG, "handleNotifyScreenTurnedOff");  mStatusBarKeyguardViewManager.onScreenTurnedOff(); mDrawnCallback = null; }}Copy the code

3. Why is the lock screen not displayed after long pressing the Power button to start the machine

If it doesn’t show up, then it’s just the way Android is designed. Haha, developer, my favorite phrase is “that’s the way it’s designed”, and then I throw up my hands and I can’t help it. Is that the design? Let’s look at the code to make sure.

We’ll look at KeyguardViewMediator. Specific implementation in Java:

1) onSystemReady() is used when the system is up, and then the call is sent to the handler for processing. The actual processing is in handleSystemReady().

HandleSystemReady (), doKeyguardLocked(), doKeyguradLocked (), doKeyguardLocked(), doKeyguradLocked () If yes, showLocked(options) is used to display the lock screen.

/** * Public void onSystemReady() {log.v (TAG,"-- "); /** public void onSystemReady() {log.v (TAG,"-- ") onSystemReady();" ); mHandler.obtainMessage(SYSTEM_READY).sendToTarget(); } private Handler mHandler = new Handler(Looper.myLooper(), null, true) { @Override public void handleMessage(Message msg) { switch (msg.what) { ...... case SYSTEM_READY: handleSystemReady(); break; . }}}; private void handleSystemReady() { 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(); }Copy the code

3) doKeyguardLocked () inside a mLockPatternUtils. IsLockScreenDisabled (KeyguardUpdateMonitor. GetCurrentUser ()), we put the the value of the print, You will find that when booting up, the printed value is false, and the lockedOrMissing and forceShow values are false by default, so it will directly return without continuing the following process.

  1. Now we know that the lockScreen is not ready, so we did not go through the process of displaying the lockScreen. (The specific reasons and judgment conditions are not prepared here, AND I have not continued tracking. I have tried to force display here, but there is no problem. There is a parameter forceShow to force display, which indicates that it is ok to force display at this time.)
private void doKeyguardLocked(Bundle options) { ...... / / -- -- -- -- -- - 1 if analysis (mLockPatternUtils. IsLockScreenDisabled (KeyguardUpdateMonitor. GetCurrentUser ()) &&! lockedOrMissing && ! forceShow) { if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off"); return; }... } if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen"); showLocked(options); }Copy the code

Fifth, I want to modify the lock screen interface content, should pay attention to which classes and layout can be quickly modified

1) Login password verification interface

I, here involves two important class KeyguardAbsKeyInputView. Java and KeyguardPasswordView. Java, password authorization verification in KeyguardAbsKeyInputView. Java classes, Interface of the display is in KeyguardPasswordView in Java for processing. We look down the corresponding code KeyguardPasswordView. The inside of the Java onEditorAction (), the processing of a verifyPasswordAndUnlock () method, Is to jump to KeyguardAbsKeyInputView. In Java code is verified.

If we want to modify the password authentication logic, you can modify KeyguardAbsKeyInputView. In Java verifyPasswordAndUnlock () the content inside.

Ii. Password validation is the corresponding layout file keyguard_password_view.xml. If we want to add content to the layout file, we can add it to this layout file.

--------KeyguardPasswordView.java -------- @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // Check if this was the result of hitting the enter key final boolean isSoftImeEvent = event == null && (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT); final boolean isKeyboardEnterKey = event ! = null && KeyEvent.isConfirmKey(event.getKeyCode()) && event.getAction() == KeyEvent.ACTION_DOWN; if (isSoftImeEvent || isKeyboardEnterKey) { verifyPasswordAndUnlock(); return true; } return false; }Copy the code

6. Problems encountered and solutions

I. Click the password login input box, sometimes the input method is not normally vacated

Found this problem and in KeyguardViewMediator. In Java call mStatusBarKeyguardViewManager. SetNeedsInput (needsInput); Have a relationship. To properly vacate the input method, first make sure setNeedInput is set to true. Behind me is modified, in KeyguardViewMediator. In Java, onSystemReady () method, according to their own needs, to set the value of the next setNeedInput again.

There is a place to revise, KeyguardPasswordView. In Java, resetState (), as shown in the figure below, blocked some code. The shielded code automatically calls the input method. Every time you enter the password login interface, you will go through the process of resetState (). However, it is strange that you do not call it normally when you take the initiative to call it once.

@Override protected void resetState() { mSecurityMessageDisplay.setMessage(""); final boolean wasDisabled = mUserNameEntry.isEnabled(); setPasswordEntryEnabled(true); setPasswordEntryInputEnabled(true); setUserNameEntryEnabled(true); setUserNameEntryInputEnabled(true); // if (wasDisabled) {// mimm.showsoftinput (mUserNameEntry, inputMethodManager.show_implicit); / /}}Copy the code

I have been engaged in Android Camera related development for 5 years

Now I work in Shenzhen

Welcome to follow my wechat official account “Xiaochi Notes”.

We learn and communicate together