Add on-property-set listener to VNS

Change-Id: Id0b0fb15f42e1af0ca4899ea625bcc77f1320db7
Fix: b/31656523
diff --git a/libvehiclenetwork/java/src/com/android/car/vehiclenetwork/VehicleNetwork.java b/libvehiclenetwork/java/src/com/android/car/vehiclenetwork/VehicleNetwork.java
index 6724247..471c2c3 100644
--- a/libvehiclenetwork/java/src/com/android/car/vehiclenetwork/VehicleNetwork.java
+++ b/libvehiclenetwork/java/src/com/android/car/vehiclenetwork/VehicleNetwork.java
@@ -20,6 +20,7 @@
 import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toFloatArray;
 import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toIntArray;
 
+import android.annotation.IntDef;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -34,6 +35,8 @@
 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
 import com.android.internal.annotations.GuardedBy;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.lang.ref.WeakReference;
 
 /**
@@ -51,6 +54,7 @@
         void onVehicleNetworkEvents(VehiclePropValues values);
         void onHalError(int errorCode, int property, int operation);
         void onHalRestart(boolean inMocking);
+        void onPropertySet(VehiclePropValue value);
     }
 
     public interface VehicleNetworkHalMock {
@@ -61,6 +65,21 @@
         void onPropertyUnsubscribe(int property);
     }
 
+    /**
+     * Flags to be used in #subscribe(int, float, int, int).
+     */
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef({
+            SubscribeFlags.HAL_EVENT,
+            SubscribeFlags.SET_CALL,
+            SubscribeFlags.DEFAULT,
+    })
+    public @interface SubscribeFlags {
+        int HAL_EVENT = 0x1;
+        int SET_CALL = 0x2;
+        int DEFAULT = HAL_EVENT;
+    }
+
     private static final String TAG = VehicleNetwork.class.getSimpleName();
 
     private final IVehicleNetwork mService;
@@ -422,12 +441,26 @@
     }
 
     /**
-     * Subscribe given property with given sample rate.
+     * Subscribe given property with given sample rate and zones.
      */
     public void subscribe(int property, float sampleRate, int zones)
             throws IllegalArgumentException {
         try {
-            mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones);
+            mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones,
+                    SubscribeFlags.DEFAULT);
+        } catch (RemoteException e) {
+            handleRemoteException(e);
+        }
+    }
+
+    /**
+     * Subscribe given property with given sample rate, zones and flags.
+     */
+    @SuppressWarnings("ResourceType")
+    public void subscribe(int property, float sampleRate, int zones, @SubscribeFlags int flags)
+            throws IllegalArgumentException {
+        try {
+            mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones, flags);
         } catch (RemoteException e) {
             handleRemoteException(e);
         }
@@ -575,11 +608,16 @@
         mListener.onHalRestart(inMocking);
     }
 
+    private void handleOnPropertySet(VehiclePropValue value) {
+        mListener.onPropertySet(value);
+    }
+
     private class EventHandler extends Handler {
 
         private static final int MSG_EVENTS = 0;
         private static final int MSG_HAL_ERROR = 1;
         private static final int MSG_HAL_RESTART = 2;
+        private static final int MSG_ON_PROPERTY_SET = 3;
 
         private EventHandler(Looper looper) {
             super(looper);
@@ -600,6 +638,11 @@
             sendMessage(msg);
         }
 
+        private void notifyPropertySet(VehiclePropValue value) {
+            Message msg = obtainMessage(MSG_ON_PROPERTY_SET, value);
+            sendMessage(msg);
+        }
+
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
@@ -612,6 +655,8 @@
                 case MSG_HAL_RESTART:
                     handleHalRestart(msg.arg1 == 1);
                     break;
+                case MSG_ON_PROPERTY_SET:
+                    handleOnPropertySet((VehiclePropValue) msg.obj);
                 default:
                     Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException());
                     break;
@@ -623,8 +668,8 @@
 
         private final WeakReference<VehicleNetwork> mVehicleNetwork;
 
-        private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
-            mVehicleNetwork = new WeakReference<>(vehicleNewotk);
+        private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNetwork) {
+            mVehicleNetwork = new WeakReference<>(vehicleNetwork);
         }
 
         @Override
@@ -636,6 +681,14 @@
         }
 
         @Override
+        public void onPropertySet(VehiclePropValueParcelable value) {
+            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
+            if (vehicleNetwork != null) {
+                vehicleNetwork.mEventHandler.notifyPropertySet(value.value);
+            }
+        }
+
+        @Override
         public void onHalError(int errorCode, int property, int operation) {
             VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
             if (vehicleNetwork != null) {