Android lock screen mechanism Principle Analysis

Source: Internet
Author: User

The last few days before the Spring Festival, there is almost no work to do. Summarize the documents, PPT files, and manuals written in the previous work. since it was completely transferred to the android + platform last year, the Android2.3 platform no longer has any projects. Using these days, we will sort out documents related to the android platform, which is a summary of the android platform. Try to publish them here for your participation. These documents are written on the network by reference, and some are sorted out from the source code. However, each part is also analyzed and verified step by step through source code analysis.

Android lock screen mechanism Principle Analysis



Main content of this article:

1. analyze the composition of the screen lock Interface

2. Source Code customization lockscreen

3. screen lock layout INTERFACE ANALYSIS

4. screen lock implementation

This article analyzes the version of Android2.3.

The file path consists of three parts:

Lock screen framework: frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \

Custom View: frameworks \ base \ core \ java \ com \ android \ internal \ widget \

Resource: frameworks \ base \ core \ res \

I. Composition of the screen lock Interface 1.1 Overview

Generally, there is only one common interface on the Android phone. After successful, the interface can be unlocked. In fact, in Android phones, the normal screen lock interface consists of two different interfaces:

The screen lock interface isLockScreen;

The unlock interface isUnLockScreen, including:

SIM card to unlock SimUnlockScreen

PatternUnlockScreen

Password unlock PasswordUnlockScreen

Account unlock AccountUnlockScreen


1.2 LockScreen Interface

First InterfaceCalledLockScreen Interface(For the sake of convenience, we call it"Screen lock page), That is, the interface we usually see. mobile phone manufacturers generally customize this interface. The interface is as follows:


The LockScreen. java class path corresponding to the custom View is located:

Frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \ LockScreen. java


1.3 UnLockScreen Interface

Second InterfaceCalledUnLockScreen Interface(For the convenience of this post, we call it"Unlock page"), Generally provided by the Android source code, there are four types:

①. PatternUnlockScreen. java class (custom LinearLayout)

Path: frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ PatternUnlockScreen. java

The interface is displayed as follows:


② PIN unlock interface ---- SimUnlockScreen. java class (custom LinearLayout)

The page is displayed as follows: (picture omitted)

③ Password unlocking interface ---- PasswordUnlockScreen. java class (custom LinearLayout)

Path: frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \ PasswordUnlockScreen. java


④. The GoogleAccount unlocking interface is the Google account unlocking interface. It is generally used when the number of Wrong Password Input exceeds the upper limit, the system will prompt you to enter a Google account to unlock. Note: To enable it, you need to manually set the account and synchronization, otherwise the interface will not come out. The corresponding source file is: AccountUnlockScreen. java class (custom LinearLayout)

Path: frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ AccountUnlockScreen. java

The interface is displayed as follows:


1.4 display rules

You can select an unlocking interface as follows:Set-> location and security-> set screen lock. Select the unlock interface.

The combination of the two interfaces has also changed a lot. The general rule is as follows: first display the LockScreen interface, and then judge whether the UnLockScreen interface is enabled. If the UnLockScreen interface is set, then, the corresponding UnLockScreen interface is displayed to unlock. However, there is a special case, that is, if we select the pattern UnLockScreen interface, it will not display the LockScreen interface, but will only display the UnLockScreen interface.


Ii. Major Work of customization

Generally, custom LockScreen is used to modify the default LockScreen. java, so we will introduce the default LockScreen.

Custom source code lockscreen:

1. screen lock Home Page

Frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ LockScreen. java

2. Sliding lock Control

Frameworks \ base \ core \ java \ com \ android \ internal \ widget \ sjavasingtab. java

3. Time Display

Frameworks \ base \ core \ java \ com \ android \ internal \ widget \ DigitalClock. java

4. landscape layout File

Frameworks \ base \ core \ res \ layout \ keyguard_screen_tab_unlock.xml

5. landscape layout File

Frameworks \ base \ core \ res \ layout \ keyguard_screen_tab_unlock_land.xml


3. screen lock page layout analysis

Next, we will analyze the layout of the entire interface. Screen lock interface (portrait screen) such:


Analysis file (taking the vertical screen as an example): frameworks \ base \ core \ res \ layout \ keyguard_screen_tab_unlock.xml

3.1 Layout Xml Code
 
 
 
     
      
      
      
      
      
      
          
           
       
      
      
      
      
      
      
      
      
      
           
       
      
  
 

The above is the source code of android2.3.

3.2 Control description

@ + Id/carrier: Display carrier InformationIf no SIM card is inserted, "No SIM card" is displayed. If no signal is displayed, "(No Service)" is displayed )"

@ + Id/emergencyCallText: Fixed string. "Only Use emergency call" is displayed when no SIM card is inserted.

@ + Id/time: display time information, Including 12-and 24-hour. A ViewGroup is written separately, which will be introduced later.

@ + Id/date: Display date information, Including weeks. If you need to modify the format, locate the desired format in the donottranslate-cldr.xml file for the language

@ + Id/status1, @ + id/status2: displays the charging and alarm information respectively.

@ + Id/tab_selector: slide to unlock

@ + Id/emergencyCallButton: Emergency dialing button. It is displayed when no SIM card is inserted.

You can modify the layout as needed, but we do not recommend that you modify the ID name because the information shown above is commonly used. Generally, mobile phones include this information. If you do want to delete a widget, check whether the controls are included in other classes. For example, call and answer interfaces.

Note:If you delete or add new controls, you must compile them all. You cannot compile only the framework-layer code. The specific reason is not found at the moment.


IV. Implementation of the screen lock Interface 4.1 main levels

