Search This Blog

18 February 2012

AIDL Step 2: Creating the Service

malachi@onyx:~/work/apkextensions$ mvn archetype:generate -DarchetypeCatalog=http://repository-malachid.forge.cloudbees.com/public-snapshot/archetype-catalog.xml
[INFO] ------------------------------------------------------------------------
[INFO] Building extension test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: http://repository-malachid.forge.cloudbees.com/public-snapshot/archetype-catalog.xml -> org.eoti.kryten:kryten-archetype (kryten-archetype)
2: http://repository-malachid.forge.cloudbees.com/public-snapshot/archetype-catalog.xml -> org.eoti.galatea:galatea-archetype (galatea-archetype)
3: http://repository-malachid.forge.cloudbees.com/public-snapshot/archetype-catalog.xml -> org.eoti.archtest:archtest-archetype (archtest-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 2
Define value for property 'groupId': : org.eoti.android.test.apkextensions.server
Define value for property 'artifactId': : TheServer
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  org.eoti.android.test.apkextensions.server: :
Confirm properties configuration:
groupId: org.eoti.android.test.apkextensions.server
artifactId: TheServer
version: 1.0-SNAPSHOT
package: org.eoti.android.test.apkextensions.server
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: galatea-archetype:1.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.eoti.android.test.apkextensions.server
[INFO] Parameter: artifactId, Value: TheServer
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: org.eoti.android.test.apkextensions.server
[INFO] Parameter: packageInPathFormat, Value: org/eoti/android/test/apkextensions/server
[INFO] Parameter: package, Value: org.eoti.android.test.apkextensions.server
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: org.eoti.android.test.apkextensions.server
[INFO] Parameter: artifactId, Value: TheServer
[WARNING] Don't override file /home/malachi/work/apkextensions/TheServer/src/main/android/res/values/strings.xml
[WARNING] Don't override file /home/malachi/work/apkextensions/TheServer/src/main/android/res/layout/main.xml
[INFO] project created from Archetype in dir: /home/malachi/work/apkextensions/TheServer
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.380s
[INFO] Finished at: Sat Feb 18 07:13:45 PST 2012
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------


In TheServer/pom.xml make whatever keysigning changes you need (or remove the keysigning as we did in the previous step)
and then add the following dependency:
        <dependency>
            <groupId>org.eoti.android.test.apkextensions.api</groupId>
            <artifactId>API</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>apklib</type>
        </dependency>

Create TheServer/src/main/java/org/eoti/android/test/apkextensions/server/TheServer.java
package org.eoti.android.test.apkextensions.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import org.eoti.android.test.apkextensions.api.v1.IServer;
import org.eoti.android.test.apkextensions.api.v1.Registration;

import java.util.concurrent.ConcurrentHashMap;

public class TheServer extends Service
{
    private static final String TAG = TheServer.class.getSimpleName();
    private ConcurrentHashMap<String, IBinder> binders = new ConcurrentHashMap<String, IBinder>();
    private ConcurrentHashMap<String, Registration> registrations = new ConcurrentHashMap<String, Registration>();

    private IBinder createV1Binder()
    {
        return new IServer.Stub(){
            @Override
            public void register(Registration registration) throws RemoteException {
                if(registration == null) throw new RemoteException(); // API15 required for 'new RemoteException(string)'
                registrations.put(registration.getName(), registration);
                Log.d(TAG, "Registration received:  " + registration.getName());
            }

            @Override
            public void unregister(String registrationName) throws RemoteException {
                if(registrationName == null)  throw new RemoteException(); // API15 required for 'new RemoteException(string)'
                registrations.remove(registrationName);
                Log.d(TAG, "Registration removed: " + registrationName);            }
        };
    }
  
    public IBinder onBind(Intent intent) {
        if(intent == null) return null;
        String action = intent.getAction();
        if(action == null) return null;
        IBinder binder = binders.get(action);
        if(binder != null)
            return binder;
      
        if(IServer.class.getName().equals(action))
            binder = createV1Binder();
      
        if(binder != null)
            binders.put(action, binder);
      
        return binder;
    }

    @Override
    public void onDestroy() {
        registrations.clear();
        super.onDestroy();
    }
}


in TheServer/src/AndroidManifest.xml
replace:
<service android:name=".TheServer"/>
with: 
       <service android:name=".TheServer">
            <intent-filter>
                <action android:name="org.eoti.android.test.apkextensions.api.v1.IServer"/>
            </intent-filter>
        </service>


Make sure it compiles...
malachi@onyx:~/work/apkextensions$ cd TheServer
malachi@onyx:~/work/apkextensions/TheServer$ mvn clean install

Next up, AIDL Step 3: Creating the Client

No comments:

Post a Comment