• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.car.CarLibLog.TAG_CAR;
20 
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.RequiresPermission;
25 import android.annotation.SdkConstant;
26 import android.annotation.SdkConstant.SdkConstantType;
27 import android.annotation.SuppressLint;
28 import android.annotation.SystemApi;
29 import android.annotation.TestApi;
30 import android.app.Activity;
31 import android.app.Service;
32 import android.car.admin.CarDevicePolicyManager;
33 import android.car.annotation.AddedInOrBefore;
34 import android.car.annotation.ApiRequirements;
35 import android.car.annotation.MandatoryFeature;
36 import android.car.annotation.OptionalFeature;
37 import android.car.app.CarActivityManager;
38 import android.car.builtin.os.BuildHelper;
39 import android.car.builtin.os.ServiceManagerHelper;
40 import android.car.cluster.CarInstrumentClusterManager;
41 import android.car.cluster.ClusterActivityState;
42 import android.car.cluster.ClusterHomeManager;
43 import android.car.content.pm.CarPackageManager;
44 import android.car.diagnostic.CarDiagnosticManager;
45 import android.car.drivingstate.CarDrivingStateManager;
46 import android.car.drivingstate.CarUxRestrictionsManager;
47 import android.car.evs.CarEvsManager;
48 import android.car.hardware.CarSensorManager;
49 import android.car.hardware.CarVendorExtensionManager;
50 import android.car.hardware.cabin.CarCabinManager;
51 import android.car.hardware.hvac.CarHvacManager;
52 import android.car.hardware.power.CarPowerManager;
53 import android.car.hardware.property.CarPropertyManager;
54 import android.car.hardware.property.ICarProperty;
55 import android.car.input.CarInputManager;
56 import android.car.media.CarAudioManager;
57 import android.car.media.CarMediaIntents;
58 import android.car.media.CarMediaManager;
59 import android.car.navigation.CarNavigationStatusManager;
60 import android.car.occupantawareness.OccupantAwarenessManager;
61 import android.car.occupantconnection.CarOccupantConnectionManager;
62 import android.car.os.CarPerformanceManager;
63 import android.car.remoteaccess.CarRemoteAccessManager;
64 import android.car.storagemonitoring.CarStorageMonitoringManager;
65 import android.car.telemetry.CarTelemetryManager;
66 import android.car.test.CarTestManager;
67 import android.car.user.CarUserManager;
68 import android.car.user.ExperimentalCarUserManager;
69 import android.car.vms.VmsClientManager;
70 import android.car.vms.VmsSubscriberManager;
71 import android.car.watchdog.CarWatchdogManager;
72 import android.content.ComponentName;
73 import android.content.Context;
74 import android.content.ContextWrapper;
75 import android.content.Intent;
76 import android.content.ServiceConnection;
77 import android.content.pm.PackageManager;
78 import android.os.Handler;
79 import android.os.IBinder;
80 import android.os.Looper;
81 import android.os.Process;
82 import android.os.RemoteException;
83 import android.os.SystemProperties;
84 import android.os.TransactionTooLargeException;
85 import android.util.ArrayMap;
86 import android.util.Log;
87 
88 import com.android.car.internal.VisibleForHiddenApiCheck;
89 import com.android.car.internal.common.CommonConstants;
90 import com.android.internal.annotations.GuardedBy;
91 import com.android.internal.annotations.VisibleForTesting;
92 
93 import java.lang.annotation.ElementType;
94 import java.lang.annotation.Retention;
95 import java.lang.annotation.RetentionPolicy;
96 import java.lang.annotation.Target;
97 import java.lang.reflect.Constructor;
98 import java.lang.reflect.InvocationTargetException;
99 import java.util.Collections;
100 import java.util.HashMap;
101 import java.util.List;
102 import java.util.Map;
103 import java.util.Objects;
104 
105 /**
106  *   Top level car API for embedded Android Auto deployments.
107  *   This API works only for devices with {@link PackageManager#FEATURE_AUTOMOTIVE}
108  *   Calling this API on a device with no such feature will lead to an exception.
109  */
110 public final class Car {
111 
112     /**
113      * System property to define platform minor version.
114      *
115      * <p>Value is int string. Check {@link #PROPERTY_PLATFORM_MINOR_INT} for further details.
116      * If not set, default value of {@code 0} is assumed.
117      */
118     private static final String PROPERTY_PLATFORM_MINOR_VERSION =
119             "ro.android.car.version.platform_minor";
120 
121     /**
122      * @deprecated - use {@code getCarApiVersion().getMajorVersion()} instead
123      */
124     @Deprecated
125     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
126             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
127     public static final int API_VERSION_MAJOR_INT = 34;
128 
129     /**
130      * @deprecated - use {@code getCarApiVersion().getMinorVersion()} instead
131      */
132     @Deprecated
133     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
134             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
135     public static final int API_VERSION_MINOR_INT = 0;
136 
137 
138     /**
139      * @deprecated - use {@code getPlatformApiVersion().getMinorVersion()} instead
140      */
141     @Deprecated
142     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
143             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
144     public static final int PLATFORM_VERSION_MINOR_INT = SystemProperties
145             .getInt(PROPERTY_PLATFORM_MINOR_VERSION, /* def= */ 0);
146 
147     private static final CarVersion CAR_VERSION = CarVersion.newInstance("Car.CAR_VERSION",
148             API_VERSION_MAJOR_INT, API_VERSION_MINOR_INT);
149 
150     private static final PlatformVersion PLATFORM_VERSION;
151 
152     /**
153      * @hide
154      */
155     @TestApi
156     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_1,
157             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
158     public static final String PROPERTY_EMULATED_PLATFORM_VERSION_MAJOR =
159             "com.android.car.internal.debug.platform_major_version";
160     /**
161      * @hide
162      */
163     @TestApi
164     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_1,
165             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
166     public static final String PROPERTY_EMULATED_PLATFORM_VERSION_MINOR =
167             "com.android.car.internal.debug.platform_minor_version";
168 
169     static {
170         PlatformVersion emulated = null;
171         if (!BuildHelper.isUserBuild()) {
172             int major = SystemProperties.getInt(PROPERTY_EMULATED_PLATFORM_VERSION_MAJOR, -1);
173             if (major != -1) {
174                 int minor = SystemProperties.getInt(PROPERTY_EMULATED_PLATFORM_VERSION_MINOR,
175                         PLATFORM_VERSION_MINOR_INT);
176                 emulated = android.car.PlatformVersion.newInstance("EMULATED", major, minor);
Log.i(TAG_CAR, "Emulating PLATFORM_VERSION version: " + emulated)177                 Log.i(TAG_CAR, "Emulating PLATFORM_VERSION version: " + emulated);
178             }
179         }
180         PLATFORM_VERSION =
181                 emulated != null ? emulated : PlatformVersion.getCurrentPlatformVersionForMinor(
182                         "Car.PLATFORM_VERSION", PLATFORM_VERSION_MINOR_INT);
183     }
184 
185     // Car service registry information.
186     // This information never changes after the static initialization completes.
187     private static final Map<Class<?>, String> CAR_SERVICE_NAMES =
188             new ArrayMap<Class<?>, String>(36);
189 
190     /**
191      * Binder service name of car service registered to service manager.
192      *
193      * @hide
194      */
195     @VisibleForHiddenApiCheck
196     @AddedInOrBefore(majorVersion = 33)
197     public static final String CAR_SERVICE_BINDER_SERVICE_NAME = "car_service";
198 
199     /**
200      * This represents AndroidManifest meta-data to tell that {@code Activity} is optimized for
201      * driving distraction.
202      *
203      * <p>Activities without this meta-data can be blocked while car is in moving / driving state.
204      *
205      * <p>Note that having this flag does not guarantee that the {@code Activity} will be always
206      * allowed for all driving states.
207      *
208      * <p>For this meta-data, android:value can be {@code true} (=optimized) or {@code false}.
209      *
210      * <p>Example usage:
211      * <xml><meta-data android:name="distractionOptimized" android:value="true"/></xml>
212      */
213     @SuppressLint("IntentName")
214     @AddedInOrBefore(majorVersion = 33)
215     public static final String META_DATA_DISTRACTION_OPTIMIZED = "distractionOptimized";
216 
217     /**
218      * This represents AndroidManifest meta-data to tell that {@code Application} requires specific
219      * car features to work.
220      *
221      * <p>Apps like launcher or installer app can use this information to filter out apps
222      * not usable in a specific car. This meta-data is not necessary for mandatory features.
223      *
224      * <p>For this meta-data, android:value should contain the feature name string defined by
225      * {@code OptionalFeature} or {@code ExperimentalFeature} annotations.
226      *
227      * <p>Example usage:
228      * <xml><meta-data android:name="requires-car-feature" android:value="diagnostic"/></xml>
229      */
230     @SuppressLint("IntentName")
231     @AddedInOrBefore(majorVersion = 33)
232     public static final String META_DATA_REQUIRES_CAR_FEATURE = "requires-car-feature";
233 
234     /**
235      * Service name for {@link CarSensorManager}, to be used in {@link #getCarManager(String)}.
236      *
237      * @deprecated  {@link CarSensorManager} is deprecated. Use {@link CarPropertyManager} instead.
238      */
239     @MandatoryFeature
240     @Deprecated
241     @AddedInOrBefore(majorVersion = 33)
242     public static final String SENSOR_SERVICE = "sensor";
243 
244     /** Service name for {@link CarInfoManager}, to be used in {@link #getCarManager(String)}. */
245     @MandatoryFeature
246     @AddedInOrBefore(majorVersion = 33)
247     public static final String INFO_SERVICE = "info";
248 
249     /** Service name for {@link CarAppFocusManager}. */
250     @MandatoryFeature
251     @AddedInOrBefore(majorVersion = 33)
252     public static final String APP_FOCUS_SERVICE = "app_focus";
253 
254     /** Service name for {@link CarPackageManager} */
255     @MandatoryFeature
256     @AddedInOrBefore(majorVersion = 33)
257     public static final String PACKAGE_SERVICE = "package";
258 
259     /** Service name for {@link CarAudioManager} */
260     @MandatoryFeature
261     @AddedInOrBefore(majorVersion = 33)
262     public static final String AUDIO_SERVICE = "audio";
263 
264     /** Service name for {@code CarNavigationStatusManager} */
265     @OptionalFeature
266     @AddedInOrBefore(majorVersion = 33)
267     public static final String CAR_NAVIGATION_SERVICE = "car_navigation_service";
268 
269     /**
270      * Service name for {@link CarOccupantConnectionManager}.
271      *
272      * @hide
273      */
274     @OptionalFeature
275     @SystemApi
276     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
277             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
278     public static final String CAR_OCCUPANT_CONNECTION_SERVICE = "car_occupant_connection_service";
279 
280     /**
281      * Service name for {@link CarRemoteDeviceManager}.
282      *
283      * @hide
284      */
285     @OptionalFeature
286     @SystemApi
287     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
288             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
289     public static final String CAR_REMOTE_DEVICE_SERVICE = "car_remote_device_service";
290 
291     /** Service name for {@link CarOccupantZoneManager} */
292     @MandatoryFeature
293     @AddedInOrBefore(majorVersion = 33)
294     public static final String CAR_OCCUPANT_ZONE_SERVICE = "car_occupant_zone_service";
295 
296     /**
297      * Service name for {@link CarUserManager}
298      *
299      * @hide
300      */
301     @MandatoryFeature
302     @SystemApi
303     @AddedInOrBefore(majorVersion = 33)
304     public static final String CAR_USER_SERVICE = "car_user_service";
305 
306     /**
307      * Service name for {@link ExperimentalCarUserManager}
308      *
309      * @hide
310      * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer
311      * supported. It will be marked @Removed in VIC and hard removed in X.
312      */
313     @Deprecated
314     @OptionalFeature
315     @AddedInOrBefore(majorVersion = 33)
316     public static final String EXPERIMENTAL_CAR_USER_SERVICE = "experimental_car_user_service";
317 
318     /**
319      * Service name for ExperimentalCarKeyguardService
320      *
321      * @hide
322      */
323     @OptionalFeature
324     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
325             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
326     public static final String EXPERIMENTAL_CAR_KEYGUARD_SERVICE =
327             "experimental_car_keyguard_service";
328 
329     /**
330      * Service name for {@link CarDevicePolicyManager}
331      *
332      * @hide
333      */
334     @MandatoryFeature
335     @SystemApi
336     @AddedInOrBefore(majorVersion = 33)
337     public static final String CAR_DEVICE_POLICY_SERVICE = "car_device_policy_service";
338 
339     /**
340      * Service name for {@link CarInstrumentClusterManager}
341      *
342      * @deprecated CarInstrumentClusterManager is being deprecated
343      * @hide
344      */
345     @OptionalFeature
346     @Deprecated
347     @AddedInOrBefore(majorVersion = 33)
348     public static final String CAR_INSTRUMENT_CLUSTER_SERVICE = "cluster_service";
349 
350     /**
351      * Service name for {@link CarCabinManager}.
352      *
353      * @deprecated {@link CarCabinManager} is deprecated. Use {@link CarPropertyManager} instead.
354      * @hide
355      */
356     @MandatoryFeature
357     @Deprecated
358     @SystemApi
359     @AddedInOrBefore(majorVersion = 33)
360     public static final String CABIN_SERVICE = "cabin";
361 
362     /**
363      * Service name for {@link android.car.diagnostic.CarDiagnosticManager}.
364      * @hide
365      */
366     @OptionalFeature
367     @SystemApi
368     @AddedInOrBefore(majorVersion = 33)
369     public static final String DIAGNOSTIC_SERVICE = "diagnostic";
370 
371     /**
372      * Service name for {@link CarHvacManager}
373      * @deprecated {@link CarHvacManager} is deprecated. Use {@link CarPropertyManager} instead.
374      * @hide
375      */
376     @MandatoryFeature
377     @Deprecated
378     @SystemApi
379     @AddedInOrBefore(majorVersion = 33)
380     public static final String HVAC_SERVICE = "hvac";
381 
382     /**
383      * Service name for {@link CarPowerManager}
384      */
385     @MandatoryFeature
386     @AddedInOrBefore(majorVersion = 33)
387     public static final String POWER_SERVICE = "power";
388 
389     /**
390      * Service name for {@link android.car.CarProjectionManager}
391      * @hide
392      */
393     @MandatoryFeature
394     @SystemApi
395     @AddedInOrBefore(majorVersion = 33)
396     public static final String PROJECTION_SERVICE = "projection";
397 
398     /**
399      * Service name for {@link CarPropertyManager}
400      */
401     @MandatoryFeature
402     @AddedInOrBefore(majorVersion = 33)
403     public static final String PROPERTY_SERVICE = "property";
404 
405     /**
406      * Service name for {@link CarVendorExtensionManager}
407      *
408      * @deprecated {@link CarVendorExtensionManager} is deprecated.
409      * Use {@link CarPropertyManager} instead.
410      * @hide
411      */
412     @MandatoryFeature
413     @Deprecated
414     @SystemApi
415     @AddedInOrBefore(majorVersion = 33)
416     public static final String VENDOR_EXTENSION_SERVICE = "vendor_extension";
417 
418     /**
419      * Service name for {@link VmsClientManager}
420      *
421      * @hide
422      */
423     @OptionalFeature
424     @SystemApi
425     @AddedInOrBefore(majorVersion = 33)
426     public static final String VEHICLE_MAP_SERVICE = "vehicle_map_service";
427 
428     /**
429      * Service name for {@link VmsSubscriberManager}
430      *
431      * @deprecated {@link VmsSubscriberManager} is deprecated. Use {@link VmsClientManager} instead.
432      * @hide
433      */
434     @OptionalFeature
435     @Deprecated
436     @SystemApi
437     @AddedInOrBefore(majorVersion = 33)
438     public static final String VMS_SUBSCRIBER_SERVICE = "vehicle_map_subscriber_service";
439 
440     /**
441      * Service name for {@link CarDrivingStateManager}
442      * @hide
443      */
444     @MandatoryFeature
445     @SystemApi
446     @AddedInOrBefore(majorVersion = 33)
447     public static final String CAR_DRIVING_STATE_SERVICE = "drivingstate";
448 
449     /**
450      * Service name for {@link CarUxRestrictionsManager}
451      */
452     @AddedInOrBefore(majorVersion = 33)
453     public static final String CAR_UX_RESTRICTION_SERVICE = "uxrestriction";
454 
455     /**
456      * Service name for {@link android.car.occupantawareness.OccupantAwarenessManager}
457      * @hide
458      */
459     @OptionalFeature
460     @SystemApi
461     @AddedInOrBefore(majorVersion = 33)
462     public static final String OCCUPANT_AWARENESS_SERVICE = "occupant_awareness";
463 
464     /**
465      * Service name for {@link android.car.media.CarMediaManager}
466      * @hide
467      */
468     @MandatoryFeature
469     @SystemApi
470     @AddedInOrBefore(majorVersion = 33)
471     public static final String CAR_MEDIA_SERVICE = "car_media";
472 
473     /**
474      * Service name for {@link android.car.CarBugreportManager}
475      * @hide
476      */
477     @MandatoryFeature
478     @SystemApi
479     @AddedInOrBefore(majorVersion = 33)
480     public static final String CAR_BUGREPORT_SERVICE = "car_bugreport";
481 
482     /**
483      * Service name for {@link android.car.storagemonitoring.CarStorageMonitoringManager}
484      * @hide
485      */
486     @OptionalFeature
487     @SystemApi
488     @AddedInOrBefore(majorVersion = 33)
489     public static final String STORAGE_MONITORING_SERVICE = "storage_monitoring";
490 
491     /**
492      * Service name for {@link android.car.watchdog.CarWatchdogManager}
493      */
494     @MandatoryFeature
495     @AddedInOrBefore(majorVersion = 33)
496     public static final String CAR_WATCHDOG_SERVICE = "car_watchdog";
497 
498     /**
499      * Service name for {@link android.car.os.CarPerformanceManager}
500      *
501      * @hide
502      */
503     @MandatoryFeature
504     @SystemApi
505     @AddedInOrBefore(majorVersion = 33)
506     public static final String CAR_PERFORMANCE_SERVICE = "car_performance";
507 
508     /**
509      * Service name for {@link android.car.input.CarInputManager}
510      * @hide
511      */
512     @MandatoryFeature
513     @SystemApi
514     @AddedInOrBefore(majorVersion = 33)
515     public static final String CAR_INPUT_SERVICE = "android.car.input";
516 
517     /**
518      * Service name for {@link android.car.cluster.ClusterHomeManager}
519      * @hide
520      */
521     @OptionalFeature
522     @AddedInOrBefore(majorVersion = 33)
523     public static final String CLUSTER_HOME_SERVICE = "cluster_home_service";
524 
525     /**
526      * Service for testing. This is system app only feature.
527      * Service name for {@link CarTestManager}, to be used in {@link #getCarManager(String)}.
528      * @hide
529      */
530     @MandatoryFeature
531     @SystemApi
532     @AddedInOrBefore(majorVersion = 33)
533     public static final String TEST_SERVICE = "car-service-test";
534 
535     /**
536      * Service name for {@link android.car.evs.CarEvsManager}
537      *
538      * @hide
539      */
540     @OptionalFeature
541     @SystemApi
542     @AddedInOrBefore(majorVersion = 33)
543     public static final String CAR_EVS_SERVICE = "car_evs_service";
544 
545     /**
546      * Service name for {@link android.car.telemetry.CarTelemetryManager}
547      *
548      * @hide
549      */
550     @OptionalFeature
551     @SystemApi
552     @AddedInOrBefore(majorVersion = 33)
553     public static final String CAR_TELEMETRY_SERVICE = "car_telemetry_service";
554 
555     /**
556      * Service name for {@link android.car.app.CarActivityManager}
557      *
558      * @hide
559      */
560     @MandatoryFeature
561     @SystemApi
562     @AddedInOrBefore(majorVersion = 33)
563     public static final String CAR_ACTIVITY_SERVICE = "car_activity_service";
564 
565     /**
566      * Service name for {@link android.car.remoteaccess.CarRemoteAccessManager}
567      *
568      * @hide
569      */
570     @OptionalFeature
571     @SystemApi
572     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
573             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
574     public static final String CAR_REMOTE_ACCESS_SERVICE = "car_remote_access_service";
575 
576     /**
577      * Permission necessary to read driver monitoring systems settings information.
578      *
579      * Examples of settings include the ENABLED properties for the supported driver monitoring
580      * features.
581      * @hide
582      */
583     @SystemApi
584     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
585             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
586     public static final String PERMISSION_READ_DRIVER_MONITORING_SETTINGS =
587             "android.car.permission.READ_DRIVER_MONITORING_SETTINGS";
588 
589     /**
590      * Permission necessary to control driver monitoring systems settings information.
591      *
592      * Examples of settings include the ENABLED properties for the supported driver monitoring
593      * features.
594      * @hide
595      */
596     @SystemApi
597     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
598             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
599     public static final String PERMISSION_CONTROL_DRIVER_MONITORING_SETTINGS =
600             "android.car.permission.CONTROL_DRIVER_MONITORING_SETTINGS";
601 
602     /**
603      * Permission necessary to read driver monitoring systems states information.
604      *
605      * Examples of states include the STATE and WARNING properties for the supported driver
606      * monitoring features.
607      *
608      * This is different from {@link PERMISSION_READ_DRIVER_MONITORING_SETTINGS}, which allows an
609      * app to read the system settings, such as whether the system is enabled or disabled.
610      * @hide
611      */
612     @SystemApi
613     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
614             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
615     public static final String PERMISSION_READ_DRIVER_MONITORING_STATES =
616             "android.car.permission.READ_DRIVER_MONITORING_STATES";
617 
618     /** Permission necessary to access car's mileage information.
619      *  @hide
620      */
621     @SystemApi
622     @AddedInOrBefore(majorVersion = 33)
623     public static final String PERMISSION_MILEAGE = "android.car.permission.CAR_MILEAGE";
624 
625     /** Permission necessary to access car's energy information. */
626     @AddedInOrBefore(majorVersion = 33)
627     public static final String PERMISSION_ENERGY = "android.car.permission.CAR_ENERGY";
628 
629     /** Permission necessary to control car's EV charge settings. */
630     @AddedInOrBefore(majorVersion = 33)
631     public static final String PERMISSION_CONTROL_CAR_ENERGY =
632             "android.car.permission.CONTROL_CAR_ENERGY";
633 
634     /**
635      * Permission necessary to change value of car's range remaining.
636      * @hide
637      */
638     @SystemApi
639     @AddedInOrBefore(majorVersion = 33)
640     public static final String PERMISSION_ADJUST_RANGE_REMAINING =
641             "android.car.permission.ADJUST_RANGE_REMAINING";
642 
643     /** Permission necessary to access car's VIN information */
644     @AddedInOrBefore(majorVersion = 33)
645     public static final String PERMISSION_IDENTIFICATION =
646             "android.car.permission.CAR_IDENTIFICATION";
647 
648     /** Permission necessary to access car's speed. */
649     @AddedInOrBefore(majorVersion = 33)
650     public static final String PERMISSION_SPEED = "android.car.permission.CAR_SPEED";
651 
652     /** Permission necessary to access car's dynamics state.
653      *  @hide
654      */
655     @SystemApi
656     @AddedInOrBefore(majorVersion = 33)
657     public static final String PERMISSION_CAR_DYNAMICS_STATE =
658             "android.car.permission.CAR_DYNAMICS_STATE";
659 
660     /** Permission necessary to access car's fuel door and ev charge port. */
661     @AddedInOrBefore(majorVersion = 33)
662     public static final String PERMISSION_ENERGY_PORTS = "android.car.permission.CAR_ENERGY_PORTS";
663 
664     /**
665      * Permission necessary to control car's fuel door and ev charge port.
666      * @hide
667      */
668     @SystemApi
669     @AddedInOrBefore(majorVersion = 33)
670     public static final String PERMISSION_CONTROL_ENERGY_PORTS =
671             "android.car.permission.CONTROL_CAR_ENERGY_PORTS";
672 
673     /**
674      * Permission necessary to read car's exterior lights information.
675      *  @hide
676      */
677     @SystemApi
678     @AddedInOrBefore(majorVersion = 33)
679     public static final String PERMISSION_EXTERIOR_LIGHTS =
680             "android.car.permission.CAR_EXTERIOR_LIGHTS";
681 
682     /**
683      * Permission necessary to read car's interior lights information.
684      */
685     @AddedInOrBefore(majorVersion = 33)
686     public static final String PERMISSION_READ_INTERIOR_LIGHTS =
687             "android.car.permission.READ_CAR_INTERIOR_LIGHTS";
688 
689     /** Permission necessary to control car's exterior lights.
690      *  @hide
691      */
692     @SystemApi
693     @AddedInOrBefore(majorVersion = 33)
694     public static final String PERMISSION_CONTROL_EXTERIOR_LIGHTS =
695             "android.car.permission.CONTROL_CAR_EXTERIOR_LIGHTS";
696 
697     /**
698      * Permission necessary to control car's interior lights.
699      */
700     @AddedInOrBefore(majorVersion = 33)
701     public static final String PERMISSION_CONTROL_INTERIOR_LIGHTS =
702             "android.car.permission.CONTROL_CAR_INTERIOR_LIGHTS";
703 
704     /** Permission necessary to access car's powertrain information.*/
705     @AddedInOrBefore(majorVersion = 33)
706     public static final String PERMISSION_POWERTRAIN = "android.car.permission.CAR_POWERTRAIN";
707 
708     /**
709      * Permission necessary to control car's powertrain information.
710      * @hide
711      */
712     @SystemApi
713     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
714             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
715     public static final String PERMISSION_CONTROL_POWERTRAIN =
716             "android.car.permission.CONTROL_CAR_POWERTRAIN";
717 
718     /**
719      * Permission necessary to change car audio volume through {@link CarAudioManager}.
720      */
721     @AddedInOrBefore(majorVersion = 33)
722     public static final String PERMISSION_CAR_CONTROL_AUDIO_VOLUME =
723             "android.car.permission.CAR_CONTROL_AUDIO_VOLUME";
724 
725     /**
726      * Permission necessary to change car audio settings through {@link CarAudioManager}.
727      */
728     @AddedInOrBefore(majorVersion = 33)
729     public static final String PERMISSION_CAR_CONTROL_AUDIO_SETTINGS =
730             "android.car.permission.CAR_CONTROL_AUDIO_SETTINGS";
731 
732     /**
733      * Permission necessary to receive full audio ducking events from car audio focus handler.
734      *
735      * @hide
736      */
737     @SystemApi
738     @AddedInOrBefore(majorVersion = 33)
739     public static final String PERMISSION_RECEIVE_CAR_AUDIO_DUCKING_EVENTS =
740             "android.car.permission.RECEIVE_CAR_AUDIO_DUCKING_EVENTS";
741 
742     /**
743      * Permission necessary to use {@code CarNavigationStatusManager}.
744      */
745     @AddedInOrBefore(majorVersion = 33)
746     public static final String PERMISSION_CAR_NAVIGATION_MANAGER =
747             "android.car.permission.CAR_NAVIGATION_MANAGER";
748 
749     /**
750      * Permission necessary to start activities in the instrument cluster through
751      * {@link CarInstrumentClusterManager}
752      *
753      * @hide
754      */
755     @SystemApi
756     @AddedInOrBefore(majorVersion = 33)
757     public static final String PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL =
758             "android.car.permission.CAR_INSTRUMENT_CLUSTER_CONTROL";
759 
760     /**
761      * Permission necessary to listen for the instrument cluster's navigation state changes.
762      *
763      * @hide
764      */
765     @AddedInOrBefore(majorVersion = 33)
766     public static final String PERMISSION_CAR_MONITOR_CLUSTER_NAVIGATION_STATE =
767             "android.car.permission.CAR_MONITOR_CLUSTER_NAVIGATION_STATE";
768 
769 
770     /**
771      * Application must have this permission in order to be launched in the instrument cluster
772      * display.
773      *
774      * @hide
775      */
776     @VisibleForHiddenApiCheck
777     @AddedInOrBefore(majorVersion = 33)
778     public static final String PERMISSION_CAR_DISPLAY_IN_CLUSTER =
779             "android.car.permission.CAR_DISPLAY_IN_CLUSTER";
780 
781     /** Permission necessary to use {@link CarInfoManager}. */
782     @AddedInOrBefore(majorVersion = 33)
783     public static final String PERMISSION_CAR_INFO = "android.car.permission.CAR_INFO";
784 
785     /** Permission necessary to access privileged car info. */
786     @AddedInOrBefore(majorVersion = 33)
787     public static final String PERMISSION_PRIVILEGED_CAR_INFO =
788             "android.car.permission.PRIVILEGED_CAR_INFO";
789 
790     /**
791      * Permission necessary to read information of vendor properties' permissions.
792      * @hide
793      */
794     @SystemApi
795     @AddedInOrBefore(majorVersion = 33)
796     public static final String PERMISSION_READ_CAR_VENDOR_PERMISSION_INFO =
797             "android.car.permission.READ_CAR_VENDOR_PERMISSION_INFO";
798 
799     /** Permission necessary to read temperature of car's exterior environment. */
800     @AddedInOrBefore(majorVersion = 33)
801     public static final String PERMISSION_EXTERIOR_ENVIRONMENT =
802             "android.car.permission.CAR_EXTERIOR_ENVIRONMENT";
803 
804     /**
805      * Permission necessary to access car specific communication channel.
806      * @hide
807      */
808     @SystemApi
809     @AddedInOrBefore(majorVersion = 33)
810     public static final String PERMISSION_VENDOR_EXTENSION =
811             "android.car.permission.CAR_VENDOR_EXTENSION";
812 
813     /**
814      * @hide
815      */
816     @SystemApi
817     @AddedInOrBefore(majorVersion = 33)
818     public static final String PERMISSION_CONTROL_APP_BLOCKING =
819             "android.car.permission.CONTROL_APP_BLOCKING";
820 
821     /**
822      * Permission necessary to access car's engine information.
823      * @hide
824      */
825     @SystemApi
826     @AddedInOrBefore(majorVersion = 33)
827     public static final String PERMISSION_CAR_ENGINE_DETAILED =
828             "android.car.permission.CAR_ENGINE_DETAILED";
829 
830     /**
831      * Permission necessary to access car's tire pressure information.
832      * @hide
833      */
834     @SystemApi
835     @AddedInOrBefore(majorVersion = 33)
836     public static final String PERMISSION_TIRES = "android.car.permission.CAR_TIRES";
837 
838     /**
839      * Permission necessary to access car's property {@link VehiclePropertyIds#EPOCH_TIME}.
840      * @hide
841      */
842     @SystemApi
843     @AddedInOrBefore(majorVersion = 33)
844     public static final String PERMISSION_CAR_EPOCH_TIME = "android.car.permission.CAR_EPOCH_TIME";
845 
846     /**
847      * Permission necessary to access car's steering angle information.
848      */
849     @AddedInOrBefore(majorVersion = 33)
850     public static final String PERMISSION_READ_STEERING_STATE =
851             "android.car.permission.READ_CAR_STEERING";
852 
853     /**
854      * Permission necessary to read and write display units for distance, fuel volume, tire pressure
855      * and ev battery.
856      */
857     @AddedInOrBefore(majorVersion = 33)
858     public static final String PERMISSION_READ_DISPLAY_UNITS =
859             "android.car.permission.READ_CAR_DISPLAY_UNITS";
860 
861     /**
862      * Permission necessary to control display units for distance, fuel volume, tire pressure
863      * and ev battery. Currently, all display unit properties require both {@code
864      * PERMISSION_CONTROL_DISPLAY_UNITS} and {@code PERMISSION_VENDOR_EXTENSION} to be granted in
865      * order to write to them.
866      */
867     @AddedInOrBefore(majorVersion = 33)
868     public static final String PERMISSION_CONTROL_DISPLAY_UNITS =
869             "android.car.permission.CONTROL_CAR_DISPLAY_UNITS";
870 
871     /**
872      * Permission necessary to control car's door.
873      * @hide
874      */
875     @SystemApi
876     @AddedInOrBefore(majorVersion = 33)
877     public static final String PERMISSION_CONTROL_CAR_DOORS =
878             "android.car.permission.CONTROL_CAR_DOORS";
879 
880     /**
881      * Permission necessary to control car's windows.
882      * @hide
883      */
884     @SystemApi
885     @AddedInOrBefore(majorVersion = 33)
886     public static final String PERMISSION_CONTROL_CAR_WINDOWS =
887             "android.car.permission.CONTROL_CAR_WINDOWS";
888 
889     /**
890      * Permission necessary to control car glove box.
891      * @hide
892      */
893     @SystemApi
894     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
895             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
896     public static final String PERMISSION_CONTROL_GLOVE_BOX =
897             "android.car.permission.CONTROL_GLOVE_BOX";
898     /**
899      * Permission necessary to control car's seats.
900      * @hide
901      */
902     @SystemApi
903     @AddedInOrBefore(majorVersion = 33)
904     public static final String PERMISSION_CONTROL_CAR_SEATS =
905             "android.car.permission.CONTROL_CAR_SEATS";
906 
907     /**
908      * Permission necessary to control car's mirrors.
909      * @hide
910      */
911     @SystemApi
912     @AddedInOrBefore(majorVersion = 33)
913     public static final String PERMISSION_CONTROL_CAR_MIRRORS =
914             "android.car.permission.CONTROL_CAR_MIRRORS";
915 
916     /**
917      * Permission necessary to access Car HVAC APIs.
918      * @hide
919      */
920     @SystemApi
921     @AddedInOrBefore(majorVersion = 33)
922     public static final String PERMISSION_CONTROL_CAR_CLIMATE =
923             "android.car.permission.CONTROL_CAR_CLIMATE";
924 
925     /**
926      * Permission necessary to enable/disable a seat's ability to deploy airbag(s) when triggered
927      * (e.g. by a crash).
928      * @hide
929      */
930     @SystemApi
931     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
932             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
933     public static final String PERMISSION_CONTROL_CAR_AIRBAGS =
934             "android.car.permission.CONTROL_CAR_AIRBAGS";
935 
936 
937     /**
938      * Permission necessary to access restrictive car power management APIs.
939      * @hide
940      */
941     @SystemApi
942     @AddedInOrBefore(majorVersion = 33)
943     public static final String PERMISSION_CAR_POWER = "android.car.permission.CAR_POWER";
944 
945     /**
946      * Permission necessary to read the current power policy or be notified of power policy change.
947      */
948     @AddedInOrBefore(majorVersion = 33)
949     public static final String PERMISSION_READ_CAR_POWER_POLICY =
950             "android.car.permission.READ_CAR_POWER_POLICY";
951 
952     /**
953      * Permission necessary to apply a new power policy.
954      * @hide
955      */
956     @SystemApi
957     @AddedInOrBefore(majorVersion = 33)
958     public static final String PERMISSION_CONTROL_CAR_POWER_POLICY =
959             "android.car.permission.CONTROL_CAR_POWER_POLICY";
960 
961     /**
962      * Permission necessary to adjust the shutdown process.
963      * @hide
964      */
965     @SystemApi
966     @AddedInOrBefore(majorVersion = 33)
967     public static final String PERMISSION_CONTROL_SHUTDOWN_PROCESS =
968             "android.car.permission.CONTROL_SHUTDOWN_PROCESS";
969 
970     /**
971      * Permission necessary to access Car PROJECTION system APIs.
972      * @hide
973      */
974     @SystemApi
975     @AddedInOrBefore(majorVersion = 33)
976     public static final String PERMISSION_CAR_PROJECTION = "android.car.permission.CAR_PROJECTION";
977 
978     /**
979      * Permission necessary to access projection status.
980      * @hide
981      */
982     @SystemApi
983     @AddedInOrBefore(majorVersion = 33)
984     public static final String PERMISSION_CAR_PROJECTION_STATUS =
985             "android.car.permission.ACCESS_CAR_PROJECTION_STATUS";
986 
987     /**
988      * Permission necessary to mock vehicle hal for testing.
989      * @hide
990      * @deprecated mocking vehicle HAL in car service is no longer supported.
991      */
992     @Deprecated
993     @SystemApi
994     @AddedInOrBefore(majorVersion = 33)
995     public static final String PERMISSION_MOCK_VEHICLE_HAL =
996             "android.car.permission.CAR_MOCK_VEHICLE_HAL";
997 
998     /**
999      * Permission necessary to access CarTestService.
1000      * @hide
1001      */
1002     @SystemApi
1003     @AddedInOrBefore(majorVersion = 33)
1004     public static final String PERMISSION_CAR_TEST_SERVICE =
1005             "android.car.permission.CAR_TEST_SERVICE";
1006 
1007     /**
1008      * Permission necessary to access CarDrivingStateService to get a Car's driving state.
1009      * @hide
1010      */
1011     @SystemApi
1012     @AddedInOrBefore(majorVersion = 33)
1013     public static final String PERMISSION_CAR_DRIVING_STATE =
1014             "android.car.permission.CAR_DRIVING_STATE";
1015 
1016     /**
1017      * Permission necessary to access VMS client service.
1018      *
1019      * @hide
1020      */
1021     @VisibleForHiddenApiCheck
1022     @AddedInOrBefore(majorVersion = 33)
1023     public static final String PERMISSION_BIND_VMS_CLIENT =
1024             "android.car.permission.BIND_VMS_CLIENT";
1025 
1026     /**
1027      * Permissions necessary to access VMS publisher APIs.
1028      *
1029      * @hide
1030      */
1031     @SystemApi
1032     @AddedInOrBefore(majorVersion = 33)
1033     public static final String PERMISSION_VMS_PUBLISHER = "android.car.permission.VMS_PUBLISHER";
1034 
1035     /**
1036      * Permissions necessary to access VMS subscriber APIs.
1037      *
1038      * @hide
1039      */
1040     @SystemApi
1041     @AddedInOrBefore(majorVersion = 33)
1042     public static final String PERMISSION_VMS_SUBSCRIBER = "android.car.permission.VMS_SUBSCRIBER";
1043 
1044     /**
1045      * Permissions necessary to read diagnostic information, including vendor-specific bits.
1046      *
1047      * @hide
1048      */
1049     @SystemApi
1050     @AddedInOrBefore(majorVersion = 33)
1051     public static final String PERMISSION_CAR_DIAGNOSTIC_READ_ALL =
1052             "android.car.permission.CAR_DIAGNOSTICS";
1053 
1054     /**
1055      * Permissions necessary to clear diagnostic information.
1056      *
1057      * @hide
1058      */
1059     @SystemApi
1060     @AddedInOrBefore(majorVersion = 33)
1061     public static final String PERMISSION_CAR_DIAGNOSTIC_CLEAR =
1062             "android.car.permission.CLEAR_CAR_DIAGNOSTICS";
1063 
1064     /**
1065      * Permission necessary to configure UX restrictions through {@link CarUxRestrictionsManager}.
1066      *
1067      * @hide
1068      */
1069     @VisibleForHiddenApiCheck
1070     @AddedInOrBefore(majorVersion = 33)
1071     @SystemApi
1072     public static final String PERMISSION_CAR_UX_RESTRICTIONS_CONFIGURATION =
1073             "android.car.permission.CAR_UX_RESTRICTIONS_CONFIGURATION";
1074 
1075     /**
1076      * Permission necessary to listen to occupant awareness state {@link OccupantAwarenessManager}.
1077      *
1078      * @hide
1079      */
1080     @SystemApi
1081     @AddedInOrBefore(majorVersion = 33)
1082     public static final String PERMISSION_READ_CAR_OCCUPANT_AWARENESS_STATE =
1083             "android.car.permission.READ_CAR_OCCUPANT_AWARENESS_STATE";
1084 
1085     /**
1086      * Permission necessary to access private display id.
1087      *
1088      * @hide
1089      */
1090     @SystemApi
1091     @AddedInOrBefore(majorVersion = 33)
1092     public static final String ACCESS_PRIVATE_DISPLAY_ID =
1093             "android.car.permission.ACCESS_PRIVATE_DISPLAY_ID";
1094 
1095     /**
1096      * @deprecated This permission is not used by any service.
1097      *
1098      * @hide
1099      */
1100     @SystemApi
1101     @AddedInOrBefore(majorVersion = 33)
1102     public static final String PERMISSION_CONTROL_CAR_OCCUPANT_AWARENESS_SYSTEM =
1103             "android.car.permission.CONTROL_CAR_OCCUPANT_AWARENESS_SYSTEM";
1104 
1105     /**
1106      * Permissions necessary to clear diagnostic information.
1107      *
1108      * @hide
1109      */
1110     @SystemApi
1111     @AddedInOrBefore(majorVersion = 33)
1112     public static final String PERMISSION_STORAGE_MONITORING =
1113             "android.car.permission.STORAGE_MONITORING";
1114 
1115     /**
1116      * Permission necessary to dynamically enable / disable optional car features.
1117      *
1118      * @hide
1119      */
1120     @SystemApi
1121     @AddedInOrBefore(majorVersion = 33)
1122     public static final String PERMISSION_CONTROL_CAR_FEATURES =
1123             "android.car.permission.CONTROL_CAR_FEATURES";
1124 
1125     /**
1126      * Permission necessary to be car watchdog clients.
1127      *
1128      * @hide
1129      */
1130     @SystemApi
1131     @AddedInOrBefore(majorVersion = 33)
1132     public static final String PERMISSION_USE_CAR_WATCHDOG =
1133             "android.car.permission.USE_CAR_WATCHDOG";
1134 
1135     /**
1136      * Permission necessary to monitor Car input events.
1137      *
1138      * @hide
1139      */
1140     @SystemApi
1141     @AddedInOrBefore(majorVersion = 33)
1142     public static final String PERMISSION_CAR_MONITOR_INPUT =
1143             "android.car.permission.CAR_MONITOR_INPUT";
1144 
1145     /**
1146      * Permission necessary to request CarEvsService to launch the special activity to show the
1147      * camera preview.
1148      *
1149      * @hide
1150      */
1151     @SystemApi
1152     @AddedInOrBefore(majorVersion = 33)
1153     public static final String PERMISSION_REQUEST_CAR_EVS_ACTIVITY =
1154             "android.car.permission.REQUEST_CAR_EVS_ACTIVITY";
1155 
1156     /**
1157      * Permission necessary to control the special activity to show the camera preview.
1158      *
1159      * @hide
1160      */
1161     @SystemApi
1162     @AddedInOrBefore(majorVersion = 33)
1163     public static final String PERMISSION_CONTROL_CAR_EVS_ACTIVITY =
1164             "android.car.permission.CONTROL_CAR_EVS_ACTIVITY";
1165 
1166     /**
1167      * Permission necessary to use the camera streams via CarEvsService.
1168      *
1169      * @hide
1170      */
1171     @SystemApi
1172     @AddedInOrBefore(majorVersion = 33)
1173     public static final String PERMISSION_USE_CAR_EVS_CAMERA =
1174             "android.car.permission.USE_CAR_EVS_CAMERA";
1175 
1176     /**
1177      * Permission necessary to monitor the status of CarEvsService.
1178      *
1179      * @hide
1180      */
1181     @SystemApi
1182     @AddedInOrBefore(majorVersion = 33)
1183     public static final String PERMISSION_MONITOR_CAR_EVS_STATUS =
1184             "android.car.permission.MONITOR_CAR_EVS_STATUS";
1185 
1186     /**
1187      * Permission necessary to use the CarTelemetryService.
1188      *
1189      * @hide
1190      */
1191     @SystemApi
1192     @AddedInOrBefore(majorVersion = 33)
1193     public static final String PERMISSION_USE_CAR_TELEMETRY_SERVICE =
1194             "android.car.permission.USE_CAR_TELEMETRY_SERVICE";
1195 
1196     /**
1197      * Type of car connection: platform runs directly in car.
1198      *
1199      * @deprecated connection type constants are no longer used
1200      */
1201     @Deprecated
1202     @AddedInOrBefore(majorVersion = 33)
1203     public static final int CONNECTION_TYPE_EMBEDDED = 5;
1204 
1205     /**
1206      * Permission necessary to be able to render template-based UI metadata on behalf of another
1207      * application.
1208      *
1209      * @hide
1210      */
1211     @SystemApi
1212     @AddedInOrBefore(majorVersion = 33)
1213     public static final String PERMISSION_TEMPLATE_RENDERER =
1214             "android.car.permission.TEMPLATE_RENDERER";
1215 
1216     /**
1217      * Permission necessary to set or retrieve car watchdog configurations.
1218      *
1219      * @hide
1220      */
1221     @SystemApi
1222     @AddedInOrBefore(majorVersion = 33)
1223     public static final String PERMISSION_CONTROL_CAR_WATCHDOG_CONFIG =
1224             "android.car.permission.CONTROL_CAR_WATCHDOG_CONFIG";
1225 
1226     /**
1227      * Permission necessary to collect metrics from car watchdog.
1228      *
1229      * @hide
1230      */
1231     @SystemApi
1232     @AddedInOrBefore(majorVersion = 33)
1233     public static final String PERMISSION_COLLECT_CAR_WATCHDOG_METRICS =
1234             "android.car.permission.COLLECT_CAR_WATCHDOG_METRICS";
1235 
1236     /**
1237      * Permission necessary to fetch car CPU information.
1238      *
1239      * @hide
1240      */
1241     @AddedInOrBefore(majorVersion = 33)
1242     public static final String PERMISSION_COLLECT_CAR_CPU_INFO =
1243             "android.car.permission.COLLECT_CAR_CPU_INFO";
1244 
1245     /**
1246      * Permission necessary to control launching applications in Car.
1247      *
1248      * @hide
1249      */
1250     @SystemApi
1251     @AddedInOrBefore(majorVersion = 33)
1252     public static final String PERMISSION_CONTROL_CAR_APP_LAUNCH =
1253             "android.car.permission.CONTROL_CAR_APP_LAUNCH";
1254 
1255     /**
1256      * Permission necessary to setting and getting thread scheduling policy and priority.
1257      *
1258      * @hide
1259      */
1260     @SystemApi
1261     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_1,
1262             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_1)
1263     public static final String PERMISSION_MANAGE_THREAD_PRIORITY =
1264             "android.car.permission.MANAGE_THREAD_PRIORITY";
1265 
1266     /**
1267      * Permission necessary to modify occupant zone settings. Will be used in
1268      * {@link CarOccupantZoneManager}.
1269      *
1270      * @hide
1271      */
1272     @SystemApi
1273     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1274             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1275     public static final String PERMISSION_MANAGE_OCCUPANT_ZONE =
1276             "android.car.permission.MANAGE_OCCUPANT_ZONE";
1277 
1278     /**
1279      * Permission necessary to use remote access.
1280      */
1281     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1282             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1283     public static final String PERMISSION_USE_REMOTE_ACCESS =
1284             "android.car.permission.USE_REMOTE_ACCESS";
1285 
1286     /**
1287      * Permission necessary to control remote access.
1288      *
1289      * @hide
1290      */
1291     @SystemApi
1292     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1293             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1294     public static final String PERMISSION_CONTROL_REMOTE_ACCESS =
1295             "android.car.permission.CONTROL_REMOTE_ACCESS";
1296     /**
1297      * Permission necessary to control car's steering wheel.
1298      * @hide
1299      */
1300     @SystemApi
1301     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1302             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1303     public static final String PERMISSION_CONTROL_STEERING_WHEEL =
1304             "android.car.permission.CONTROL_STEERING_WHEEL";
1305 
1306     /**
1307      * Permission necessary to read ADAS settings information.
1308      *
1309      * Examples of settings include the ENABLED properties for the supported ADAS features.
1310      *
1311      * @hide
1312      */
1313     @SystemApi
1314     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1315             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1316     public static final String PERMISSION_READ_ADAS_SETTINGS =
1317             "android.car.permission.READ_ADAS_SETTINGS";
1318 
1319     /**
1320      * Permission necessary to control ADAS settings information.
1321      *
1322      * Examples of settings include the ENABLED properties for the supported ADAS features.
1323      *
1324      * @hide
1325      */
1326     @SystemApi
1327     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1328             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1329     public static final String PERMISSION_CONTROL_ADAS_SETTINGS =
1330             "android.car.permission.CONTROL_ADAS_SETTINGS";
1331 
1332     /**
1333      * Permission necessary to read ADAS states information.
1334      *
1335      * Examples include the STATE properties for the supported ADAS features.
1336      *
1337      * @hide
1338      */
1339     @SystemApi
1340     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1341             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1342     public static final String PERMISSION_READ_ADAS_STATES =
1343             "android.car.permission.READ_ADAS_STATES";
1344 
1345     /**
1346      * Permission necessary to control ADAS states information.
1347      *
1348      * Examples include the STATE properties for the supported ADAS features.
1349      *
1350      * @hide
1351      */
1352     @SystemApi
1353     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1354             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1355     public static final String PERMISSION_CONTROL_ADAS_STATES =
1356             "android.car.permission.CONTROL_ADAS_STATES";
1357 
1358     /**
1359      * Permission necessary to monitor the states of other occupant zones in the car and peer apps
1360      * (apps that have the same package name as the caller) installed in those zones,
1361      * and manage the power of those zones.
1362      *
1363      * @hide
1364      */
1365     @SystemApi
1366     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1367             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1368     public static final String PERMISSION_MANAGE_REMOTE_DEVICE =
1369             "android.car.permission.MANAGE_REMOTE_DEVICE";
1370 
1371     /**
1372      * Permission necessary to establish connection and communicate to peer apps (apps that have
1373      * the same package name as the caller) installed in other occupant zones in the car.
1374      *
1375      * @hide
1376      */
1377     @SystemApi
1378     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1379             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1380     public static final String PERMISSION_MANAGE_OCCUPANT_CONNECTION =
1381             "android.car.permission.MANAGE_OCCUPANT_CONNECTION";
1382 
1383     /**
1384      * Permission to access the mirrored Surface using the Token generated by
1385      * {@link CarActivityManager#createTaskMirroringToken(int)}.
1386      *
1387      * @hide
1388      */
1389     @SystemApi
1390     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1391             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1392     public static final String PERMISSION_ACCESS_MIRRORRED_SURFACE =
1393             "android.car.permission.ACCESS_MIRRORED_SURFACE";
1394 
1395     /**
1396      * Permission to create the mirroring token for the Display.
1397      * See {@link CarActivityManager#createDisplayMirroringToken(int)} (int)}.
1398      *
1399      * @hide
1400      */
1401     @SystemApi
1402     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1403             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1404     public static final String PERMISSION_MIRROR_DISPLAY =
1405             "android.car.permission.MIRROR_DISPLAY";
1406 
1407     /**
1408      * Permission necessary to read car's windshield wipers.
1409      * @hide
1410      */
1411     @SystemApi
1412     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1413             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1414     public static final String PERMISSION_READ_WINDSHIELD_WIPERS =
1415             "android.car.permission.READ_WINDSHIELD_WIPERS";
1416 
1417     /**
1418      * Permission necessary to control car's windshield wipers.
1419      * @hide
1420      */
1421     @SystemApi
1422     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1423             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1424     public static final String PERMISSION_CONTROL_WINDSHIELD_WIPERS =
1425             "android.car.permission.CONTROL_WINDSHIELD_WIPERS";
1426 
1427     /**
1428      * Permission necessary to register a {@link SystemUIProxy} that can be used by other apps to
1429      * manage the system ui and create task views.
1430      *
1431      * @hide
1432      */
1433     @SystemApi
1434     @ApiRequirements(
1435             minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1436             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1437     public static final String PERMISSION_REGISTER_CAR_SYSTEM_UI_PROXY =
1438             "android.car.permission.REGISTER_CAR_SYSTEM_UI_PROXY";
1439 
1440     /**
1441      * Permission necessary to communicate with the car system ui for creating task views or
1442      * getting notified about system ui changes.
1443      * @hide
1444      */
1445     @SystemApi
1446     @ApiRequirements(
1447             minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1448             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1449     public static final String PERMISSION_MANAGE_CAR_SYSTEM_UI =
1450             "android.car.permission.MANAGE_CAR_SYSTEM_UI";
1451 
1452     /**
1453      * Intent for connecting to the template renderer. Services that handle this intent must also
1454      * hold {@link #PERMISSION_TEMPLATE_RENDERER}. Applications would not bind to this service
1455      * directly, but instead they would use
1456      * <a href="https://developer.android.com/reference/com/google/android/libraries/car/app/packages">
1457      * Android for Cars App Library</a>.
1458      *
1459      * @hide
1460      */
1461     @SdkConstant(SdkConstantType.SERVICE_ACTION)
1462     @AddedInOrBefore(majorVersion = 33)
1463     public static final String CAR_TEMPLATE_HOST_RENDERER_SERVICE =
1464             "android.car.template.host.RendererService";
1465 
1466     /** @hide */
1467     @IntDef({CONNECTION_TYPE_EMBEDDED})
1468     @Retention(RetentionPolicy.SOURCE)
1469     public @interface ConnectionType {}
1470 
1471     /**
1472      * @deprecated Use {@link CarMediaIntents#ACTION_MEDIA_TEMPLATE} instead.
1473      */
1474     @Deprecated
1475     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
1476     @AddedInOrBefore(majorVersion = 33)
1477     public static final String CAR_INTENT_ACTION_MEDIA_TEMPLATE =
1478             "android.car.intent.action.MEDIA_TEMPLATE";
1479 
1480     /**
1481      * @deprecated Use {@link CarMediaIntents#EXTRA_MEDIA_COMPONENT} instead.
1482      */
1483     @Deprecated
1484     @AddedInOrBefore(majorVersion = 33)
1485     public static final String CAR_EXTRA_MEDIA_COMPONENT =
1486             "android.car.intent.extra.MEDIA_COMPONENT";
1487 
1488     /**
1489      *
1490      * @deprecated Use {@link #CAR_EXTRA_MEDIA_COMPONENT} instead.
1491      * @removed Using this for specifying MediaBrowserService was not supported since API level 29
1492      * and above. Apps must use {@link #CAR_EXTRA_MEDIA_COMPONENT} instead.
1493      */
1494     @Deprecated
1495     @AddedInOrBefore(majorVersion = 33)
1496     public static final String CAR_EXTRA_MEDIA_PACKAGE = "android.car.intent.extra.MEDIA_PACKAGE";
1497 
1498     /**
1499      * Used as a string extra field of media session to specify the service corresponding to the
1500      * session.
1501      */
1502     @AddedInOrBefore(majorVersion = 33)
1503     public static final String CAR_EXTRA_BROWSE_SERVICE_FOR_SESSION =
1504             "android.media.session.BROWSE_SERVICE";
1505 
1506     /**
1507      * Intent for being recognized as a remote task client service.
1508      *
1509      * <p>Services that use this intent must have a {@code PERMISSION_CONTROL_REMOTE_ACCESS}.
1510      *
1511      * @hide
1512      */
1513     @SystemApi
1514     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1515             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1516     public static final String CAR_REMOTEACCESS_REMOTE_TASK_CLIENT_SERVICE =
1517             "android.car.remoteaccess.RemoteTaskClientService";
1518 
1519     /**
1520      * Intent for binding the implementation of {@link
1521      * android.car.occupantconnection.AbstractReceiverService} in the app.
1522      *
1523      * @hide
1524      */
1525     @SdkConstant(SdkConstantType.SERVICE_ACTION)
1526     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
1527             minPlatformVersion = ApiRequirements.PlatformVersion.UPSIDE_DOWN_CAKE_0)
1528     public static final String CAR_INTENT_ACTION_RECEIVER_SERVICE =
1529             "android.car.intent.action.RECEIVER_SERVICE";
1530 
1531     /** @hide */
1532     @VisibleForHiddenApiCheck
1533     @AddedInOrBefore(majorVersion = 33)
1534     public static final String CAR_SERVICE_INTERFACE_NAME = CommonConstants.CAR_SERVICE_INTERFACE;
1535 
1536     private static final String CAR_SERVICE_PACKAGE = "com.android.car";
1537 
1538     private static final String CAR_SERVICE_CLASS = "com.android.car.CarService";
1539 
1540     /**
1541      * Category used by navigation applications to indicate which activity should be launched on
1542      * the instrument cluster when such application holds
1543      * {@link CarAppFocusManager#APP_FOCUS_TYPE_NAVIGATION} focus.
1544      *
1545      * @hide
1546      */
1547     @VisibleForHiddenApiCheck
1548     @AddedInOrBefore(majorVersion = 33)
1549     public static final String CAR_CATEGORY_NAVIGATION = "android.car.cluster.NAVIGATION";
1550 
1551     /**
1552      * When an activity is launched in the cluster, it will receive {@link ClusterActivityState} in
1553      * the intent's extra under this key, containing instrument cluster information such as
1554      * unobscured area, visibility, etc.
1555      *
1556      * @hide
1557      */
1558     @SystemApi
1559     @AddedInOrBefore(majorVersion = 33)
1560     public static final String CAR_EXTRA_CLUSTER_ACTIVITY_STATE =
1561             "android.car.cluster.ClusterActivityState";
1562 
1563 
1564     /**
1565      * Callback to notify the Lifecycle of car service.
1566      *
1567      * <p>Access to car service should happen
1568      * after {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} call with
1569      * {@code ready} set {@code true}.</p>
1570      *
1571      * <p>When {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} is
1572      * called with ready set to false, access to car service should stop until car service is ready
1573      * again from {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} call
1574      * with {@code ready} set to {@code true}.</p>
1575      */
1576     public interface CarServiceLifecycleListener {
1577         /**
1578          * Car service has gone through status change.
1579          *
1580          * <p>This is always called in the main thread context.</p>
1581          *
1582          * @param car {@code Car} object that was originally associated with this lister from
1583          *            {@link #createCar(Context, Handler, long, Car.CarServiceLifecycleListener)}
1584          *            call.
1585          * @param ready When {@code true, car service is ready and all accesses are ok.
1586          *              Otherwise car service has crashed or killed and will be restarted.
1587          */
1588         @AddedInOrBefore(majorVersion = 33)
onLifecycleChanged(@onNull Car car, boolean ready)1589         void onLifecycleChanged(@NonNull Car car, boolean ready);
1590     }
1591 
1592     /**
1593      * {@link #createCar(Context, Handler, long, CarServiceLifecycleListener)}'s
1594      * waitTimeoutMs value to use to wait forever inside the call until car service is ready.
1595      */
1596     @AddedInOrBefore(majorVersion = 33)
1597     public static final long CAR_WAIT_TIMEOUT_WAIT_FOREVER = -1;
1598 
1599     /**
1600      * {@link #createCar(Context, Handler, long, CarServiceLifecycleListener)}'s
1601      * waitTimeoutMs value to use to skip any waiting inside the call.
1602      */
1603     @AddedInOrBefore(majorVersion = 33)
1604     public static final long CAR_WAIT_TIMEOUT_DO_NOT_WAIT = 0;
1605 
1606     private static final long CAR_SERVICE_BIND_RETRY_INTERVAL_MS = 500;
1607     private static final long CAR_SERVICE_BIND_MAX_RETRY = 20;
1608 
1609     private static final long CAR_SERVICE_BINDER_POLLING_INTERVAL_MS = 50;
1610     private static final long CAR_SERVICE_BINDER_POLLING_MAX_RETRY = 100;
1611 
1612     private static final int STATE_DISCONNECTED = 0;
1613     private static final int STATE_CONNECTING = 1;
1614     private static final int STATE_CONNECTED = 2;
1615 
1616     /** @hide */
1617     @Retention(RetentionPolicy.SOURCE)
1618     @IntDef(prefix = "STATE_", value = {
1619             STATE_DISCONNECTED,
1620             STATE_CONNECTING,
1621             STATE_CONNECTED,
1622     })
1623     @Target({ElementType.TYPE_USE})
1624     public @interface StateTypeEnum {}
1625 
1626     /**
1627      * The enabling request was successful and requires reboot to take effect.
1628      * @hide
1629      */
1630     @SystemApi
1631     @AddedInOrBefore(majorVersion = 33)
1632     public static final int FEATURE_REQUEST_SUCCESS = 0;
1633     /**
1634      * The requested feature is already enabled or disabled as requested. No need to reboot the
1635      * system.
1636      * @hide
1637      */
1638     @SystemApi
1639     @AddedInOrBefore(majorVersion = 33)
1640     public static final int FEATURE_REQUEST_ALREADY_IN_THE_STATE = 1;
1641     /**
1642      * The requested feature is mandatory cannot be enabled or disabled. It is always enabled.
1643      * @hide
1644      */
1645     @SystemApi
1646     @AddedInOrBefore(majorVersion = 33)
1647     public static final int FEATURE_REQUEST_MANDATORY = 2;
1648     /**
1649      * The requested feature is not available and cannot be enabled or disabled.
1650      * @hide
1651      */
1652     @SystemApi
1653     @AddedInOrBefore(majorVersion = 33)
1654     public static final int FEATURE_REQUEST_NOT_EXISTING = 3;
1655 
1656     /** @hide */
1657     @Retention(RetentionPolicy.SOURCE)
1658     @IntDef(prefix = "FEATURE_REQUEST_", value = {
1659             FEATURE_REQUEST_SUCCESS,
1660             FEATURE_REQUEST_ALREADY_IN_THE_STATE,
1661             FEATURE_REQUEST_MANDATORY,
1662             FEATURE_REQUEST_NOT_EXISTING,
1663     })
1664     @Target({ElementType.TYPE_USE})
1665     public @interface FeaturerRequestEnum {}
1666 
1667     private static final boolean DBG = false;
1668 
1669     private final Context mContext;
1670 
1671     private final Exception mConstructionStack;
1672 
1673     private final Object mLock = new Object();
1674 
1675     @GuardedBy("mLock")
1676     private ICar mService;
1677     @GuardedBy("mLock")
1678     private boolean mServiceBound;
1679 
1680     @GuardedBy("mLock")
1681     @StateTypeEnum
1682     private int mConnectionState;
1683     @GuardedBy("mLock")
1684     private int mConnectionRetryCount;
1685 
1686     private final Runnable mConnectionRetryRunnable = new Runnable() {
1687         @Override
1688         public void run() {
1689             startCarService();
1690         }
1691     };
1692 
1693     private final Runnable mConnectionRetryFailedRunnable = new Runnable() {
1694         @Override
1695         public void run() {
1696             mServiceConnectionListener.onServiceDisconnected(new ComponentName(CAR_SERVICE_PACKAGE,
1697                     CAR_SERVICE_CLASS));
1698         }
1699     };
1700 
1701     private final ServiceConnection mServiceConnectionListener =
1702             new ServiceConnection() {
1703         @Override
1704         public void onServiceConnected(ComponentName name, IBinder service) {
1705             synchronized (mLock) {
1706                 ICar newService = ICar.Stub.asInterface(service);
1707                 if (newService == null) {
1708                     Log.wtf(TAG_CAR, "null binder service", new RuntimeException());
1709                     return;  // should not happen.
1710                 }
1711                 if (mService != null && mService.asBinder().equals(newService.asBinder())) {
1712                     // already connected.
1713                     return;
1714                 }
1715                 mConnectionState = STATE_CONNECTED;
1716                 mService = newService;
1717             }
1718             if (mStatusChangeCallback != null) {
1719                 mStatusChangeCallback.onLifecycleChanged(Car.this, true);
1720             } else if (mServiceConnectionListenerClient != null) {
1721                 mServiceConnectionListenerClient.onServiceConnected(name, service);
1722             }
1723         }
1724 
1725         @Override
1726         public void onServiceDisconnected(ComponentName name) {
1727             // Car service can pick up feature changes after restart.
1728             mFeatures.resetCache();
1729             synchronized (mLock) {
1730                 if (mConnectionState  == STATE_DISCONNECTED) {
1731                     // can happen when client calls disconnect before onServiceDisconnected call.
1732                     return;
1733                 }
1734                 handleCarDisconnectLocked();
1735             }
1736             if (mStatusChangeCallback != null) {
1737                 mStatusChangeCallback.onLifecycleChanged(Car.this, false);
1738             } else if (mServiceConnectionListenerClient != null) {
1739                 mServiceConnectionListenerClient.onServiceDisconnected(name);
1740             } else {
1741                 // This client does not handle car service restart, so should be terminated.
1742                 finishClient();
1743             }
1744         }
1745     };
1746 
1747     @Nullable
1748     private final ServiceConnection mServiceConnectionListenerClient;
1749 
1750     /** Can be added after ServiceManagerHelper.getService call */
1751     @Nullable
1752     private final CarServiceLifecycleListener mStatusChangeCallback;
1753 
1754     @GuardedBy("mLock")
1755     private final HashMap<String, CarManagerBase> mServiceMap = new HashMap<>();
1756 
1757     /** Handler for generic event dispatching. */
1758     private final Handler mEventHandler;
1759 
1760     private final Handler mMainThreadEventHandler;
1761 
1762     private final CarFeatures mFeatures = new CarFeatures();
1763 
1764     static {
CAR_SERVICE_NAMES.put(CarSensorManager.class, SENSOR_SERVICE)1765         CAR_SERVICE_NAMES.put(CarSensorManager.class, SENSOR_SERVICE);
CAR_SERVICE_NAMES.put(CarInfoManager.class, INFO_SERVICE)1766         CAR_SERVICE_NAMES.put(CarInfoManager.class, INFO_SERVICE);
CAR_SERVICE_NAMES.put(CarAppFocusManager.class, APP_FOCUS_SERVICE)1767         CAR_SERVICE_NAMES.put(CarAppFocusManager.class, APP_FOCUS_SERVICE);
CAR_SERVICE_NAMES.put(CarPackageManager.class, PACKAGE_SERVICE)1768         CAR_SERVICE_NAMES.put(CarPackageManager.class, PACKAGE_SERVICE);
CAR_SERVICE_NAMES.put(CarAudioManager.class, AUDIO_SERVICE)1769         CAR_SERVICE_NAMES.put(CarAudioManager.class, AUDIO_SERVICE);
CAR_SERVICE_NAMES.put(CarNavigationStatusManager.class, CAR_NAVIGATION_SERVICE)1770         CAR_SERVICE_NAMES.put(CarNavigationStatusManager.class, CAR_NAVIGATION_SERVICE);
CAR_SERVICE_NAMES.put(CarOccupantZoneManager.class, CAR_OCCUPANT_ZONE_SERVICE)1771         CAR_SERVICE_NAMES.put(CarOccupantZoneManager.class, CAR_OCCUPANT_ZONE_SERVICE);
CAR_SERVICE_NAMES.put(CarUserManager.class, CAR_USER_SERVICE)1772         CAR_SERVICE_NAMES.put(CarUserManager.class, CAR_USER_SERVICE);
CAR_SERVICE_NAMES.put(ExperimentalCarUserManager.class, EXPERIMENTAL_CAR_USER_SERVICE)1773         CAR_SERVICE_NAMES.put(ExperimentalCarUserManager.class, EXPERIMENTAL_CAR_USER_SERVICE);
CAR_SERVICE_NAMES.put(CarDevicePolicyManager.class, CAR_DEVICE_POLICY_SERVICE)1774         CAR_SERVICE_NAMES.put(CarDevicePolicyManager.class, CAR_DEVICE_POLICY_SERVICE);
CAR_SERVICE_NAMES.put(CarInstrumentClusterManager.class, CAR_INSTRUMENT_CLUSTER_SERVICE)1775         CAR_SERVICE_NAMES.put(CarInstrumentClusterManager.class, CAR_INSTRUMENT_CLUSTER_SERVICE);
CAR_SERVICE_NAMES.put(CarCabinManager.class, CABIN_SERVICE)1776         CAR_SERVICE_NAMES.put(CarCabinManager.class, CABIN_SERVICE);
CAR_SERVICE_NAMES.put(CarDiagnosticManager.class, DIAGNOSTIC_SERVICE)1777         CAR_SERVICE_NAMES.put(CarDiagnosticManager.class, DIAGNOSTIC_SERVICE);
CAR_SERVICE_NAMES.put(CarHvacManager.class, HVAC_SERVICE)1778         CAR_SERVICE_NAMES.put(CarHvacManager.class, HVAC_SERVICE);
CAR_SERVICE_NAMES.put(CarPowerManager.class, POWER_SERVICE)1779         CAR_SERVICE_NAMES.put(CarPowerManager.class, POWER_SERVICE);
CAR_SERVICE_NAMES.put(CarProjectionManager.class, PROJECTION_SERVICE)1780         CAR_SERVICE_NAMES.put(CarProjectionManager.class, PROJECTION_SERVICE);
CAR_SERVICE_NAMES.put(CarPropertyManager.class, PROPERTY_SERVICE)1781         CAR_SERVICE_NAMES.put(CarPropertyManager.class, PROPERTY_SERVICE);
CAR_SERVICE_NAMES.put(CarVendorExtensionManager.class, VENDOR_EXTENSION_SERVICE)1782         CAR_SERVICE_NAMES.put(CarVendorExtensionManager.class, VENDOR_EXTENSION_SERVICE);
CAR_SERVICE_NAMES.put(VmsClientManager.class, VEHICLE_MAP_SERVICE)1783         CAR_SERVICE_NAMES.put(VmsClientManager.class, VEHICLE_MAP_SERVICE);
CAR_SERVICE_NAMES.put(VmsSubscriberManager.class, VMS_SUBSCRIBER_SERVICE)1784         CAR_SERVICE_NAMES.put(VmsSubscriberManager.class, VMS_SUBSCRIBER_SERVICE);
CAR_SERVICE_NAMES.put(CarDrivingStateManager.class, CAR_DRIVING_STATE_SERVICE)1785         CAR_SERVICE_NAMES.put(CarDrivingStateManager.class, CAR_DRIVING_STATE_SERVICE);
CAR_SERVICE_NAMES.put(CarUxRestrictionsManager.class, CAR_UX_RESTRICTION_SERVICE)1786         CAR_SERVICE_NAMES.put(CarUxRestrictionsManager.class, CAR_UX_RESTRICTION_SERVICE);
CAR_SERVICE_NAMES.put(OccupantAwarenessManager.class, OCCUPANT_AWARENESS_SERVICE)1787         CAR_SERVICE_NAMES.put(OccupantAwarenessManager.class, OCCUPANT_AWARENESS_SERVICE);
CAR_SERVICE_NAMES.put(CarMediaManager.class, CAR_MEDIA_SERVICE)1788         CAR_SERVICE_NAMES.put(CarMediaManager.class, CAR_MEDIA_SERVICE);
CAR_SERVICE_NAMES.put(CarBugreportManager.class, CAR_BUGREPORT_SERVICE)1789         CAR_SERVICE_NAMES.put(CarBugreportManager.class, CAR_BUGREPORT_SERVICE);
CAR_SERVICE_NAMES.put(CarStorageMonitoringManager.class, STORAGE_MONITORING_SERVICE)1790         CAR_SERVICE_NAMES.put(CarStorageMonitoringManager.class, STORAGE_MONITORING_SERVICE);
CAR_SERVICE_NAMES.put(CarWatchdogManager.class, CAR_WATCHDOG_SERVICE)1791         CAR_SERVICE_NAMES.put(CarWatchdogManager.class, CAR_WATCHDOG_SERVICE);
CAR_SERVICE_NAMES.put(CarPerformanceManager.class, CAR_PERFORMANCE_SERVICE)1792         CAR_SERVICE_NAMES.put(CarPerformanceManager.class, CAR_PERFORMANCE_SERVICE);
CAR_SERVICE_NAMES.put(CarInputManager.class, CAR_INPUT_SERVICE)1793         CAR_SERVICE_NAMES.put(CarInputManager.class, CAR_INPUT_SERVICE);
CAR_SERVICE_NAMES.put(ClusterHomeManager.class, CLUSTER_HOME_SERVICE)1794         CAR_SERVICE_NAMES.put(ClusterHomeManager.class, CLUSTER_HOME_SERVICE);
CAR_SERVICE_NAMES.put(CarTestManager.class, TEST_SERVICE)1795         CAR_SERVICE_NAMES.put(CarTestManager.class, TEST_SERVICE);
CAR_SERVICE_NAMES.put(CarEvsManager.class, CAR_EVS_SERVICE)1796         CAR_SERVICE_NAMES.put(CarEvsManager.class, CAR_EVS_SERVICE);
CAR_SERVICE_NAMES.put(CarTelemetryManager.class, CAR_TELEMETRY_SERVICE)1797         CAR_SERVICE_NAMES.put(CarTelemetryManager.class, CAR_TELEMETRY_SERVICE);
CAR_SERVICE_NAMES.put(CarActivityManager.class, CAR_ACTIVITY_SERVICE)1798         CAR_SERVICE_NAMES.put(CarActivityManager.class, CAR_ACTIVITY_SERVICE);
CAR_SERVICE_NAMES.put(CarRemoteAccessManager.class, CAR_REMOTE_ACCESS_SERVICE)1799         CAR_SERVICE_NAMES.put(CarRemoteAccessManager.class, CAR_REMOTE_ACCESS_SERVICE);
CAR_SERVICE_NAMES.put(CarOccupantConnectionManager.class, CAR_OCCUPANT_CONNECTION_SERVICE)1800         CAR_SERVICE_NAMES.put(CarOccupantConnectionManager.class, CAR_OCCUPANT_CONNECTION_SERVICE);
CAR_SERVICE_NAMES.put(CarRemoteDeviceManager.class, CAR_REMOTE_DEVICE_SERVICE)1801         CAR_SERVICE_NAMES.put(CarRemoteDeviceManager.class, CAR_REMOTE_DEVICE_SERVICE);
1802         // Note: if a new entry is added here, the capacity of CAR_SERVICE_NAMES should be increased
1803         // as well.
1804     }
1805 
1806     /**
1807      * Defines the {@link CarVersion version} of the {@code Car} APIs in the device.
1808      *
1809      * <p>Starting on {@link android.os.Build.VERSION_CODES#TIRAMISU Android 13}, the {@code Car}
1810      * APIs can be upgraded without an OTA, so it's possible that these APIs are higher than the
1811      * {@link #getPlatformVersion() platform's}.
1812      */
1813     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_1,
1814             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1815     @NonNull
getCarVersion()1816     public static android.car.CarVersion getCarVersion() {
1817         return CAR_VERSION;
1818     }
1819 
1820     /**
1821      * Defines the {@link PlatformVersion version} of the standard {@code SDK} APIs in the
1822      * device.
1823      *
1824      * <p>Its {@link ApiVersion#getMajorVersion() major version} will be the same as
1825      * {@link android.os.Build.VERSION#SDK_INT} for released build but will be
1826      * {@link android.os.Build.VERSION_CODES#CUR_DEVELOPMENT} for platform still under development.
1827      */
1828     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_1,
1829             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1830     @NonNull
getPlatformVersion()1831     public static android.car.PlatformVersion getPlatformVersion() {
1832         return PLATFORM_VERSION;
1833     }
1834 
1835     /**
1836      * @deprecated - use {@code getCarApiVersion().isAtLeast(CarVersion.forMajorAndMinorVersions(
1837      * requiredApiVersionMajor))} instead
1838      */
1839     @Deprecated
1840     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
1841             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
isApiVersionAtLeast(int requiredApiVersionMajor)1842     public static boolean isApiVersionAtLeast(int requiredApiVersionMajor) {
1843         return getCarVersion().isAtLeast(CarVersion.forMajorVersion(requiredApiVersionMajor));
1844     }
1845 
1846     /**
1847      * @deprecated - use {@code getCarVersion().isAtLeast(CarVersion.forMajorAndMinorVersions(
1848      * requiredApiVersionMajor, requiredApiVersionMinor)} instead
1849      */
1850     @Deprecated
1851     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
1852             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
isApiVersionAtLeast(int requiredApiVersionMajor, int requiredApiVersionMinor)1853     public static boolean isApiVersionAtLeast(int requiredApiVersionMajor,
1854             int requiredApiVersionMinor) {
1855         return getCarVersion()
1856                 .isAtLeast(CarVersion.forMajorAndMinorVersions(requiredApiVersionMajor,
1857                         requiredApiVersionMinor));
1858     }
1859 
1860     /**
1861      * @deprecated - use
1862      * {@code getCarVersion().isAtLeast(CarVersion.forMajorVersion(requiredApiVersionMajor))
1863      * && getPlatformVersion().isAtLeast(PlatformVersion.forMajorVersion(minPlatformSdkInt))}
1864      * instead.
1865      */
1866     @Deprecated
1867     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
1868             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
isApiAndPlatformVersionAtLeast(int requiredApiVersionMajor, int minPlatformSdkInt)1869     public static boolean isApiAndPlatformVersionAtLeast(int requiredApiVersionMajor,
1870             int minPlatformSdkInt) {
1871         return getCarVersion().isAtLeast(CarVersion.forMajorVersion(requiredApiVersionMajor))
1872                 && getPlatformVersion()
1873                         .isAtLeast(PlatformVersion.forMajorVersion(minPlatformSdkInt));
1874     }
1875 
1876     /**
1877      * @deprecated - use {@code getCarVersion().isAtLeast(CarVersion.forMajorAndMinorVersions(
1878      * requiredApiVersionMajor, requiredApiVersionMinor)) && getPlatformVersion().isAtLeast(
1879      * PlatformVersion.forMajorVersion(minPlatformSdkInt))} instead.
1880      */
1881     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_0,
1882             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
1883     @Deprecated
isApiAndPlatformVersionAtLeast(int requiredApiVersionMajor, int requiredApiVersionMinor, int minPlatformSdkInt)1884     public static boolean isApiAndPlatformVersionAtLeast(int requiredApiVersionMajor,
1885             int requiredApiVersionMinor, int minPlatformSdkInt) {
1886         return getCarVersion()
1887                 .isAtLeast(CarVersion.forMajorAndMinorVersions(requiredApiVersionMajor,
1888                         requiredApiVersionMinor))
1889                 && getPlatformVersion()
1890                         .isAtLeast(PlatformVersion.forMajorVersion(minPlatformSdkInt));
1891     }
1892 
1893     /**
1894      * A factory method that creates Car instance for all Car API access.
1895      *
1896      * <p>Instance created with this should be disconnected from car service by calling
1897      * {@link #disconnect()} before the passed {code Context} is released.
1898      *
1899      * @param context This should not be {@code null}. If you are passing {@link ContextWrapper},
1900      *                make sure that its {@link ContextWrapper#getBaseContext() base context} is not
1901      *                {@code null} as well.
1902      *                Otherwise it will throw {@link java.lang.NullPointerException}.
1903      * @param serviceConnectionListener listener for monitoring service connection.
1904      * @param handler the handler on which the callback should execute, or null to execute on the
1905      * service's main thread. Note: the service connection listener will be always on the main
1906      * thread regardless of the handler given.
1907      * @return Car instance if system is in car environment and returns {@code null} otherwise.
1908      *
1909      * @deprecated use {@link #createCar(Context, Handler)} instead.
1910      */
1911     @Deprecated
1912     @AddedInOrBefore(majorVersion = 33)
createCar(Context context, ServiceConnection serviceConnectionListener, @Nullable Handler handler)1913     public static Car createCar(Context context, ServiceConnection serviceConnectionListener,
1914             @Nullable Handler handler) {
1915         assertNonNullContext(context);
1916         if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
1917             Log.e(TAG_CAR, "FEATURE_AUTOMOTIVE not declared while android.car is used");
1918             return null;
1919         }
1920         try {
1921             return new Car(context, /* service= */ null , serviceConnectionListener,
1922                     /* statusChangeListener= */ null, handler);
1923         } catch (IllegalArgumentException e) {
1924             // Expected when car service loader is not available.
1925         }
1926         return null;
1927     }
1928 
1929     /**
1930      * A factory method that creates Car instance for all Car API access using main thread {@code
1931      * Looper}.
1932      *
1933      * <p>Instance created with this should be disconnected from car service by calling
1934      * {@link #disconnect()} before the passed {code Context} is released.
1935      *
1936      * @see #createCar(Context, ServiceConnection, Handler)
1937      *
1938      * @deprecated use {@link #createCar(Context, Handler)} instead.
1939      */
1940     @Deprecated
1941     @AddedInOrBefore(majorVersion = 33)
createCar(Context context, ServiceConnection serviceConnectionListener)1942     public static Car createCar(Context context, ServiceConnection serviceConnectionListener) {
1943         return createCar(context, serviceConnectionListener, null);
1944     }
1945 
1946     /**
1947      * Creates new {@link Car} object which connected synchronously to Car Service and ready to use.
1948      *
1949      * <p>Instance created with this should be disconnected from car service by calling
1950      * {@link #disconnect()} before the passed {code Context} is released.
1951      *
1952      * @param context application's context
1953      *
1954      * @return Car object if operation succeeded, otherwise null.
1955      */
1956     @Nullable
1957     @AddedInOrBefore(majorVersion = 33)
createCar(Context context)1958     public static Car createCar(Context context) {
1959         return createCar(context, (Handler) null);
1960     }
1961 
1962     /**
1963      * Creates new {@link Car} object which connected synchronously to Car Service and ready to use.
1964      *
1965      * <p>Instance created with this should be disconnected from car service by calling
1966      * {@link #disconnect()} before the passed {code Context} is released.
1967      *
1968      * @param context This should not be {@code null}. If you are passing {@link ContextWrapper},
1969      *                make sure that its {@link ContextWrapper#getBaseContext() base context} is not
1970      *                {@code null} as well.
1971      *                Otherwise it will throw {@link java.lang.NullPointerException}.
1972      * @param handler the handler on which the manager's callbacks will be executed, or null to
1973      * execute on the application's main thread.
1974      *
1975      * @return Car object if operation succeeded, otherwise null.
1976      */
1977     @Nullable
1978     @AddedInOrBefore(majorVersion = 33)
createCar(Context context, @Nullable Handler handler)1979     public static Car createCar(Context context, @Nullable Handler handler) {
1980         assertNonNullContext(context);
1981         Car car = null;
1982         IBinder service = null;
1983         boolean started = false;
1984         int retryCount = 0;
1985         while (true) {
1986             service = ServiceManagerHelper.getService(CAR_SERVICE_BINDER_SERVICE_NAME);
1987             if (car == null) {
1988                 // service can be still null. The constructor is safe for null service.
1989                 car = new Car(context, ICar.Stub.asInterface(service),
1990                         null /*serviceConnectionListener*/, null /*statusChangeListener*/, handler);
1991             }
1992             if (service != null) {
1993                 if (!started) {  // specialization for most common case.
1994                     // Do this to crash client when car service crashes.
1995                     car.startCarService();
1996                     return car;
1997                 }
1998                 break;
1999             }
2000             if (!started) {
2001                 car.startCarService();
2002                 started = true;
2003             }
2004             retryCount++;
2005             if (retryCount > CAR_SERVICE_BINDER_POLLING_MAX_RETRY) {
2006                 Log.e(TAG_CAR, "cannot get car_service, waited for car service (ms):"
2007                                 + CAR_SERVICE_BINDER_POLLING_INTERVAL_MS
2008                                 * CAR_SERVICE_BINDER_POLLING_MAX_RETRY,
2009                         new RuntimeException());
2010                 return null;
2011             }
2012             try {
2013                 Thread.sleep(CAR_SERVICE_BINDER_POLLING_INTERVAL_MS);
2014             } catch (InterruptedException e) {
2015                 Log.e(CarLibLog.TAG_CAR, "interrupted while waiting for car_service",
2016                         new RuntimeException());
2017                 return null;
2018             }
2019         }
2020         // Can be accessed from mServiceConnectionListener in main thread.
2021         synchronized (car) {
2022             if (car.mService == null) {
2023                 car.mService = ICar.Stub.asInterface(service);
2024                 Log.w(TAG_CAR,
2025                         "waited for car_service (ms):"
2026                                 + CAR_SERVICE_BINDER_POLLING_INTERVAL_MS * retryCount,
2027                         new RuntimeException());
2028             }
2029             car.mConnectionState = STATE_CONNECTED;
2030         }
2031         return car;
2032     }
2033 
2034     /**
2035      * Creates new {@link Car} object with {@link CarServiceLifecycleListener}.
2036      *
2037      * <p>Instance created with this should be disconnected from car service by calling
2038      * {@link #disconnect()} before the passed {code Context} is released.
2039      *
2040      * <p> If car service is ready inside this call and if the caller is running in the main thread,
2041      * {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} will be called
2042      * with ready set to be true. Otherwise,
2043      * {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} will be called
2044      * from the main thread later. </p>
2045      *
2046      * <p>This call can block up to specified waitTimeoutMs to wait for car service to be ready.
2047      * If car service is not ready within the given time, it will return a Car instance in
2048      * disconnected state. Blocking main thread forever can lead into getting ANR (Application Not
2049      * Responding) killing from system and should not be used if the app is supposed to survive
2050      * across the crash / restart of car service. It can be still useful in case the app cannot do
2051      * anything without car service being ready. In any waiting, if the thread is getting
2052      * interrupted, it will return immediately.
2053      * </p>
2054      *
2055      * <p>Note that returned {@link Car} object is not guaranteed to be connected when there is
2056      * a limited timeout. Regardless of returned car being connected or not, it is recommended to
2057      * implement all car related initialization inside
2058      * {@link CarServiceLifecycleListener#onLifecycleChanged(Car, boolean)} and avoid the
2059      * needs to check if returned {@link Car} is connected or not from returned {@link Car}.</p>
2060      *
2061      * @param context This should not be {@code null}. If you are passing {@link ContextWrapper},
2062      *                make sure that its {@link ContextWrapper#getBaseContext() base context} is not
2063      *                {@code null} as well.
2064      *                Otherwise it will throw {@link java.lang.NullPointerException}.
2065      * @param handler dispatches all Car*Manager events to this Handler. Exception is
2066      *                {@link CarServiceLifecycleListener} which will be always dispatched to main
2067      *                thread. Passing null leads into dispatching all Car*Manager callbacks to main
2068      *                thread as well.
2069      * @param waitTimeoutMs Setting this to {@link #CAR_WAIT_TIMEOUT_DO_NOT_WAIT} will guarantee
2070      *                      that the API does not wait for the car service at all. Setting this to
2071      *                      to {@link #CAR_WAIT_TIMEOUT_WAIT_FOREVER} will block the call forever
2072      *                      until the car service is ready. Setting any positive value will be
2073      *                      interpreted as timeout value.
2074      */
2075     @NonNull
2076     @AddedInOrBefore(majorVersion = 33)
createCar(@onNull Context context, @Nullable Handler handler, long waitTimeoutMs, @NonNull CarServiceLifecycleListener statusChangeListener)2077     public static Car createCar(@NonNull Context context,
2078             @Nullable Handler handler, long waitTimeoutMs,
2079             @NonNull CarServiceLifecycleListener statusChangeListener) {
2080         assertNonNullContext(context);
2081         Objects.requireNonNull(statusChangeListener);
2082         Car car = null;
2083         IBinder service = null;
2084         boolean started = false;
2085         int retryCount = 0;
2086         long maxRetryCount = 0;
2087         if (waitTimeoutMs > 0) {
2088             maxRetryCount = waitTimeoutMs / CAR_SERVICE_BINDER_POLLING_INTERVAL_MS;
2089             // at least wait once if it is positive value.
2090             if (maxRetryCount == 0) {
2091                 maxRetryCount = 1;
2092             }
2093         }
2094         boolean isMainThread = Looper.myLooper() == Looper.getMainLooper();
2095         while (true) {
2096             service = ServiceManagerHelper.getService(CAR_SERVICE_BINDER_SERVICE_NAME);
2097             if (car == null) {
2098                 // service can be still null. The constructor is safe for null service.
2099                 car = new Car(context, ICar.Stub.asInterface(service), null, statusChangeListener,
2100                         handler);
2101             }
2102             if (service != null) {
2103                 if (!started) {  // specialization for most common case : car service already ready
2104                     car.dispatchCarReadyToMainThread(isMainThread);
2105                     // Needs this for CarServiceLifecycleListener. Note that ServiceConnection
2106                     // will skip the callback as valid mService is set already.
2107                     car.startCarService();
2108                     return car;
2109                 }
2110                 // service available after starting.
2111                 break;
2112             }
2113             if (!started) {
2114                 car.startCarService();
2115                 started = true;
2116             }
2117             retryCount++;
2118             if (waitTimeoutMs < 0 && retryCount >= CAR_SERVICE_BINDER_POLLING_MAX_RETRY
2119                     && retryCount % CAR_SERVICE_BINDER_POLLING_MAX_RETRY == 0) {
2120                 // Log warning if car service is not alive even for waiting forever case.
2121                 Log.w(TAG_CAR, "car_service not ready, waited for car service (ms):"
2122                                 + retryCount * CAR_SERVICE_BINDER_POLLING_INTERVAL_MS,
2123                         new RuntimeException());
2124             } else if (waitTimeoutMs >= 0 && retryCount > maxRetryCount) {
2125                 if (waitTimeoutMs > 0) {
2126                     Log.w(TAG_CAR, "car_service not ready, waited for car service (ms):"
2127                                     + waitTimeoutMs,
2128                             new RuntimeException());
2129                 }
2130                 return car;
2131             }
2132 
2133             try {
2134                 Thread.sleep(CAR_SERVICE_BINDER_POLLING_INTERVAL_MS);
2135             } catch (InterruptedException e) {
2136                 Thread.currentThread().interrupt();
2137                 Log.w(TAG_CAR, "interrupted", new RuntimeException());
2138                 return car;
2139             }
2140         }
2141         // Can be accessed from mServiceConnectionListener in main thread.
2142         synchronized (car.mLock) {
2143             Log.w(TAG_CAR,
2144                     "waited for car_service (ms):"
2145                             + retryCount * CAR_SERVICE_BINDER_POLLING_INTERVAL_MS,
2146                     new RuntimeException());
2147             // ServiceConnection has handled everything.
2148             if (car.mService != null) {
2149                 return car;
2150             }
2151             // mService check in ServiceConnection prevents calling
2152             // onLifecycleChanged. So onLifecycleChanged should be called explicitly
2153             // but do it outside lock.
2154             car.mService = ICar.Stub.asInterface(service);
2155             car.mConnectionState = STATE_CONNECTED;
2156         }
2157         car.dispatchCarReadyToMainThread(isMainThread);
2158         return car;
2159     }
2160 
assertNonNullContext(Context context)2161     private static void assertNonNullContext(Context context) {
2162         Objects.requireNonNull(context);
2163         if (context instanceof ContextWrapper
2164                 && ((ContextWrapper) context).getBaseContext() == null) {
2165             throw new NullPointerException(
2166                     "ContextWrapper with null base passed as Context, forgot to set base Context?");
2167         }
2168     }
2169 
dispatchCarReadyToMainThread(boolean isMainThread)2170     private void dispatchCarReadyToMainThread(boolean isMainThread) {
2171         if (isMainThread) {
2172             mStatusChangeCallback.onLifecycleChanged(this, true);
2173         } else {
2174             // should dispatch to main thread.
2175             mMainThreadEventHandler.post(
2176                     () -> mStatusChangeCallback.onLifecycleChanged(this, true));
2177         }
2178     }
2179 
Car(Context context, @Nullable ICar service, @Nullable ServiceConnection serviceConnectionListener, @Nullable CarServiceLifecycleListener statusChangeListener, @Nullable Handler handler)2180     private Car(Context context, @Nullable ICar service,
2181             @Nullable ServiceConnection serviceConnectionListener,
2182             @Nullable CarServiceLifecycleListener statusChangeListener,
2183             @Nullable Handler handler) {
2184         mContext = context;
2185         mEventHandler = determineEventHandler(handler);
2186         mMainThreadEventHandler = determineMainThreadEventHandler(mEventHandler);
2187 
2188         mService = service;
2189         if (service != null) {
2190             mConnectionState = STATE_CONNECTED;
2191         } else {
2192             mConnectionState = STATE_DISCONNECTED;
2193         }
2194         mServiceConnectionListenerClient = serviceConnectionListener;
2195         mStatusChangeCallback = statusChangeListener;
2196         // Store construction stack so that client can get help when it crashes when car service
2197         // crashes.
2198         if (serviceConnectionListener == null && statusChangeListener == null) {
2199             mConstructionStack = new RuntimeException();
2200         } else {
2201             mConstructionStack = null;
2202         }
2203     }
2204 
2205     /**
2206      * Car constructor when ICar binder is already available. The binder can be null.
2207      * @hide
2208      */
Car(Context context, @Nullable ICar service, @Nullable Handler handler)2209     public Car(Context context, @Nullable ICar service, @Nullable Handler handler) {
2210         this(context, service, null /*serviceConnectionListener*/, null /*statusChangeListener*/,
2211                 handler);
2212     }
2213 
determineMainThreadEventHandler(Handler eventHandler)2214     private static Handler determineMainThreadEventHandler(Handler eventHandler) {
2215         Looper mainLooper = Looper.getMainLooper();
2216         return (eventHandler.getLooper() == mainLooper) ? eventHandler : new Handler(mainLooper);
2217     }
2218 
determineEventHandler(@ullable Handler eventHandler)2219     private static Handler determineEventHandler(@Nullable Handler eventHandler) {
2220         Handler handler = eventHandler;
2221 
2222         if (handler == null) {
2223             Looper looper = Looper.getMainLooper();
2224             handler = new Handler(looper);
2225         }
2226         return handler;
2227     }
2228 
2229     /**
2230      * Connect to car service. This can be called while it is disconnected.
2231      * @throws IllegalStateException If connection is still on-going from previous
2232      *         connect call or it is already connected
2233      *
2234      * @deprecated this method is not need if this object is created via
2235      * {@link #createCar(Context, Handler)}.
2236      */
2237     @Deprecated
2238     @AddedInOrBefore(majorVersion = 33)
connect()2239     public void connect() throws IllegalStateException {
2240         synchronized (mLock) {
2241             if (mConnectionState != STATE_DISCONNECTED) {
2242                 throw new IllegalStateException("already connected or connecting");
2243             }
2244             mConnectionState = STATE_CONNECTING;
2245             startCarService();
2246         }
2247     }
2248 
handleCarDisconnectLocked()2249     private void handleCarDisconnectLocked() {
2250         if (mConnectionState == STATE_DISCONNECTED) {
2251             // can happen when client calls disconnect with onServiceDisconnected already called.
2252             return;
2253         }
2254         mEventHandler.removeCallbacks(mConnectionRetryRunnable);
2255         mMainThreadEventHandler.removeCallbacks(mConnectionRetryFailedRunnable);
2256         mConnectionRetryCount = 0;
2257         tearDownCarManagersLocked();
2258         mService = null;
2259         mConnectionState = STATE_DISCONNECTED;
2260     }
2261 
2262     /**
2263      * Disconnect from car service. This can be called while disconnected. Once disconnect is
2264      * called, all Car*Managers from this instance become invalid, and
2265      * {@link Car#getCarManager(String)} or {@link Car#getCarManager(Class<T>)} will return a
2266      * different instance if it is connected again.
2267      */
2268     @AddedInOrBefore(majorVersion = 33)
disconnect()2269     public void disconnect() {
2270         synchronized (mLock) {
2271             handleCarDisconnectLocked();
2272             if (mServiceBound) {
2273                 mContext.unbindService(mServiceConnectionListener);
2274                 mServiceBound = false;
2275             }
2276         }
2277     }
2278 
2279     @Override
2280     @SuppressWarnings("GenericException")
finalize()2281     protected void finalize() throws Throwable {
2282         if (mLock == null) {
2283             // There's no point of trying anything - even logging - when the object state is already
2284             // cleared
2285             super.finalize();
2286             return;
2287         }
2288         try {
2289             Log.v(TAG_CAR, "Calling disconnect() on finalize()");
2290             disconnect();
2291         } finally {
2292             super.finalize();
2293         }
2294     }
2295 
2296     /**
2297      * Tells if it is connected to the service or not. This will return false if it is still
2298      * connecting.
2299      * @return
2300      */
2301     @AddedInOrBefore(majorVersion = 33)
isConnected()2302     public boolean isConnected() {
2303         synchronized (mLock) {
2304             return mService != null;
2305         }
2306     }
2307 
2308     /**
2309      * Tells if this instance is already connecting to car service or not.
2310      * @return
2311      */
2312     @AddedInOrBefore(majorVersion = 33)
isConnecting()2313     public boolean isConnecting() {
2314         synchronized (mLock) {
2315             return mConnectionState == STATE_CONNECTING;
2316         }
2317     }
2318 
2319     /** @hide */
2320     @VisibleForTesting
2321     @AddedInOrBefore(majorVersion = 33)
getServiceConnectionListener()2322     public ServiceConnection getServiceConnectionListener() {
2323         return mServiceConnectionListener;
2324     }
2325 
2326     /**
2327      * Get car specific service manager as in {@link Context#getSystemService(String)}. Returned
2328      * {@link Object} should be type-casted to the desired service manager.
2329      *
2330      * <p>For example, to get the manager for sensor service,
2331      * <code>
2332      * CarSensorManager carSensorManager = (CarSensorManager) car.getCarManager(Car.SENSOR_SERVICE);
2333      * </code>
2334      *
2335      * @param serviceName Name of service that should be created like {@link #SENSOR_SERVICE}.
2336      * @return Matching service manager or null if there is no such service.
2337      */
2338     @Nullable
2339     @AddedInOrBefore(majorVersion = 33)
getCarManager(String serviceName)2340     public Object getCarManager(String serviceName) {
2341         CarManagerBase manager;
2342         synchronized (mLock) {
2343             if (mService == null) {
2344                 Log.w(TAG_CAR, "getCarManager not working while car service not ready");
2345                 return null;
2346             }
2347             manager = mServiceMap.get(serviceName);
2348             if (manager == null) {
2349                 try {
2350                     IBinder binder = mService.getCarService(serviceName);
2351                     if (binder == null) {
2352                         Log.w(TAG_CAR, "getCarManager could not get binder for service:"
2353                                 + serviceName);
2354                         return null;
2355                     }
2356                     manager = createCarManagerLocked(serviceName, binder);
2357                     if (manager == null) {
2358                         Log.w(TAG_CAR, "getCarManager could not create manager for service:"
2359                                         + serviceName);
2360                         return null;
2361                     }
2362                     mServiceMap.put(serviceName, manager);
2363                 } catch (RemoteException e) {
2364                     handleRemoteExceptionFromCarService(e);
2365                 }
2366             }
2367         }
2368         return manager;
2369     }
2370 
2371     /**
2372      * Get car specific service manager by class as in {@link Context#getSystemService(Class<T>)}.
2373      * Returns the desired service. No type casting is needed.
2374      *
2375      * <p>For example, to get the manager for sensor service,
2376      * <code>CarSensorManager carSensorManager = car.getCarManager(CarSensorManager.class);</code>
2377      *
2378      * @param serviceClass The class of the desired service.
2379      * @return Matching service manager or {@code null} if there is no such service.
2380      */
2381     @Nullable
2382     @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0,
2383             minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
getCarManager(@onNull Class<T> serviceClass)2384     public <T> T getCarManager(@NonNull Class<T> serviceClass) {
2385         String serviceName = CAR_SERVICE_NAMES.get(serviceClass);
2386         return serviceName != null ? (T) getCarManager(serviceName) : null;
2387     }
2388 
2389     /**
2390      * @return the type of currently connected car.
2391      *
2392      * @deprecated connection type will be always {@link #CONNECTION_TYPE_EMBEDDED}
2393      */
2394     @ConnectionType
2395     @Deprecated
2396     @AddedInOrBefore(majorVersion = 33)
getCarConnectionType()2397     public int getCarConnectionType() {
2398         return CONNECTION_TYPE_EMBEDDED;
2399     }
2400 
2401     /**
2402      * Checks if {code featureName} is enabled in this car.
2403      *
2404      * <p>For optional features, this can return false if the car cannot support it. Optional
2405      * features should be used only when they are supported.</p>
2406      *
2407      * <p>For mandatory features, this will always return true.
2408      */
2409     @AddedInOrBefore(majorVersion = 33)
isFeatureEnabled(@onNull String featureName)2410     public boolean isFeatureEnabled(@NonNull String featureName) {
2411         ICar service;
2412         synchronized (mLock) {
2413             if (mService == null) {
2414                 return false;
2415             }
2416             service = mService;
2417         }
2418         return mFeatures.isFeatureEnabled(service, featureName);
2419     }
2420 
2421     /**
2422      * Enables the requested car feature. It becomes no-op if the feature is already enabled. The
2423      * change take effects after reboot.
2424      *
2425      * @return true if the feature is enabled or was enabled before.
2426      *
2427      * @hide
2428      */
2429     @SystemApi
2430     @RequiresPermission(PERMISSION_CONTROL_CAR_FEATURES)
2431     @FeaturerRequestEnum
2432     @AddedInOrBefore(majorVersion = 33)
enableFeature(@onNull String featureName)2433     public int enableFeature(@NonNull String featureName) {
2434         ICar service;
2435         synchronized (mLock) {
2436             if (mService == null) {
2437                 return FEATURE_REQUEST_NOT_EXISTING;
2438             }
2439             service = mService;
2440         }
2441         try {
2442             return service.enableFeature(featureName);
2443         } catch (RemoteException e) {
2444             return handleRemoteExceptionFromCarService(e, FEATURE_REQUEST_NOT_EXISTING);
2445         }
2446     }
2447 
2448     /**
2449      * Disables the requested car feature. It becomes no-op if the feature is already disabled. The
2450      * change take effects after reboot.
2451      *
2452      * @return true if the request succeeds or if it was already disabled.
2453      *
2454      * @hide
2455      */
2456     @SystemApi
2457     @RequiresPermission(PERMISSION_CONTROL_CAR_FEATURES)
2458     @FeaturerRequestEnum
2459     @AddedInOrBefore(majorVersion = 33)
disableFeature(@onNull String featureName)2460     public int disableFeature(@NonNull String featureName) {
2461         ICar service;
2462         synchronized (mLock) {
2463             if (mService == null) {
2464                 return FEATURE_REQUEST_NOT_EXISTING;
2465             }
2466             service = mService;
2467         }
2468         try {
2469             return service.disableFeature(featureName);
2470         } catch (RemoteException e) {
2471             return handleRemoteExceptionFromCarService(e, FEATURE_REQUEST_NOT_EXISTING);
2472         }
2473     }
2474 
2475     /**
2476      * Returns all =enabled features at the moment including mandatory, optional, and
2477      * experimental features.
2478      *
2479      * @hide
2480      */
2481     @SystemApi
2482     @RequiresPermission(PERMISSION_CONTROL_CAR_FEATURES)
2483     @AddedInOrBefore(majorVersion = 33)
getAllEnabledFeatures()2484     @NonNull public List<String> getAllEnabledFeatures() {
2485         ICar service;
2486         synchronized (mLock) {
2487             if (mService == null) {
2488                 return Collections.EMPTY_LIST;
2489             }
2490             service = mService;
2491         }
2492         try {
2493             return service.getAllEnabledFeatures();
2494         } catch (RemoteException e) {
2495             return handleRemoteExceptionFromCarService(e, Collections.EMPTY_LIST);
2496         }
2497     }
2498 
2499     /**
2500      * Returns the list of disabled features which are not effective yet. Those features will be
2501      * disabled when system restarts later.
2502      *
2503      * @hide
2504      */
2505     @SystemApi
2506     @RequiresPermission(PERMISSION_CONTROL_CAR_FEATURES)
2507     @AddedInOrBefore(majorVersion = 33)
getAllPendingDisabledFeatures()2508     @NonNull public List<String> getAllPendingDisabledFeatures() {
2509         ICar service;
2510         synchronized (mLock) {
2511             if (mService == null) {
2512                 return Collections.EMPTY_LIST;
2513             }
2514             service = mService;
2515         }
2516         try {
2517             return service.getAllPendingDisabledFeatures();
2518         } catch (RemoteException e) {
2519             return handleRemoteExceptionFromCarService(e, Collections.EMPTY_LIST);
2520         }
2521     }
2522 
2523     /**
2524      * Returns the list of enabled features which are not effective yet. Those features will be
2525      * enabled when system restarts later.
2526      *
2527      * @hide
2528      */
2529     @SystemApi
2530     @RequiresPermission(PERMISSION_CONTROL_CAR_FEATURES)
2531     @AddedInOrBefore(majorVersion = 33)
getAllPendingEnabledFeatures()2532     @NonNull public List<String> getAllPendingEnabledFeatures() {
2533         ICar service;
2534         synchronized (mLock) {
2535             if (mService == null) {
2536                 return Collections.EMPTY_LIST;
2537             }
2538             service = mService;
2539         }
2540         try {
2541             return service.getAllPendingEnabledFeatures();
2542         } catch (RemoteException e) {
2543             return handleRemoteExceptionFromCarService(e, Collections.EMPTY_LIST);
2544         }
2545     }
2546 
2547     /** @hide */
2548     @VisibleForHiddenApiCheck
2549     @AddedInOrBefore(majorVersion = 33)
getContext()2550     public Context getContext() {
2551         return mContext;
2552     }
2553 
2554     /** @hide */
2555     @VisibleForTesting
2556     @AddedInOrBefore(majorVersion = 33)
getEventHandler()2557     public Handler getEventHandler() {
2558         return mEventHandler;
2559     }
2560 
2561     /** @hide */
2562     @VisibleForTesting
2563     @AddedInOrBefore(majorVersion = 33)
handleRemoteExceptionFromCarService(RemoteException e, T returnValue)2564     public <T> T handleRemoteExceptionFromCarService(RemoteException e, T returnValue) {
2565         handleRemoteExceptionFromCarService(e);
2566         return returnValue;
2567     }
2568 
2569     /** @hide */
2570     @VisibleForHiddenApiCheck
2571     @AddedInOrBefore(majorVersion = 33)
handleRemoteExceptionFromCarService(RemoteException e)2572     public void handleRemoteExceptionFromCarService(RemoteException e) {
2573         if (e instanceof TransactionTooLargeException) {
2574             Log.w(TAG_CAR, "Car service threw TransactionTooLargeException", e);
2575             throw new CarTransactionException(e, "Car service threw TransactionTooLargeException");
2576         } else {
2577             Log.w(TAG_CAR, "Car service has crashed", e);
2578         }
2579     }
2580 
finishClient()2581     private void finishClient() {
2582         if (mContext == null) {
2583             throw new IllegalStateException("Car service has crashed, null Context");
2584         }
2585         if (mContext instanceof Activity) {
2586             Activity activity = (Activity) mContext;
2587             if (!activity.isFinishing()) {
2588                 Log.w(TAG_CAR,
2589                         "Car service crashed, client not handling it, finish Activity, created "
2590                                 + "from " + mConstructionStack);
2591                 activity.finish();
2592             }
2593             return;
2594         } else if (mContext instanceof Service) {
2595             Service service = (Service) mContext;
2596             killClient(service.getPackageName() + "," + service.getClass().getSimpleName());
2597         } else {
2598             killClient(/* clientInfo= */ null);
2599         }
2600     }
2601 
killClient(@ullable String clientInfo)2602     private void killClient(@Nullable String clientInfo) {
2603         Log.w(TAG_CAR, "**Car service has crashed. Client(" + clientInfo + ") is not handling it."
2604                         + " Client should use Car.createCar(..., CarServiceLifecycleListener, .."
2605                         + ".) to handle it properly. Check printed callstack to check where other "
2606                         + "version of Car.createCar() was called. Killing the client process**",
2607                 mConstructionStack);
2608         Process.killProcess(Process.myPid());
2609     }
2610 
2611     /** @hide */
2612     @VisibleForHiddenApiCheck
2613     @AddedInOrBefore(majorVersion = 33)
handleRemoteExceptionFromCarService(Service service, RemoteException e, T returnValue)2614     public static <T> T handleRemoteExceptionFromCarService(Service service, RemoteException e,
2615             T returnValue) {
2616         handleRemoteExceptionFromCarService(service, e);
2617         return returnValue;
2618     }
2619 
2620     /** @hide */
2621     @VisibleForHiddenApiCheck
2622     @AddedInOrBefore(majorVersion = 33)
handleRemoteExceptionFromCarService(Service service, RemoteException e)2623     public static  void handleRemoteExceptionFromCarService(Service service, RemoteException e) {
2624         if (e instanceof TransactionTooLargeException) {
2625             Log.w(TAG_CAR, "Car service threw TransactionTooLargeException, client:"
2626                     + service.getPackageName() + ","
2627                     + service.getClass().getSimpleName(), e);
2628             throw new CarTransactionException(e, "Car service threw TransactionTooLargeException, "
2629                 + "client: %s, %s", service.getPackageName(), service.getClass().getSimpleName());
2630         } else {
2631             Log.w(TAG_CAR, "Car service has crashed, client:"
2632                     + service.getPackageName() + ","
2633                     + service.getClass().getSimpleName(), e);
2634             service.stopSelf();
2635         }
2636     }
2637 
2638     @Nullable
createCarManagerLocked(String serviceName, IBinder binder)2639     private CarManagerBase createCarManagerLocked(String serviceName, IBinder binder) {
2640         CarManagerBase manager = null;
2641         switch (serviceName) {
2642             case AUDIO_SERVICE:
2643                 manager = new CarAudioManager(this, binder);
2644                 break;
2645             case SENSOR_SERVICE:
2646                 manager = new CarSensorManager(this, binder);
2647                 break;
2648             case INFO_SERVICE:
2649                 manager = new CarInfoManager(this, binder);
2650                 break;
2651             case APP_FOCUS_SERVICE:
2652                 manager = new CarAppFocusManager(this, binder);
2653                 break;
2654             case PACKAGE_SERVICE:
2655                 manager = new CarPackageManager(this, binder);
2656                 break;
2657             case CAR_OCCUPANT_ZONE_SERVICE:
2658                 manager = new CarOccupantZoneManager(this, binder);
2659                 break;
2660             case CAR_NAVIGATION_SERVICE:
2661                 manager = new CarNavigationStatusManager(this, binder);
2662                 break;
2663             case CABIN_SERVICE:
2664                 manager = new CarCabinManager(this, binder);
2665                 break;
2666             case DIAGNOSTIC_SERVICE:
2667                 manager = new CarDiagnosticManager(this, binder);
2668                 break;
2669             case HVAC_SERVICE:
2670                 manager = new CarHvacManager(this, binder);
2671                 break;
2672             case POWER_SERVICE:
2673                 manager = new CarPowerManager(this, binder);
2674                 break;
2675             case PROJECTION_SERVICE:
2676                 manager = new CarProjectionManager(this, binder);
2677                 break;
2678             case PROPERTY_SERVICE:
2679                 manager = new CarPropertyManager(this, ICarProperty.Stub.asInterface(binder));
2680                 break;
2681             case VENDOR_EXTENSION_SERVICE:
2682                 manager = new CarVendorExtensionManager(this, binder);
2683                 break;
2684             case CAR_INSTRUMENT_CLUSTER_SERVICE:
2685                 manager = new CarInstrumentClusterManager(this, binder);
2686                 break;
2687             case TEST_SERVICE:
2688                 /* CarTestManager exist in static library. So instead of constructing it here,
2689                  * only pass binder wrapper so that CarTestManager can be constructed outside. */
2690                 manager = new CarTestManager(this, binder);
2691                 break;
2692             case VEHICLE_MAP_SERVICE:
2693                 manager = new VmsClientManager(this, binder);
2694                 break;
2695             case VMS_SUBSCRIBER_SERVICE:
2696                 manager = VmsSubscriberManager.wrap(this,
2697                         (VmsClientManager) getCarManager(VEHICLE_MAP_SERVICE));
2698                 break;
2699             case STORAGE_MONITORING_SERVICE:
2700                 manager = new CarStorageMonitoringManager(this, binder);
2701                 break;
2702             case CAR_DRIVING_STATE_SERVICE:
2703                 manager = new CarDrivingStateManager(this, binder);
2704                 break;
2705             case CAR_UX_RESTRICTION_SERVICE:
2706                 manager = new CarUxRestrictionsManager(this, binder);
2707                 break;
2708             case OCCUPANT_AWARENESS_SERVICE:
2709                 manager = new OccupantAwarenessManager(this, binder);
2710                 break;
2711             case CAR_MEDIA_SERVICE:
2712                 manager = new CarMediaManager(this, binder);
2713                 break;
2714             case CAR_BUGREPORT_SERVICE:
2715                 manager = new CarBugreportManager(this, binder);
2716                 break;
2717             case CAR_USER_SERVICE:
2718                 manager = new CarUserManager(this, binder);
2719                 break;
2720             case EXPERIMENTAL_CAR_USER_SERVICE:
2721                 manager = new ExperimentalCarUserManager(this, binder);
2722                 break;
2723             case CAR_WATCHDOG_SERVICE:
2724                 manager = new CarWatchdogManager(this, binder);
2725                 break;
2726             case CAR_INPUT_SERVICE:
2727                 manager = new CarInputManager(this, binder);
2728                 break;
2729             case CAR_DEVICE_POLICY_SERVICE:
2730                 manager = new CarDevicePolicyManager(this, binder);
2731                 break;
2732             case CLUSTER_HOME_SERVICE:
2733                 manager = new ClusterHomeManager(this, binder);
2734                 break;
2735             case CAR_EVS_SERVICE:
2736                 manager = new CarEvsManager(this, binder);
2737                 break;
2738             case CAR_TELEMETRY_SERVICE:
2739                 manager = new CarTelemetryManager(this, binder);
2740                 break;
2741             case CAR_ACTIVITY_SERVICE:
2742                 manager = new CarActivityManager(this, binder);
2743                 break;
2744             case CAR_PERFORMANCE_SERVICE:
2745                 manager = new CarPerformanceManager(this, binder);
2746                 break;
2747             case CAR_REMOTE_ACCESS_SERVICE:
2748                 manager = new CarRemoteAccessManager(this, binder);
2749                 break;
2750             case CAR_OCCUPANT_CONNECTION_SERVICE:
2751                 manager = new CarOccupantConnectionManager(this, binder);
2752                 break;
2753             case CAR_REMOTE_DEVICE_SERVICE:
2754                 manager = new CarRemoteDeviceManager(this, binder);
2755                 break;
2756             default:
2757                 // Experimental or non-existing
2758                 String className = null;
2759                 try {
2760                     className = mService.getCarManagerClassForFeature(serviceName);
2761                 } catch (RemoteException e) {
2762                     handleRemoteExceptionFromCarService(e);
2763                     return null;
2764                 }
2765                 if (className == null) {
2766                     Log.e(TAG_CAR, "Cannot construct CarManager for service:" + serviceName
2767                             + " : no class defined");
2768                     return null;
2769                 }
2770                 manager = constructCarManager(className, binder);
2771                 break;
2772         }
2773         return manager;
2774     }
2775 
constructCarManager(String className, IBinder binder)2776     private CarManagerBase constructCarManager(String className, IBinder binder) {
2777         try {
2778             // Should use class loader for the Context as class loader for car api does not
2779             // see the class.
2780             ClassLoader loader = mContext.getClassLoader();
2781             Class managerClass = loader.loadClass(className);
2782             Constructor constructor = managerClass.getConstructor(Car.class, IBinder.class);
2783             CarManagerBase manager = (CarManagerBase) constructor.newInstance(this, binder);
2784             return manager;
2785         } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException
2786                 | InstantiationException | InvocationTargetException e) {
2787             Log.e(TAG_CAR, "Cannot construct CarManager, class:" + className, e);
2788             return null;
2789         }
2790     }
2791 
startCarService()2792     private void startCarService() {
2793         Intent intent = new Intent();
2794         intent.setPackage(CAR_SERVICE_PACKAGE);
2795         intent.setAction(Car.CAR_SERVICE_INTERFACE_NAME);
2796         boolean bound = mContext.bindService(intent, mServiceConnectionListener,
2797                 Context.BIND_AUTO_CREATE);
2798         synchronized (mLock) {
2799             if (!bound) {
2800                 mConnectionRetryCount++;
2801                 if (mConnectionRetryCount > CAR_SERVICE_BIND_MAX_RETRY) {
2802                     Log.w(TAG_CAR, "cannot bind to car service after max retry");
2803                     mMainThreadEventHandler.post(mConnectionRetryFailedRunnable);
2804                 } else {
2805                     mEventHandler.postDelayed(mConnectionRetryRunnable,
2806                             CAR_SERVICE_BIND_RETRY_INTERVAL_MS);
2807                 }
2808             } else {
2809                 mEventHandler.removeCallbacks(mConnectionRetryRunnable);
2810                 mMainThreadEventHandler.removeCallbacks(mConnectionRetryFailedRunnable);
2811                 mConnectionRetryCount = 0;
2812                 mServiceBound = true;
2813             }
2814         }
2815     }
2816 
tearDownCarManagersLocked()2817     private void tearDownCarManagersLocked() {
2818         // All disconnected handling should be only doing its internal cleanup.
2819         for (CarManagerBase manager: mServiceMap.values()) {
2820             manager.onCarDisconnected();
2821         }
2822         mServiceMap.clear();
2823     }
2824 }
2825