1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.car; 18 19 import static java.lang.Integer.toHexString; 20 21 import android.annotation.Nullable; 22 import android.car.annotation.ValueTypeDef; 23 import android.car.hardware.CarPropertyValue; 24 import android.car.hardware.property.ICarProperty; 25 import android.os.Bundle; 26 import android.os.IBinder; 27 import android.os.RemoteException; 28 import android.util.Log; 29 30 31 /** 32 * Utility to retrieve various static information from car. Each data are grouped as {@link Bundle} 33 * and relevant data can be checked from {@link Bundle} using pre-specified keys. 34 */ 35 public final class CarInfoManager implements CarManagerBase{ 36 37 private static final boolean DBG = false; 38 private static final String TAG = "CarInfoManager"; 39 /** 40 * Key for manufacturer of the car. Passed in basic info Bundle. 41 * @hide 42 */ 43 @ValueTypeDef(type = Integer.class) 44 public static final int BASIC_INFO_KEY_MANUFACTURER = 0x11100101; 45 /** 46 * Key for model name of the car. This information may not necessarily allow distinguishing 47 * different car models as the same name may be used for different cars depending on 48 * manufacturers. Passed in basic info Bundle. 49 * @hide 50 */ 51 @ValueTypeDef(type = Integer.class) 52 public static final int BASIC_INFO_KEY_MODEL = 0x11100102; 53 /** 54 * Key for model year of the car in AC. Passed in basic info Bundle. 55 * @hide 56 */ 57 @ValueTypeDef(type = Integer.class) 58 public static final int BASIC_INFO_KEY_MODEL_YEAR = 0x11400103; 59 /** 60 * Key for unique identifier for the car. This is not VIN, and id is persistent until user 61 * resets it. Passed in basic info Bundle. 62 * @hide 63 */ 64 @ValueTypeDef(type = String.class) 65 public static final String BASIC_INFO_KEY_VEHICLE_ID = "android.car.vehicle-id"; 66 67 /** 68 * Key for product configuration info. 69 * @FutureFeature Cannot drop due to usage in non-flag protected place. 70 * @hide 71 */ 72 @ValueTypeDef(type = String.class) 73 public static final String INFO_KEY_PRODUCT_CONFIGURATION = "android.car.product-config"; 74 75 /* TODO bug: 32059999 76 //@ValueTypeDef(type = Integer.class) 77 //public static final String KEY_DRIVER_POSITION = "driver-position"; 78 79 //@ValueTypeDef(type = int[].class) 80 //public static final String KEY_SEAT_CONFIGURATION = "seat-configuration"; 81 82 //@ValueTypeDef(type = Integer.class) 83 //public static final String KEY_WINDOW_CONFIGURATION = "window-configuration"; 84 85 //MT, AT, CVT, ... 86 //@ValueTypeDef(type = Integer.class) 87 //public static final String KEY_TRANSMISSION_TYPE = "transmission-type"; 88 89 // add: transmission gear available selection, gear available steps 90 // drive wheel: FWD, RWD, AWD, 4WD */ 91 /** 92 * Key for Fuel Capacity in milliliters. Passed in basic info Bundle. 93 * @hide 94 */ 95 @ValueTypeDef(type = Integer.class) 96 public static final int BASIC_INFO_FUEL_CAPACITY = 0x11600104; 97 /** 98 * Key for Fuel Types. This is an array of fuel types the vehicle supports. 99 * Passed in basic info Bundle. 100 * @hide 101 */ 102 @ValueTypeDef(type = Integer.class) 103 public static final int BASIC_INFO_FUEL_TYPES = 0x11410105; 104 /** 105 * Key for EV Battery Capacity in WH. Passed in basic info Bundle. 106 * @hide 107 */ 108 @ValueTypeDef(type = Integer.class) 109 public static final int BASIC_INFO_EV_BATTERY_CAPACITY = 0x11600106; 110 /** 111 * Key for EV Connector Types. This is an array of connector types the vehicle supports. 112 * Passed in basic info Bundle. 113 * @hide 114 */ 115 @ValueTypeDef(type = Integer.class) 116 public static final int BASIC_INFO_EV_CONNECTOR_TYPES = 0x11410107; 117 118 private final ICarProperty mService; 119 120 /** 121 * @return Manufacturer of the car. Null if not available. 122 */ 123 @Nullable 124 public String getManufacturer() throws CarNotConnectedException { 125 CarPropertyValue<String> carProp = getProperty(String.class, 126 BASIC_INFO_KEY_MANUFACTURER, 0); 127 return carProp != null ? carProp.getValue() : null; 128 } 129 130 /** 131 * @return Model name of the car, null if not available. This information 132 * may not necessarily allow distinguishing different car models as the same 133 * name may be used for different cars depending on manufacturers. 134 */ 135 @Nullable 136 public String getModel() throws CarNotConnectedException { 137 CarPropertyValue<String> carProp = getProperty(String.class, BASIC_INFO_KEY_MODEL, 0); 138 return carProp != null ? carProp.getValue() : null; 139 } 140 141 /** 142 * @return Model year of the car in AC. Null if not available. 143 */ 144 @Nullable 145 public String getModelYear() throws CarNotConnectedException { 146 CarPropertyValue<String> carProp = getProperty(String.class, 147 BASIC_INFO_KEY_MODEL_YEAR, 0); 148 return carProp != null ? carProp.getValue() : null; 149 } 150 151 /** 152 * @return Unique identifier for the car. This is not VIN, and vehicle id is 153 * persistent until user resets it. This ID is guaranteed to be always 154 * available. 155 * TODO: BASIC_INFO_KEY_VEHICLE_ID property? 156 */ 157 public String getVehicleId() throws CarNotConnectedException { 158 return ""; 159 } 160 161 /** 162 * @return Fuel capacity of the car in milliliters. 0 if car doesn't run on 163 * fuel. 164 */ 165 public float getFuelCapacity() throws CarNotConnectedException { 166 CarPropertyValue<Float> carProp = getProperty(Float.class, 167 BASIC_INFO_FUEL_CAPACITY, 0); 168 return carProp != null ? carProp.getValue() : 0f; 169 } 170 171 /** 172 * @return Array of FUEL_TYPEs available in the car. Empty array if no fuel 173 * types available. 174 */ 175 public @FuelType.Enum int[] getFuelTypes() throws CarNotConnectedException { 176 CarPropertyValue<int[]> carProp = getProperty(int[].class, BASIC_INFO_FUEL_TYPES, 0); 177 return carProp != null ? carProp.getValue() : new int[0]; 178 } 179 180 /** 181 * @return Battery capacity of the car in WH. 0 if car doesn't run on 182 * battery. 183 */ 184 public float getEvBatteryCapacity() throws CarNotConnectedException { 185 CarPropertyValue<Float> carProp = getProperty(Float.class, 186 BASIC_INFO_EV_BATTERY_CAPACITY, 0); 187 return carProp != null ? carProp.getValue() : 0f; 188 } 189 190 /** 191 * @return Array of EV_CONNECTOR_TYPEs available in the car. Empty array if 192 * no connector types available. 193 */ 194 public @EvConnectorType.Enum int[] getEvConnectorTypes() throws CarNotConnectedException { 195 CarPropertyValue<int[]> carProp = getProperty(int[].class, 196 BASIC_INFO_EV_CONNECTOR_TYPES, 0); 197 return carProp != null ? carProp.getValue() : new int[0]; 198 } 199 200 /** @hide */ 201 CarInfoManager(IBinder service) { 202 mService = ICarProperty.Stub.asInterface(service); 203 } 204 205 /** @hide */ 206 public void onCarDisconnected() { 207 } 208 209 private <E> CarPropertyValue<E> getProperty(Class<E> clazz, int propId, int area) 210 throws CarNotConnectedException { 211 if (DBG) { 212 Log.d(TAG, "getProperty, propId: 0x" + toHexString(propId) 213 + ", area: 0x" + toHexString(area) + ", class: " + clazz); 214 } 215 try { 216 CarPropertyValue<E> propVal = mService.getProperty(propId, area); 217 if (propVal != null && propVal.getValue() != null) { 218 Class<?> actualClass = propVal.getValue().getClass(); 219 if (actualClass != clazz) { 220 throw new IllegalArgumentException("Invalid property type. " + "Expected: " 221 + clazz + ", but was: " + actualClass); 222 } 223 } 224 return propVal; 225 } catch (RemoteException e) { 226 Log.e(TAG, "getProperty failed with " + e.toString() 227 + ", propId: 0x" + toHexString(propId) + ", area: 0x" + toHexString(area), e); 228 throw new CarNotConnectedException(e); 229 } catch (IllegalArgumentException e) { 230 return null; 231 } 232 } 233 } 234