Article 9 Android package management service mechanism-interpreter Mode

Source: Internet
Author: User
Tags xml parser

Packagemanagerservice is one of the Android system services. It provides the parsing, installation, update, mobile, and uninstallation functions of application packages.

The system class diagram is as follows:

Packagemanagerservice implements functions through the following classes: installer, usermanager, appdirobserver, settings, defaultinerservice, packagehelper, nativelibraryhelper, packagehandler, and packageparser.

The installer class establishes a localsocket connection with the local service installd, and uses installd to install, remove, dexopt, and other functions of the package.

The usermanager class installs, creates, and manages user-related user data and packages, including the creation and deletion of user and user data.

Appdirobserver monitors the add and remove events in/system/framework,/system/APP,/vendor/APP,/data/APP,/data/APP-private, and other directories, dynamically install and uninstall packages.

The settings class reads and manages (creates, updates, and so on) files related to package installation information under the Data/system/directory, parses and reads the following packages. XML, "packages-backup.xml", "packages. list "," packages-stopped.xml "," packages-stopped-backup.xml"
XML file.

Packages. XM is created by writelp () during APK parsing, which records the permissions of the system and the name of each APK.
, Codepath, pkgflags, timestamp, versioncode, uesrid, and other information, mainly through the androidmanifest of APK. XML parsing is obtained. After the APK is parsed, the updated information is written to this file and saved to flash. After the APK is parsed, the system reads the information from the file and adds it To the memory list. This file is updated when an APK is upgraded, installed, or deleted.

Packages. List records the following data: pkgname, userid, debugflag, and datapath (package data path ).

The packages-stopped.xml records information about a stopped package, including the package name, notlaunched status, and so on.


Defaultcontainerservice is an application service that stores APK and other related resource files in internal or external storage. The defaultinerservice provides an imediacontainerservice. Stub object. Packagemanagerservice the package management service first establishes a connection with the service after the packagehandler object receives the application installation message (through bindservice ). After the service is connected, the onserviceconnected callback is called. In the onserviceconnected callback function, a remote proxy object is created based on the object reference of imediacontainerservice. Stub passed in. In the future, the packagemanagerservice will access the defaultcontainerservice service through this proxy object.

Packagehelper is a helper class that provides the mountservice API methods used for package installation.

Nativelibraryhelper is a helper class that provides methods for installing, deleting, and calculating the size of library files contained in APK.

Packagehandler processes messages related to package processing, such as APK installation request messages, such as ADB install. Packagehandler runs in a separate thread.

The packagehandler object uses the template and command mode to process the request of the application package, and transmits the request to the processing function as the object through the message; package Size and message requests for package installation, movement, and retrieval are respectively packaged as installparams, moveparams, and measureparams objects. All three classes are inherited from the handlerparams superclass, the handlerparams superclass defines a template function startcopy. The three functions used in the template function startcopy are not implemented in the superclass and are virtual functions. The specific functions are determined by the specific class; for example, the virtual function handlestartcopy implements package installation for the installparams class, the moveparams class implements package movement, and the handlestartcopy function in the measureparams class implements package measurement.

The following three classes are available: installparams, moveparams, and measureparams: ipackageinstallobserver, ipackagemoveobserver, and ipackagestatsobserver, the three interfaces are passed in as parameters when the packagemanagerservice client object calls the package installation, movement, and package size APIs of the packagemanagerservice, the corresponding observer interface is assigned when three objects are instantiated: installparams, moveparams, and measureparams. Therefore, the three observer interfaces actually point to the remote proxy object of the observer object (pile object) provided by the client, the installparams, moveparams, and measureparams objects send notifications to the remote client through the corresponding observer interface. The proxy mode and observer mode are used in combination.

The command design mode is also used for processing post_install messages. The request parameters are packaged as postinstalldata objects. The postinstalldata objects include installargs and packageinstalledinfo objects. installargs is a virtual class and provides interfaces such as copyapk, the specific implementation of the interface is completed in its specific classes fileinstallargs and sdinstallargs.

Packageparser implements the application package parsing function. It mainly resolves the androidmanifest. xml file of each APK, processes resource files such as asset/RES, and establishes the configuration structure information of each APK. Packageparser is the basis for implementing other class functions and is the most important class of the package management service. The packageparser class adopts the interpreter mode when parsing the application package. Corresponding classes are created for each syntax structure in the application package to collect the corresponding information in the application package. The class structure is as follows:

