Removed duplicated code in CarPropertyService.
getPropertySafe can leverage getProperty code to avoid code duplication.
Test: None
Fix: 232999533
Change-Id: Ic238c7f7095614fa8c38759421c78798ab3e5f3d
diff --git a/service/src/com/android/car/CarPropertyService.java b/service/src/com/android/car/CarPropertyService.java
index 58b42d4..ab19ac9 100644
--- a/service/src/com/android/car/CarPropertyService.java
+++ b/service/src/com/android/car/CarPropertyService.java
@@ -23,6 +23,7 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.car.Car;
+import android.car.VehiclePropertyIds;
import android.car.builtin.util.Slogf;
import android.car.hardware.CarPropertyConfig;
import android.car.hardware.CarPropertyValue;
@@ -300,7 +301,7 @@
List<CarPropertyEvent> events = new ArrayList<>();
int propId = config.getPropertyId();
if (config.isGlobalProperty()) {
- CarPropertyValue value = mHal.getPropertySafe(propId, 0);
+ CarPropertyValue value = getPropertySafe(propId, 0);
if (value != null) {
CarPropertyEvent event = new CarPropertyEvent(
CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, value);
@@ -308,7 +309,7 @@
}
} else {
for (int areaId : config.getAreaIds()) {
- CarPropertyValue value = mHal.getPropertySafe(propId, areaId);
+ CarPropertyValue value = getPropertySafe(propId, areaId);
if (value != null) {
CarPropertyEvent event = new CarPropertyEvent(
CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, value);
@@ -489,21 +490,16 @@
/**
* Get property value for car service's internal usage.
*
- * @param prop property id
- * @param zone area id
* @return null if property is not implemented or there is an exception in the vehicle.
*/
- public CarPropertyValue getPropertySafe(int prop, int zone) {
- synchronized (mLock) {
- if (mConfigs.get(prop) == null) {
- // Do not attempt to register an invalid propId
- Slogf.e(TAG, "getPropertySafe: propId is not in config list:0x"
- + toHexString(prop));
- return null;
- }
+ public CarPropertyValue getPropertySafe(int propertyId, int areaId) {
+ try {
+ return getProperty(propertyId, areaId);
+ } catch (Exception e) {
+ Slogf.w(TAG, e, "getPropertySafe() failed for property id: %s area id: 0x%s",
+ VehiclePropertyIds.toString(propertyId), toHexString(areaId));
+ return null;
}
- CarServiceUtils.assertPermission(mContext, mHal.getReadPermission(prop));
- return mHal.getPropertySafe(prop, zone);
}
@Nullable
diff --git a/service/src/com/android/car/hal/PropertyHalService.java b/service/src/com/android/car/hal/PropertyHalService.java
index bfd28ed..0aee7c6 100644
--- a/service/src/com/android/car/hal/PropertyHalService.java
+++ b/service/src/com/android/car/hal/PropertyHalService.java
@@ -191,20 +191,6 @@
}
/**
- * Return property or null if property is not ready yet or there is an exception in HAL.
- */
- @Nullable
- public CarPropertyValue getPropertySafe(int mgrPropId, int areaId) {
- try {
- return getProperty(mgrPropId, areaId);
- } catch (Exception e) {
- Slogf.w(TAG, e, "getProperty() failed for property id: %s area id: 0x%s",
- VehiclePropertyIds.toString(mgrPropId), toHexString(areaId));
- return null;
- }
- }
-
- /**
* Returns sample rate for the property
*/
public float getSampleRate(int mgrPropId) {
diff --git a/tests/carservice_unit_test/src/com/android/car/CarPropertyServiceUnitTest.java b/tests/carservice_unit_test/src/com/android/car/CarPropertyServiceUnitTest.java
index f7588f7..ed1bc5b 100644
--- a/tests/carservice_unit_test/src/com/android/car/CarPropertyServiceUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/CarPropertyServiceUnitTest.java
@@ -111,7 +111,7 @@
// Initially SPEED_ID is not subscribed, so should return -1.
when(mHalService.getSampleRate(SPEED_ID)).thenReturn(-1f);
CarPropertyValue mValue = mock(CarPropertyValue.class);
- when(mHalService.getPropertySafe(SPEED_ID, 0)).thenReturn(mValue);
+ when(mHalService.getProperty(SPEED_ID, 0)).thenReturn(mValue);
// Register the first listener.
mService.registerListener(SPEED_ID, /* rate= */ 10, mMockHandler1);
@@ -120,7 +120,7 @@
verify(mMockHandler1, timeout(5000)).onEvent(any());
verify(mHalService).subscribeProperty(SPEED_ID, 10f);
- verify(mHalService).getPropertySafe(SPEED_ID, 0);
+ verify(mHalService).getProperty(SPEED_ID, 0);
// Clean up invocation state.
clearInvocations(mHalService);
@@ -133,7 +133,7 @@
verify(mMockHandler2, timeout(5000)).onEvent(any());
verify(mHalService).subscribeProperty(SPEED_ID, 20f);
- verify(mHalService).getPropertySafe(SPEED_ID, 0);
+ verify(mHalService).getProperty(SPEED_ID, 0);
// Clean up invocation state.
clearInvocations(mHalService);
@@ -166,7 +166,7 @@
// Initially HVAC_TEMP is not subscribed, so should return -1.
when(mHalService.getSampleRate(HVAC_TEMP)).thenReturn(-1f);
CarPropertyValue mValue = mock(CarPropertyValue.class);
- when(mHalService.getPropertySafe(HVAC_TEMP, 0)).thenReturn(mValue);
+ when(mHalService.getProperty(HVAC_TEMP, 0)).thenReturn(mValue);
// Register the first listener.
mService.registerListener(HVAC_TEMP, /* rate= */ SENSOR_RATE_ONCHANGE, mMockHandler1);
@@ -175,7 +175,7 @@
verify(mMockHandler1, timeout(5000)).onEvent(any());
verify(mHalService).subscribeProperty(HVAC_TEMP, 0f);
- verify(mHalService).getPropertySafe(HVAC_TEMP, 0);
+ verify(mHalService).getProperty(HVAC_TEMP, 0);
// Clean up invocation state.
clearInvocations(mHalService);
@@ -189,7 +189,7 @@
// Must not subscribe again.
verify(mHalService, never()).subscribeProperty(anyInt(), anyFloat());
- verify(mHalService).getPropertySafe(HVAC_TEMP, 0);
+ verify(mHalService).getProperty(HVAC_TEMP, 0);
// Clean up invocation state.
clearInvocations(mHalService);
@@ -260,7 +260,7 @@
// Initially HVAC_TEMP is not subscribed, so should return -1.
when(mHalService.getSampleRate(HVAC_TEMP)).thenReturn(-1f);
CarPropertyValue<Float> value = new CarPropertyValue<Float>(HVAC_TEMP, 0, 1.0f);
- when(mHalService.getPropertySafe(HVAC_TEMP, 0)).thenReturn(value);
+ when(mHalService.getProperty(HVAC_TEMP, 0)).thenReturn(value);
EventListener listener = new EventListener(mService);
mService.registerListener(HVAC_TEMP, /* rate= */ SENSOR_RATE_ONCHANGE, listener);