Any interface is composed of various views/viewgroups (including custom ones, of course), and then the display status of these views is updated based on changes in the system's corresponding status values, the same is true for the screen lock interface. The top layer of the screen lock interface is controlled by FrameLayout. Of course, many layers are nested in the screen. The advantage of increasing the number of embedded layers is that we can separate the layers, update each layer. The difficulty is that the code is very depressing.

When the interface is complex, you can use the tool Hierarchy Viewer to clearly understand the Hierarchy of the entire View tree and a layout structure. Of course, it is also necessary to View the source code.

The Hierarchy of the entire screen lock interface is as follows (some of them are configured with a pattern unlocking interface). You can use Hierarchy Viewer to view the complete picture.



Important views are described as follows:

LockPatternKeyguardViewInherited from FrameLayout: Used as the carrier of LockScreen and UnLockScreen to control the display LockScreen or UnLockScreen interface.

LockScreenInherited from FrameLayout

PatterUnlockScreenViewGroup type: Pattern unlocking Interface

KeyguardViewHostInherited from FrameLayout. The ViewGroup is used as the top-level View and added to the window as the decoration object of WindowManager.

It has the same relationship with LockPatternKeyguardView as the resource layout set in DecorView and our Activity.

4.2 two broadcasts

To cancel the display page when the screen is on, we also need to know two broadcasts:

The screen is dimmed and the screen is lit.

Android. intent. action. SCREEN_ON ----- brightness

Android. intent. action. SCREEN_OFF ----- the screen is dark.

4.3 general process
4.3 analysis of important related documents

1. KeyguardScreen Interface

Function: The main function of this interface is to define four methods for each interface LockScreen or UnLockScreen to be displayed, so that it can be processed in different States.

Path: \ frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \ KeyguardScreen. java

The source code is interpreted as follows:

/*** Common interface of each {@ link android. view. view} that is a screen of * {@ link LockPatternKeyguardView }. */public interface KeyguardScreen {/** Return true if your view needs input, so shoshould allow the soft * keyboard to be displayed. */boolean needsInput (); // whether a value is required for the View, that is, the keyboard must enter a value for This interface/** This screen is no longer in front of the user. */void onPause (); // called when the interface is not in the foreground, including in GONE or the interface will be removed soon/** This screen is going to be in front of the user. */void onResume (); // compared with the onPause () method, This method is called when the interface is not in the foreground interface, and when the interface is in the VISIBLE state, it is called/** This view is going away; a hook to do cleanup. */void cleanUp (); // This interface will be removed soon, that is, it is not needed}

2. KeyguardScreenCallbackClass Interface

Function: each interface that needs to be displayed: LockScreen or UnLockScreen stores a unique instance of the object to report the situation to the control interface.

Path: frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \ KeyguardScreenCallback. java

The source code is interpreted as follows:

/** Within a keyguard, there may be several screens that need a callback * to the host keyguard view. */public interface KeyguardScreenCallback extends KeyguardViewCallback {/** Transition to the lock screen */void goToLockScreen (); // the current interface jumps to LockScreen, instead of UnLockScreen/** Transition to the unlock screen. */void goToUnlockScreen (); // The LockScreen is successfully unlocked and whether UnLockScreen needs to be displayed. Otherwise, the lock is successfully unlocked. // If you forget the unlock pattern, you need to jump to the Google account to unlock it. Void forgotPattern (boolean isForgotten); boolean isSecure (); // whether the current machine is secure, for example, boolean isVerifyUnlockOnly (), such as pattern and password unlocking;/** Stay on me, but recreate me (so I can use a different layout ). */void recreateMe (Configuration config); // display the corresponding Screen based on the current status of the mobile phone. /** Take action to send an emergency call. */void takeEmergencyCallAction (); // handling behavior during emergency calls. /** Report that the user had a failed attempt to unlock with password or pattern. */void reportFailedUnlockAttempt (); // handle the failure in UnLockScreen login/** Report that the user successfully entered their password or pattern. */void reportSuccessfulUnlockAttempt (); // processing when the UnLockScreen interface is successfully logged in/** Report whether we there's another way to unlock the device. * @ return true */boolean doesFallbackUnlockScreenExist ();}

3. KeyguardViewCallback classInterface

Function: provides some interfaces to accept the user's Screen operation results.

Path: frameworks \ base \ policy \ src \ com \ android \ internal \ policy \ impl \ KeyguardViewCallback. java

The source code is interpreted as follows:

/*** The callback used by the keyguard view to tell the {@ link KeyguardViewMediator} * various things. */public interface KeyguardViewCallback {/** Request the wakelock to be poked for the default amount of time. */void pokeWakelock (); // save that the screen is on for a certain period of time, the default time is 5s or 10 s/** Request the wakelock to be poked for a specific amount of time. */void pokeWakelock (int millis); // keep the screen on display in the event Segment Based on the specified time value/ ** Report that the keyguard is done. * @ param authenticated Whether the user securely got past the keyguard. * the only reason for this to be false is if the keyguard was has ucted * to appear temporarily to verify the user is supposed to get past the * keyguard, and the user fails to do so. * /// the lock is successfully unlocked. You can enter the mobile phone interface. If the parameter is true, it indicates whether the lock is bright. For example, the pattern is correct and the password is entered correctly. Void keyguardDone (boolean authenticated);/** Report that the keyguard is done drawing. */void keyguardDoneDrawing (); // this method is called back when drawing is complete during the draw () Process on the screen lock page .}
 



Note:

The screen lock mechanism of the Android platform is not very different. The screen lock mechanism of the later versions is improved and improved for various reasons, however, as long as you understand the lock principle and process of one version, the lock modules of other versions will be quickly understood.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.