In the figure, all classes except resources and xmlpullparser are internal classes of the packageparser class. The corresponding classes of each syntax structure of the application package are derived from the componet class or internal members of the componet class, A package class (a package and a package object) is an aggregation class that aggregates the componet In the parsed application package into the package class for unified management, the packageparser class adds each parsed component and other information to the package.

Resources and xmlpullparser are responsible for reading XML resources.

The entire parsing process:

1,Call the scandirli function in the constructor of the packagemanagerservice to perform the following operations: frameworkdir (/system/framework), systemappdir (/system/APP), and vendorappdir (/vendor/APP), appinstalldir (/data/APP), and drmappprivateinstalldir (/data/APP-private;

2,The scandirli function calls scanpackageli (file,
Flags | packageparser. parse_must_be_apk, scanmode, currenttime) function parses each APK file and returns the packageparser. Package object containing the parsing information. The scandirli function also deletes the invalid APK file after parsing fails;

3,In the scanpackageli function, a packageparser object is first instantiated, And the parsepackage (scanfile, scanpath,
Parse RICS, parseflags) function to parse the parsing file;

4,After determining the file in the parsepackage function, instantiate an assetmanager object and call the addassetpath function of the assetmanager object to add the path of the parsed file to the assetmanager object, after successful addition, instantiate a resources object and call the openxmlresourceparser (cookie,
The android_manifest_filename function opens an XML parser that parses the androidmanifest. xml file, and then calls the parsepackage (Res, parser, flags, errortext) function to start specific parsing;

5,
The parsepackage function first calls the parsepackagename function to parse the package name, instantiate a package object based on the package name, and then start from androidmanifest. the XML file parses global attribute information such as versioncode, versionname, and installlocation of the parsed package, and then parses other components contained in the XML file cyclically Based on the labels of the XML file, when the tagname is the application tag, the parseapplication function is called to parse the components and other information contained in the application tag (only one application tag can be parsed ); the parsepermissiongroup function is called for parsing when the tagname is "permission-group"; the parsepermission function is called for parsing when the tagname is "permission; the parsepermissiontree function is called for parsing when the tagname is "permission-Tree"; the parsepermissiontree function is called for parsing when the tagname is "permission-Tree; when the tagname is "Instrumentation", the parseinstrumentation function is called for parsing, and other labels are parsed or skipped, such as "uses-feature", "uses-configuration", "uses-SDK", and "supports-screens" to obtain other attributes of the application package;

6,In the parseapplication function, the application attribute information contained in the application tag is parsed first, and other components contained in the application tag are parsed cyclically Based on the tag of the XML file; the "activity" label calls the parseactivity function for parsing, returns an activity object, and adds it to the activities list of the package object. For the "worker Er" label, the parseactivity function is also called for parsing, return an activity object and add it to the receivers list of the package object. Call the parseservice function for parsing the "service" label, return a service object, and add it to the services list of the package object; the "provider" label calls the parseprovider function for parsing, returns a provider object, and adds it to the providers list of the package object. The "activity-alias" label calls the parseactivityalias function for parsing, return an activity object and add it to the activities list of the package object. For the "meta-data" label, call the parsemetadata function to parse the metadata of the application. parse or skip other labels, such as "uses-library" and "uses-package;

7,In the parseactivity function, first instantiate a parsecomponentargs object and activityinfo object, and then instantiate the activity object (the instantiated parsecomponentargs object and activityinfo object are used as parameters to the constructor of the activity object ), then, the "activity" tag is parsed and assigned to the corresponding attributes of the internal activityinfo object of the activity object; the "activity" tag contains other parts according to the tag of the XML file. For the "intent-filter" tag, an activityintentinfo object is instantiated first, call parseintent to parse the "intent-filter" section. The parsed information is placed in the activityintentinfo object that is passed as a parameter to the parseintent function, after parsing, add the activityintentinfo object containing the parsing information to the intents list of the activity object. Call the parsemetadata function for the "meta-data" tag to parse the metadata corresponding to the activity, other tags are skipped. After the tag loop resolution is complete, the activity object containing the parsing information is returned;

8,Parse other components in the same process.

 

Each derived class of the componet class has a corresponding Info Object, pointing to the information contained in each componet. The class diagram is as follows:

Each componet Information Class is derived from the packageinfo class and packageiteminfo subclass componentinfo. The packageinfo class is a container class, and the information parsed in the application package is aggregated into the packageinfo class for unified management.

You are welcome to repost the document. Please respect the source of the original article.

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.