1 /* 2 * Copyright (C) 2016 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.hardware.cabin; 18 19 import android.annotation.IntDef; 20 import android.annotation.SystemApi; 21 import android.car.Car; 22 import android.car.CarManagerBase; 23 import android.car.CarNotConnectedException; 24 import android.car.hardware.CarPropertyConfig; 25 import android.car.hardware.CarPropertyValue; 26 import android.car.hardware.property.CarPropertyManagerBase; 27 import android.car.hardware.property.CarPropertyManagerBase.CarPropertyEventCallback; 28 import android.content.Context; 29 import android.os.Handler; 30 import android.os.IBinder; 31 import android.os.Looper; 32 import android.util.ArraySet; 33 34 import java.lang.annotation.Retention; 35 import java.lang.annotation.RetentionPolicy; 36 import java.lang.ref.WeakReference; 37 import java.util.Collection; 38 import java.util.List; 39 40 /** 41 * API for controlling Cabin system in cars. 42 * Most Car Cabin properties have both a MOVE and POSITION parameter associated with them. 43 * 44 * The MOVE parameter will start moving the device in the indicated direction. Magnitude 45 * indicates relative speed. For instance, setting the WINDOW_MOVE parameter to +1 rolls 46 * the window up. Setting it to +2 (if available) will roll it up faster. 47 * 48 * POSITION parameter will move the device to the desired position. For instance, if the 49 * WINDOW_POS has a range of 0-100, setting this parameter to 50 will open the window 50 * halfway. Depending upon the initial position, the window may move up or down to the 51 * 50% value. 52 * 53 * One or both of the MOVE/POSITION parameters may be implemented depending upon the 54 * capability of the hardware. 55 * @hide 56 */ 57 @SystemApi 58 public final class CarCabinManager implements CarManagerBase { 59 private final static boolean DBG = false; 60 private final static String TAG = "CarCabinManager"; 61 private final CarPropertyManagerBase mMgr; 62 private final ArraySet<CarCabinEventCallback> mCallbacks = new ArraySet<>(); 63 private CarPropertyEventListenerToBase mListenerToBase = null; 64 65 /** Door properties are zoned by VehicleDoor */ 66 /** 67 * door position, int type 68 * Max value indicates fully open, min value (0) indicates fully closed. 69 * 70 * Some vehicles (minivans) can open the door electronically. Hence, the ability 71 * to write this property. 72 */ 73 public static final int ID_DOOR_POS = 0x0001; 74 /** door move, int type 75 * Positive values open the door, negative values close it. 76 */ 77 public static final int ID_DOOR_MOVE = 0x0002; 78 /** door lock, bool type 79 * 'true' indicates door is locked. 80 */ 81 public static final int ID_DOOR_LOCK = 0x0003; 82 83 /** Mirror properties are zoned by VehicleMirror */ 84 /** 85 * mirror z position, int type 86 * Positive value indicates tilt upwards, negative value tilt downwards. 87 */ 88 public static final int ID_MIRROR_Z_POS = 0x1001; 89 /** mirror z move, int type 90 * Positive value tilts the mirror upwards, negative value tilts downwards. 91 */ 92 public static final int ID_MIRROR_Z_MOVE = 0x1002; 93 /** 94 * mirror y position, int type 95 * Positive value indicates tilt right, negative value tilt left 96 */ 97 public static final int ID_MIRROR_Y_POS = 0x1003; 98 /** mirror y move, int type 99 * Positive value tilts the mirror right, negative value tilts left. 100 */ 101 public static final int ID_MIRROR_Y_MOVE = 0x1004; 102 /** 103 * mirror lock, bool type 104 * True indicates mirror positions are locked and not changeable. 105 */ 106 public static final int ID_MIRROR_LOCK = 0x1005; 107 /** 108 * mirror fold, bool type 109 * True indicates mirrors are folded. 110 */ 111 public static final int ID_MIRROR_FOLD = 0x1006; 112 113 /** Seat properties are zoned by VehicleSeat */ 114 /** 115 * seat memory select, int type 116 * This parameter selects the memory preset to use to select the seat position. 117 * The minValue is always 1, and the maxValue determines the number of seat 118 * positions available. 119 * 120 * For instance, if the driver's seat has 3 memory presets, the maxValue will be 3. 121 * When the user wants to select a preset, the desired preset number (1, 2, or 3) 122 * is set. 123 */ 124 public static final int ID_SEAT_MEMORY_SELECT = 0x2001; 125 /** 126 * seat memory set, int type 127 * This setting allows the user to save the current seat position settings into 128 * the selected preset slot. The maxValue for each seat position shall match 129 * the maxValue for VEHICLE_PROPERTY_SEAT_MEMORY_SELECT. 130 */ 131 public static final int ID_SEAT_MEMORY_SET = 0x2002; 132 /** 133 * seat belt buckled, bool type 134 * True indicates belt is buckled. 135 */ 136 public static final int ID_SEAT_BELT_BUCKLED = 0x2003; 137 /** 138 * seat belt height position, int type 139 * Adjusts the shoulder belt anchor point. 140 * Max value indicates highest position. 141 * Min value indicates lowest position. 142 */ 143 public static final int ID_SEAT_BELT_HEIGHT_POS = 0x2004; 144 /** seat belt height move, int type 145 * Adjusts the shoulder belt anchor point. 146 * Positive value moves towards highest point. 147 * Negative value moves towards lowest point. 148 */ 149 public static final int ID_SEAT_BELT_HEIGHT_MOVE = 0x2005; 150 /** 151 * seat fore/aft position, int type 152 * Sets the seat position forward (closer to steering wheel) and backwards. 153 * Max value indicates closest to wheel, min value indicates most rearward position. 154 */ 155 public static final int ID_SEAT_FORE_AFT_POS = 0x2006; 156 /** 157 * seat fore/aft move, int type 158 * Positive value moves seat forward (closer to steering wheel). 159 * Negative value moves seat rearward. 160 */ 161 public static final int ID_SEAT_FORE_AFT_MOVE = 0x2007; 162 /** 163 * seat backrest angle #1 position, int type 164 * Backrest angle 1 is the actuator closest to the bottom of the seat. 165 * Max value indicates angling forward towards the steering wheel. 166 * Min value indicates full recline. 167 */ 168 public static final int ID_SEAT_BACKREST_ANGLE_1_POS = 0x2008; 169 /** seat backrest angle #1 move, int type 170 * Backrest angle 1 is the actuator closest to the bottom of the seat. 171 * Positive value angles seat towards the steering wheel. 172 * Negatie value angles away from steering wheel. 173 */ 174 public static final int ID_SEAT_BACKREST_ANGLE_1_MOVE = 0x2009; 175 /** 176 * seat backrest angle #2 position, int type 177 * Backrest angle 2 is the next actuator up from the bottom of the seat. 178 * Max value indicates angling forward towards the steering wheel. 179 * Min value indicates full recline. 180 */ 181 public static final int ID_SEAT_BACKREST_ANGLE_2_POS = 0x200A; 182 /** seat backrest angle #2 move, int type 183 * Backrest angle 2 is the next actuator up from the bottom of the seat. 184 * Positive value tilts forward towards the steering wheel. 185 * Negative value tilts backwards. 186 */ 187 public static final int ID_SEAT_BACKREST_ANGLE_2_MOVE = 0x200B; 188 /** 189 * seat height position, int type 190 * Sets the seat height. 191 * Max value indicates highest position. 192 * Min value indicates lowest position. 193 */ 194 public static final int ID_SEAT_HEIGHT_POS = 0x200C; 195 /** seat height move, int type 196 * Sets the seat height. 197 * Positive value raises the seat. 198 * Negative value lowers the seat. 199 * */ 200 public static final int ID_SEAT_HEIGHT_MOVE = 0x200D; 201 /** 202 * seat depth position, int type 203 * Sets the seat depth, distance from back rest to front edge of seat. 204 * Max value indicates longest depth position. 205 * Min value indicates shortest position. 206 */ 207 public static final int ID_SEAT_DEPTH_POS = 0x200E; 208 /** seat depth move, int type 209 * Adjusts the seat depth, distance from back rest to front edge of seat. 210 * Positive value increases the distance from back rest to front edge of seat. 211 * Negative value decreases this distance. 212 */ 213 public static final int ID_SEAT_DEPTH_MOVE = 0x200F; 214 /** 215 * seat tilt position, int type 216 * Sets the seat tilt. 217 * Max value indicates front edge of seat higher than back edge. 218 * Min value indicates front edge of seat lower than back edge. 219 */ 220 public static final int ID_SEAT_TILT_POS = 0x2010; 221 /** seat tilt move, int type 222 * Adjusts the seat tilt. 223 * Positive value lifts front edge of seat higher than back edge. 224 * Negative value lowers front edge of seat in relation to back edge. 225 */ 226 public static final int ID_SEAT_TILT_MOVE = 0x2011; 227 /** 228 * seat lumbar fore/aft position, int type 229 * Pushes the lumbar support forward and backwards. 230 * Max value indicates most forward position. 231 * Min value indicates most rearward position. 232 */ 233 public static final int ID_SEAT_LUMBAR_FORE_AFT_POS = 0x2012; 234 /** seat lumbar fore/aft move, int type 235 * Adjusts the lumbar support forwards and backwards. 236 * Positive value moves lumbar support forward. 237 * Negative value moves lumbar support rearward. 238 */ 239 public static final int ID_SEAT_LUMBAR_FORE_AFT_MOVE = 0x2013; 240 /** 241 * seat lumbar side support position, int type 242 * Sets the amount of lateral lumbar support. 243 * Max value indicates widest lumbar setting (i.e. least support) 244 * Min value indicates thinnest lumbar setting. 245 */ 246 public static final int ID_SEAT_LUMBAR_SIDE_SUPPORT_POS = 0x2014; 247 /** seat lumbar side support move, int type 248 * Adjusts the amount of lateral lumbar support. 249 * Positive value widens the lumbar area. 250 * Negative value makes the lumbar area thinner. 251 */ 252 public static final int ID_SEAT_LUMBAR_SIDE_SUPPORT_MOVE = 0x2015; 253 /** 254 * seat headrest height position, int type 255 * Sets the headrest height. 256 * Max value indicates tallest setting. 257 * Min value indicates shortest setting. 258 */ 259 public static final int ID_SEAT_HEADREST_HEIGHT_POS = 0x2016; 260 /** seat headrest height move, int type 261 * Postive value moves the headrest higher. 262 * Negative value moves the headrest lower. 263 */ 264 public static final int ID_SEAT_HEADREST_HEIGHT_MOVE = 0x2017; 265 /** 266 * seat headrest angle position, int type 267 * Sets the angle of the headrest. 268 * Max value indicates most upright angle. 269 * Min value indicates shallowest headrest angle. 270 */ 271 public static final int ID_SEAT_HEADREST_ANGLE_POS = 0x2018; 272 /** seat headrest angle move, int type 273 * Adjusts the angle of the headrest. 274 * Positive value angles headrest towards most upright angle. 275 * Negative value angles headrest towards shallowest headrest angle. 276 */ 277 public static final int ID_SEAT_HEADREST_ANGLE_MOVE = 0x2019; 278 /** 279 * seat headrest fore/aft position, int type 280 * Sets the headrest forwards and backwards. 281 * Max value indicates position closest to front of car. 282 * Min value indicates position closest to rear of car. 283 */ 284 public static final int ID_SEAT_HEADREST_FORE_AFT_POS = 0x201A; 285 /** seat headrest fore/aft move, int type 286 * Adjsuts the headrest forwards and backwards. 287 * Positive value moves the headrest closer to front of car. 288 * Negative value moves the headrest closer to rear of car. 289 */ 290 public static final int ID_SEAT_HEADREST_FORE_AFT_MOVE = 0x201B; 291 292 /** Window properties are zoned by VehicleWindow */ 293 /** 294 * window position, int type 295 * Max = window up / closed. 296 * Min = window down / open. 297 */ 298 public static final int ID_WINDOW_POS = 0x3001; 299 /** window move, int type 300 * Positive value moves window up / closes window. 301 * Negative value moves window down / opens window. 302 */ 303 public static final int ID_WINDOW_MOVE = 0x3002; 304 /** 305 * window vent position, int type 306 * This feature is used to control the vent feature on a sunroof. 307 * Max = vent open. 308 * Min = vent closed. 309 */ 310 public static final int ID_WINDOW_VENT_POS = 0x3003; 311 /** window vent move, int type 312 * This feature is used to control the vent feature on a sunroof. 313 * Positive value opens the vent. 314 * Negative value closes the vent. 315 */ 316 public static final int ID_WINDOW_VENT_MOVE = 0x3004; 317 /** 318 * window lock, bool type 319 * True indicates windows are locked and can't be moved. 320 */ 321 public static final int ID_WINDOW_LOCK = 0x3005; 322 323 /** @hide */ 324 @IntDef({ 325 ID_DOOR_POS, 326 ID_DOOR_MOVE, 327 ID_DOOR_LOCK, 328 ID_MIRROR_Z_POS, 329 ID_MIRROR_Z_MOVE, 330 ID_MIRROR_Y_POS, 331 ID_MIRROR_Y_MOVE, 332 ID_MIRROR_LOCK, 333 ID_MIRROR_FOLD, 334 ID_SEAT_MEMORY_SELECT, 335 ID_SEAT_MEMORY_SET, 336 ID_SEAT_BELT_BUCKLED, 337 ID_SEAT_BELT_HEIGHT_POS, 338 ID_SEAT_BELT_HEIGHT_MOVE, 339 ID_SEAT_FORE_AFT_POS, 340 ID_SEAT_FORE_AFT_MOVE, 341 ID_SEAT_BACKREST_ANGLE_1_POS, 342 ID_SEAT_BACKREST_ANGLE_1_MOVE, 343 ID_SEAT_BACKREST_ANGLE_2_POS, 344 ID_SEAT_BACKREST_ANGLE_2_MOVE, 345 ID_SEAT_HEIGHT_POS, 346 ID_SEAT_HEIGHT_MOVE, 347 ID_SEAT_DEPTH_POS, 348 ID_SEAT_DEPTH_MOVE, 349 ID_SEAT_TILT_POS, 350 ID_SEAT_TILT_MOVE, 351 ID_SEAT_LUMBAR_FORE_AFT_POS, 352 ID_SEAT_LUMBAR_FORE_AFT_MOVE, 353 ID_SEAT_LUMBAR_SIDE_SUPPORT_POS, 354 ID_SEAT_LUMBAR_SIDE_SUPPORT_MOVE, 355 ID_SEAT_HEADREST_HEIGHT_POS, 356 ID_SEAT_HEADREST_HEIGHT_MOVE, 357 ID_SEAT_HEADREST_ANGLE_POS, 358 ID_SEAT_HEADREST_ANGLE_MOVE, 359 ID_SEAT_HEADREST_FORE_AFT_POS, 360 ID_SEAT_HEADREST_FORE_AFT_MOVE, 361 ID_WINDOW_POS, 362 ID_WINDOW_MOVE, 363 ID_WINDOW_VENT_POS, 364 ID_WINDOW_VENT_MOVE, 365 ID_WINDOW_LOCK 366 }) 367 @Retention(RetentionPolicy.SOURCE) 368 public @interface PropertyId {} 369 370 /** 371 * Application registers CarCabinEventCallback object to receive updates and changes to 372 * subscribed Car Cabin properties. 373 */ 374 public interface CarCabinEventCallback { 375 /** 376 * Called when a property is updated 377 * @param value Property that has been updated. 378 */ 379 void onChangeEvent(CarPropertyValue value); 380 381 /** 382 * Called when an error is detected with a property 383 * @param propertyId 384 * @param zone 385 */ 386 void onErrorEvent(@PropertyId int propertyId, int zone); 387 } 388 389 private static class CarPropertyEventListenerToBase implements CarPropertyEventCallback { 390 private final WeakReference<CarCabinManager> mManager; 391 392 public CarPropertyEventListenerToBase(CarCabinManager manager) { 393 mManager = new WeakReference<>(manager); 394 } 395 396 @Override 397 public void onChangeEvent(CarPropertyValue value) { 398 CarCabinManager manager = mManager.get(); 399 if (manager != null) { 400 manager.handleOnChangeEvent(value); 401 } 402 } 403 404 @Override 405 public void onErrorEvent(int propertyId, int zone) { 406 CarCabinManager manager = mManager.get(); 407 if (manager != null) { 408 manager.handleOnErrorEvent(propertyId, zone); 409 } 410 } 411 } 412 413 private void handleOnChangeEvent(CarPropertyValue value) { 414 Collection<CarCabinEventCallback> callbacks; 415 synchronized (this) { 416 callbacks = new ArraySet<>(mCallbacks); 417 } 418 for (CarCabinEventCallback l: callbacks) { 419 l.onChangeEvent(value); 420 } 421 } 422 423 private void handleOnErrorEvent(int propertyId, int zone) { 424 Collection<CarCabinEventCallback> listeners; 425 synchronized (this) { 426 listeners = new ArraySet<>(mCallbacks); 427 } 428 if (!listeners.isEmpty()) { 429 for (CarCabinEventCallback l: listeners) { 430 l.onErrorEvent(propertyId, zone); 431 } 432 } 433 } 434 435 /** 436 * Get an instance of CarCabinManager 437 * 438 * Should not be obtained directly by clients, use {@link Car#getCarManager(String)} instead. 439 * @param service 440 * @param context 441 * @param handler 442 * @hide 443 */ 444 public CarCabinManager(IBinder service, Context context, Handler handler) { 445 mMgr = new CarPropertyManagerBase(service, handler, DBG, TAG); 446 } 447 448 /** 449 * All properties in CarCabinManager are zoned. 450 * @param propertyId 451 * @return true if property is a zoned type 452 */ 453 public static boolean isZonedProperty(@PropertyId int propertyId) { 454 return true; 455 } 456 457 /** 458 * Implement wrappers for contained CarPropertyManagerBase object 459 * @param callback 460 * @throws CarNotConnectedException 461 */ 462 public synchronized void registerCallback(CarCabinEventCallback callback) throws 463 CarNotConnectedException { 464 if (mCallbacks.isEmpty()) { 465 mListenerToBase = new CarPropertyEventListenerToBase(this); 466 mMgr.registerCallback(mListenerToBase); 467 } 468 mCallbacks.add(callback); 469 } 470 471 /** 472 * Stop getting property updates for the given callback. If there are multiple registrations for 473 * this listener, all listening will be stopped. 474 * @param callback 475 */ 476 public synchronized void unregisterCallback(CarCabinEventCallback callback) { 477 mCallbacks.remove(callback); 478 if (mCallbacks.isEmpty()) { 479 mMgr.unregisterCallback(); 480 mListenerToBase = null; 481 } 482 } 483 484 /** 485 * Get list of properties available to Car Cabin Manager 486 * @return List of CarPropertyConfig objects available via Car Cabin Manager. 487 * @throws CarNotConnectedException if the connection to the car service has been lost. 488 */ 489 public List<CarPropertyConfig> getPropertyList() throws CarNotConnectedException { 490 return mMgr.getPropertyList(); 491 } 492 493 /** 494 * Get value of boolean property 495 * @param propertyId 496 * @param area 497 * @return value of requested boolean property 498 * @throws CarNotConnectedException 499 */ 500 public boolean getBooleanProperty(@PropertyId int propertyId, int area) 501 throws CarNotConnectedException { 502 return mMgr.getBooleanProperty(propertyId, area); 503 } 504 505 /** 506 * Get value of float property 507 * @param propertyId 508 * @param area 509 * @return value of requested float property 510 * @throws CarNotConnectedException 511 */ 512 public float getFloatProperty(@PropertyId int propertyId, int area) 513 throws CarNotConnectedException { 514 return mMgr.getFloatProperty(propertyId, area); 515 } 516 517 /** 518 * Get value of integer property 519 * @param propertyId 520 * @param area 521 * @return value of requested integer property 522 * @throws CarNotConnectedException 523 */ 524 public int getIntProperty(@PropertyId int propertyId, int area) 525 throws CarNotConnectedException { 526 return mMgr.getIntProperty(propertyId, area); 527 } 528 529 /** 530 * Set the value of a boolean property 531 * @param propertyId 532 * @param area 533 * @param val 534 * @throws CarNotConnectedException 535 */ 536 public void setBooleanProperty(@PropertyId int propertyId, int area, boolean val) 537 throws CarNotConnectedException { 538 mMgr.setBooleanProperty(propertyId, area, val); 539 } 540 541 /** 542 * Set the value of a float property 543 * @param propertyId 544 * @param area 545 * @param val 546 * @throws CarNotConnectedException 547 */ 548 public void setFloatProperty(@PropertyId int propertyId, int area, float val) 549 throws CarNotConnectedException { 550 mMgr.setFloatProperty(propertyId, area, val); 551 } 552 553 /** 554 * Set the value of an integer property 555 * @param propertyId 556 * @param area 557 * @param val 558 * @throws CarNotConnectedException 559 */ 560 public void setIntProperty(@PropertyId int propertyId, int area, int val) 561 throws CarNotConnectedException { 562 mMgr.setIntProperty(propertyId, area, val); 563 } 564 565 /** @hide */ 566 @Override 567 public void onCarDisconnected() { 568 mMgr.onCarDisconnected(); 569 } 570 } 571