这里我们主要是梳理下图中的架构,以IMountService为例。
在StorageManager的构造函数中,用到了IMountService
/**
* Constructs a StorageManager object through which an application can
* can communicate with the systems mount service.
*
* @param tgtLooper The {@android.os.Looper} which events will be received on.
*
* <p>Applications can get instance of this class by calling
* {@link android.content.Context#getSystemService(java.lang.String)} with an argument
* of {@link android.content.Context#STORAGE_SERVICE}.
*
* @hide
*/
public StorageManager(Looper tgtLooper) throws RemoteException {
mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
if (mMountService == null) {
Log.e(TAG, "Unable to connect to mount service! - is it running yet?");
return;
}
mTgtLooper = tgtLooper;
mBinderListener = new MountServiceBinderListener();
mMountService.registerListener(mBinderListener);
}
我们看一下IMountService.java中asInterface()的定义
/**
* Cast an IBinder object into an IMountService interface, generating a
* proxy if needed.
*/
public static IMountService asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (iin != null && iin instanceof IMountService) {
return (IMountService) iin;
}
return new IMountService.Stub.Proxy(obj);
}
它new了一个Proxy,很明显StorageManager的构造函数中得到的只是一个Proxy,也就是代理。这个可以看一下声明
/**
* WARNING! Update IMountService.h and IMountService.cpp if you change this
* file. In particular, the ordering of the methods below must match the
* _TRANSACTION enum in IMountService.cpp
*
* @hide - Applications should use android.os.storage.StorageManager to access
* storage functions.
*/
public interface IMountService extends IInterface {
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends Binder implements IMountService {
private static class Proxy implements IMountService {
private final IBinder mRemote;
Proxy(IBinder remote) {
mRemote = remote;
}
public IBinder asBinder() {
return mRemote;
}
public String getInterfaceDescriptor() {
return DESCRIPTOR;
}
/**
* Registers an IMountServiceListener for receiving async
* notifications.
*/
public void registerListener(IMountServiceListener listener) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((listener != null ? listener.asBinder() : null));
mRemote.transact(Stub.TRANSACTION_registerListener, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
从中可以看出,Proxy包含于Stub中,而Stub包含于IMountService接口类中。
图中的Proxy和Stub都在IMountService.java中实现。
Proxy中有的函数,在Stub中也有对应的函数,这是因为它们都implements IMountService。
我们以registerListener()为例,从上面的代码可以看出其在Proxy中的定义,它调用了mRemote.transact(),其实它是跟Stub的onTransact()相对应,我们看看它的定义
/** Construct the stub at attach it to the interface. */
public Stub() {
attachInterface(this, DESCRIPTOR);
}
public IBinder asBinder() {
return this;
}
@Override
public boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_registerListener: {
data.enforceInterface(DESCRIPTOR);
IMountServiceListener listener;
listener = IMountServiceListener.Stub.asInterface(data.readStrongBinder());
registerListener(listener);
reply.writeNoException();
return true;
}
可以看到,在onTransact()中,也调到了registerListener(),但是Stub没有实现这个函数,这样就会调用它的实例中的实现。
而这个实例就是MountService。我们看一下MountService的声明
/**
* MountService implements back-end services for platform storage
* management.
* @hide - Applications should use android.os.storage.StorageManager
* to access the MountService.
*/
class MountService extends IMountService.Stub
implements INativeDaemonConnectorCallbacks, Watchdog.Monitor {
private static final boolean LOCAL_LOGD = false;
private static final boolean DEBUG_UNMOUNT = true;
private static final boolean DEBUG_EVENTS = true;
private static final boolean DEBUG_OBB = false;
可以看出,MountService继承自IMountService.Stub,它才是上图中真正的Service。它在SystemServer.java中被调用,并实例化。
而IMountService.Stub就是图中的Stub。它应该会在MountService启动时被实例化。
IMountService.Stub.Proxy是图中的Proxy,它在StorageManager.java中被实例化。
StorageManager就是图中的Client。