Skip to content

Instantly share code, notes, and snippets.

@alsutton
Created May 19, 2022 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsutton/e50979b42eba07efbf2af20e0b39e2a7 to your computer and use it in GitHub Desktop.
Save alsutton/e50979b42eba07efbf2af20e0b39e2a7 to your computer and use it in GitHub Desktop.
Alternative for the mega-switch statement
project packages/services/Car/
diff --git a/car-lib/src/android/car/VehiclePropertyIds.java b/car-lib/src/android/car/VehiclePropertyIds.java
index 450732d56..6062512dc 100644
--- a/car-lib/src/android/car/VehiclePropertyIds.java
+++ b/car-lib/src/android/car/VehiclePropertyIds.java
@@ -20,6 +20,12 @@ import android.annotation.RequiresPermission;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.VehicleElectronicTollCollectionCardStatus;
import android.car.hardware.property.VehicleElectronicTollCollectionCardType;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.GuardedBy;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
/**
* Copy from android.hardware.automotive.vehicle-V2.0-java_gen_java/gen/android/hardware/automotive
@@ -1843,6 +1849,18 @@ public final class VehiclePropertyIds {
@RequiresPermission(Car.PERMISSION_CAR_INFO)
public static final int ELECTRONIC_TOLL_COLLECTION_CARD_STATUS = 289410874;
+ /**
+ * Guard object for initialising intToStringArray
+ */
+
+ private static Object intToStringArrayGuard = new Object();
+
+ /**
+ * Used to translate the integer values into Strings
+ */
+ @GuardedBy("intToStringArrayGuard")
+ private static SparseArray<String> intToStringArray;
+
/**
* @deprecated to prevent others from instantiating this class
*/
@@ -1851,320 +1869,52 @@ public final class VehiclePropertyIds {
}
/**
- * Gets a user-friendly representation of a property.
+ * Gets a String representation of a property given its
+ * int representation.
+ *
+ * @param property The int which represents the property.
*/
public static String toString(int property) {
- switch (property) {
- case INVALID:
- return "INVALID";
- case INFO_VIN:
- return "INFO_VIN";
- case INFO_MAKE:
- return "INFO_MAKE";
- case INFO_MODEL:
- return "INFO_MODEL";
- case INFO_MODEL_YEAR:
- return "INFO_MODEL_YEAR";
- case INFO_FUEL_CAPACITY:
- return "INFO_FUEL_CAPACITY";
- case INFO_FUEL_TYPE:
- return "INFO_FUEL_TYPE";
- case INFO_EV_BATTERY_CAPACITY:
- return "INFO_EV_BATTERY_CAPACITY";
- case INFO_MULTI_EV_PORT_LOCATIONS:
- return "INFO_MULTI_EV_PORT_LOCATIONS";
- case INFO_EV_CONNECTOR_TYPE:
- return "INFO_EV_CONNECTOR_TYPE";
- case INFO_FUEL_DOOR_LOCATION:
- return "INFO_FUEL_DOOR_LOCATION";
- case INFO_EV_PORT_LOCATION:
- return "INFO_EV_PORT_LOCATION";
- case INFO_DRIVER_SEAT:
- return "INFO_DRIVER_SEAT";
- case INFO_EXTERIOR_DIMENSIONS:
- return "INFO_EXTERIOR_DIMENSIONS";
- case PERF_ODOMETER:
- return "PERF_ODOMETER";
- case PERF_VEHICLE_SPEED:
- return "PERF_VEHICLE_SPEED";
- case PERF_VEHICLE_SPEED_DISPLAY:
- return "PERF_VEHICLE_SPEED_DISPLAY";
- case PERF_STEERING_ANGLE:
- return "PERF_STEERING_ANGLE";
- case PERF_REAR_STEERING_ANGLE:
- return "PERF_REAR_STEERING_ANGLE";
- case ENGINE_COOLANT_TEMP:
- return "ENGINE_COOLANT_TEMP";
- case ENGINE_OIL_LEVEL:
- return "ENGINE_OIL_LEVEL";
- case ENGINE_OIL_TEMP:
- return "ENGINE_OIL_TEMP";
- case ENGINE_RPM:
- return "ENGINE_RPM";
- case WHEEL_TICK:
- return "WHEEL_TICK";
- case FUEL_LEVEL:
- return "FUEL_LEVEL";
- case FUEL_DOOR_OPEN:
- return "FUEL_DOOR_OPEN";
- case EV_BATTERY_LEVEL:
- return "EV_BATTERY_LEVEL";
- case EV_CHARGE_PORT_OPEN:
- return "EV_CHARGE_PORT_OPEN";
- case EV_CHARGE_PORT_CONNECTED:
- return "EV_CHARGE_PORT_CONNECTED";
- case EV_BATTERY_INSTANTANEOUS_CHARGE_RATE:
- return "EV_BATTERY_INSTANTANEOUS_CHARGE_RATE";
- case RANGE_REMAINING:
- return "RANGE_REMAINING";
- case TIRE_PRESSURE:
- return "TIRE_PRESSURE";
- case CRITICALLY_LOW_TIRE_PRESSURE:
- return "CRITICALLY_LOW_TIRE_PRESSURE";
- case GEAR_SELECTION:
- return "GEAR_SELECTION";
- case CURRENT_GEAR:
- return "CURRENT_GEAR";
- case PARKING_BRAKE_ON:
- return "PARKING_BRAKE_ON";
- case PARKING_BRAKE_AUTO_APPLY:
- return "PARKING_BRAKE_AUTO_APPLY";
- case FUEL_LEVEL_LOW:
- return "FUEL_LEVEL_LOW";
- case NIGHT_MODE:
- return "NIGHT_MODE";
- case TURN_SIGNAL_STATE:
- return "TURN_SIGNAL_STATE";
- case IGNITION_STATE:
- return "IGNITION_STATE";
- case ABS_ACTIVE:
- return "ABS_ACTIVE";
- case TRACTION_CONTROL_ACTIVE:
- return "TRACTION_CONTROL_ACTIVE";
- case HVAC_FAN_SPEED:
- return "HVAC_FAN_SPEED";
- case HVAC_FAN_DIRECTION:
- return "HVAC_FAN_DIRECTION";
- case HVAC_TEMPERATURE_CURRENT:
- return "HVAC_TEMPERATURE_CURRENT";
- case HVAC_TEMPERATURE_SET:
- return "HVAC_TEMPERATURE_SET";
- case HVAC_TEMPERATURE_VALUE_SUGGESTION:
- return "HVAC_TEMPERATURE_VALUE_SUGGESTION";
- case HVAC_DEFROSTER:
- return "HVAC_DEFROSTER";
- case HVAC_AC_ON:
- return "HVAC_AC_ON";
- case HVAC_MAX_AC_ON:
- return "HVAC_MAX_AC_ON";
- case HVAC_MAX_DEFROST_ON:
- return "HVAC_MAX_DEFROST_ON";
- case HVAC_RECIRC_ON:
- return "HVAC_RECIRC_ON";
- case HVAC_DUAL_ON:
- return "HVAC_DUAL_ON";
- case HVAC_AUTO_ON:
- return "HVAC_AUTO_ON";
- case HVAC_SEAT_TEMPERATURE:
- return "HVAC_SEAT_TEMPERATURE";
- case HVAC_SIDE_MIRROR_HEAT:
- return "HVAC_SIDE_MIRROR_HEAT";
- case HVAC_STEERING_WHEEL_HEAT:
- return "HVAC_STEERING_WHEEL_HEAT";
- case HVAC_TEMPERATURE_DISPLAY_UNITS:
- return "HVAC_TEMPERATURE_DISPLAY_UNITS";
- case HVAC_ACTUAL_FAN_SPEED_RPM:
- return "HVAC_ACTUAL_FAN_SPEED_RPM";
- case HVAC_POWER_ON:
- return "HVAC_POWER_ON";
- case HVAC_FAN_DIRECTION_AVAILABLE:
- return "HVAC_FAN_DIRECTION_AVAILABLE";
- case HVAC_AUTO_RECIRC_ON:
- return "HVAC_AUTO_RECIRC_ON";
- case HVAC_SEAT_VENTILATION:
- return "HVAC_SEAT_VENTILATION";
- case HVAC_ELECTRIC_DEFROSTER_ON:
- return "HVAC_ELECTRIC_DEFROSTER_ON";
- case DISTANCE_DISPLAY_UNITS:
- return "DISTANCE_DISPLAY_UNITS";
- case FUEL_VOLUME_DISPLAY_UNITS:
- return "FUEL_VOLUME_DISPLAY_UNITS";
- case TIRE_PRESSURE_DISPLAY_UNITS:
- return "TIRE_PRESSURE_DISPLAY_UNITS";
- case EV_BATTERY_DISPLAY_UNITS:
- return "EV_BATTERY_DISPLAY_UNITS";
- case FUEL_CONSUMPTION_UNITS_DISTANCE_OVER_VOLUME:
- return "FUEL_CONSUMPTION_UNITS_DISTANCE_OVER_VOLUME";
- case ENV_OUTSIDE_TEMPERATURE:
- return "ENV_OUTSIDE_TEMPERATURE";
- case AP_POWER_STATE_REQ:
- return "AP_POWER_STATE_REQ";
- case AP_POWER_STATE_REPORT:
- return "AP_POWER_STATE_REPORT";
- case AP_POWER_BOOTUP_REASON:
- return "AP_POWER_BOOTUP_REASON";
- case DISPLAY_BRIGHTNESS:
- return "DISPLAY_BRIGHTNESS";
- case HW_KEY_INPUT:
- return "HW_KEY_INPUT";
- case DOOR_POS:
- return "DOOR_POS";
- case DOOR_MOVE:
- return "DOOR_MOVE";
- case DOOR_LOCK:
- return "DOOR_LOCK";
- case MIRROR_Z_POS:
- return "MIRROR_Z_POS";
- case MIRROR_Z_MOVE:
- return "MIRROR_Z_MOVE";
- case MIRROR_Y_POS:
- return "MIRROR_Y_POS";
- case MIRROR_Y_MOVE:
- return "MIRROR_Y_MOVE";
- case MIRROR_LOCK:
- return "MIRROR_LOCK";
- case MIRROR_FOLD:
- return "MIRROR_FOLD";
- case SEAT_MEMORY_SELECT:
- return "SEAT_MEMORY_SELECT";
- case SEAT_MEMORY_SET:
- return "SEAT_MEMORY_SET";
- case SEAT_BELT_BUCKLED:
- return "SEAT_BELT_BUCKLED";
- case SEAT_BELT_HEIGHT_POS:
- return "SEAT_BELT_HEIGHT_POS";
- case SEAT_BELT_HEIGHT_MOVE:
- return "SEAT_BELT_HEIGHT_MOVE";
- case SEAT_FORE_AFT_POS:
- return "SEAT_FORE_AFT_POS";
- case SEAT_FORE_AFT_MOVE:
- return "SEAT_FORE_AFT_MOVE";
- case SEAT_BACKREST_ANGLE_1_POS:
- return "SEAT_BACKREST_ANGLE_1_POS";
- case SEAT_BACKREST_ANGLE_1_MOVE:
- return "SEAT_BACKREST_ANGLE_1_MOVE";
- case SEAT_BACKREST_ANGLE_2_POS:
- return "SEAT_BACKREST_ANGLE_2_POS";
- case SEAT_BACKREST_ANGLE_2_MOVE:
- return "SEAT_BACKREST_ANGLE_2_MOVE";
- case SEAT_HEIGHT_POS:
- return "SEAT_HEIGHT_POS";
- case SEAT_HEIGHT_MOVE:
- return "SEAT_HEIGHT_MOVE";
- case SEAT_DEPTH_POS:
- return "SEAT_DEPTH_POS";
- case SEAT_DEPTH_MOVE:
- return "SEAT_DEPTH_MOVE";
- case SEAT_TILT_POS:
- return "SEAT_TILT_POS";
- case SEAT_TILT_MOVE:
- return "SEAT_TILT_MOVE";
- case SEAT_LUMBAR_FORE_AFT_POS:
- return "SEAT_LUMBAR_FORE_AFT_POS";
- case SEAT_LUMBAR_FORE_AFT_MOVE:
- return "SEAT_LUMBAR_FORE_AFT_MOVE";
- case SEAT_LUMBAR_SIDE_SUPPORT_POS:
- return "SEAT_LUMBAR_SIDE_SUPPORT_POS";
- case SEAT_LUMBAR_SIDE_SUPPORT_MOVE:
- return "SEAT_LUMBAR_SIDE_SUPPORT_MOVE";
- case SEAT_HEADREST_HEIGHT_POS:
- return "SEAT_HEADREST_HEIGHT_POS";
- case SEAT_HEADREST_HEIGHT_MOVE:
- return "SEAT_HEADREST_HEIGHT_MOVE";
- case SEAT_HEADREST_ANGLE_POS:
- return "SEAT_HEADREST_ANGLE_POS";
- case SEAT_HEADREST_ANGLE_MOVE:
- return "SEAT_HEADREST_ANGLE_MOVE";
- case SEAT_HEADREST_FORE_AFT_POS:
- return "SEAT_HEADREST_FORE_AFT_POS";
- case SEAT_HEADREST_FORE_AFT_MOVE:
- return "SEAT_HEADREST_FORE_AFT_MOVE";
- case SEAT_OCCUPANCY:
- return "SEAT_OCCUPANCY";
- case WINDOW_POS:
- return "WINDOW_POS";
- case WINDOW_MOVE:
- return "WINDOW_MOVE";
- case WINDOW_LOCK:
- return "WINDOW_LOCK";
- case VEHICLE_MAP_SERVICE:
- return "VEHICLE_MAP_SERVICE";
- case OBD2_LIVE_FRAME:
- return "OBD2_LIVE_FRAME";
- case OBD2_FREEZE_FRAME:
- return "OBD2_FREEZE_FRAME";
- case OBD2_FREEZE_FRAME_INFO:
- return "OBD2_FREEZE_FRAME_INFO";
- case OBD2_FREEZE_FRAME_CLEAR:
- return "OBD2_FREEZE_FRAME_CLEAR";
- case HEADLIGHTS_STATE:
- return "HEADLIGHTS_STATE";
- case HIGH_BEAM_LIGHTS_STATE:
- return "HIGH_BEAM_LIGHTS_STATE";
- case FOG_LIGHTS_STATE:
- return "FOG_LIGHTS_STATE";
- case HAZARD_LIGHTS_STATE:
- return "HAZARD_LIGHTS_STATE";
- case HEADLIGHTS_SWITCH:
- return "HEADLIGHTS_SWITCH";
- case HIGH_BEAM_LIGHTS_SWITCH:
- return "HIGH_BEAM_LIGHTS_SWITCH";
- case FOG_LIGHTS_SWITCH:
- return "FOG_LIGHTS_SWITCH";
- case HAZARD_LIGHTS_SWITCH:
- return "HAZARD_LIGHTS_SWITCH";
- case CABIN_LIGHTS_STATE:
- return "CABIN_LIGHTS_STATE";
- case CABIN_LIGHTS_SWITCH:
- return "CABIN_LIGHTS_SWITCH";
- case READING_LIGHTS_STATE:
- return "READING_LIGHTS_STATE";
- case READING_LIGHTS_SWITCH:
- return "READING_LIGHTS_SWITCH";
- case VEHICLE_SPEED_DISPLAY_UNITS:
- return "VEHICLE_SPEED_DISPLAY_UNITS";
- case INITIAL_USER_INFO:
- return "INITIAL_USER_INFO";
- case SWITCH_USER:
- return "SWITCH_USER";
- case CREATE_USER:
- return "CREATE_USER";
- case REMOVE_USER:
- return "REMOVE_USER";
- case USER_IDENTIFICATION_ASSOCIATION:
- return "USER_IDENTIFICATION_ASSOCIATION";
- case POWER_POLICY_REQ:
- return "POWER_POLICY_REQ";
- case POWER_POLICY_GROUP_REQ:
- return "POWER_POLICY_GROUP_REQ";
- case CURRENT_POWER_POLICY:
- return "CURRENT_POWER_POLICY";
- case WATCHDOG_ALIVE:
- return "WATCHDOG_ALIVE";
- case WATCHDOG_TERMINATED_PROCESS:
- return "WATCHDOG_TERMINATED_PROCESS";
- case VHAL_HEARTBEAT:
- return "VHAL_HEARTBEAT";
- case CLUSTER_SWITCH_UI:
- return "CLUSTER_SWITCH_UI";
- case CLUSTER_DISPLAY_STATE:
- return "CLUSTER_DISPLAY_STATE";
- case CLUSTER_REPORT_STATE:
- return "CLUSTER_REPORT_STATE";
- case CLUSTER_REQUEST_DISPLAY:
- return "CLUSTER_REQUEST_DISPLAY";
- case CLUSTER_NAVIGATION_STATE:
- return "CLUSTER_NAVIGATION_STATE";
- case EPOCH_TIME:
- return "EPOCH_TIME";
- case STORAGE_ENCRYPTION_BINDING_SEED:
- return "STORAGE_ENCRYPTION_BINDING_SEED";
- case ELECTRONIC_TOLL_COLLECTION_CARD_STATUS:
- return "ELECTRONIC_TOLL_COLLECTION_CARD_STATUS";
- case ELECTRONIC_TOLL_COLLECTION_CARD_TYPE:
- return "ELECTRONIC_TOLL_COLLECTION_CARD_TYPE";
- default:
- return "0x" + Integer.toHexString(property);
+ if (intToStringArray == null) {
+ synchronized (intToStringArrayGuard) {
+ // Double initialisation protection
+ if (intToStringArray == null) {
+ populateStringSparseArray();
+ }
+ }
}
+
+ String value = intToStringArray.get(property);
+ if (value == null) {
+ // Unknown properties have their hex value returned
+ // as a string.
+ return "0x" + Integer.toHexString(property);
+ }
+ return value;
+ }
+
+ /**
+ * Populates the SparseArray with values directly from this class
+ */
+
+ private static void populateStringSparseArray() {
+ Field[] classFields = VehiclePropertyIds.class.getDeclaredFields();
+ SparseArray<String> constuctionArea = new SparseArray(classFields.length);
+ try {
+ for(Field field: classFields) {
+ if(isPropertyField(field)) {
+ constuctionArea.put(field.getInt(null), field.getName());
+ }
+ }
+ intToStringArray = constuctionArea;
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Unable to build SparseArray", e);
+ }
+ }
+
+ private static boolean isPropertyField(Field field) {
+ // We only want public static final int values
+ return field.getType() == int.class
+ && (field.getModifiers() & (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC)) != 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment