Wednesday, August 27, 2008

Generate Callback Listener Helpers from AIDL

As I redesign Five I find myself tweaking/expanding the callback listeners in my interface which are handled through the RemoteCallbackList class in the service implementation. This class is great, however for each call code must be written similar to:

int N = mCallbacks.beginBroadcast();

for (int i = 0; i < N; i++) {
try {
mCallbacks.getBroadcastItem(i).onSomeEvent();
} catch (RemoteException e) {}
}

mCallbacks.finishBroadcast();

This can quickly pollute the service implementation, and even if you factor these calls out to a separate class it can be a pain to create and maintain them. So, I came up with a solution in the form of a Perl script which takes as input an AIDL file defining the callback interface and outputs a class implementing the extended RemoteCallbackList. To use, simply type:

$ ./aidl-cbliststub.pl < IFooListener.aidl > IFooListenerCallbackList.java

Download: aidl-cblistsub.pl

1 comment:

Diego Pino said...

Got to look again at Services, but I think it will be very handy next time I have to code a Service.

I tried on ISecondary.aidl in the from ApiDemos, got broken though. Two pieces of advice:

$f =~ s/\/\*.*?\*\///sg; Add global marker to erase all comments

$m->{arglist} do not need to die if is not set, think of a method which have no arguments.