1. AIDL介面
對於WindowManagerService,如果client需要訪問它,需要通過AIDL介面來訪問,其介面在IWindowManager.aidl中定義,ADIL介面在編譯時間將產生一個IWindowManager.java檔案(此檔案在out目錄下),其主要相關代碼如下:
/** * System private interface to the window manager service. * * {@hide} */ public interface IWindowManager extends android.os.IInterface{ ... public static abstract class Stub extends android.os.Binder implements android.view.IWindowManager { ... /** * Cast an IBinder object into an android.view.IWindowManager interface, * generating a proxy if needed. */ public static android.view.IWindowManager asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof android.view.IWindowManager))) { return ((android.view.IWindowManager)iin); } return new android.view.IWindowManager.Stub.Proxy(obj); } //end asInterface .... private static class Proxy implements android.view.IWindowManager { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } public android.os.IBinder asBinder() { return mRemote; } ... public android.view.IWindowSession openSession(com.android.internal.view.IInputMethodClient client, com.android.internal.view.IInputContext inputContext) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); android.view.IWindowSession _result; try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeStrongBinder((((client!=null))?(client.asBinder()):(null))); _data.writeStrongBinder((((inputContext!=null))?(inputContext.asBinder()):(null))); mRemote.transact(Stub.TRANSACTION_openSession, _data, _reply, 0); _reply.readException(); _result = android.view.IWindowSession.Stub.asInterface(_reply.readStrongBinder()); } finally { _reply.recycle(); _data.recycle(); } return _result; } // end openSession ... } // end Proxy }//end Stub ...}
由上代碼可知, 它總共定義了以下三個類:
1) IWindowManager
2) IWindowManager.Stub
3) IWindowManager.Stub.Proxy
2. 在伺服器端:
WindowManagerService負責實現服務,它的定義如下:
public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs
很顯然,它實現了Stub中定義的方法。
3. 在Client端:
當擷取服務時,通過調用以下函數:
IWindowManager.Stub.asInterface(ServiceManager.getService("window"))
返回IWindowManager.Stub.Proxy,從上面的代碼可知,它就是我們熟悉的BpBinder所做的事,只是通過aidl的方式簡化的代碼的開發,代碼由編譯時間自動產生。
4. AIDL工作流程