Getting PackageManagerService source code analysis of (a)

Disclaimer: This article is a blogger original article, reproduced welcome but must indicate the source Thank you! https://blog.csdn.net/dongxianfei/article/details/52717981

1. Background

To analyze the contents of today I believe we certainly are not unfamiliar, perhaps you usually do not go directly to call its methods, but the Android system, but to use it all the time, it is what we want to focus on Android core services PackageManagerService analysis today.

Maybe you usually do not feel it's there, maybe that is common for all services it, but it pivotal in the system. For example, usually when we restart the phone, it will silently all installed applications scanning operation is performed in the background, the other is when we install the application it will perform the installation.

Here I do not say that much of a digression, we go directly to the theme of today, due to the PackageManagerService source more (their work is very tiring), so I am going to be interpreted in several stages.

2, the overall architecture

We have to know, like the core of such a system-level service Android will not allow us to directly call their methods, Android will certainly find ways to package and provide an external interface for developers to use, so today we have from developer the perspective of a step by step insight.

Well into our topic today, we all know that Context is an abstract class that defines the true realization of all those methods is ContextImpl class, if you can not understand refer to my other article Android in Context source code analysis , we enter getPackageManager methods of the class.

class ContextImpl extends Context {

    private PackageManager mPackageManager;

    //当不为null时直接返回
    public PackageManager getPackageManager() {
        if (mPackageManager != null) {
            return mPackageManager;
        }

        //这里先调用ActivityThread的getPackageManager获得IPackageManager对象
        IPackageManager pm = ActivityThread.getPackageManager();
        if (pm != null) {
            // Doesn't matter if we make more than one instance.

            //创建ApplicationPackageManager对象
            return (mPackageManager = new ApplicationPackageManager(this, pm));
        }

        return null;
    }
}

Let's look ActivityThread.getPackageManager method is to obtain IPackageManager object.

public final class ActivityThread {

    static IPackageManager sPackageManager;

    public static IPackageManager getPackageManager() {
        if (sPackageManager != null) {
            return sPackageManager;
        }

        //获得PackageManagerService的Binder对象
        IBinder b = ServiceManager.getService("package");
        //传入IPackageManager.Stub.asInterface方法中获得IPackageManager对象
        sPackageManager = IPackageManager.Stub.asInterface(b);
        //最终返回一个IPackageManager对象
        return sPackageManager;
    }
}

Here we can see that the object is to get IPackageManager by IPackageManager.Stub.asInterface method, we are to look into the method.

public static android.content.pm.IPackageManager asInterface(android.os.IBinder obj)
    {
        if ((obj==null)) {
            return null;
        }
    android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
    if (((iin!=null)&&(iin instanceof android.content.pm.IPackageManager))) {
        return ((android.content.pm.IPackageManager)iin);
    }
    //这里最终通过创建IPackageManager的内部Proxy对象并返回
    return new android.content.pm.IPackageManager.Stub.Proxy(obj);
    }

It should explain, this is not IPackageManager file in the source code, under the final document out directory.

We look at the Proxy class.

public interface IPackageManager extends android.os.IInterface

    public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageManager{

        private static class Proxy implements android.content.pm.IPackageManager{

            //Proxy类有一个成员变量mRemote的Binder对象,该对象用于和PackageManagerService进行通信
            private android.os.IBinder mRemote;
            Proxy(android.os.IBinder remote)
            {
                mRemote = remote;
            }
            @Override public android.os.IBinder asBinder()
            {
                return mRemote;
            }
        }
    }
}

Well, after the analysis here we will IPackageManager pm getPackageManager () method = ActivityThread.getPackageManager (); it has been analyzed and, ultimately, a IPackageManager objects here (it really is IPackageManager of Proxy objects), then we continue analysis.

getPackageManager () method to start creating the next ApplicationPackageManager object, and the object and IPackageManager ContextImpl proxy object created previously passed as a parameter, we enter the constructor ApplicationPackageManager look.

final class ApplicationPackageManager extends PackageManager {

    private final ContextImpl mContext;
    private final IPackageManager mPM;

    ApplicationPackageManager(ContextImpl context,
                              IPackageManager pm) {
        mContext = context;
        mPM = pm;
    }
}

ApplicationPackageManager in the constructor IPackageManager Proxy proxy object assigned to mPM member variables, such mPM member variable points to a variable of type IPackageManager.Stub.Proxy.

Well, this class and we will have roughly PackageManagerService related reason is clear again, and we now come to sum up the relationship between these classes.

3, summary

First we look at the relationship between a class and then to analyze.

PKMS related class diagram

From this diagram can be seen, the mPM variable points Proxy agent, available client object via ApplicationPackageManager Context.getPackageManager (), the method will be called directly call ApplicationPackageManager Proxy to proxy method IPackageManager at this time to continue Binder type variable mRemote call PackageManagerService methods to begin PackageManagerService method.

Next we summarized as follows for this article:
(1) IPackageManager interface defines the methods server and client communication, it also defines an inner class Proxy, class inherits Binder and implement IPackageManager interface.

(2) PackageManagerService IPackageManager.Stub class inherit, due to inherited Stub Binder, so PackageManagerService Binder may be used as a communication server.

(3) Stub also defines a Proxy internal class that has a member variable mRemote Binder, PakcageManagerService and server for communication.

(4) IPackageManager interface defines a number of business functions, Android to provide a subset for developers to access, this subset is PackageManager abstract class, can () method was adopted Context.getPackageManager, we also said earlier analysis, the ultimate method It will return a subclass of PackageManager ApplicationPackageManager objects.

(5) ApplicationPackageManager inherited from PackageManager and its abstract methods, before the code we have done the analysis, in Context.getPackageManager () will IPackageManager Proxy proxy object assigned to the member variables ApplicationPackageManager mPM, so mPM member variables IPackageManager.Stub.Proxy proxy object pointed to, and then through an indirect call to PackageManagerService Binder method.

Well, today's entry-PackageMangerService we stop here, the next article we will analyze PackageMangerService official function of the first phase of PackageManagerService source code analysis of the (two) .

Guess you like

Origin blog.csdn.net/dongxianfei/article/details/52717981