Search This Blog

18 February 2012

AIDL Step 4: Upgrading the API

Let's create a new API/src/main/java/org/eoti/android/test/apkextensions/api/v2/Registration.java
package org.eoti.android.test.apkextensions.api.v2;

import android.os.Parcel;
import android.os.Parcelable;

public class Registration
implements Parcelable
{
    private String name = "unset";
    private String version = "unset";

    public static final Creator<Registration> CREATOR = new Creator<Registration>()
    {
        @Override
        public Registration createFromParcel(Parcel parcel) {
            return new Registration(parcel);
        }

        @Override
        public Registration[] newArray(int size) {
            return new Registration[size];
        }
    };

    public Registration()
    {

    }

    public Registration(Parcel in)
    {
        readFromParcel(in);
    }

    public Registration(org.eoti.android.test.apkextensions.api.v1.Registration deprecated)
    {
        this();
        this.name = deprecated.getName();
    }

    public void setName(String name){this.name = name;}
    public String getName(){return name;}
    public void setVersion(String version){this.version = version;}
    public String getVersion(){return version;}

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(getName());
        out.writeString(getVersion());
    }

    public void readFromParcel(Parcel in)
    {
        setName(in.readString());
        setVersion(in.readString());
    }
}


You might be wondering why we don't extend the previous class.  It's because of the final Creator method.  If you try to send a v2 registration across the previous AIDL, it will send it as a v1.  Using a base class that they both extend would be doable - but I decided to keep the example simple.

Create a new API/src/main/java/org/eoti/android/test/apkextensions/api/v2/Registration.aidl
package org.eoti.android.test.apkextensions.api.v2;

parcelable Registration;


And let's create our new server API that contains some extra functionality...
API/src/main/java/org/eoti/android/test/apkextensions/api/v2/IServer.aidl
package org.eoti.android.test.apkextensions.api.v2;

import org.eoti.android.test.apkextensions.api.v2.Registration;

interface IServer
{
    void register(inout Registration registration);
    void unregister(in String registrationName);
    String getServerIdentifier();
}


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

Next up, AIDL Step 5: Upgrading the Service

No comments:

Post a Comment