Question

To avoid useless answer about how to implement AIDL in your own project - I understand how to use AIDL in your own project.

What's unclear for me is how it is used in Android source code?

For example, there is IAudioService AIDL interface in android.media package. And there is AudioService java file. And there are a lot of examples where interface is defined in IInterfaceServiceName.aidl and implementation is in InterfaceServiceName.java. So, I thought that internals of AIDL look for Service with the name without first capital I, and then use it. However, this theory was demolished by IWifiManager and WifiService.

All of this AIDL implementations, however, share one thing - they all extend IInterfaceName.Stub class. So, that means AIDL internals look for classes extending IInterfaceName.Stub? But what if there are multiple classes extending it? And those classes aren't services at all...

Or there is some Service Manager, which somehow creates Services with those AIDL implementations as Binders? If so, please direct me to this mysterious service manager.

EDIT: I've found this service manager. It is ServiceManager in package android.os. Now it's understandable why those SomethingService aren't actually services but are actually IBinders - the service manager somehow creates services with given IBinder by method add service(String, IBinder). But now confusion only becomes even worse. The implementation of this service also lies in somewhat strange ServiceManagerNative, which involves remote transaction with unknown IBinder. Now I'm totally lost. How then does this ServiceManager starts itself up? How are the services added by it?

Was it helpful?

Solution

ServiceManager has not a random but a predefined value. It is a Service with Binder handle equal to 0. During the boot of the system ServiceManager is one of the first services that are started by the system (you can check this in init.rc file). Binder permits only one service to act as a name resolver. All other services started in the system are registered with ServiceManager.

OTHER TIPS

IAudioService.aidl defines public methods that are visible for other processes. During compilation, from .aidl there is an interface IAudioService generated in which you can find Stub class:

public static abstract class Stub extends android.os.Binder implements android.media.IAudioService{

now the class AudioService extends this Stub

public class AudioService extends IAudioService.Stub implements OnFinished {

locations for all mentioned files, from main android folder:

./frameworks/base/media/java/android/media/IAudioService.aidl
./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/media/java/android/media/IAudioService.java
./frameworks/base/media/java/android/media/AudioService.java
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top