blob: 5297af9d92daef771c4c0cab1eaf6b80b7e933ef [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
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
17package com.android.car;
18
Pavel Maltsevabd47232017-10-10 16:54:57 -070019import android.annotation.MainThread;
felipeal080e5652019-11-02 17:04:04 -070020import android.annotation.Nullable;
Keun young Parkf3523cd2019-04-08 10:09:17 -070021import android.app.ActivityManager;
Keun young Park09ca8492019-10-15 10:34:11 -070022import android.app.ActivityOptions;
Yao Chene33f07e2016-07-26 12:02:51 -070023import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080024import android.car.Car;
Keun young Park9a91efb2019-11-15 18:10:47 -080025import android.car.CarFeatures;
Keun-young Parke54ac272016-02-16 19:02:18 -080026import android.car.ICar;
Pavel Maltsev0477e292016-05-27 12:22:36 -070027import android.car.cluster.renderer.IInstrumentClusterNavigation;
Felipe Lemee93218b2020-02-13 14:10:52 -080028import android.car.user.CarUserManager;
Ying Zheng9fc99402018-09-19 14:23:59 -070029import android.car.userlib.CarUserManagerHelper;
Keun young Park09ca8492019-10-15 10:34:11 -070030import android.content.ComponentName;
keunyoungca515072015-07-10 12:21:47 -070031import android.content.Context;
Keun young Park09ca8492019-10-15 10:34:11 -070032import android.content.Intent;
keunyoung1ab8e182015-09-24 09:25:22 -070033import android.content.pm.PackageManager;
Keun young Parkf3523cd2019-04-08 10:09:17 -070034import android.content.res.Resources;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080035import android.hardware.automotive.vehicle.V2_0.IVehicle;
Felipe Lemea5e69382020-02-11 17:53:20 -080036import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction;
37import android.hardware.automotive.vehicle.V2_0.UsersInfo;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080038import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070039import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070040import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070041import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070042import android.os.Process;
felipeal72bf6422019-11-02 17:04:04 -070043import android.os.RemoteException;
44import android.os.ResultReceiver;
45import android.os.ShellCallback;
46import android.os.ShellCommand;
Keun young Park313dfaf2019-12-19 11:32:38 -080047import android.os.SystemClock;
Steve Paikc302c7c2017-08-04 14:01:58 -070048import android.os.Trace;
Anthony Hugh9932a252019-06-12 16:19:56 -070049import android.os.UserManager;
keunyoungca515072015-07-10 12:21:47 -070050import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070051import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070052import android.util.TimingsTraceLog;
Keun young Park313dfaf2019-12-19 11:32:38 -080053import android.view.KeyEvent;
Gregory Clark26fa6012018-03-14 18:38:56 -070054
Keun young Parka4d12202019-10-03 10:51:12 -070055import com.android.car.am.FixedActivityService;
Hongwei Wang38549092018-08-22 12:32:42 -070056import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080057import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070058import com.android.car.garagemode.GarageModeService;
Keun young Park313dfaf2019-12-19 11:32:38 -080059import com.android.car.hal.InputHalService;
Felipe Lemea5e69382020-02-11 17:53:20 -080060import com.android.car.hal.UserHalHelper;
61import com.android.car.hal.UserHalService;
62import com.android.car.hal.UserHalService.HalCallback;
keunyoungcc449f72015-08-12 10:46:27 -070063import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080064import com.android.car.pm.CarPackageManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -070065import com.android.car.stats.CarStatsService;
Enrico Granatab19bc322017-10-12 12:25:06 -070066import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070067import com.android.car.trust.CarTrustedDeviceService;
Keun young Park7af7d6c2019-09-12 10:31:48 -070068import com.android.car.user.CarUserNoticeService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070069import com.android.car.user.CarUserService;
Felipe Lemee93218b2020-02-13 14:10:52 -080070import com.android.car.user.UserMetrics;
Mark Tabry6fa123d2020-01-10 19:52:59 -080071import com.android.car.vms.VmsNewBrokerService;
Eric Jeongae2c04c2020-02-21 09:18:31 -080072import com.android.car.watchdog.CarWatchdogService;
keunyoungca515072015-07-10 12:21:47 -070073import com.android.internal.annotations.GuardedBy;
Felipe Leme6ef89652019-12-19 16:47:14 -080074import com.android.internal.annotations.VisibleForTesting;
Steve Paik1d8bea22018-02-03 01:54:06 +000075import com.android.internal.car.ICarServiceHelper;
Felipe Lemee2600fc2020-02-26 11:06:04 -080076import com.android.internal.os.IResultReceiver;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070077import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070078
Enrico Granatae8056ca2018-04-03 13:19:52 -070079import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070080import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070081import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080082import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070083import java.util.List;
Felipe Lemea5e69382020-02-11 17:53:20 -080084import java.util.concurrent.CountDownLatch;
85import java.util.concurrent.TimeUnit;
keunyounga3b28d82015-08-25 13:05:15 -070086
keunyoungca515072015-07-10 12:21:47 -070087public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070088
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080089 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070090 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
91 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080092
keunyoungca515072015-07-10 12:21:47 -070093 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080094 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070095
Keun young Park9a91efb2019-11-15 18:10:47 -080096 private final CarFeatureController mFeatureController;
97
Anthony Chen12aec302018-04-25 16:41:48 -070098 private final SystemInterface mSystemInterface;
99
Keun-young Park4727da32016-05-31 10:00:51 -0700100 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -0700101 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800102 private final CarPackageManagerService mCarPackageManagerService;
103 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800104 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800105 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800106 private final OccupantAwarenessService mOccupantAwarenessService;
keunyoungd32f4e62015-09-21 11:33:06 -0700107 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800108 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700109 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800110 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700111 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -0700112 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -0800113 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800114 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -0800115 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700116 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800117 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700118 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700119 private final CarDiagnosticService mCarDiagnosticService;
120 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700121 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700122 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800123 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700124 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800125 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700126 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700127 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800128 private final VmsNewBrokerService mVmsBrokerService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700129 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700130 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800131 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
Eric Jeongae2c04c2020-02-21 09:18:31 -0800132 private final CarWatchdogService mCarWatchdogService;
keunyounga74b9ca2015-10-21 13:33:58 -0700133
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700134 private final CarServiceBase[] mAllServices;
135
Steve Paikc302c7c2017-08-04 14:01:58 -0700136 private static final String TAG = "ICarImpl";
137 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700138
139 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700140
keunyoung1ab8e182015-09-24 09:25:22 -0700141 /** Test only service. Populate it only when necessary. */
142 @GuardedBy("this")
143 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700144
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700145 @GuardedBy("this")
146 private ICarServiceHelper mICarServiceHelper;
147
Enrico Granatae8056ca2018-04-03 13:19:52 -0700148 private final String mVehicleInterfaceName;
149
Felipe Lemee93218b2020-02-13 14:10:52 -0800150 private final UserMetrics mUserMetrics = new UserMetrics();
151
Pavel Maltsevec83b632017-01-05 15:10:55 -0800152 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700153 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
Felipe Leme6ef89652019-12-19 16:47:14 -0800154 this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
Eric Jeongae2c04c2020-02-21 09:18:31 -0800155 /* carUserService= */ null, /* carWatchdogService= */ null);
Felipe Leme6ef89652019-12-19 16:47:14 -0800156 }
157
158 @VisibleForTesting
159 ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
160 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
Eric Jeongae2c04c2020-02-21 09:18:31 -0800161 @Nullable CarUserService carUserService,
162 @Nullable CarWatchdogService carWatchdogService) {
keunyoungca515072015-07-10 12:21:47 -0700163 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800164 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700165 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800166 Resources res = mContext.getResources();
167 String[] defaultEnabledFeatures = res.getStringArray(
168 R.array.config_allowed_optional_car_features);
169 // Do this before any other service components to allow feature check. It should work
170 // even without init.
171 // TODO (b/144504820) Add vhal plumbing
172 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
173 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
174 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700175 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800176 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Felipe Leme6ef89652019-12-19 16:47:14 -0800177 if (carUserService != null) {
178 mCarUserService = carUserService;
179 } else {
180 UserManager userManager =
181 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
182 int maxRunningUsers = res.getInteger(
183 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Felipe Leme58412202020-01-09 13:45:33 -0800184 mCarUserService = new CarUserService(serviceContext, mHal.getUserHal(),
185 mUserManagerHelper, userManager, ActivityManager.getService(), maxRunningUsers);
Felipe Leme6ef89652019-12-19 16:47:14 -0800186 }
Keun young Parkbb877e22019-08-02 10:38:01 -0700187 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700188 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800189 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Felipe Lemee3cab982020-03-12 11:39:29 -0700190 systemInterface, mUserManagerHelper, mCarUserService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800191 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
192 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
193 } else {
194 mCarUserNoticeService = null;
195 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700196 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
197 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800198 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800199 mCarDrivingStateService, mCarPropertyService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800200 if (mFeatureController.isFeatureEnabled(Car.OCCUPANT_AWARENESS_SERVICE)) {
201 mOccupantAwarenessService = new OccupantAwarenessService(serviceContext);
202 } else {
203 mOccupantAwarenessService = null;
204 }
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800205 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
206 mCarUXRestrictionsService,
Mayank Garg31e73042020-01-23 00:10:38 -0800207 mSystemActivityMonitoringService,
208 mCarUserService);
209 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext, mCarUserService);
Sal Savage703c46f2019-04-15 08:39:25 -0700210 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700211 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800212 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800213 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700214 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700215 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800216 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700217 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700218 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700219 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700220 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700221 mSystemStateControllerService = new SystemStateControllerService(
222 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700223 mCarStatsService = new CarStatsService(serviceContext);
Ruchir Rastogia683d742020-01-22 00:17:51 -0800224 mCarStatsService.init();
Mark Tabry62e749c2020-01-10 19:52:59 -0800225 if (mFeatureController.isFeatureEnabled(Car.VEHICLE_MAP_SERVICE)
226 || mFeatureController.isFeatureEnabled(Car.VMS_SUBSCRIBER_SERVICE)) {
Mark Tabry6fa123d2020-01-10 19:52:59 -0800227 mVmsBrokerService = new VmsNewBrokerService(mContext, mCarStatsService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800228 } else {
229 mVmsBrokerService = null;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800230 }
Keun young Parkc6d80af2020-01-17 18:14:28 -0800231 if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
232 mCarDiagnosticService = new CarDiagnosticService(serviceContext,
233 mHal.getDiagnosticHal());
234 } else {
235 mCarDiagnosticService = null;
236 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800237 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
238 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
239 systemInterface);
240 } else {
241 mCarStorageMonitoringService = null;
242 }
Anthony Chen12aec302018-04-25 16:41:48 -0700243 mCarConfigurationService =
244 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700245 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700246 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Mayank Garg31e73042020-01-23 00:10:38 -0800247 mCarMediaService = new CarMediaService(serviceContext, mCarUserService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700248 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800249 if (!Build.IS_USER) {
250 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
251 serviceContext);
252 } else {
253 mCarExperimentalFeatureServiceController = null;
254 }
Eric Jeongae2c04c2020-02-21 09:18:31 -0800255 if (carWatchdogService == null) {
256 mCarWatchdogService = new CarWatchdogService(serviceContext);
257 } else {
258 mCarWatchdogService = carWatchdogService;
259 }
keunyounga74b9ca2015-10-21 13:33:58 -0700260
Keun young Parka5fa4782019-04-16 18:56:27 -0700261 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700262 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800263 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700264 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Mayank Gargfc24d572020-02-14 11:32:34 -0800265 CarLocalServices.addService(CarUserNoticeService.class, mCarUserNoticeService);
Gregory Clarka440e812019-02-14 16:05:51 -0800266 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700267 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700268 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700269 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Mark Tabry62e749c2020-01-10 19:52:59 -0800270 CarLocalServices.addService(VmsNewBrokerService.class, mVmsBrokerService);
Oscar Azucenaa48a1182020-02-20 16:43:15 -0800271 CarLocalServices.addService(CarOccupantZoneService.class, mCarOccupantZoneService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800272
keunyounga3b28d82015-08-25 13:05:15 -0700273 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700274 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800275 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800276 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700277 allServices.add(mSystemActivityMonitoringService);
278 allServices.add(mCarPowerManagementService);
279 allServices.add(mCarPropertyService);
280 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700281 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700282 allServices.add(mCarUXRestrictionsService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800283 addServiceIfNonNull(allServices, mOccupantAwarenessService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700284 allServices.add(mCarPackageManagerService);
285 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700286 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800287 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700288 allServices.add(mAppFocusService);
289 allServices.add(mCarAudioService);
290 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700291 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700292 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700293 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700294 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800295 allServices.add(mCarBluetoothService);
296 allServices.add(mCarProjectionService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800297 addServiceIfNonNull(allServices, mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800298 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700299 allServices.add(mCarConfigurationService);
Mark Tabry6fa123d2020-01-10 19:52:59 -0800300 addServiceIfNonNull(allServices, mVmsBrokerService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700301 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800302 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700303 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700304 allServices.add(mCarBugreportManagerService);
Eric Jeongae2c04c2020-02-21 09:18:31 -0800305 allServices.add(mCarWatchdogService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800306 // Always put mCarExperimentalFeatureServiceController in last.
307 addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700308 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700309 }
310
Keun young Park9a91efb2019-11-15 18:10:47 -0800311 private void addServiceIfNonNull(List<CarServiceBase> services, CarServiceBase service) {
312 if (service != null) {
313 services.add(service);
314 }
315 }
316
Pavel Maltsevabd47232017-10-10 16:54:57 -0700317 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700318 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700319 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700320 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700321 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700322 traceEnd();
323 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800324 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700325 service.init();
326 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700327 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700328 }
329
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700330 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700331 // release done in opposite order from init
332 for (int i = mAllServices.length - 1; i >= 0; i--) {
333 mAllServices[i].release();
334 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700335 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700336 }
337
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700338 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800339 mHal.vehicleHalReconnected(vehicle);
340 for (CarServiceBase service : mAllServices) {
341 service.vehicleHalReconnected();
342 }
343 }
344
keunyoungca515072015-07-10 12:21:47 -0700345 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700346 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700347 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700348 synchronized (this) {
349 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800350 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700351 }
352 }
353
354 @Override
Felipe Lemee93218b2020-02-13 14:10:52 -0800355 public void onUserLifecycleEvent(int eventType, long timestampMs, int fromUserId,
356 int toUserId) {
357 assertCallingFromSystemProcess();
358 Log.i(TAG, "onUserLifecycleEvent(" + CarUserManager.lifecycleEventTypeToString(eventType)
Felipe Lemee2600fc2020-02-26 11:06:04 -0800359 + ", " + toUserId + ")");
Felipe Lemee93218b2020-02-13 14:10:52 -0800360 mUserMetrics.onEvent(eventType, timestampMs, fromUserId, toUserId);
Antonio Kantek991ba0b2020-03-12 14:28:21 -0700361 if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKING) {
362 setUserLockStatus(toUserId);
363 } else if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING) {
364 onSwitchUser(toUserId);
365 }
366 }
367
368 private void setUserLockStatus(int userId) {
369 mCarUserService.setUserLockStatus(userId, /* unlocked= */ true);
370 mCarMediaService.setUserLockStatus(userId, /* unlocked= */ true);
371 }
372
373 private void onSwitchUser(int userId) {
374 Log.i(TAG, "Foreground user switched to " + userId);
375 mCarUserService.onSwitchUser(userId);
Felipe Lemee93218b2020-02-13 14:10:52 -0800376 }
377
Keun young Park9a91efb2019-11-15 18:10:47 -0800378 @Override
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800379 public void onFirstUserUnlocked(int userId, long timestampMs, long duration) {
380 mUserMetrics.logFirstUnlockedUser(userId, timestampMs, duration);
381 }
382
383 @Override
Felipe Lemee2600fc2020-02-26 11:06:04 -0800384 public void getInitialUserInfo(int requestType, int timeoutMs, IBinder binder) {
385 IResultReceiver receiver = IResultReceiver.Stub.asInterface(binder);
386 mCarUserService.getInitialUserInfo(requestType, timeoutMs, receiver);
387 }
388
389 @Override
Keun young Park9a91efb2019-11-15 18:10:47 -0800390 public boolean isFeatureEnabled(String featureName) {
391 return mFeatureController.isFeatureEnabled(featureName);
392 }
393
394 @Override
395 public int enableFeature(String featureName) {
396 // permission check inside the controller
397 return mFeatureController.enableFeature(featureName);
398 }
399
400 @Override
401 public int disableFeature(String featureName) {
402 // permission check inside the controller
403 return mFeatureController.disableFeature(featureName);
404 }
405
406 @Override
407 public List<String> getAllEnabledFeatures() {
408 // permission check inside the controller
409 return mFeatureController.getAllEnabledFeatures();
410 }
411
412 @Override
413 public List<String> getAllPendingDisabledFeatures() {
414 // permission check inside the controller
415 return mFeatureController.getAllPendingDisabledFeatures();
416 }
417
418 @Override
419 public List<String> getAllPendingEnabledFeatures() {
420 // permission check inside the controller
421 return mFeatureController.getAllPendingEnabledFeatures();
422 }
423
424 @Override
425 public String getCarManagerClassForFeature(String featureName) {
426 if (mCarExperimentalFeatureServiceController == null) {
427 return null;
428 }
429 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
430 }
431
Jim Kayee5133162019-04-22 12:50:27 -0700432 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800433 int uid = Binder.getCallingUid();
434 if (uid != Process.SYSTEM_UID) {
435 throw new SecurityException("Only allowed from system");
436 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800437 }
438
Keun young Parkaabecd92019-05-03 17:31:27 -0700439 /**
440 * Assert if binder call is coming from system process like system server or if it is called
441 * from its own process even if it is not system. The latter can happen in test environment.
442 * Note that car service runs as system user but test like car service test will not.
443 */
444 static void assertCallingFromSystemProcessOrSelf() {
445 int uid = Binder.getCallingUid();
446 int pid = Binder.getCallingPid();
447 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
448 throw new SecurityException("Only allowed from system or self");
449 }
450 }
451
Keun-young Parkd462a912019-02-11 08:53:42 -0800452 @Override
keunyoungca515072015-07-10 12:21:47 -0700453 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800454 if (!mFeatureController.isFeatureEnabled(serviceName)) {
455 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
456 return null;
457 }
keunyoungca515072015-07-10 12:21:47 -0700458 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800459 case Car.AUDIO_SERVICE:
460 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700461 case Car.APP_FOCUS_SERVICE:
462 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800463 case Car.PACKAGE_SERVICE:
464 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800465 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700466 assertAnyDiagnosticPermission(mContext);
467 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800468 case Car.POWER_SERVICE:
469 assertPowerPermission(mContext);
470 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700471 case Car.CABIN_SERVICE:
472 case Car.HVAC_SERVICE:
473 case Car.INFO_SERVICE:
474 case Car.PROPERTY_SERVICE:
475 case Car.SENSOR_SERVICE:
476 case Car.VENDOR_EXTENSION_SERVICE:
477 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800478 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700479 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700480 IInstrumentClusterNavigation navService =
481 mInstrumentClusterService.getNavigationService();
482 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700483 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
484 assertClusterManagerPermission(mContext);
485 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800486 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800487 return mCarProjectionService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800488 case Car.VEHICLE_MAP_SERVICE:
489 assertAnyVmsPermission(mContext);
490 return mVmsBrokerService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800491 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700492 assertVmsSubscriberPermission(mContext);
Mark Tabry62e749c2020-01-10 19:52:59 -0800493 return mVmsBrokerService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800494 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700495 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700496 synchronized (this) {
497 if (mCarTestService == null) {
498 mCarTestService = new CarTestService(mContext, this);
499 }
500 return mCarTestService;
501 }
502 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700503 case Car.BLUETOOTH_SERVICE:
504 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700505 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700506 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700507 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800508 case Car.CAR_DRIVING_STATE_SERVICE:
509 assertDrivingStatePermission(mContext);
510 return mCarDrivingStateService;
511 case Car.CAR_UX_RESTRICTION_SERVICE:
512 return mCarUXRestrictionsService;
Michael Kellerac2ed202020-01-30 14:11:16 -0800513 case Car.OCCUPANT_AWARENESS_SERVICE:
514 return mOccupantAwarenessService;
Anthony Chend4203d82018-05-16 16:21:52 -0700515 case Car.CAR_CONFIGURATION_SERVICE:
516 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800517 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
518 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700519 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800520 case Car.CAR_MEDIA_SERVICE:
521 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700522 case Car.CAR_OCCUPANT_ZONE_SERVICE:
523 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700524 case Car.CAR_BUGREPORT_SERVICE:
525 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700526 case Car.CAR_USER_SERVICE:
527 return mCarUserService;
Eric Jeongae2c04c2020-02-21 09:18:31 -0800528 case Car.CAR_WATCHDOG_SERVICE:
529 return mCarWatchdogService;
Keun young Park401479c2020-02-19 14:15:51 -0800530 case Car.CAR_INPUT_SERVICE:
531 return mCarInputService;
keunyoungca515072015-07-10 12:21:47 -0700532 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800533 IBinder service = null;
534 if (mCarExperimentalFeatureServiceController != null) {
535 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
536 }
537 if (service == null) {
538 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
539 + serviceName);
540 }
541 return service;
keunyoungca515072015-07-10 12:21:47 -0700542 }
543 }
544
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800545 @Override
546 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700547 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800548 }
549
Keun-young Parka28d7b22016-02-29 16:54:29 -0800550 public CarServiceBase getCarInternalService(String serviceName) {
551 switch (serviceName) {
552 case INTERNAL_INPUT_SERVICE:
553 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700554 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
555 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800556 default:
557 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
558 serviceName);
559 return null;
560 }
561 }
562
keunyoung1ab8e182015-09-24 09:25:22 -0700563 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700564 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700565 }
566
Keun-young Parke31a8b22016-03-16 17:34:08 -0700567 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700568 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800569 }
570
Pavel Maltsev905968c2017-07-16 19:48:57 -0700571 public static void assertClusterManagerPermission(Context context) {
572 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
573 }
574
Steve Paik388d7772018-02-12 10:54:51 -0800575 public static void assertPowerPermission(Context context) {
576 assertPermission(context, Car.PERMISSION_CAR_POWER);
577 }
578
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800579 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700580 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
581 }
582
Pavel Maltsev079873b2019-02-25 12:15:09 -0800583 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
584 public static void assertProjectionStatusPermission(Context context) {
585 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
586 }
587
Enrico Granata3c7a6662017-02-23 18:07:59 -0800588 public static void assertAnyDiagnosticPermission(Context context) {
589 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700590 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800591 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
592 }
593
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800594 public static void assertDrivingStatePermission(Context context) {
595 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
596 }
597
Mark Tabry6fa123d2020-01-10 19:52:59 -0800598 /**
599 * Verify the calling context has either {@link Car#PERMISSION_VMS_SUBSCRIBER} or
600 * {@link Car#PERMISSION_VMS_PUBLISHER}
601 */
602 public static void assertAnyVmsPermission(Context context) {
603 assertAnyPermission(context,
604 Car.PERMISSION_VMS_SUBSCRIBER,
605 Car.PERMISSION_VMS_PUBLISHER);
606 }
607
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800608 public static void assertVmsPublisherPermission(Context context) {
609 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
610 }
611
Antonio Cortese4619c72017-02-02 07:53:27 -0800612 public static void assertVmsSubscriberPermission(Context context) {
613 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
614 }
615
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800616 /**
617 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800618 */
619 public static void assertTrustAgentEnrollmentPermission(Context context) {
620 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
621 }
622
Steve Paik461ecc62016-06-08 15:28:32 -0700623 public static void assertPermission(Context context, String permission) {
624 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
625 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800626 }
627 }
628
Steve Paik9ec53d72018-04-27 13:28:31 -0700629 /**
630 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700631 *
632 * @return boolean TRUE if caller has the permission.
633 */
634 public static boolean hasPermission(Context context, String permission) {
635 return context.checkCallingOrSelfPermission(permission)
636 == PackageManager.PERMISSION_GRANTED;
637 }
638
Enrico Granata3c7a6662017-02-23 18:07:59 -0800639 public static void assertAnyPermission(Context context, String... permissions) {
640 for (String permission : permissions) {
Antonio Kantek991ba0b2020-03-12 14:28:21 -0700641 if (context.checkCallingOrSelfPermission(permission)
642 == PackageManager.PERMISSION_GRANTED) {
Enrico Granata3c7a6662017-02-23 18:07:59 -0800643 return;
644 }
645 }
646 throw new SecurityException("requires any of " + Arrays.toString(permissions));
647 }
648
Enrico Granatae8056ca2018-04-03 13:19:52 -0700649 @Override
650 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
651 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700652 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700653 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700654 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
655 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700656 return;
keunyounga3b28d82015-08-25 13:05:15 -0700657 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700658
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700659 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
660 writer.println("*Dump car service*");
Mark Tabry7e2a7822019-10-17 10:18:32 -0700661 dumpAllServices(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800662 dumpAllHals(writer);
Felipe Lemee93218b2020-02-13 14:10:52 -0800663 mUserMetrics.dump(writer);
felipeal080e5652019-11-02 17:04:04 -0700664 } else if ("--list".equals(args[0])) {
665 dumpListOfServices(writer);
666 return;
667 } else if ("--services".equals(args[0])) {
668 if (args.length < 2) {
Felipe Leme177464b2020-03-11 17:32:21 -0700669 writer.println("Must pass services to dump when using --services");
felipeal080e5652019-11-02 17:04:04 -0700670 return;
671 }
672 int length = args.length - 1;
673 String[] services = new String[length];
674 System.arraycopy(args, 1, services, 0, length);
675 dumpIndividualServices(writer, services);
676 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700677 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700678 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800679 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700680 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700681 } else if ("--vms-hal".equals(args[0])) {
682 mHal.getVmsHal().dumpMetrics(fd);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800683 } else if ("--hal".equals(args[0])) {
684 if (args.length == 1) {
685 dumpAllHals(writer);
686 return;
687 }
688 int length = args.length - 1;
689 String[] halNames = new String[length];
690 System.arraycopy(args, 1, halNames, 0, length);
691 mHal.dumpSpecificHals(writer, halNames);
692
693 } else if ("--list-hals".equals(args[0])) {
694 mHal.dumpListHals(writer);
695 return;
Felipe Lemee93218b2020-02-13 14:10:52 -0800696 } else if ("--user-metrics".equals(args[0])) {
697 mUserMetrics.dump(writer);
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800698 } else if ("--first-user-metrics".equals(args[0])) {
699 mUserMetrics.dumpFirstUserUnlockDuration(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800700 } else if ("--help".equals(args[0])) {
701 showDumpHelp(writer);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700702 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
703 execShellCmd(args, writer);
704 } else {
705 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800706 }
707 }
708
Felipe Lemef07c3f92020-02-12 14:26:33 -0800709 private void dumpAllHals(PrintWriter writer) {
710 writer.println("*Dump Vehicle HAL*");
711 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
712 try {
713 // TODO dump all feature flags by creating a dumpable interface
714 mHal.dump(writer);
715 } catch (Exception e) {
716 writer.println("Failed dumping: " + mHal.getClass().getName());
717 e.printStackTrace(writer);
718 }
719 }
720
721 private void showDumpHelp(PrintWriter writer) {
722 writer.println("Car service dump usage:");
723 writer.println("[NO ARG]");
724 writer.println("\t dumps everything (all services and HALs)");
725 writer.println("--help");
726 writer.println("\t shows this help");
727 writer.println("--list");
728 writer.println("\t lists the name of all services");
Oscar Azucenaa350a2e2020-02-21 20:08:02 -0800729 writer.println("--list-hals");
730 writer.println("\t lists the name of all HALs");
Felipe Lemef07c3f92020-02-12 14:26:33 -0800731 writer.println("--services <SVC1> [SVC2] [SVCN]");
732 writer.println("\t dumps just the specific services, where SVC is just the service class");
733 writer.println("\t name (like CarUserService)");
734 writer.println("--vms-hal");
735 writer.println("\t dumps the VMS HAL metrics");
736 writer.println("--hal [HAL1] [HAL2] [HALN]");
737 writer.println("\t dumps just the specified HALs (or all of them if none specified),");
738 writer.println("\t where HAL is just the class name (like UserHalService)");
Felipe Lemee93218b2020-02-13 14:10:52 -0800739 writer.println("--user-metrics");
740 writer.println("\t dumps user switching and stopping metrics ");
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800741 writer.println("--first-user-metrics");
742 writer.println("\t dumps how long it took to unlock first user since Android started\n");
743 writer.println("\t (or -1 if not unlocked)");
Felipe Lemef07c3f92020-02-12 14:26:33 -0800744 writer.println("-h");
745 writer.println("\t shows commands usage (NOTE: commands are not available on USER builds");
746 writer.println("[ANYTHING ELSE]");
747 writer.println("\t runs the given command (use --h to see the available commands)");
748 }
749
felipeal72bf6422019-11-02 17:04:04 -0700750 @Override
751 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
752 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
753 throws RemoteException {
754 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
755 }
756
felipeal080e5652019-11-02 17:04:04 -0700757 private void dumpListOfServices(PrintWriter writer) {
758 for (CarServiceBase service : mAllServices) {
759 writer.println(service.getClass().getName());
760 }
761 }
762
Mark Tabry7e2a7822019-10-17 10:18:32 -0700763 private void dumpAllServices(PrintWriter writer) {
Felipe Lemef07c3f92020-02-12 14:26:33 -0800764 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700765 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700766 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700767 }
768 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700769 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700770 }
felipeal080e5652019-11-02 17:04:04 -0700771 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700772
felipeal080e5652019-11-02 17:04:04 -0700773 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
774 for (String serviceName : serviceNames) {
775 writer.println("** Dumping " + serviceName + "\n");
776 CarServiceBase service = getCarServiceBySubstring(serviceName);
777 if (service == null) {
778 writer.println("No such service!");
779 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700780 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700781 }
782 writer.println();
783 }
784 }
785
786 @Nullable
787 private CarServiceBase getCarServiceBySubstring(String className) {
788 return Arrays.asList(mAllServices).stream()
789 .filter(s -> s.getClass().getSimpleName().equals(className))
790 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700791 }
792
Mark Tabry7e2a7822019-10-17 10:18:32 -0700793 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800794 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700795 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800796 } catch (Exception e) {
797 writer.println("Failed dumping: " + service.getClass().getName());
798 e.printStackTrace(writer);
799 }
keunyoungcc449f72015-08-12 10:46:27 -0700800 }
Yao Chene33f07e2016-07-26 12:02:51 -0700801
802 void execShellCmd(String[] args, PrintWriter writer) {
803 new CarShellCommand().exec(args, writer);
804 }
805
Pavel Maltsevabd47232017-10-10 16:54:57 -0700806 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700807 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700808 Slog.i(TAG, name);
809 mBootTiming.traceBegin(name);
810 }
811
Pavel Maltsevabd47232017-10-10 16:54:57 -0700812 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700813 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700814 mBootTiming.traceEnd();
815 }
816
felipeal72bf6422019-11-02 17:04:04 -0700817 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700818 private static final String COMMAND_HELP = "-h";
819 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800820 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700821 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800822 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800823 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800824 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800825 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700826 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700827 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800828 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700829 private static final String COMMAND_RESUME = "resume";
830 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700831 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
832 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700833 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700834 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
835 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800836 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
837 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800838 private static final String COMMAND_INJECT_KEY = "inject-key";
Felipe Lemea5e69382020-02-11 17:53:20 -0800839 private static final String COMMAND_GET_INITIAL_USER_INFO = "get-initial-user-info";
Felipe Leme5bf135b2020-02-07 12:22:14 -0800840
Yao Chene33f07e2016-07-26 12:02:51 -0700841 private static final String PARAM_DAY_MODE = "day";
842 private static final String PARAM_NIGHT_MODE = "night";
843 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800844 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800845 private static final String PARAM_ON_MODE = "on";
846 private static final String PARAM_OFF_MODE = "off";
847 private static final String PARAM_QUERY_MODE = "query";
Jim Kaye9a834ea2020-02-07 15:50:53 -0800848 private static final String PARAM_REBOOT = "reboot";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800849
felipeal72bf6422019-11-02 17:04:04 -0700850 private static final int RESULT_OK = 0;
851 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
852
853
854 @Override
855 public int onCommand(String cmd) {
856 if (cmd == null) {
857 onHelp();
858 return RESULT_ERROR;
859 }
860 ArrayList<String> argsList = new ArrayList<>();
861 argsList.add(cmd);
862 String arg = null;
863 do {
864 arg = getNextArg();
865 if (arg != null) {
866 argsList.add(arg);
867 }
868 } while (arg != null);
869 String[] args = new String[argsList.size()];
870 argsList.toArray(args);
871 return exec(args, getOutPrintWriter());
872 }
873
874 @Override
875 public void onHelp() {
876 dumpHelp(getOutPrintWriter());
877 }
Yao Chene33f07e2016-07-26 12:02:51 -0700878
879 private void dumpHelp(PrintWriter pw) {
880 pw.println("Car service commands:");
881 pw.println("\t-h");
882 pw.println("\t Print this help text.");
883 pw.println("\tday-night-mode [day|night|sensor]");
884 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800885 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800886 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700887 pw.println("\tinject-error-event property zone errorCode");
888 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700889 pw.println("\tenable-uxr true|false");
890 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800891 pw.println("\tgarage-mode [on|off|query|reboot]");
892 pw.println("\t Force into or out of garage mode, or check status.");
893 pw.println("\t With 'reboot', enter garage mode, then reboot when it completes.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800894 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800895 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800896 pw.println("\tget-carpropertyconfig [propertyId]");
897 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700898 pw.println("\tget-property-value [propertyId] [areaId]");
899 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
900 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700901 pw.println("\tsuspend");
902 pw.println("\t Suspend the system to Deep Sleep.");
903 pw.println("\tresume");
904 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700905 pw.println("\tenable-trusted-device true|false");
906 pw.println("\t Enable/Disable Trusted device feature.");
907 pw.println("\tremove-trusted-devices");
908 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700909 pw.println("\tprojection-tethering [true|false]");
910 pw.println("\t Whether tethering should be used when creating access point for"
911 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700912 pw.println("\t--metrics");
913 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700914 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800915 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700916 pw.println("\tstart-fixed-activity displayId packageName activityName");
917 pw.println("\t Start an Activity the specified display as fixed mode");
918 pw.println("\tstop-fixed-mode displayId");
919 pw.println("\t Stop fixed Activity mode for the given display. "
920 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800921 pw.println("\tenable-feature featureName");
922 pw.println("\t Enable the requested feature. Change will happen after reboot.");
923 pw.println("\t This requires root/su.");
924 pw.println("\tdisable-feature featureName");
925 pw.println("\t Disable the requested feature. Change will happen after reboot");
926 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800927 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
928 pw.println("\t inject key down / up event to car service");
929 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
930 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
931 pw.println("\t it will be 0");
932 pw.println("\t key_code: int key code defined in android KeyEvent");
Felipe Lemea5e69382020-02-11 17:53:20 -0800933 pw.printf("\t%s <REQ_TYPE> [--timeout TIMEOUT_MS]\n", COMMAND_GET_INITIAL_USER_INFO);
934 pw.println("\t Calls the Vehicle HAL to get the initial boot info, passing the given");
935 pw.println("\t REQ_TYPE (which could be either FIRST_BOOT, FIRST_BOOT_AFTER_OTA, ");
936 pw.println("\t COLD_BOOT, RESUME, or any numeric value that would be passed 'as-is')");
937 pw.println("\t and an optional TIMEOUT_MS to wait for the HAL response (if not set,");
938 pw.println("\t it will use a default value).");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700939 }
940
felipeal72bf6422019-11-02 17:04:04 -0700941 private int dumpInvalidArguments(PrintWriter pw) {
942 pw.println("Incorrect number of arguments.");
943 dumpHelp(pw);
944 return RESULT_ERROR;
945 }
946
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700947 private String runSetZoneIdForUid(String zoneString, String uidString) {
948 int uid = Integer.parseInt(uidString);
949 int zoneId = Integer.parseInt(zoneString);
950 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
951 return "zoneid " + zoneId + " not found";
952 }
953 mCarAudioService.setZoneIdForUid(zoneId, uid);
954 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700955 }
956
felipeal72bf6422019-11-02 17:04:04 -0700957 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700958 String arg = args[0];
959 switch (arg) {
960 case COMMAND_HELP:
961 dumpHelp(writer);
962 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800963 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800964 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700965 forceDayNightMode(value, writer);
966 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800967 }
968 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800969 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800970 forceGarageMode(value, writer);
971 break;
972 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800973 case COMMAND_INJECT_VHAL_EVENT:
974 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
975 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800976 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700977 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800978 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800979 // Zoned
980 zone = args[2];
981 data = args[3];
982 } else {
983 // Global
984 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800985 }
Kai4b098cf2019-06-05 15:03:28 -0700986 injectVhalEvent(args[1], zone, data, false, writer);
987 break;
988 case COMMAND_INJECT_ERROR_EVENT:
989 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700990 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700991 }
992 String errorAreaId = args[2];
993 String errorCode = args[3];
994 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800995 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800996 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800997 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700998 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800999 }
1000 boolean enableBlocking = Boolean.valueOf(args[1]);
1001 if (mCarPackageManagerService != null) {
1002 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
1003 }
1004 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -08001005 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -08001006 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001007 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -08001008 }
1009 String pkgName = args[1].toLowerCase();
1010 if (mCarPackageManagerService != null) {
1011 String[] doActivities =
1012 mCarPackageManagerService.getDistractionOptimizedActivities(
1013 pkgName);
1014 if (doActivities != null) {
1015 writer.println("DO Activities for " + pkgName);
1016 for (String a : doActivities) {
1017 writer.println(a);
1018 }
1019 } else {
1020 writer.println("No DO Activities for " + pkgName);
1021 }
1022 }
1023 break;
Kaidd4ef5a2019-03-04 14:07:26 -08001024 case COMMAND_GET_CARPROPERTYCONFIG:
1025 String propertyId = args.length < 2 ? "" : args[1];
1026 mHal.dumpPropertyConfigs(writer, propertyId);
1027 break;
Kai29fba112019-04-22 18:37:38 -07001028 case COMMAND_GET_PROPERTY_VALUE:
1029 String propId = args.length < 2 ? "" : args[1];
1030 String areaId = args.length < 3 ? "" : args[2];
1031 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
1032 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -08001033 case COMMAND_PROJECTION_UI_MODE:
1034 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001035 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -08001036 }
1037 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -07001038 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001039 case COMMAND_PROJECTION_AP_TETHERING:
1040 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001041 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001042 }
1043 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
1044 break;
Jim Kayed76c2742019-04-02 11:33:27 -07001045 case COMMAND_RESUME:
1046 mCarPowerManagementService.forceSimulatedResume();
1047 writer.println("Resume: Simulating resuming from Deep Sleep");
1048 break;
1049 case COMMAND_SUSPEND:
Jim Kaye9a834ea2020-02-07 15:50:53 -08001050 mCarPowerManagementService.forceSuspendAndMaybeReboot(false);
Jim Kayed76c2742019-04-02 11:33:27 -07001051 writer.println("Resume: Simulating powering down to Deep Sleep");
1052 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001053 case COMMAND_ENABLE_TRUSTED_DEVICE:
1054 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001055 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001056 }
1057 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
1058 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
1059 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
1060 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
1061 break;
1062 case COMMAND_REMOVE_TRUSTED_DEVICES:
1063 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -07001064 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001065 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001066 case COMMAND_SET_UID_TO_ZONE:
1067 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -07001068 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001069 }
1070 String results = runSetZoneIdForUid(args[1], args[2]);
1071 if (results != null) {
1072 writer.println(results);
1073 dumpHelp(writer);
1074 }
1075 break;
Keun young Park09ca8492019-10-15 10:34:11 -07001076 case COMMAND_START_FIXED_ACTIVITY_MODE:
1077 handleStartFixedActivity(args, writer);
1078 break;
1079 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
1080 handleStopFixedMode(args, writer);
1081 break;
Keun young Park9a91efb2019-11-15 18:10:47 -08001082 case COMMAND_ENABLE_FEATURE:
1083 if (args.length != 2) {
1084 return dumpInvalidArguments(writer);
1085 }
1086 handleEnableDisableFeature(args, writer, /* enable= */ true);
1087 break;
1088 case COMMAND_DISABLE_FEATURE:
1089 if (args.length != 2) {
1090 return dumpInvalidArguments(writer);
1091 }
1092 handleEnableDisableFeature(args, writer, /* enable= */ false);
1093 break;
Keun young Park313dfaf2019-12-19 11:32:38 -08001094 case COMMAND_INJECT_KEY:
1095 if (args.length < 2) {
1096 return dumpInvalidArguments(writer);
1097 }
1098 handleInjectKey(args, writer);
1099 break;
Felipe Lemea5e69382020-02-11 17:53:20 -08001100 case COMMAND_GET_INITIAL_USER_INFO:
1101 handleGetInitialUserInfo(args, writer);
1102 break;
Yao Chene33f07e2016-07-26 12:02:51 -07001103 default:
Jim Kayef10b7d82019-02-26 12:53:22 -08001104 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -07001105 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -07001106 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -07001107 }
felipeal72bf6422019-11-02 17:04:04 -07001108 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -07001109 }
1110
Keun young Park09ca8492019-10-15 10:34:11 -07001111 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1112 if (args.length != 4) {
1113 writer.println("Incorrect number of arguments");
1114 dumpHelp(writer);
1115 return;
1116 }
1117 int displayId;
1118 try {
1119 displayId = Integer.parseInt(args[1]);
1120 } catch (NumberFormatException e) {
1121 writer.println("Wrong display id:" + args[1]);
1122 return;
1123 }
1124 String packageName = args[2];
1125 String activityName = args[3];
1126 Intent intent = new Intent();
1127 intent.setComponent(new ComponentName(packageName, activityName));
1128 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1129 ActivityOptions options = ActivityOptions.makeBasic();
1130 options.setLaunchDisplayId(displayId);
1131 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1132 displayId, ActivityManager.getCurrentUser())) {
1133 writer.println("Failed to start");
1134 return;
1135 }
1136 writer.println("Succeeded");
1137 }
1138
1139 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1140 if (args.length != 2) {
1141 writer.println("Incorrect number of arguments");
1142 dumpHelp(writer);
1143 return;
1144 }
1145 int displayId;
1146 try {
1147 displayId = Integer.parseInt(args[1]);
1148 } catch (NumberFormatException e) {
1149 writer.println("Wrong display id:" + args[1]);
1150 return;
1151 }
1152 mFixedActivityService.stopFixedActivityMode(displayId);
1153 }
1154
Keun young Park9a91efb2019-11-15 18:10:47 -08001155 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1156 if (Binder.getCallingUid() != Process.ROOT_UID) {
1157 writer.println("Only allowed to root/su");
1158 return;
1159 }
1160 String featureName = args[1];
1161 long id = Binder.clearCallingIdentity();
1162 // no permission check here
1163 int r;
1164 if (enable) {
1165 r = mFeatureController.enableFeature(featureName);
1166 } else {
1167 r = mFeatureController.disableFeature(featureName);
1168 }
1169 switch (r) {
1170 case Car.FEATURE_REQUEST_SUCCESS:
1171 if (enable) {
1172 writer.println("Enabled feature:" + featureName);
1173 } else {
1174 writer.println("Disabled feature:" + featureName);
1175 }
1176 break;
1177 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1178 if (enable) {
1179 writer.println("Already enabled:" + featureName);
1180 } else {
1181 writer.println("Already disabled:" + featureName);
1182 }
1183 break;
1184 case Car.FEATURE_REQUEST_MANDATORY:
1185 writer.println("Cannot change mandatory feature:" + featureName);
1186 break;
1187 case Car.FEATURE_REQUEST_NOT_EXISTING:
1188 writer.println("Non-existing feature:" + featureName);
1189 break;
1190 default:
1191 writer.println("Unknown error:" + r);
1192 break;
1193 }
1194 Binder.restoreCallingIdentity(id);
1195 }
1196
Keun young Park313dfaf2019-12-19 11:32:38 -08001197 private void handleInjectKey(String[] args, PrintWriter writer) {
1198 int i = 1; // 0 is command itself
1199 int display = InputHalService.DISPLAY_MAIN;
1200 int delayMs = 0;
1201 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1202 try {
1203 while (i < args.length) {
1204 switch (args[i]) {
1205 case "-d":
1206 i++;
1207 display = Integer.parseInt(args[i]);
1208 break;
1209 case "-t":
1210 i++;
1211 delayMs = Integer.parseInt(args[i]);
1212 break;
1213 default:
1214 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1215 throw new IllegalArgumentException("key_code already set:"
1216 + keyCode);
1217 }
1218 keyCode = Integer.parseInt(args[i]);
1219 }
1220 i++;
1221 }
1222 } catch (Exception e) {
1223 writer.println("Invalid args:" + e);
1224 dumpHelp(writer);
1225 return;
1226 }
1227 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1228 writer.println("Missing key code or invalid keycode");
1229 dumpHelp(writer);
1230 return;
1231 }
1232 if (display != InputHalService.DISPLAY_MAIN
1233 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1234 writer.println("Invalid display:" + display);
1235 dumpHelp(writer);
1236 return;
1237 }
1238 if (delayMs < 0) {
1239 writer.println("Invalid delay:" + delayMs);
1240 dumpHelp(writer);
1241 return;
1242 }
1243 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1244 mCarInputService.onKeyEvent(keyDown, display);
1245 SystemClock.sleep(delayMs);
1246 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1247 mCarInputService.onKeyEvent(keyUp, display);
1248 writer.println("Succeeded");
1249 }
1250
Felipe Lemea5e69382020-02-11 17:53:20 -08001251 private void handleGetInitialUserInfo(String[] args, PrintWriter writer) {
1252 if (args.length < 2) {
1253 writer.println("Insufficient number of args");
1254 return;
1255 }
1256
1257 // Gets the request type
1258 String typeArg = args[1];
1259 int requestType = UserHalHelper.parseInitialUserInfoRequestType(typeArg);
1260
1261 int timeout = 1_000;
1262 for (int i = 2; i < args.length; i++) {
1263 String arg = args[i];
1264 switch (arg) {
1265 case "--timeout":
1266 timeout = Integer.parseInt(args[++i]);
1267 break;
1268 default:
1269 writer.println("Invalid option at index " + i + ": " + arg);
1270 return;
1271
1272 }
1273 }
1274
1275 Log.d(TAG, "handleGetInitialUserInfo(): type=" + requestType + " (" + typeArg
1276 + "), timeout=" + timeout);
1277
1278 UserHalService userHal = mHal.getUserHal();
Felipe Leme8f30b312020-02-28 18:01:25 -08001279 // TODO(b/150413515): use UserHalHelper to populate it with current users
Felipe Lemea5e69382020-02-11 17:53:20 -08001280 UsersInfo usersInfo = new UsersInfo();
1281 CountDownLatch latch = new CountDownLatch(1);
1282
1283 userHal.getInitialUserInfo(requestType, timeout, usersInfo, (status, resp) -> {
1284 try {
1285 Log.d(TAG, "GetUserInfoResponse: status=" + status + ", resp=" + resp);
1286 writer.printf("Status: %s\n", UserHalHelper.halCallbackStatusToString(status));
1287 if (status != HalCallback.STATUS_OK) {
1288 return;
1289 }
1290 writer.printf("Request id: %d\n", resp.requestId);
1291 writer.printf("Action: ");
1292 switch (resp.action) {
1293 case InitialUserInfoResponseAction.DEFAULT:
1294 writer.println("default");
1295 break;
1296 case InitialUserInfoResponseAction.SWITCH:
1297 writer.printf("switch to user %d\n", resp.userToSwitchOrCreate.userId);
1298 break;
1299 case InitialUserInfoResponseAction.CREATE:
1300 writer.printf("create user: name=%s, flags=%d\n", resp.userNameToCreate,
1301 resp.userToSwitchOrCreate.flags);
1302 break;
1303 default:
1304 writer.printf("unknown (%d)\n", resp.action);
1305 break;
1306 }
1307 } finally {
1308 latch.countDown();
1309 }
1310 });
1311
1312 try {
1313 if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
1314 writer.printf("HAL didn't respond in %dms\n", timeout);
1315 }
1316 } catch (InterruptedException e) {
1317 Thread.currentThread().interrupt();
1318 writer.println("Interrupted waiting for HAL");
1319 }
1320 return;
1321 }
1322
Yao Chene33f07e2016-07-26 12:02:51 -07001323 private void forceDayNightMode(String arg, PrintWriter writer) {
1324 int mode;
1325 switch (arg) {
1326 case PARAM_DAY_MODE:
1327 mode = CarNightService.FORCED_DAY_MODE;
1328 break;
1329 case PARAM_NIGHT_MODE:
1330 mode = CarNightService.FORCED_NIGHT_MODE;
1331 break;
1332 case PARAM_SENSOR_MODE:
1333 mode = CarNightService.FORCED_SENSOR_MODE;
1334 break;
1335 default:
1336 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1337 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1338 return;
1339 }
1340 int current = mCarNightService.forceDayNightMode(mode);
1341 String currentMode = null;
1342 switch (current) {
1343 case UiModeManager.MODE_NIGHT_AUTO:
1344 currentMode = PARAM_SENSOR_MODE;
1345 break;
1346 case UiModeManager.MODE_NIGHT_YES:
1347 currentMode = PARAM_NIGHT_MODE;
1348 break;
1349 case UiModeManager.MODE_NIGHT_NO:
1350 currentMode = PARAM_DAY_MODE;
1351 break;
1352 }
1353 writer.println("DayNightMode changed to: " + currentMode);
1354 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001355
Bryan Eylera32a7c12018-02-27 15:40:00 -08001356 private void forceGarageMode(String arg, PrintWriter writer) {
1357 switch (arg) {
1358 case PARAM_ON_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001359 mSystemInterface.setDisplayState(false);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001360 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001361 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001362 break;
1363 case PARAM_OFF_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001364 mSystemInterface.setDisplayState(true);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001365 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001366 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001367 break;
1368 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001369 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001370 break;
Jim Kaye9a834ea2020-02-07 15:50:53 -08001371 case PARAM_REBOOT:
1372 mCarPowerManagementService.forceSuspendAndMaybeReboot(true);
1373 writer.println("Entering Garage Mode. Will reboot when it completes.");
1374 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -08001375 default:
1376 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
Jim Kaye9a834ea2020-02-07 15:50:53 -08001377 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE + "|" + PARAM_REBOOT);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001378 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001379 }
1380
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001381 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001382 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001383 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001384 * @param property the Vehicle property Id as defined in the HAL
1385 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001386 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001387 * @param value Data value of the event
1388 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001389 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001390 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001391 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001392 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1393 if (!isPropertyAreaTypeGlobal(property)) {
1394 writer.println("Property area type inconsistent with given zone");
1395 return;
1396 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001397 }
1398 try {
Kai4b098cf2019-06-05 15:03:28 -07001399 if (isErrorEvent) {
1400 mHal.injectOnPropertySetError(property, zone, value);
1401 } else {
1402 mHal.injectVhalEvent(property, zone, value);
1403 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001404 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001405 writer.println("Invalid property Id zone Id or value" + e);
1406 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001407 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001408 }
1409
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001410 // Check if the given property is global
1411 private boolean isPropertyAreaTypeGlobal(String property) {
1412 if (property == null) {
1413 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001414 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001415 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001416 }
Yao Chene33f07e2016-07-26 12:02:51 -07001417 }
kevinjm55822c42018-08-15 11:26:00 -07001418}