"In-depth understanding of Android2" Reading notes (iii)

Source: Internet
Author: User

Read the Reading notes of "in-depth understanding of Android2" (ii)

Packagemanagerservice

Packagemanagerservice is responsible for the management of the package in the system, application installation, uninstallation, information inquiry and so on.

The 1.IPackageManager interface class defines the business functions for server and client communication, and also defines an internal stub that derives from Binder and implements the Ipackagemanager interface.

2.PackageManagerService inherits from the Ipackagemanager.stub class, because the stub class derives from Binder, so Packagemanagerservice communicates with the binder as a server.

An inner class proxy is defined in the 3.Stub class that has a member variable mremote,mremote of type IBinder (actual type Binderproxy) used for packagemanagerservice communication with the server.

The 4.IPackageManager interface is similar to defining many business functions, but for security reasons, Android provides only a subset of the external (i.e. SDK) that is encapsulated in the abstract class Packagemanager. The client typically returns a type of Packagemanager object through the Getpackagemanager function of the context, which is the actual type of the Packagemanager subclass Applicationpackagemanager. This interface-based programming approach, while greatly reducing the coupling between modules, has brought a lot of trouble to code analysis.

The 5.ApplicationPackageManager class inherits from the Packagemanager class. Instead of directly participating in binder communication, it points to an object of type IPackageManager.Stub.Proxy through the MPM member variable.

 Public Static Packagemanagerservice Main (context context, Installer Installer,        booleanboolean  Onlycore) {    new  packagemanagerservice (context, installer,            factorytest, onlycore);    Servicemanager.addservice ("package", m);     return m;}

The pkms constructor has more than 600 lines, the main function: scanning the android system in several target folders in the APK, so as to establish a suitable data structure to manage such as package information, four components of information, access information and other information. PKMS will parse the Androidmanifest.xml in the APK package and create a corresponding object for safekeeping based on the activity tag declared therein.

The following analysis constructors

eventlog.writeevent (Eventlogtags.boot_progress_pms_start, Systemclock.uptimemillis ());if(msdkversion <= 0) {SLOG.W (TAG,"* * * * RO.BUILD.VERSION.SDK not set!");} Mcontext=context;mfactorytest=Factorytest;monlycore=onlycore;mlazydexopt= "eng". Equals (Systemproperties.get ("Ro.build.type")); Mmetrics=Newdisplaymetrics (); Msettings=NewSettings (mpackages); MSETTINGS.ADDSHAREDUSERLPW ("Android.uid.system", Process.system_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged); MSETTINGS.ADDSHAREDUSERLPW ("Android.uid.phone", Radio_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged); mSettings.addSharedUserLPw ("Android.uid.log", Log_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged); MSETTINGS.ADDSHAREDUSERLPW ( /c4>"ANDROID.UID.NFC", Nfc_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged); MSETTINGS.ADDSHAREDUSERLPW ( /c5>"Android.uid.bluetooth", Bluetooth_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged); MSETTINGS.ADDSHAREDUSERLPW ("Android.uid.shell", Shell_uid, Applicationinfo.flag_system, applicationinfo.private_flag_privileged);

Setting

Sharedusersetting ADDSHAREDUSERLPW (String name,intUidintPkgflags,intpkgprivateflags) {sharedusersetting s=msharedusers.get (name); if(s! =NULL) {        if(S.userid = =uid) {            returns; } packagemanagerservice.reportsettingsproblem (Log.error,"Adding duplicate shared user, keeping first:" +name); return NULL; } s=Newsharedusersetting (name, Pkgflags, pkgprivateflags); S.userid=uid; if(ADDUSERIDLPW (UID, S, name)) {Msharedusers.put (name, s); returns; }    return NULL;}

Get sharedusersetting

The 1.Setting class defines a msharedusers member, which is a hashmap, with a string (such as Android.uid.system) as key, and the corresponding value is a Sharedusersetting object.

2.SharedUserSetting derives from the Grantedpermission class, known from the name of the Grantedpermissions class, and it is related to permissions. Sharedusersetting defines a member variable packages, type HashSet, that holds the permission setting information for the package that declares the same shareduserid.

3. Each package has its own set of permissions. The concept of permissions is expressed by the Packagesetting class. This class inherits from Packagesettingbase, and Packagesettingbase inherits from Grantedpermissions.

There are two members in 4.Settings, one is Muserids, the other is Motheruserids, and the two-bit members are ArrayList and Sparsearray respectively. The purpose is to get the corresponding Sharedusersetting object with the UID index. In general, it is much faster to get the array elements in an index than to get the elements in the HashMap with key.

Private BooleanADDUSERIDLPW (intuid, object obj, object name) {    if(UID >process.last_application_uid) {        return false; }    if(UID >=process.first_application_uid) {        intN =muserids.size (); Final intindex = UID-Process.first_application_uid;  while(Index >=N) {Muserids.add (NULL); N++; }        if(Muserids.get (index)! =NULL) {Packagemanagerservice.reportsettingsproblem (Log.error,"Adding Duplicate User id:" +UID+ "Name=" +name); return false;    } muserids.set (index, obj); } Else {        if(Motheruserids.get (UID)! =NULL) {Packagemanagerservice.reportsettingsproblem (Log.error,"Adding Duplicate Shared ID:" +UID+ "Name=" +name); return false;    } motheruserids.put (uid, obj); }    return true;}

The first phase of the pkms constructor is to scan and parse the XML file and save the information in a specific data structure.

PKMs the second stage of the work is to scan the system APK, because the need to scan files, so the more mobile phone installed, the more work pkms, the more the system startup speed is slower.

PKMS will scan the following directories:

1./system/frameworks: The files in this directory are system libraries, such as Framework.jar, Services.jar, framework-res.apk. But Scandirli only scans the apk file, so framework-res.apk is the only "favored" file in the directory

2./system/app: The directory is full of default system applications, such as browser.apk, settingsprovider.apk, and so on.

3./vendor/app: The files in this directory are provided by vendors, which are all vendor-specific apk files, and currently the vendors in the market place their applications in the/system/app directory

Private voidScandirli (File dir,intParseflags,intScanflags,Longcurrenttime) {    Finalfile[] Files =Dir.listfiles (); if(Arrayutils.isempty (Files)) {LOG.D (TAG,"No files in app dir" +dir); return; }    if(debug_package_scanning) {log.d (TAG,"Scanning app dir" + dir + "scanflags=" +Scanflags+ "flags=0x" +integer.tohexstring (parseflags)); }     for(File file:files) {Final BooleanIsPackage = (isapkfile (file) | |file.isdirectory ())&&!Packageinstallerservice.isstagename (File.getname ()); if(!ispackage) {            //Ignore entries which is not packages            Continue; }        Try{scanpackageli (file, Parseflags|packageparser.parse_must_be_apk, Scanflags, CurrentTime,NULL); } Catch(packagemanagerexception e) {SLOG.W (TAG,"Failed to parse" + File + ":" +e.getmessage ()); //Delete Invalid UserData apps            if((Parseflags & packageparser.parse_is_system) = = 0 &&E.error==packagemanager.install_failed_invalid_apk) {Logcriticalinfo (Log.warn,"Deleting Invalid package at" +file); if(File.isdirectory ()) {Minstaller.rmpackagedir (File.getabsolutepath ()); } Else{file.delete (); }            }        }    }}

The Scanpackageli function first calls Packageparser to parse the apk file, packageparse the conversion from the physical file to the corresponding data structure. Packageparser function is single, is to parse androidmanifest.xml in the various labels,

"In-depth understanding of Android2" Reading notes (iii)

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.