package com.openxc;

import com.openxc.measurements.Measurement;
import com.openxc.measurements.VehicleMeasurement;

import com.openxc.remote.RemoteVehicleService;
import com.openxc.remote.RemoteVehicleServiceInterface;

import com.openxc.units.Unit;

import android.content.Context;
import android.app.Service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

import android.os.Binder;
import android.os.IBinder;

public class VehicleService extends Service {
    private IBinder mBinder = new VehicleServiceBinder();
    private RemoteVehicleServiceInterface mRemoteService;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mRemoteService = RemoteVehicleServiceInterface.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName className) {
            mRemoteService = null;
        }
    };

    public class VehicleServiceBinder extends Binder {
        VehicleService getService() {
            return VehicleService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        bindService(new Intent(RemoteVehicleService.class.getName()),
                mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public Measurement<Unit> get(Class<VehicleMeasurement> measurementType) {
        return new Measurement<Unit>(mRemoteService.getNumericalMeasurement((String)(
                    measurementType.getField("ID").get(measurementType))));
    }

    public void addListener(VehicleMeasurement.Listener listener) {
    }
}

@@ -14,16 +14,107 @@
 penxc;%0A%0A
+import java.lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetException;%0A%0A
 import c
@@ -145,24 +145,24 @@
 easurement;%0A
-
 import com.o
@@ -561,16 +561,51 @@
 IBinder;
+%0Aimport android.os.RemoteException;
 %0A%0Apublic
@@ -1698,16 +1698,23 @@
  public 
+Vehicle
 Measurem
@@ -1716,22 +1716,16 @@
 surement
-%3CUnit%3E
  get(Cla
@@ -1767,172 +1767,1212 @@
 ype)
- %7B%0A        return new Measurement%3CUnit%3E(mRemoteService.getNumericalMeasurement((String)(%0A                    measurementType.getField(%22ID%22).get(measurementType))));
+%0A            throws RemoteException %7B%0A        String measurementId;%0A        try %7B%0A            measurementId = (String)(%0A                    measurementType.getField(%22ID%22).get(measurementType));%0A        %7D catch(NoSuchFieldException e) %7B%0A            return null;%0A        %7D catch(IllegalAccessException e) %7B%0A            return null;%0A        %7D%0A%0A        Constructor%3CVehicleMeasurement%3E constructor;%0A        try %7B%0A            constructor = measurementType.getConstructor(Double.class);%0A            return constructor.newInstance(%0A                    mRemoteService.getNumericalMeasurement(measurementId));%0A        %7D catch(NoSuchMethodException e) %7B%0A        %7D catch(InstantiationException e) %7B%0A        %7D catch(IllegalAccessException e) %7B%0A        %7D catch(InvocationTargetException e) %7B%0A        %7D%0A%0A        try %7B%0A            constructor = measurementType.getConstructor(String.class);%0A            return constructor.newInstance(mRemoteService.getStateMeasurement(%0A                        measurementId));%0A        %7D catch(NoSuchMethodException e) %7B%0A        %7D catch(InstantiationException e) %7B%0A        %7D catch(IllegalAccessException e) %7B%0A        %7D catch(InvocationTargetException e) %7B%0A        %7D%0A        return null;%0A
 %0A   

Use reflection to figure out which type of measurement to construct.