blob: dc2419a19cb5f9048756f15e8c549c68324beb80 [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;
Ying Zheng9fc99402018-09-19 14:23:59 -070028import android.car.userlib.CarUserManagerHelper;
Keun young Park09ca8492019-10-15 10:34:11 -070029import android.content.ComponentName;
keunyoungca515072015-07-10 12:21:47 -070030import android.content.Context;
Keun young Park09ca8492019-10-15 10:34:11 -070031import android.content.Intent;
keunyoung1ab8e182015-09-24 09:25:22 -070032import android.content.pm.PackageManager;
Keun young Parkf3523cd2019-04-08 10:09:17 -070033import android.content.res.Resources;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080034import android.hardware.automotive.vehicle.V2_0.IVehicle;
Felipe Lemea5e69382020-02-11 17:53:20 -080035import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction;
36import android.hardware.automotive.vehicle.V2_0.UsersInfo;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080037import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070038import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070039import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070040import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070041import android.os.Process;
felipeal72bf6422019-11-02 17:04:04 -070042import android.os.RemoteException;
43import android.os.ResultReceiver;
44import android.os.ShellCallback;
45import android.os.ShellCommand;
Keun young Park313dfaf2019-12-19 11:32:38 -080046import android.os.SystemClock;
Steve Paikc302c7c2017-08-04 14:01:58 -070047import android.os.Trace;
Anthony Hugh9932a252019-06-12 16:19:56 -070048import android.os.UserManager;
keunyoungca515072015-07-10 12:21:47 -070049import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070050import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070051import android.util.TimingsTraceLog;
Keun young Park313dfaf2019-12-19 11:32:38 -080052import android.view.KeyEvent;
Gregory Clark26fa6012018-03-14 18:38:56 -070053
Keun young Parka4d12202019-10-03 10:51:12 -070054import com.android.car.am.FixedActivityService;
Hongwei Wang38549092018-08-22 12:32:42 -070055import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080056import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070057import com.android.car.garagemode.GarageModeService;
Keun young Park313dfaf2019-12-19 11:32:38 -080058import com.android.car.hal.InputHalService;
Felipe Lemea5e69382020-02-11 17:53:20 -080059import com.android.car.hal.UserHalHelper;
60import com.android.car.hal.UserHalService;
61import com.android.car.hal.UserHalService.HalCallback;
keunyoungcc449f72015-08-12 10:46:27 -070062import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080063import com.android.car.pm.CarPackageManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -070064import com.android.car.stats.CarStatsService;
Enrico Granatab19bc322017-10-12 12:25:06 -070065import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070066import com.android.car.trust.CarTrustedDeviceService;
Keun young Park7af7d6c2019-09-12 10:31:48 -070067import com.android.car.user.CarUserNoticeService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070068import com.android.car.user.CarUserService;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080069import com.android.car.vms.VmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080070import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070071import com.android.internal.annotations.GuardedBy;
Felipe Leme6ef89652019-12-19 16:47:14 -080072import com.android.internal.annotations.VisibleForTesting;
Steve Paik1d8bea22018-02-03 01:54:06 +000073import com.android.internal.car.ICarServiceHelper;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070074import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070075
Enrico Granatae8056ca2018-04-03 13:19:52 -070076import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070077import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070078import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080079import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070080import java.util.List;
Felipe Lemea5e69382020-02-11 17:53:20 -080081import java.util.concurrent.CountDownLatch;
82import java.util.concurrent.TimeUnit;
keunyounga3b28d82015-08-25 13:05:15 -070083
keunyoungca515072015-07-10 12:21:47 -070084public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070085
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080086 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070087 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
88 "system_activity_monitoring";
Yan Zhub07585d2019-10-29 14:16:40 -070089 public static final String INTERNAL_VMS_MANAGER = "vms_manager";
Keun-young Parka28d7b22016-02-29 16:54:29 -080090
keunyoungca515072015-07-10 12:21:47 -070091 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080092 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070093
Keun young Park9a91efb2019-11-15 18:10:47 -080094 private final CarFeatureController mFeatureController;
95
Anthony Chen12aec302018-04-25 16:41:48 -070096 private final SystemInterface mSystemInterface;
97
Keun-young Park4727da32016-05-31 10:00:51 -070098 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070099 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800100 private final CarPackageManagerService mCarPackageManagerService;
101 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800102 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800103 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800104 private final OccupantAwarenessService mOccupantAwarenessService;
keunyoungd32f4e62015-09-21 11:33:06 -0700105 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800106 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700107 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800108 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700109 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -0700110 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -0800111 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800112 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -0800113 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700114 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800115 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700116 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700117 private final CarDiagnosticService mCarDiagnosticService;
118 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700119 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700120 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800121 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700122 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800123 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700124 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700125 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800126 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800127 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800128 private final VmsSubscriberService mVmsSubscriberService;
129 private final VmsPublisherService mVmsPublisherService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700130 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700131 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800132 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
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
Pavel Maltsevec83b632017-01-05 15:10:55 -0800150 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700151 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
Felipe Leme6ef89652019-12-19 16:47:14 -0800152 this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
153 /* carUserService= */ null);
154 }
155
156 @VisibleForTesting
157 ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
158 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
159 @Nullable CarUserService carUserService) {
keunyoungca515072015-07-10 12:21:47 -0700160 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800161 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700162 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800163 Resources res = mContext.getResources();
164 String[] defaultEnabledFeatures = res.getStringArray(
165 R.array.config_allowed_optional_car_features);
166 // Do this before any other service components to allow feature check. It should work
167 // even without init.
168 // TODO (b/144504820) Add vhal plumbing
169 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
170 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
171 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700172 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800173 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Felipe Leme6ef89652019-12-19 16:47:14 -0800174 if (carUserService != null) {
175 mCarUserService = carUserService;
176 } else {
177 UserManager userManager =
178 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
179 int maxRunningUsers = res.getInteger(
180 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Felipe Leme58412202020-01-09 13:45:33 -0800181 mCarUserService = new CarUserService(serviceContext, mHal.getUserHal(),
182 mUserManagerHelper, userManager, ActivityManager.getService(), maxRunningUsers);
Felipe Leme6ef89652019-12-19 16:47:14 -0800183 }
Keun young Parkbb877e22019-08-02 10:38:01 -0700184 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700185 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800186 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700187 systemInterface, mUserManagerHelper);
Keun young Park9a91efb2019-11-15 18:10:47 -0800188 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
189 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
190 } else {
191 mCarUserNoticeService = null;
192 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700193 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
194 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800195 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800196 mCarDrivingStateService, mCarPropertyService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800197 if (mFeatureController.isFeatureEnabled(Car.OCCUPANT_AWARENESS_SERVICE)) {
198 mOccupantAwarenessService = new OccupantAwarenessService(serviceContext);
199 } else {
200 mOccupantAwarenessService = null;
201 }
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800202 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
203 mCarUXRestrictionsService,
Mayank Garg31e73042020-01-23 00:10:38 -0800204 mSystemActivityMonitoringService,
205 mCarUserService);
206 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext, mCarUserService);
Sal Savage703c46f2019-04-15 08:39:25 -0700207 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700208 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800209 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800210 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700211 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700212 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800213 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700214 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700215 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700216 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700217 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700218 mSystemStateControllerService = new SystemStateControllerService(
219 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700220 mCarStatsService = new CarStatsService(serviceContext);
Ruchir Rastogia683d742020-01-22 00:17:51 -0800221 mCarStatsService.init();
Keun young Parkc6d80af2020-01-17 18:14:28 -0800222 if (mFeatureController.isFeatureEnabled(Car.VMS_SUBSCRIBER_SERVICE)) {
223 mVmsBrokerService = new VmsBrokerService();
224 mVmsClientManager = new VmsClientManager(
225 // CarStatsService needs to be passed to the constructor due to HAL init order
226 serviceContext, mCarStatsService, mCarUserService, mVmsBrokerService,
227 mHal.getVmsHal());
228 mVmsSubscriberService = new VmsSubscriberService(
229 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
230 mVmsPublisherService = new VmsPublisherService(
231 serviceContext, mCarStatsService, mVmsBrokerService, mVmsClientManager);
232 } else {
233 mVmsBrokerService = null;
234 mVmsClientManager = null;
235 mVmsSubscriberService = null;
236 mVmsPublisherService = null;
237 }
238 if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
239 mCarDiagnosticService = new CarDiagnosticService(serviceContext,
240 mHal.getDiagnosticHal());
241 } else {
242 mCarDiagnosticService = null;
243 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800244 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
245 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
246 systemInterface);
247 } else {
248 mCarStorageMonitoringService = null;
249 }
Anthony Chen12aec302018-04-25 16:41:48 -0700250 mCarConfigurationService =
251 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700252 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700253 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Mayank Garg31e73042020-01-23 00:10:38 -0800254 mCarMediaService = new CarMediaService(serviceContext, mCarUserService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700255 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800256 if (!Build.IS_USER) {
257 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
258 serviceContext);
259 } else {
260 mCarExperimentalFeatureServiceController = null;
261 }
keunyounga74b9ca2015-10-21 13:33:58 -0700262
Keun young Parka5fa4782019-04-16 18:56:27 -0700263 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700264 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800265 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700266 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800267 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700268 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700269 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700270 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800271
keunyounga3b28d82015-08-25 13:05:15 -0700272 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700273 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800274 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800275 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700276 allServices.add(mSystemActivityMonitoringService);
277 allServices.add(mCarPowerManagementService);
278 allServices.add(mCarPropertyService);
279 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700280 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700281 allServices.add(mCarUXRestrictionsService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800282 addServiceIfNonNull(allServices, mOccupantAwarenessService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700283 allServices.add(mCarPackageManagerService);
284 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700285 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800286 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700287 allServices.add(mAppFocusService);
288 allServices.add(mCarAudioService);
289 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700290 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700291 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700292 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700293 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800294 allServices.add(mCarBluetoothService);
295 allServices.add(mCarProjectionService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800296 addServiceIfNonNull(allServices, mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800297 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700298 allServices.add(mCarConfigurationService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800299 addServiceIfNonNull(allServices, mVmsClientManager);
300 addServiceIfNonNull(allServices, mVmsSubscriberService);
301 addServiceIfNonNull(allServices, mVmsPublisherService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700302 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800303 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700304 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700305 allServices.add(mCarBugreportManagerService);
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
Eric Jeong1545f3b2019-09-16 13:56:52 -0700355 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700356 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700357 mCarUserService.setUserLockStatus(userId, unlocked == 1);
358 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700359 }
360
361 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700362 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700363 assertCallingFromSystemProcess();
364
Eric Jeong1545f3b2019-09-16 13:56:52 -0700365 Log.i(TAG, "Foreground user switched to " + userId);
366 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700367 }
368
Keun young Park9a91efb2019-11-15 18:10:47 -0800369 @Override
370 public boolean isFeatureEnabled(String featureName) {
371 return mFeatureController.isFeatureEnabled(featureName);
372 }
373
374 @Override
375 public int enableFeature(String featureName) {
376 // permission check inside the controller
377 return mFeatureController.enableFeature(featureName);
378 }
379
380 @Override
381 public int disableFeature(String featureName) {
382 // permission check inside the controller
383 return mFeatureController.disableFeature(featureName);
384 }
385
386 @Override
387 public List<String> getAllEnabledFeatures() {
388 // permission check inside the controller
389 return mFeatureController.getAllEnabledFeatures();
390 }
391
392 @Override
393 public List<String> getAllPendingDisabledFeatures() {
394 // permission check inside the controller
395 return mFeatureController.getAllPendingDisabledFeatures();
396 }
397
398 @Override
399 public List<String> getAllPendingEnabledFeatures() {
400 // permission check inside the controller
401 return mFeatureController.getAllPendingEnabledFeatures();
402 }
403
404 @Override
405 public String getCarManagerClassForFeature(String featureName) {
406 if (mCarExperimentalFeatureServiceController == null) {
407 return null;
408 }
409 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
410 }
411
Jim Kayee5133162019-04-22 12:50:27 -0700412 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800413 int uid = Binder.getCallingUid();
414 if (uid != Process.SYSTEM_UID) {
415 throw new SecurityException("Only allowed from system");
416 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800417 }
418
Keun young Parkaabecd92019-05-03 17:31:27 -0700419 /**
420 * Assert if binder call is coming from system process like system server or if it is called
421 * from its own process even if it is not system. The latter can happen in test environment.
422 * Note that car service runs as system user but test like car service test will not.
423 */
424 static void assertCallingFromSystemProcessOrSelf() {
425 int uid = Binder.getCallingUid();
426 int pid = Binder.getCallingPid();
427 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
428 throw new SecurityException("Only allowed from system or self");
429 }
430 }
431
Keun-young Parkd462a912019-02-11 08:53:42 -0800432 @Override
keunyoungca515072015-07-10 12:21:47 -0700433 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800434 if (!mFeatureController.isFeatureEnabled(serviceName)) {
435 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
436 return null;
437 }
keunyoungca515072015-07-10 12:21:47 -0700438 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800439 case Car.AUDIO_SERVICE:
440 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700441 case Car.APP_FOCUS_SERVICE:
442 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800443 case Car.PACKAGE_SERVICE:
444 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800445 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700446 assertAnyDiagnosticPermission(mContext);
447 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800448 case Car.POWER_SERVICE:
449 assertPowerPermission(mContext);
450 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700451 case Car.CABIN_SERVICE:
452 case Car.HVAC_SERVICE:
453 case Car.INFO_SERVICE:
454 case Car.PROPERTY_SERVICE:
455 case Car.SENSOR_SERVICE:
456 case Car.VENDOR_EXTENSION_SERVICE:
457 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800458 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700459 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700460 IInstrumentClusterNavigation navService =
461 mInstrumentClusterService.getNavigationService();
462 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700463 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
464 assertClusterManagerPermission(mContext);
465 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800466 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800467 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800468 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700469 assertVmsSubscriberPermission(mContext);
470 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800471 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700472 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700473 synchronized (this) {
474 if (mCarTestService == null) {
475 mCarTestService = new CarTestService(mContext, this);
476 }
477 return mCarTestService;
478 }
479 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700480 case Car.BLUETOOTH_SERVICE:
481 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700482 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700483 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700484 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800485 case Car.CAR_DRIVING_STATE_SERVICE:
486 assertDrivingStatePermission(mContext);
487 return mCarDrivingStateService;
488 case Car.CAR_UX_RESTRICTION_SERVICE:
489 return mCarUXRestrictionsService;
Michael Kellerac2ed202020-01-30 14:11:16 -0800490 case Car.OCCUPANT_AWARENESS_SERVICE:
491 return mOccupantAwarenessService;
Anthony Chend4203d82018-05-16 16:21:52 -0700492 case Car.CAR_CONFIGURATION_SERVICE:
493 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800494 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
495 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700496 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800497 case Car.CAR_MEDIA_SERVICE:
498 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700499 case Car.CAR_OCCUPANT_ZONE_SERVICE:
500 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700501 case Car.CAR_BUGREPORT_SERVICE:
502 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700503 case Car.CAR_USER_SERVICE:
504 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700505 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800506 IBinder service = null;
507 if (mCarExperimentalFeatureServiceController != null) {
508 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
509 }
510 if (service == null) {
511 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
512 + serviceName);
513 }
514 return service;
keunyoungca515072015-07-10 12:21:47 -0700515 }
516 }
517
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800518 @Override
519 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700520 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800521 }
522
Keun-young Parka28d7b22016-02-29 16:54:29 -0800523 public CarServiceBase getCarInternalService(String serviceName) {
524 switch (serviceName) {
525 case INTERNAL_INPUT_SERVICE:
526 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700527 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
528 return mSystemActivityMonitoringService;
Yan Zhub07585d2019-10-29 14:16:40 -0700529 // TODO(b/144027497): temporary until tests are refactored to not use it
530 case INTERNAL_VMS_MANAGER:
531 return mVmsClientManager;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800532 default:
533 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
534 serviceName);
535 return null;
536 }
537 }
538
keunyoung1ab8e182015-09-24 09:25:22 -0700539 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700540 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700541 }
542
Keun-young Parke31a8b22016-03-16 17:34:08 -0700543 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700544 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800545 }
546
Pavel Maltsev905968c2017-07-16 19:48:57 -0700547 public static void assertClusterManagerPermission(Context context) {
548 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
549 }
550
Steve Paik388d7772018-02-12 10:54:51 -0800551 public static void assertPowerPermission(Context context) {
552 assertPermission(context, Car.PERMISSION_CAR_POWER);
553 }
554
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800555 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700556 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
557 }
558
Pavel Maltsev079873b2019-02-25 12:15:09 -0800559 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
560 public static void assertProjectionStatusPermission(Context context) {
561 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
562 }
563
Enrico Granata3c7a6662017-02-23 18:07:59 -0800564 public static void assertAnyDiagnosticPermission(Context context) {
565 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700566 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800567 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
568 }
569
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800570 public static void assertDrivingStatePermission(Context context) {
571 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
572 }
573
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800574 public static void assertVmsPublisherPermission(Context context) {
575 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
576 }
577
Antonio Cortese4619c72017-02-02 07:53:27 -0800578 public static void assertVmsSubscriberPermission(Context context) {
579 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
580 }
581
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800582 /**
583 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800584 */
585 public static void assertTrustAgentEnrollmentPermission(Context context) {
586 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
587 }
588
Steve Paik461ecc62016-06-08 15:28:32 -0700589 public static void assertPermission(Context context, String permission) {
590 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
591 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800592 }
593 }
594
Steve Paik9ec53d72018-04-27 13:28:31 -0700595 /**
596 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700597 *
598 * @return boolean TRUE if caller has the permission.
599 */
600 public static boolean hasPermission(Context context, String permission) {
601 return context.checkCallingOrSelfPermission(permission)
602 == PackageManager.PERMISSION_GRANTED;
603 }
604
Enrico Granata3c7a6662017-02-23 18:07:59 -0800605 public static void assertAnyPermission(Context context, String... permissions) {
606 for (String permission : permissions) {
607 if (context.checkCallingOrSelfPermission(permission) ==
608 PackageManager.PERMISSION_GRANTED) {
609 return;
610 }
611 }
612 throw new SecurityException("requires any of " + Arrays.toString(permissions));
613 }
614
Enrico Granatae8056ca2018-04-03 13:19:52 -0700615 @Override
616 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
617 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700618 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700619 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700620 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
621 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700622 return;
keunyounga3b28d82015-08-25 13:05:15 -0700623 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700624
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700625 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
626 writer.println("*Dump car service*");
Enrico Granatae8056ca2018-04-03 13:19:52 -0700627 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700628
Mark Tabry7e2a7822019-10-17 10:18:32 -0700629 dumpAllServices(writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700630
Enrico Granatae8056ca2018-04-03 13:19:52 -0700631 writer.println("*Dump Vehicle HAL*");
632 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
633 try {
634 // TODO dump all feature flags by creating a dumpable interface
635 mHal.dump(writer);
636 } catch (Exception e) {
637 writer.println("Failed dumping: " + mHal.getClass().getName());
638 e.printStackTrace(writer);
639 }
felipeal080e5652019-11-02 17:04:04 -0700640 } else if ("--list".equals(args[0])) {
641 dumpListOfServices(writer);
642 return;
643 } else if ("--services".equals(args[0])) {
644 if (args.length < 2) {
645 writer.print("Must pass services to dump when using --services");
646 return;
647 }
648 int length = args.length - 1;
649 String[] services = new String[length];
650 System.arraycopy(args, 1, services, 0, length);
651 dumpIndividualServices(writer, services);
652 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700653 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700654 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800655 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700656 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700657 } else if ("--vms-hal".equals(args[0])) {
658 mHal.getVmsHal().dumpMetrics(fd);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700659 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
660 execShellCmd(args, writer);
661 } else {
662 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800663 }
664 }
665
felipeal72bf6422019-11-02 17:04:04 -0700666 @Override
667 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
668 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
669 throws RemoteException {
670 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
671 }
672
felipeal080e5652019-11-02 17:04:04 -0700673 private void dumpListOfServices(PrintWriter writer) {
674 for (CarServiceBase service : mAllServices) {
675 writer.println(service.getClass().getName());
676 }
677 }
678
Mark Tabry7e2a7822019-10-17 10:18:32 -0700679 private void dumpAllServices(PrintWriter writer) {
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700680 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700681 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700682 }
683 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700684 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700685 }
felipeal080e5652019-11-02 17:04:04 -0700686 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700687
felipeal080e5652019-11-02 17:04:04 -0700688 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
689 for (String serviceName : serviceNames) {
690 writer.println("** Dumping " + serviceName + "\n");
691 CarServiceBase service = getCarServiceBySubstring(serviceName);
692 if (service == null) {
693 writer.println("No such service!");
694 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700695 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700696 }
697 writer.println();
698 }
699 }
700
701 @Nullable
702 private CarServiceBase getCarServiceBySubstring(String className) {
703 return Arrays.asList(mAllServices).stream()
704 .filter(s -> s.getClass().getSimpleName().equals(className))
705 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700706 }
707
Mark Tabry7e2a7822019-10-17 10:18:32 -0700708 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800709 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700710 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800711 } catch (Exception e) {
712 writer.println("Failed dumping: " + service.getClass().getName());
713 e.printStackTrace(writer);
714 }
keunyoungcc449f72015-08-12 10:46:27 -0700715 }
Yao Chene33f07e2016-07-26 12:02:51 -0700716
717 void execShellCmd(String[] args, PrintWriter writer) {
718 new CarShellCommand().exec(args, writer);
719 }
720
Pavel Maltsevabd47232017-10-10 16:54:57 -0700721 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700722 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700723 Slog.i(TAG, name);
724 mBootTiming.traceBegin(name);
725 }
726
Pavel Maltsevabd47232017-10-10 16:54:57 -0700727 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700728 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700729 mBootTiming.traceEnd();
730 }
731
felipeal72bf6422019-11-02 17:04:04 -0700732 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700733 private static final String COMMAND_HELP = "-h";
734 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800735 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700736 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800737 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800738 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800739 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800740 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700741 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700742 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800743 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700744 private static final String COMMAND_RESUME = "resume";
745 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700746 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
747 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700748 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700749 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
750 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800751 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
752 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800753 private static final String COMMAND_INJECT_KEY = "inject-key";
Felipe Lemea5e69382020-02-11 17:53:20 -0800754 private static final String COMMAND_GET_INITIAL_USER_INFO = "get-initial-user-info";
Felipe Leme5bf135b2020-02-07 12:22:14 -0800755
Yao Chene33f07e2016-07-26 12:02:51 -0700756 private static final String PARAM_DAY_MODE = "day";
757 private static final String PARAM_NIGHT_MODE = "night";
758 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800759 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800760 private static final String PARAM_ON_MODE = "on";
761 private static final String PARAM_OFF_MODE = "off";
762 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800763
felipeal72bf6422019-11-02 17:04:04 -0700764 private static final int RESULT_OK = 0;
765 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
766
767
768 @Override
769 public int onCommand(String cmd) {
770 if (cmd == null) {
771 onHelp();
772 return RESULT_ERROR;
773 }
774 ArrayList<String> argsList = new ArrayList<>();
775 argsList.add(cmd);
776 String arg = null;
777 do {
778 arg = getNextArg();
779 if (arg != null) {
780 argsList.add(arg);
781 }
782 } while (arg != null);
783 String[] args = new String[argsList.size()];
784 argsList.toArray(args);
785 return exec(args, getOutPrintWriter());
786 }
787
788 @Override
789 public void onHelp() {
790 dumpHelp(getOutPrintWriter());
791 }
Yao Chene33f07e2016-07-26 12:02:51 -0700792
793 private void dumpHelp(PrintWriter pw) {
794 pw.println("Car service commands:");
795 pw.println("\t-h");
796 pw.println("\t Print this help text.");
797 pw.println("\tday-night-mode [day|night|sensor]");
798 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800799 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800800 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700801 pw.println("\tinject-error-event property zone errorCode");
802 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700803 pw.println("\tenable-uxr true|false");
804 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800805 pw.println("\tgarage-mode [on|off|query]");
806 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800807 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800808 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800809 pw.println("\tget-carpropertyconfig [propertyId]");
810 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700811 pw.println("\tget-property-value [propertyId] [areaId]");
812 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
813 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700814 pw.println("\tsuspend");
815 pw.println("\t Suspend the system to Deep Sleep.");
816 pw.println("\tresume");
817 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700818 pw.println("\tenable-trusted-device true|false");
819 pw.println("\t Enable/Disable Trusted device feature.");
820 pw.println("\tremove-trusted-devices");
821 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700822 pw.println("\tprojection-tethering [true|false]");
823 pw.println("\t Whether tethering should be used when creating access point for"
824 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700825 pw.println("\t--metrics");
826 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700827 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
828 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700829 pw.println("\tstart-fixed-activity displayId packageName activityName");
830 pw.println("\t Start an Activity the specified display as fixed mode");
831 pw.println("\tstop-fixed-mode displayId");
832 pw.println("\t Stop fixed Activity mode for the given display. "
833 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800834 pw.println("\tenable-feature featureName");
835 pw.println("\t Enable the requested feature. Change will happen after reboot.");
836 pw.println("\t This requires root/su.");
837 pw.println("\tdisable-feature featureName");
838 pw.println("\t Disable the requested feature. Change will happen after reboot");
839 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800840 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
841 pw.println("\t inject key down / up event to car service");
842 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
843 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
844 pw.println("\t it will be 0");
845 pw.println("\t key_code: int key code defined in android KeyEvent");
Felipe Lemea5e69382020-02-11 17:53:20 -0800846 pw.printf("\t%s <REQ_TYPE> [--timeout TIMEOUT_MS]\n", COMMAND_GET_INITIAL_USER_INFO);
847 pw.println("\t Calls the Vehicle HAL to get the initial boot info, passing the given");
848 pw.println("\t REQ_TYPE (which could be either FIRST_BOOT, FIRST_BOOT_AFTER_OTA, ");
849 pw.println("\t COLD_BOOT, RESUME, or any numeric value that would be passed 'as-is')");
850 pw.println("\t and an optional TIMEOUT_MS to wait for the HAL response (if not set,");
851 pw.println("\t it will use a default value).");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700852 }
853
felipeal72bf6422019-11-02 17:04:04 -0700854 private int dumpInvalidArguments(PrintWriter pw) {
855 pw.println("Incorrect number of arguments.");
856 dumpHelp(pw);
857 return RESULT_ERROR;
858 }
859
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700860 private String runSetZoneIdForUid(String zoneString, String uidString) {
861 int uid = Integer.parseInt(uidString);
862 int zoneId = Integer.parseInt(zoneString);
863 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
864 return "zoneid " + zoneId + " not found";
865 }
866 mCarAudioService.setZoneIdForUid(zoneId, uid);
867 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700868 }
869
felipeal72bf6422019-11-02 17:04:04 -0700870 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700871 String arg = args[0];
872 switch (arg) {
873 case COMMAND_HELP:
874 dumpHelp(writer);
875 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800876 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800877 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700878 forceDayNightMode(value, writer);
879 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800880 }
881 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800882 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800883 forceGarageMode(value, writer);
884 break;
885 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800886 case COMMAND_INJECT_VHAL_EVENT:
887 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
888 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800889 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700890 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800891 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800892 // Zoned
893 zone = args[2];
894 data = args[3];
895 } else {
896 // Global
897 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800898 }
Kai4b098cf2019-06-05 15:03:28 -0700899 injectVhalEvent(args[1], zone, data, false, writer);
900 break;
901 case COMMAND_INJECT_ERROR_EVENT:
902 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700903 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700904 }
905 String errorAreaId = args[2];
906 String errorCode = args[3];
907 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800908 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800909 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800910 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700911 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800912 }
913 boolean enableBlocking = Boolean.valueOf(args[1]);
914 if (mCarPackageManagerService != null) {
915 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
916 }
917 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800918 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800919 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700920 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800921 }
922 String pkgName = args[1].toLowerCase();
923 if (mCarPackageManagerService != null) {
924 String[] doActivities =
925 mCarPackageManagerService.getDistractionOptimizedActivities(
926 pkgName);
927 if (doActivities != null) {
928 writer.println("DO Activities for " + pkgName);
929 for (String a : doActivities) {
930 writer.println(a);
931 }
932 } else {
933 writer.println("No DO Activities for " + pkgName);
934 }
935 }
936 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800937 case COMMAND_GET_CARPROPERTYCONFIG:
938 String propertyId = args.length < 2 ? "" : args[1];
939 mHal.dumpPropertyConfigs(writer, propertyId);
940 break;
Kai29fba112019-04-22 18:37:38 -0700941 case COMMAND_GET_PROPERTY_VALUE:
942 String propId = args.length < 2 ? "" : args[1];
943 String areaId = args.length < 3 ? "" : args[2];
944 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
945 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800946 case COMMAND_PROJECTION_UI_MODE:
947 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700948 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -0800949 }
950 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700951 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700952 case COMMAND_PROJECTION_AP_TETHERING:
953 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700954 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700955 }
956 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
957 break;
Jim Kayed76c2742019-04-02 11:33:27 -0700958 case COMMAND_RESUME:
959 mCarPowerManagementService.forceSimulatedResume();
960 writer.println("Resume: Simulating resuming from Deep Sleep");
961 break;
962 case COMMAND_SUSPEND:
963 mCarPowerManagementService.forceSimulatedSuspend();
964 writer.println("Resume: Simulating powering down to Deep Sleep");
965 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700966 case COMMAND_ENABLE_TRUSTED_DEVICE:
967 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700968 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700969 }
970 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
971 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
972 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
973 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
974 break;
975 case COMMAND_REMOVE_TRUSTED_DEVICES:
976 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -0700977 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700978 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700979 case COMMAND_SET_UID_TO_ZONE:
980 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -0700981 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700982 }
983 String results = runSetZoneIdForUid(args[1], args[2]);
984 if (results != null) {
985 writer.println(results);
986 dumpHelp(writer);
987 }
988 break;
Keun young Park09ca8492019-10-15 10:34:11 -0700989 case COMMAND_START_FIXED_ACTIVITY_MODE:
990 handleStartFixedActivity(args, writer);
991 break;
992 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
993 handleStopFixedMode(args, writer);
994 break;
Keun young Park9a91efb2019-11-15 18:10:47 -0800995 case COMMAND_ENABLE_FEATURE:
996 if (args.length != 2) {
997 return dumpInvalidArguments(writer);
998 }
999 handleEnableDisableFeature(args, writer, /* enable= */ true);
1000 break;
1001 case COMMAND_DISABLE_FEATURE:
1002 if (args.length != 2) {
1003 return dumpInvalidArguments(writer);
1004 }
1005 handleEnableDisableFeature(args, writer, /* enable= */ false);
1006 break;
Keun young Park313dfaf2019-12-19 11:32:38 -08001007 case COMMAND_INJECT_KEY:
1008 if (args.length < 2) {
1009 return dumpInvalidArguments(writer);
1010 }
1011 handleInjectKey(args, writer);
1012 break;
Felipe Lemea5e69382020-02-11 17:53:20 -08001013 case COMMAND_GET_INITIAL_USER_INFO:
1014 handleGetInitialUserInfo(args, writer);
1015 break;
Yao Chene33f07e2016-07-26 12:02:51 -07001016 default:
Jim Kayef10b7d82019-02-26 12:53:22 -08001017 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -07001018 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -07001019 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -07001020 }
felipeal72bf6422019-11-02 17:04:04 -07001021 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -07001022 }
1023
Keun young Park09ca8492019-10-15 10:34:11 -07001024 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1025 if (args.length != 4) {
1026 writer.println("Incorrect number of arguments");
1027 dumpHelp(writer);
1028 return;
1029 }
1030 int displayId;
1031 try {
1032 displayId = Integer.parseInt(args[1]);
1033 } catch (NumberFormatException e) {
1034 writer.println("Wrong display id:" + args[1]);
1035 return;
1036 }
1037 String packageName = args[2];
1038 String activityName = args[3];
1039 Intent intent = new Intent();
1040 intent.setComponent(new ComponentName(packageName, activityName));
1041 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1042 ActivityOptions options = ActivityOptions.makeBasic();
1043 options.setLaunchDisplayId(displayId);
1044 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1045 displayId, ActivityManager.getCurrentUser())) {
1046 writer.println("Failed to start");
1047 return;
1048 }
1049 writer.println("Succeeded");
1050 }
1051
1052 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1053 if (args.length != 2) {
1054 writer.println("Incorrect number of arguments");
1055 dumpHelp(writer);
1056 return;
1057 }
1058 int displayId;
1059 try {
1060 displayId = Integer.parseInt(args[1]);
1061 } catch (NumberFormatException e) {
1062 writer.println("Wrong display id:" + args[1]);
1063 return;
1064 }
1065 mFixedActivityService.stopFixedActivityMode(displayId);
1066 }
1067
Keun young Park9a91efb2019-11-15 18:10:47 -08001068 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1069 if (Binder.getCallingUid() != Process.ROOT_UID) {
1070 writer.println("Only allowed to root/su");
1071 return;
1072 }
1073 String featureName = args[1];
1074 long id = Binder.clearCallingIdentity();
1075 // no permission check here
1076 int r;
1077 if (enable) {
1078 r = mFeatureController.enableFeature(featureName);
1079 } else {
1080 r = mFeatureController.disableFeature(featureName);
1081 }
1082 switch (r) {
1083 case Car.FEATURE_REQUEST_SUCCESS:
1084 if (enable) {
1085 writer.println("Enabled feature:" + featureName);
1086 } else {
1087 writer.println("Disabled feature:" + featureName);
1088 }
1089 break;
1090 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1091 if (enable) {
1092 writer.println("Already enabled:" + featureName);
1093 } else {
1094 writer.println("Already disabled:" + featureName);
1095 }
1096 break;
1097 case Car.FEATURE_REQUEST_MANDATORY:
1098 writer.println("Cannot change mandatory feature:" + featureName);
1099 break;
1100 case Car.FEATURE_REQUEST_NOT_EXISTING:
1101 writer.println("Non-existing feature:" + featureName);
1102 break;
1103 default:
1104 writer.println("Unknown error:" + r);
1105 break;
1106 }
1107 Binder.restoreCallingIdentity(id);
1108 }
1109
Keun young Park313dfaf2019-12-19 11:32:38 -08001110 private void handleInjectKey(String[] args, PrintWriter writer) {
1111 int i = 1; // 0 is command itself
1112 int display = InputHalService.DISPLAY_MAIN;
1113 int delayMs = 0;
1114 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1115 try {
1116 while (i < args.length) {
1117 switch (args[i]) {
1118 case "-d":
1119 i++;
1120 display = Integer.parseInt(args[i]);
1121 break;
1122 case "-t":
1123 i++;
1124 delayMs = Integer.parseInt(args[i]);
1125 break;
1126 default:
1127 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1128 throw new IllegalArgumentException("key_code already set:"
1129 + keyCode);
1130 }
1131 keyCode = Integer.parseInt(args[i]);
1132 }
1133 i++;
1134 }
1135 } catch (Exception e) {
1136 writer.println("Invalid args:" + e);
1137 dumpHelp(writer);
1138 return;
1139 }
1140 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1141 writer.println("Missing key code or invalid keycode");
1142 dumpHelp(writer);
1143 return;
1144 }
1145 if (display != InputHalService.DISPLAY_MAIN
1146 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1147 writer.println("Invalid display:" + display);
1148 dumpHelp(writer);
1149 return;
1150 }
1151 if (delayMs < 0) {
1152 writer.println("Invalid delay:" + delayMs);
1153 dumpHelp(writer);
1154 return;
1155 }
1156 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1157 mCarInputService.onKeyEvent(keyDown, display);
1158 SystemClock.sleep(delayMs);
1159 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1160 mCarInputService.onKeyEvent(keyUp, display);
1161 writer.println("Succeeded");
1162 }
1163
Felipe Lemea5e69382020-02-11 17:53:20 -08001164 private void handleGetInitialUserInfo(String[] args, PrintWriter writer) {
1165 if (args.length < 2) {
1166 writer.println("Insufficient number of args");
1167 return;
1168 }
1169
1170 // Gets the request type
1171 String typeArg = args[1];
1172 int requestType = UserHalHelper.parseInitialUserInfoRequestType(typeArg);
1173
1174 int timeout = 1_000;
1175 for (int i = 2; i < args.length; i++) {
1176 String arg = args[i];
1177 switch (arg) {
1178 case "--timeout":
1179 timeout = Integer.parseInt(args[++i]);
1180 break;
1181 default:
1182 writer.println("Invalid option at index " + i + ": " + arg);
1183 return;
1184
1185 }
1186 }
1187
1188 Log.d(TAG, "handleGetInitialUserInfo(): type=" + requestType + " (" + typeArg
1189 + "), timeout=" + timeout);
1190
1191 UserHalService userHal = mHal.getUserHal();
1192 // TODO(b/146207078): use UserHalHelper to populate it with current users
1193 UsersInfo usersInfo = new UsersInfo();
1194 CountDownLatch latch = new CountDownLatch(1);
1195
1196 userHal.getInitialUserInfo(requestType, timeout, usersInfo, (status, resp) -> {
1197 try {
1198 Log.d(TAG, "GetUserInfoResponse: status=" + status + ", resp=" + resp);
1199 writer.printf("Status: %s\n", UserHalHelper.halCallbackStatusToString(status));
1200 if (status != HalCallback.STATUS_OK) {
1201 return;
1202 }
1203 writer.printf("Request id: %d\n", resp.requestId);
1204 writer.printf("Action: ");
1205 switch (resp.action) {
1206 case InitialUserInfoResponseAction.DEFAULT:
1207 writer.println("default");
1208 break;
1209 case InitialUserInfoResponseAction.SWITCH:
1210 writer.printf("switch to user %d\n", resp.userToSwitchOrCreate.userId);
1211 break;
1212 case InitialUserInfoResponseAction.CREATE:
1213 writer.printf("create user: name=%s, flags=%d\n", resp.userNameToCreate,
1214 resp.userToSwitchOrCreate.flags);
1215 break;
1216 default:
1217 writer.printf("unknown (%d)\n", resp.action);
1218 break;
1219 }
1220 } finally {
1221 latch.countDown();
1222 }
1223 });
1224
1225 try {
1226 if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
1227 writer.printf("HAL didn't respond in %dms\n", timeout);
1228 }
1229 } catch (InterruptedException e) {
1230 Thread.currentThread().interrupt();
1231 writer.println("Interrupted waiting for HAL");
1232 }
1233 return;
1234 }
1235
Yao Chene33f07e2016-07-26 12:02:51 -07001236 private void forceDayNightMode(String arg, PrintWriter writer) {
1237 int mode;
1238 switch (arg) {
1239 case PARAM_DAY_MODE:
1240 mode = CarNightService.FORCED_DAY_MODE;
1241 break;
1242 case PARAM_NIGHT_MODE:
1243 mode = CarNightService.FORCED_NIGHT_MODE;
1244 break;
1245 case PARAM_SENSOR_MODE:
1246 mode = CarNightService.FORCED_SENSOR_MODE;
1247 break;
1248 default:
1249 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1250 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1251 return;
1252 }
1253 int current = mCarNightService.forceDayNightMode(mode);
1254 String currentMode = null;
1255 switch (current) {
1256 case UiModeManager.MODE_NIGHT_AUTO:
1257 currentMode = PARAM_SENSOR_MODE;
1258 break;
1259 case UiModeManager.MODE_NIGHT_YES:
1260 currentMode = PARAM_NIGHT_MODE;
1261 break;
1262 case UiModeManager.MODE_NIGHT_NO:
1263 currentMode = PARAM_DAY_MODE;
1264 break;
1265 }
1266 writer.println("DayNightMode changed to: " + currentMode);
1267 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001268
Bryan Eylera32a7c12018-02-27 15:40:00 -08001269 private void forceGarageMode(String arg, PrintWriter writer) {
1270 switch (arg) {
1271 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001272 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001273 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001274 break;
1275 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001276 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001277 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001278 break;
1279 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001280 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001281 break;
1282 default:
1283 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
1284 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001285 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001286 }
1287
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001288 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001289 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001290 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001291 * @param property the Vehicle property Id as defined in the HAL
1292 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001293 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001294 * @param value Data value of the event
1295 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001296 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001297 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001298 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001299 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1300 if (!isPropertyAreaTypeGlobal(property)) {
1301 writer.println("Property area type inconsistent with given zone");
1302 return;
1303 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001304 }
1305 try {
Kai4b098cf2019-06-05 15:03:28 -07001306 if (isErrorEvent) {
1307 mHal.injectOnPropertySetError(property, zone, value);
1308 } else {
1309 mHal.injectVhalEvent(property, zone, value);
1310 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001311 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001312 writer.println("Invalid property Id zone Id or value" + e);
1313 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001314 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001315 }
1316
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001317 // Check if the given property is global
1318 private boolean isPropertyAreaTypeGlobal(String property) {
1319 if (property == null) {
1320 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001321 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001322 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001323 }
Yao Chene33f07e2016-07-26 12:02:51 -07001324 }
kevinjm55822c42018-08-15 11:26:00 -07001325}