Android source proxy mode

Android source proxy mode

Introduction proxy mode

Proxy mode (Proxy Pattern) also known entrusting mode, the first mode is the structural design patterns.

Define a proxy mode

Providing a proxy to control access to the object to other objects.

Proxy mode usage scenarios

When you can not or do not want to directly access an object or an object access difficulties, can be accessed indirectly through a proxy object, in order to ensure the transparency of the use of the client, delegate object and proxy object needs to implement the same interface.

Android source code of the agent model to achieve

When the application accesses the ActivityManager ActivityManagerService interface method, essentially generated by a proxy object IActivityManager access, it is a typical proxy mode.

Aidl file with a custom similar, IActivityManager.java is generated by IActivityManager.aidl, but IActivityManager.aidl is defined in the Framework aidl file system.

IActivityManager

IActivityManager 位于 frameworks/base/core/java/android/app/IActivityManager.aidl。

IActivityManager interface is shown below:

...
/**
 * System private API for talking with the activity manager service.  This
 * provides calls from the application back to the activity manager.
 *
 * {@hide}
 */
interface IActivityManager {
    ...
    @UnsupportedAppUsage
    int startActivity(in IApplicationThread caller, in String callingPackage, in Intent intent,
            in String resolvedType, in IBinder resultTo, in String resultWho, int requestCode,
            int flags, in ProfilerInfo profilerInfo, in Bundle options);
    ...
}

ActivityManagerService

Interface true realization in ActivityManagerService class.

public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    ...
}

It can be seen ActivityManagerService inherited IActivityManager.Stub class. IActivityManager.Stub internal abstract class IActivityManager.aidl generated java file.

startActivity ActivityManagerService method to achieve the following:

    @Override
    public final int startActivity(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
                resultWho, requestCode, startFlags, profilerInfo, bOptions,
                UserHandle.getCallingUserId());
    }

ActivityManager getService

ActivityMangerService local proxy objects is IActivityManager by ActivityManager The getService () method is obtained.

    public static IActivityManager getService() {
        return IActivityManagerSingleton.get();
    }

    private static final Singleton<IActivityManager> IActivityManagerSingleton =
            new Singleton<IActivityManager>() {
                @Override
                protected IActivityManager create() {
                    final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                    final IActivityManager am = IActivityManager.Stub.asInterface(b);
                    return am;
                }
            };

First obtains a single embodiment IActivityManager IActivityManagerSingleton by the get method. IActivityManager obtained by getService IBinder object proxy ServiceManager method, and the method by IActivityManager.Stub.asInterface IBinder agent into IActivityManager interface.

Therefore, the use of the interface as a direct call IActivityManager as ActivityMangerService. But in fact, when you call the agent IActivityManager, cross-process data package will be passed to the ActivityMangerService processing Server end and returns the result. These processes are done in-house to achieve IActivityManager.java in, so hide the details of the internal transact looks when calling agents.

Published 105 original articles · won praise 2 · Views 3751

Guess you like

Origin blog.csdn.net/caoshen2014/article/details/103134383