fun with binders
TRANSCRIPT
©2015 GlobalLogic Inc. CONFIDENTIAL
©2015 GlobalLogic Inc. CONFIDENTIAL
Fun with Binders
CONFIDENTIAL
1. AIDL Crash Course
2. Binders and Security
3. Tips and Tricks
4. Q&A
Agenda
CONFIDENTIAL©2015 GlobalLogic Inc.
AIDL Crash Course
CONFIDENTIAL
AIDL Crash Course
Process A
Activity
Process B
Service
Target
bindService()AIDL Stub
onBind
AIDL ProxyonServiceConnected
CONFIDENTIAL
AIDL Crash Course
IDummy.aidl
Stub class
Proxy class
IDummy.java
// IDummy.aidlinterface IDummy { void send(String message);}
CONFIDENTIAL
AIDL Crash Courseprivate class Proxy implements IDummy {
IBinder mRemote;
public void send(String text) throws RemoteException {Parcel data = Parcel.obtain();Parcel reply = Parcel.obtain();try {
data.writeString(text);mRemote.transact(Stub.TRANSACTION_send, data, reply,
0);} finally {
data.recycle();reply.recycle();
}}
}
CONFIDENTIAL
AIDL Crash Coursepublic abstract class Stub extends Binder implements IDummy {
private static final int TRANSACTION_send = 1;
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException{
if (code == TRANSACTION_send) {String arg0 = data.readString();send(arg0);return true;
}return false;
}}
CONFIDENTIAL
Process A
transact()
Process B
onTransact()
AIDL Crash Course
Activityshow(“123”)
Binder
Magic
show(“123”)
AIDL Proxy AIDL Stub
Target
CONFIDENTIAL©2015 GlobalLogic Inc.
Binders and Security
CONFIDENTIAL
Binders and Security - Basics
Process BProcess A
Binder
IBinder #1
IBinder #2
IBinder #3
==
==
CONFIDENTIAL
System Process
Binders and Security - Basics
User Process
ActivityManagerActivityManager
Service
WindowManagerWindowManager
Service
PowerManagerWindowManager
Service
...Manager...Manager
Service
CONFIDENTIAL
Binders and Security - PowerManagerpublic final class WakeLock {
private final IBinder mToken = new Binder();
public void acquire() { mService.acquireWakeLock(mToken, ...); }
public void release() { mService.releaseWakeLock(mToken); }
}
CONFIDENTIAL
Binders and Security - InputMethodManagerpublic final class InputMethodManager {
final IInputMethodClient.Stub mClient = new IInputMethodClient.Stub() { // ... }
public boolean showSoftInput(View view, int flags) { // ... return mService.showSoftInput(mClient, flags); }
public boolean hideSoftInputFromWindow(IBinder windowToken, int flags) { // ... return mService.hideSoftInput(mClient, flags); }
}
CONFIDENTIAL©2015 GlobalLogic Inc.
Tips and Tricks
CONFIDENTIAL
Tips and Tricks
Binder
Context.startActivity
Context.sendBroadcast
Context.startService
Context.bindService
ContentResolver.call
...
Parcel
CONFIDENTIAL
Tips and Tricks - Binder & ContentProvider
Process #1
Service
Process #2
Service
Process #N
Service
...
Process #N+1
Activity
Process #N+2
Activity
Process #N+M
Activity
CONFIDENTIAL
Tips and Tricks - Binder & ContentProvider
Process A
ContentProvider
Process B
Activity
Binder / Stub
call
CONFIDENTIAL
Tips and Tricks - Binder & ContentProviderpublic class Dummy {
private static final Uri AUTHORITY = /*...*/; private static IDummy sDummy; private static ContentResolver sResolver = /*...*/;
public static IDummy getProxy() { if (sDummy != null && sDummy.asBinder().pingBinder()) { return sDummy; } Bundle bundle = sResolver.call(AUTHORITY, "get_binder", null, null); return sDummy = IDummy.Stub.asInterface(bundle.getBinder("binder")); }}
CONFIDENTIAL
Tips and Tricks - Binder & ContentProvider
Dummy.getProxy().send("test");
bindService(/**/, new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {
IDummy.Stub.asInterface(service).send("test");}
@Overridepublic void onServiceDisconnected(ComponentName name) {}
});
CONFIDENTIAL
Useful Links● Official Binder documentation
http://developer.android.com/reference/android/os/Binder.html
● Binders as tokens
http://www.androiddesignpatterns.com/2013/07/binders-window-tokens.html
● AIDL and ContentProviders
http://mdev-android.blogspot.com/2014/12/androids-alternative-ipc-approche.html
CONFIDENTIAL©2015 GlobalLogic Inc.
Q&A
CONFIDENTIAL©2015 GlobalLogic Inc.
Rostyslav Lesovyi
Thank You!