blob: a68e171e52cb3ba599cb2a4450eb480a13f51e26 [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;
keunyoungca515072015-07-10 12:21:47 -070072import com.android.internal.annotations.GuardedBy;
Felipe Leme6ef89652019-12-19 16:47:14 -080073import com.android.internal.annotations.VisibleForTesting;
Steve Paik1d8bea22018-02-03 01:54:06 +000074import com.android.internal.car.ICarServiceHelper;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070075import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070076
Enrico Granatae8056ca2018-04-03 13:19:52 -070077import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070078import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070079import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080080import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070081import java.util.List;
Felipe Lemea5e69382020-02-11 17:53:20 -080082import java.util.concurrent.CountDownLatch;
83import java.util.concurrent.TimeUnit;
keunyounga3b28d82015-08-25 13:05:15 -070084
keunyoungca515072015-07-10 12:21:47 -070085public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070086
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080087 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070088 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
89 "system_activity_monitoring";
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 Tabry6fa123d2020-01-10 19:52:59 -0800126 private final VmsNewBrokerService mVmsBrokerService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700127 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700128 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800129 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
keunyounga74b9ca2015-10-21 13:33:58 -0700130
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700131 private final CarServiceBase[] mAllServices;
132
Steve Paikc302c7c2017-08-04 14:01:58 -0700133 private static final String TAG = "ICarImpl";
134 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700135
136 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700137
keunyoung1ab8e182015-09-24 09:25:22 -0700138 /** Test only service. Populate it only when necessary. */
139 @GuardedBy("this")
140 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700141
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700142 @GuardedBy("this")
143 private ICarServiceHelper mICarServiceHelper;
144
Enrico Granatae8056ca2018-04-03 13:19:52 -0700145 private final String mVehicleInterfaceName;
146
Felipe Lemee93218b2020-02-13 14:10:52 -0800147 private final UserMetrics mUserMetrics = new UserMetrics();
148
Pavel Maltsevec83b632017-01-05 15:10:55 -0800149 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700150 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
Felipe Leme6ef89652019-12-19 16:47:14 -0800151 this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
152 /* carUserService= */ null);
153 }
154
155 @VisibleForTesting
156 ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
157 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
158 @Nullable CarUserService carUserService) {
keunyoungca515072015-07-10 12:21:47 -0700159 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800160 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700161 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800162 Resources res = mContext.getResources();
163 String[] defaultEnabledFeatures = res.getStringArray(
164 R.array.config_allowed_optional_car_features);
165 // Do this before any other service components to allow feature check. It should work
166 // even without init.
167 // TODO (b/144504820) Add vhal plumbing
168 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
169 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
170 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700171 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800172 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Felipe Leme6ef89652019-12-19 16:47:14 -0800173 if (carUserService != null) {
174 mCarUserService = carUserService;
175 } else {
176 UserManager userManager =
177 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
178 int maxRunningUsers = res.getInteger(
179 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Felipe Leme58412202020-01-09 13:45:33 -0800180 mCarUserService = new CarUserService(serviceContext, mHal.getUserHal(),
181 mUserManagerHelper, userManager, ActivityManager.getService(), maxRunningUsers);
Felipe Leme6ef89652019-12-19 16:47:14 -0800182 }
Keun young Parkbb877e22019-08-02 10:38:01 -0700183 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700184 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800185 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700186 systemInterface, mUserManagerHelper);
Keun young Park9a91efb2019-11-15 18:10:47 -0800187 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
188 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
189 } else {
190 mCarUserNoticeService = null;
191 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700192 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
193 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800194 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800195 mCarDrivingStateService, mCarPropertyService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800196 if (mFeatureController.isFeatureEnabled(Car.OCCUPANT_AWARENESS_SERVICE)) {
197 mOccupantAwarenessService = new OccupantAwarenessService(serviceContext);
198 } else {
199 mOccupantAwarenessService = null;
200 }
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800201 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
202 mCarUXRestrictionsService,
Mayank Garg31e73042020-01-23 00:10:38 -0800203 mSystemActivityMonitoringService,
204 mCarUserService);
205 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext, mCarUserService);
Sal Savage703c46f2019-04-15 08:39:25 -0700206 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700207 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800208 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800209 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700210 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700211 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800212 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700213 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700214 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700215 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700216 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700217 mSystemStateControllerService = new SystemStateControllerService(
218 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700219 mCarStatsService = new CarStatsService(serviceContext);
Ruchir Rastogia683d742020-01-22 00:17:51 -0800220 mCarStatsService.init();
Mark Tabry62e749c2020-01-10 19:52:59 -0800221 if (mFeatureController.isFeatureEnabled(Car.VEHICLE_MAP_SERVICE)
222 || mFeatureController.isFeatureEnabled(Car.VMS_SUBSCRIBER_SERVICE)) {
Mark Tabry6fa123d2020-01-10 19:52:59 -0800223 mVmsBrokerService = new VmsNewBrokerService(mContext, mCarStatsService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800224 } else {
225 mVmsBrokerService = null;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800226 }
Keun young Parkc6d80af2020-01-17 18:14:28 -0800227 if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
228 mCarDiagnosticService = new CarDiagnosticService(serviceContext,
229 mHal.getDiagnosticHal());
230 } else {
231 mCarDiagnosticService = null;
232 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800233 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
234 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
235 systemInterface);
236 } else {
237 mCarStorageMonitoringService = null;
238 }
Anthony Chen12aec302018-04-25 16:41:48 -0700239 mCarConfigurationService =
240 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700241 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700242 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Mayank Garg31e73042020-01-23 00:10:38 -0800243 mCarMediaService = new CarMediaService(serviceContext, mCarUserService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700244 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800245 if (!Build.IS_USER) {
246 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
247 serviceContext);
248 } else {
249 mCarExperimentalFeatureServiceController = null;
250 }
keunyounga74b9ca2015-10-21 13:33:58 -0700251
Keun young Parka5fa4782019-04-16 18:56:27 -0700252 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700253 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800254 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700255 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800256 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700257 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700258 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700259 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Mark Tabry62e749c2020-01-10 19:52:59 -0800260 CarLocalServices.addService(VmsNewBrokerService.class, mVmsBrokerService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800261
keunyounga3b28d82015-08-25 13:05:15 -0700262 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700263 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800264 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800265 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700266 allServices.add(mSystemActivityMonitoringService);
267 allServices.add(mCarPowerManagementService);
268 allServices.add(mCarPropertyService);
269 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700270 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700271 allServices.add(mCarUXRestrictionsService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800272 addServiceIfNonNull(allServices, mOccupantAwarenessService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700273 allServices.add(mCarPackageManagerService);
274 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700275 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800276 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700277 allServices.add(mAppFocusService);
278 allServices.add(mCarAudioService);
279 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700280 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700281 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700282 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700283 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800284 allServices.add(mCarBluetoothService);
285 allServices.add(mCarProjectionService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800286 addServiceIfNonNull(allServices, mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800287 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700288 allServices.add(mCarConfigurationService);
Mark Tabry6fa123d2020-01-10 19:52:59 -0800289 addServiceIfNonNull(allServices, mVmsBrokerService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700290 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800291 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700292 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700293 allServices.add(mCarBugreportManagerService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800294 // Always put mCarExperimentalFeatureServiceController in last.
295 addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700296 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700297 }
298
Keun young Park9a91efb2019-11-15 18:10:47 -0800299 private void addServiceIfNonNull(List<CarServiceBase> services, CarServiceBase service) {
300 if (service != null) {
301 services.add(service);
302 }
303 }
304
Pavel Maltsevabd47232017-10-10 16:54:57 -0700305 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700306 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700307 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700308 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700309 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700310 traceEnd();
311 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800312 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700313 service.init();
314 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700315 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700316 }
317
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700318 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700319 // release done in opposite order from init
320 for (int i = mAllServices.length - 1; i >= 0; i--) {
321 mAllServices[i].release();
322 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700323 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700324 }
325
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700326 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800327 mHal.vehicleHalReconnected(vehicle);
328 for (CarServiceBase service : mAllServices) {
329 service.vehicleHalReconnected();
330 }
331 }
332
keunyoungca515072015-07-10 12:21:47 -0700333 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700334 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700335 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700336 synchronized (this) {
337 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800338 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700339 }
340 }
341
342 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700343 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700344 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700345 mCarUserService.setUserLockStatus(userId, unlocked == 1);
346 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700347 }
348
349 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700350 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700351 assertCallingFromSystemProcess();
352
Eric Jeong1545f3b2019-09-16 13:56:52 -0700353 Log.i(TAG, "Foreground user switched to " + userId);
354 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700355 }
356
Felipe Lemee93218b2020-02-13 14:10:52 -0800357 // TODO(b/146207078): this method is currently used just for metrics logging purposes, but we
358 // should fold the other too (onSwitchUser() and setUserLockStatus()) onto it.
359 @Override
360 public void onUserLifecycleEvent(int eventType, long timestampMs, int fromUserId,
361 int toUserId) {
362 assertCallingFromSystemProcess();
363 Log.i(TAG, "onUserLifecycleEvent(" + CarUserManager.lifecycleEventTypeToString(eventType)
364 + ", " + toUserId);
365 mUserMetrics.onEvent(eventType, timestampMs, fromUserId, toUserId);
366 }
367
Keun young Park9a91efb2019-11-15 18:10:47 -0800368 @Override
369 public boolean isFeatureEnabled(String featureName) {
370 return mFeatureController.isFeatureEnabled(featureName);
371 }
372
373 @Override
374 public int enableFeature(String featureName) {
375 // permission check inside the controller
376 return mFeatureController.enableFeature(featureName);
377 }
378
379 @Override
380 public int disableFeature(String featureName) {
381 // permission check inside the controller
382 return mFeatureController.disableFeature(featureName);
383 }
384
385 @Override
386 public List<String> getAllEnabledFeatures() {
387 // permission check inside the controller
388 return mFeatureController.getAllEnabledFeatures();
389 }
390
391 @Override
392 public List<String> getAllPendingDisabledFeatures() {
393 // permission check inside the controller
394 return mFeatureController.getAllPendingDisabledFeatures();
395 }
396
397 @Override
398 public List<String> getAllPendingEnabledFeatures() {
399 // permission check inside the controller
400 return mFeatureController.getAllPendingEnabledFeatures();
401 }
402
403 @Override
404 public String getCarManagerClassForFeature(String featureName) {
405 if (mCarExperimentalFeatureServiceController == null) {
406 return null;
407 }
408 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
409 }
410
Jim Kayee5133162019-04-22 12:50:27 -0700411 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800412 int uid = Binder.getCallingUid();
413 if (uid != Process.SYSTEM_UID) {
414 throw new SecurityException("Only allowed from system");
415 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800416 }
417
Keun young Parkaabecd92019-05-03 17:31:27 -0700418 /**
419 * Assert if binder call is coming from system process like system server or if it is called
420 * from its own process even if it is not system. The latter can happen in test environment.
421 * Note that car service runs as system user but test like car service test will not.
422 */
423 static void assertCallingFromSystemProcessOrSelf() {
424 int uid = Binder.getCallingUid();
425 int pid = Binder.getCallingPid();
426 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
427 throw new SecurityException("Only allowed from system or self");
428 }
429 }
430
Keun-young Parkd462a912019-02-11 08:53:42 -0800431 @Override
keunyoungca515072015-07-10 12:21:47 -0700432 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800433 if (!mFeatureController.isFeatureEnabled(serviceName)) {
434 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
435 return null;
436 }
keunyoungca515072015-07-10 12:21:47 -0700437 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800438 case Car.AUDIO_SERVICE:
439 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700440 case Car.APP_FOCUS_SERVICE:
441 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800442 case Car.PACKAGE_SERVICE:
443 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800444 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700445 assertAnyDiagnosticPermission(mContext);
446 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800447 case Car.POWER_SERVICE:
448 assertPowerPermission(mContext);
449 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700450 case Car.CABIN_SERVICE:
451 case Car.HVAC_SERVICE:
452 case Car.INFO_SERVICE:
453 case Car.PROPERTY_SERVICE:
454 case Car.SENSOR_SERVICE:
455 case Car.VENDOR_EXTENSION_SERVICE:
456 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800457 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700458 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700459 IInstrumentClusterNavigation navService =
460 mInstrumentClusterService.getNavigationService();
461 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700462 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
463 assertClusterManagerPermission(mContext);
464 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800465 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800466 return mCarProjectionService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800467 case Car.VEHICLE_MAP_SERVICE:
468 assertAnyVmsPermission(mContext);
469 return mVmsBrokerService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800470 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700471 assertVmsSubscriberPermission(mContext);
Mark Tabry62e749c2020-01-10 19:52:59 -0800472 return mVmsBrokerService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800473 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700474 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700475 synchronized (this) {
476 if (mCarTestService == null) {
477 mCarTestService = new CarTestService(mContext, this);
478 }
479 return mCarTestService;
480 }
481 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700482 case Car.BLUETOOTH_SERVICE:
483 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700484 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700485 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700486 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800487 case Car.CAR_DRIVING_STATE_SERVICE:
488 assertDrivingStatePermission(mContext);
489 return mCarDrivingStateService;
490 case Car.CAR_UX_RESTRICTION_SERVICE:
491 return mCarUXRestrictionsService;
Michael Kellerac2ed202020-01-30 14:11:16 -0800492 case Car.OCCUPANT_AWARENESS_SERVICE:
493 return mOccupantAwarenessService;
Anthony Chend4203d82018-05-16 16:21:52 -0700494 case Car.CAR_CONFIGURATION_SERVICE:
495 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800496 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
497 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700498 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800499 case Car.CAR_MEDIA_SERVICE:
500 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700501 case Car.CAR_OCCUPANT_ZONE_SERVICE:
502 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700503 case Car.CAR_BUGREPORT_SERVICE:
504 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700505 case Car.CAR_USER_SERVICE:
506 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700507 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800508 IBinder service = null;
509 if (mCarExperimentalFeatureServiceController != null) {
510 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
511 }
512 if (service == null) {
513 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
514 + serviceName);
515 }
516 return service;
keunyoungca515072015-07-10 12:21:47 -0700517 }
518 }
519
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800520 @Override
521 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700522 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800523 }
524
Keun-young Parka28d7b22016-02-29 16:54:29 -0800525 public CarServiceBase getCarInternalService(String serviceName) {
526 switch (serviceName) {
527 case INTERNAL_INPUT_SERVICE:
528 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700529 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
530 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800531 default:
532 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
533 serviceName);
534 return null;
535 }
536 }
537
keunyoung1ab8e182015-09-24 09:25:22 -0700538 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700539 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700540 }
541
Keun-young Parke31a8b22016-03-16 17:34:08 -0700542 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700543 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800544 }
545
Pavel Maltsev905968c2017-07-16 19:48:57 -0700546 public static void assertClusterManagerPermission(Context context) {
547 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
548 }
549
Steve Paik388d7772018-02-12 10:54:51 -0800550 public static void assertPowerPermission(Context context) {
551 assertPermission(context, Car.PERMISSION_CAR_POWER);
552 }
553
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800554 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700555 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
556 }
557
Pavel Maltsev079873b2019-02-25 12:15:09 -0800558 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
559 public static void assertProjectionStatusPermission(Context context) {
560 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
561 }
562
Enrico Granata3c7a6662017-02-23 18:07:59 -0800563 public static void assertAnyDiagnosticPermission(Context context) {
564 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700565 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800566 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
567 }
568
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800569 public static void assertDrivingStatePermission(Context context) {
570 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
571 }
572
Mark Tabry6fa123d2020-01-10 19:52:59 -0800573 /**
574 * Verify the calling context has either {@link Car#PERMISSION_VMS_SUBSCRIBER} or
575 * {@link Car#PERMISSION_VMS_PUBLISHER}
576 */
577 public static void assertAnyVmsPermission(Context context) {
578 assertAnyPermission(context,
579 Car.PERMISSION_VMS_SUBSCRIBER,
580 Car.PERMISSION_VMS_PUBLISHER);
581 }
582
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800583 public static void assertVmsPublisherPermission(Context context) {
584 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
585 }
586
Antonio Cortese4619c72017-02-02 07:53:27 -0800587 public static void assertVmsSubscriberPermission(Context context) {
588 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
589 }
590
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800591 /**
592 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800593 */
594 public static void assertTrustAgentEnrollmentPermission(Context context) {
595 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
596 }
597
Steve Paik461ecc62016-06-08 15:28:32 -0700598 public static void assertPermission(Context context, String permission) {
599 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
600 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800601 }
602 }
603
Steve Paik9ec53d72018-04-27 13:28:31 -0700604 /**
605 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700606 *
607 * @return boolean TRUE if caller has the permission.
608 */
609 public static boolean hasPermission(Context context, String permission) {
610 return context.checkCallingOrSelfPermission(permission)
611 == PackageManager.PERMISSION_GRANTED;
612 }
613
Enrico Granata3c7a6662017-02-23 18:07:59 -0800614 public static void assertAnyPermission(Context context, String... permissions) {
615 for (String permission : permissions) {
616 if (context.checkCallingOrSelfPermission(permission) ==
617 PackageManager.PERMISSION_GRANTED) {
618 return;
619 }
620 }
621 throw new SecurityException("requires any of " + Arrays.toString(permissions));
622 }
623
Enrico Granatae8056ca2018-04-03 13:19:52 -0700624 @Override
625 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
626 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700627 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700628 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700629 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
630 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700631 return;
keunyounga3b28d82015-08-25 13:05:15 -0700632 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700633
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700634 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
635 writer.println("*Dump car service*");
Mark Tabry7e2a7822019-10-17 10:18:32 -0700636 dumpAllServices(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800637 dumpAllHals(writer);
Felipe Lemee93218b2020-02-13 14:10:52 -0800638 mUserMetrics.dump(writer);
felipeal080e5652019-11-02 17:04:04 -0700639 } else if ("--list".equals(args[0])) {
640 dumpListOfServices(writer);
641 return;
642 } else if ("--services".equals(args[0])) {
643 if (args.length < 2) {
644 writer.print("Must pass services to dump when using --services");
645 return;
646 }
647 int length = args.length - 1;
648 String[] services = new String[length];
649 System.arraycopy(args, 1, services, 0, length);
650 dumpIndividualServices(writer, services);
651 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700652 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700653 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800654 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700655 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700656 } else if ("--vms-hal".equals(args[0])) {
657 mHal.getVmsHal().dumpMetrics(fd);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800658 } else if ("--hal".equals(args[0])) {
659 if (args.length == 1) {
660 dumpAllHals(writer);
661 return;
662 }
663 int length = args.length - 1;
664 String[] halNames = new String[length];
665 System.arraycopy(args, 1, halNames, 0, length);
666 mHal.dumpSpecificHals(writer, halNames);
667
668 } else if ("--list-hals".equals(args[0])) {
669 mHal.dumpListHals(writer);
670 return;
Felipe Lemee93218b2020-02-13 14:10:52 -0800671 } else if ("--user-metrics".equals(args[0])) {
672 mUserMetrics.dump(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800673 } else if ("--help".equals(args[0])) {
674 showDumpHelp(writer);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700675 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
676 execShellCmd(args, writer);
677 } else {
678 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800679 }
680 }
681
Felipe Lemef07c3f92020-02-12 14:26:33 -0800682 private void dumpAllHals(PrintWriter writer) {
683 writer.println("*Dump Vehicle HAL*");
684 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
685 try {
686 // TODO dump all feature flags by creating a dumpable interface
687 mHal.dump(writer);
688 } catch (Exception e) {
689 writer.println("Failed dumping: " + mHal.getClass().getName());
690 e.printStackTrace(writer);
691 }
692 }
693
694 private void showDumpHelp(PrintWriter writer) {
695 writer.println("Car service dump usage:");
696 writer.println("[NO ARG]");
697 writer.println("\t dumps everything (all services and HALs)");
698 writer.println("--help");
699 writer.println("\t shows this help");
700 writer.println("--list");
701 writer.println("\t lists the name of all services");
702 writer.println("--list");
703 writer.println("\t lists the name of all HAls");
704 writer.println("--services <SVC1> [SVC2] [SVCN]");
705 writer.println("\t dumps just the specific services, where SVC is just the service class");
706 writer.println("\t name (like CarUserService)");
707 writer.println("--vms-hal");
708 writer.println("\t dumps the VMS HAL metrics");
709 writer.println("--hal [HAL1] [HAL2] [HALN]");
710 writer.println("\t dumps just the specified HALs (or all of them if none specified),");
711 writer.println("\t where HAL is just the class name (like UserHalService)");
Felipe Lemee93218b2020-02-13 14:10:52 -0800712 writer.println("--user-metrics");
713 writer.println("\t dumps user switching and stopping metrics ");
Felipe Lemef07c3f92020-02-12 14:26:33 -0800714 writer.println("-h");
715 writer.println("\t shows commands usage (NOTE: commands are not available on USER builds");
716 writer.println("[ANYTHING ELSE]");
717 writer.println("\t runs the given command (use --h to see the available commands)");
718 }
719
felipeal72bf6422019-11-02 17:04:04 -0700720 @Override
721 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
722 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
723 throws RemoteException {
724 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
725 }
726
felipeal080e5652019-11-02 17:04:04 -0700727 private void dumpListOfServices(PrintWriter writer) {
728 for (CarServiceBase service : mAllServices) {
729 writer.println(service.getClass().getName());
730 }
731 }
732
Mark Tabry7e2a7822019-10-17 10:18:32 -0700733 private void dumpAllServices(PrintWriter writer) {
Felipe Lemef07c3f92020-02-12 14:26:33 -0800734 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700735 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700736 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700737 }
738 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700739 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700740 }
felipeal080e5652019-11-02 17:04:04 -0700741 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700742
felipeal080e5652019-11-02 17:04:04 -0700743 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
744 for (String serviceName : serviceNames) {
745 writer.println("** Dumping " + serviceName + "\n");
746 CarServiceBase service = getCarServiceBySubstring(serviceName);
747 if (service == null) {
748 writer.println("No such service!");
749 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700750 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700751 }
752 writer.println();
753 }
754 }
755
756 @Nullable
757 private CarServiceBase getCarServiceBySubstring(String className) {
758 return Arrays.asList(mAllServices).stream()
759 .filter(s -> s.getClass().getSimpleName().equals(className))
760 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700761 }
762
Mark Tabry7e2a7822019-10-17 10:18:32 -0700763 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800764 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700765 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800766 } catch (Exception e) {
767 writer.println("Failed dumping: " + service.getClass().getName());
768 e.printStackTrace(writer);
769 }
keunyoungcc449f72015-08-12 10:46:27 -0700770 }
Yao Chene33f07e2016-07-26 12:02:51 -0700771
772 void execShellCmd(String[] args, PrintWriter writer) {
773 new CarShellCommand().exec(args, writer);
774 }
775
Pavel Maltsevabd47232017-10-10 16:54:57 -0700776 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700777 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700778 Slog.i(TAG, name);
779 mBootTiming.traceBegin(name);
780 }
781
Pavel Maltsevabd47232017-10-10 16:54:57 -0700782 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700783 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700784 mBootTiming.traceEnd();
785 }
786
felipeal72bf6422019-11-02 17:04:04 -0700787 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700788 private static final String COMMAND_HELP = "-h";
789 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800790 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700791 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800792 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800793 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800794 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800795 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700796 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700797 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800798 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700799 private static final String COMMAND_RESUME = "resume";
800 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700801 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
802 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700803 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700804 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
805 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800806 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
807 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800808 private static final String COMMAND_INJECT_KEY = "inject-key";
Felipe Lemea5e69382020-02-11 17:53:20 -0800809 private static final String COMMAND_GET_INITIAL_USER_INFO = "get-initial-user-info";
Felipe Leme5bf135b2020-02-07 12:22:14 -0800810
Yao Chene33f07e2016-07-26 12:02:51 -0700811 private static final String PARAM_DAY_MODE = "day";
812 private static final String PARAM_NIGHT_MODE = "night";
813 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800814 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800815 private static final String PARAM_ON_MODE = "on";
816 private static final String PARAM_OFF_MODE = "off";
817 private static final String PARAM_QUERY_MODE = "query";
Jim Kaye9a834ea2020-02-07 15:50:53 -0800818 private static final String PARAM_REBOOT = "reboot";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800819
felipeal72bf6422019-11-02 17:04:04 -0700820 private static final int RESULT_OK = 0;
821 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
822
823
824 @Override
825 public int onCommand(String cmd) {
826 if (cmd == null) {
827 onHelp();
828 return RESULT_ERROR;
829 }
830 ArrayList<String> argsList = new ArrayList<>();
831 argsList.add(cmd);
832 String arg = null;
833 do {
834 arg = getNextArg();
835 if (arg != null) {
836 argsList.add(arg);
837 }
838 } while (arg != null);
839 String[] args = new String[argsList.size()];
840 argsList.toArray(args);
841 return exec(args, getOutPrintWriter());
842 }
843
844 @Override
845 public void onHelp() {
846 dumpHelp(getOutPrintWriter());
847 }
Yao Chene33f07e2016-07-26 12:02:51 -0700848
849 private void dumpHelp(PrintWriter pw) {
850 pw.println("Car service commands:");
851 pw.println("\t-h");
852 pw.println("\t Print this help text.");
853 pw.println("\tday-night-mode [day|night|sensor]");
854 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800855 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800856 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700857 pw.println("\tinject-error-event property zone errorCode");
858 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700859 pw.println("\tenable-uxr true|false");
860 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800861 pw.println("\tgarage-mode [on|off|query|reboot]");
862 pw.println("\t Force into or out of garage mode, or check status.");
863 pw.println("\t With 'reboot', enter garage mode, then reboot when it completes.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800864 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800865 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800866 pw.println("\tget-carpropertyconfig [propertyId]");
867 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700868 pw.println("\tget-property-value [propertyId] [areaId]");
869 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
870 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700871 pw.println("\tsuspend");
872 pw.println("\t Suspend the system to Deep Sleep.");
873 pw.println("\tresume");
874 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700875 pw.println("\tenable-trusted-device true|false");
876 pw.println("\t Enable/Disable Trusted device feature.");
877 pw.println("\tremove-trusted-devices");
878 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700879 pw.println("\tprojection-tethering [true|false]");
880 pw.println("\t Whether tethering should be used when creating access point for"
881 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700882 pw.println("\t--metrics");
883 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700884 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800885 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700886 pw.println("\tstart-fixed-activity displayId packageName activityName");
887 pw.println("\t Start an Activity the specified display as fixed mode");
888 pw.println("\tstop-fixed-mode displayId");
889 pw.println("\t Stop fixed Activity mode for the given display. "
890 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800891 pw.println("\tenable-feature featureName");
892 pw.println("\t Enable the requested feature. Change will happen after reboot.");
893 pw.println("\t This requires root/su.");
894 pw.println("\tdisable-feature featureName");
895 pw.println("\t Disable the requested feature. Change will happen after reboot");
896 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800897 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
898 pw.println("\t inject key down / up event to car service");
899 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
900 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
901 pw.println("\t it will be 0");
902 pw.println("\t key_code: int key code defined in android KeyEvent");
Felipe Lemea5e69382020-02-11 17:53:20 -0800903 pw.printf("\t%s <REQ_TYPE> [--timeout TIMEOUT_MS]\n", COMMAND_GET_INITIAL_USER_INFO);
904 pw.println("\t Calls the Vehicle HAL to get the initial boot info, passing the given");
905 pw.println("\t REQ_TYPE (which could be either FIRST_BOOT, FIRST_BOOT_AFTER_OTA, ");
906 pw.println("\t COLD_BOOT, RESUME, or any numeric value that would be passed 'as-is')");
907 pw.println("\t and an optional TIMEOUT_MS to wait for the HAL response (if not set,");
908 pw.println("\t it will use a default value).");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700909 }
910
felipeal72bf6422019-11-02 17:04:04 -0700911 private int dumpInvalidArguments(PrintWriter pw) {
912 pw.println("Incorrect number of arguments.");
913 dumpHelp(pw);
914 return RESULT_ERROR;
915 }
916
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700917 private String runSetZoneIdForUid(String zoneString, String uidString) {
918 int uid = Integer.parseInt(uidString);
919 int zoneId = Integer.parseInt(zoneString);
920 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
921 return "zoneid " + zoneId + " not found";
922 }
923 mCarAudioService.setZoneIdForUid(zoneId, uid);
924 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700925 }
926
felipeal72bf6422019-11-02 17:04:04 -0700927 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700928 String arg = args[0];
929 switch (arg) {
930 case COMMAND_HELP:
931 dumpHelp(writer);
932 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800933 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800934 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700935 forceDayNightMode(value, writer);
936 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800937 }
938 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800939 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800940 forceGarageMode(value, writer);
941 break;
942 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800943 case COMMAND_INJECT_VHAL_EVENT:
944 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
945 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800946 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700947 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800948 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800949 // Zoned
950 zone = args[2];
951 data = args[3];
952 } else {
953 // Global
954 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800955 }
Kai4b098cf2019-06-05 15:03:28 -0700956 injectVhalEvent(args[1], zone, data, false, writer);
957 break;
958 case COMMAND_INJECT_ERROR_EVENT:
959 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700960 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700961 }
962 String errorAreaId = args[2];
963 String errorCode = args[3];
964 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800965 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800966 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800967 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700968 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800969 }
970 boolean enableBlocking = Boolean.valueOf(args[1]);
971 if (mCarPackageManagerService != null) {
972 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
973 }
974 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800975 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800976 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700977 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800978 }
979 String pkgName = args[1].toLowerCase();
980 if (mCarPackageManagerService != null) {
981 String[] doActivities =
982 mCarPackageManagerService.getDistractionOptimizedActivities(
983 pkgName);
984 if (doActivities != null) {
985 writer.println("DO Activities for " + pkgName);
986 for (String a : doActivities) {
987 writer.println(a);
988 }
989 } else {
990 writer.println("No DO Activities for " + pkgName);
991 }
992 }
993 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800994 case COMMAND_GET_CARPROPERTYCONFIG:
995 String propertyId = args.length < 2 ? "" : args[1];
996 mHal.dumpPropertyConfigs(writer, propertyId);
997 break;
Kai29fba112019-04-22 18:37:38 -0700998 case COMMAND_GET_PROPERTY_VALUE:
999 String propId = args.length < 2 ? "" : args[1];
1000 String areaId = args.length < 3 ? "" : args[2];
1001 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
1002 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -08001003 case COMMAND_PROJECTION_UI_MODE:
1004 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001005 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -08001006 }
1007 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -07001008 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001009 case COMMAND_PROJECTION_AP_TETHERING:
1010 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001011 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001012 }
1013 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
1014 break;
Jim Kayed76c2742019-04-02 11:33:27 -07001015 case COMMAND_RESUME:
1016 mCarPowerManagementService.forceSimulatedResume();
1017 writer.println("Resume: Simulating resuming from Deep Sleep");
1018 break;
1019 case COMMAND_SUSPEND:
Jim Kaye9a834ea2020-02-07 15:50:53 -08001020 mCarPowerManagementService.forceSuspendAndMaybeReboot(false);
Jim Kayed76c2742019-04-02 11:33:27 -07001021 writer.println("Resume: Simulating powering down to Deep Sleep");
1022 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001023 case COMMAND_ENABLE_TRUSTED_DEVICE:
1024 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001025 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001026 }
1027 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
1028 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
1029 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
1030 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
1031 break;
1032 case COMMAND_REMOVE_TRUSTED_DEVICES:
1033 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -07001034 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001035 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001036 case COMMAND_SET_UID_TO_ZONE:
1037 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -07001038 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001039 }
1040 String results = runSetZoneIdForUid(args[1], args[2]);
1041 if (results != null) {
1042 writer.println(results);
1043 dumpHelp(writer);
1044 }
1045 break;
Keun young Park09ca8492019-10-15 10:34:11 -07001046 case COMMAND_START_FIXED_ACTIVITY_MODE:
1047 handleStartFixedActivity(args, writer);
1048 break;
1049 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
1050 handleStopFixedMode(args, writer);
1051 break;
Keun young Park9a91efb2019-11-15 18:10:47 -08001052 case COMMAND_ENABLE_FEATURE:
1053 if (args.length != 2) {
1054 return dumpInvalidArguments(writer);
1055 }
1056 handleEnableDisableFeature(args, writer, /* enable= */ true);
1057 break;
1058 case COMMAND_DISABLE_FEATURE:
1059 if (args.length != 2) {
1060 return dumpInvalidArguments(writer);
1061 }
1062 handleEnableDisableFeature(args, writer, /* enable= */ false);
1063 break;
Keun young Park313dfaf2019-12-19 11:32:38 -08001064 case COMMAND_INJECT_KEY:
1065 if (args.length < 2) {
1066 return dumpInvalidArguments(writer);
1067 }
1068 handleInjectKey(args, writer);
1069 break;
Felipe Lemea5e69382020-02-11 17:53:20 -08001070 case COMMAND_GET_INITIAL_USER_INFO:
1071 handleGetInitialUserInfo(args, writer);
1072 break;
Yao Chene33f07e2016-07-26 12:02:51 -07001073 default:
Jim Kayef10b7d82019-02-26 12:53:22 -08001074 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -07001075 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -07001076 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -07001077 }
felipeal72bf6422019-11-02 17:04:04 -07001078 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -07001079 }
1080
Keun young Park09ca8492019-10-15 10:34:11 -07001081 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1082 if (args.length != 4) {
1083 writer.println("Incorrect number of arguments");
1084 dumpHelp(writer);
1085 return;
1086 }
1087 int displayId;
1088 try {
1089 displayId = Integer.parseInt(args[1]);
1090 } catch (NumberFormatException e) {
1091 writer.println("Wrong display id:" + args[1]);
1092 return;
1093 }
1094 String packageName = args[2];
1095 String activityName = args[3];
1096 Intent intent = new Intent();
1097 intent.setComponent(new ComponentName(packageName, activityName));
1098 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1099 ActivityOptions options = ActivityOptions.makeBasic();
1100 options.setLaunchDisplayId(displayId);
1101 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1102 displayId, ActivityManager.getCurrentUser())) {
1103 writer.println("Failed to start");
1104 return;
1105 }
1106 writer.println("Succeeded");
1107 }
1108
1109 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1110 if (args.length != 2) {
1111 writer.println("Incorrect number of arguments");
1112 dumpHelp(writer);
1113 return;
1114 }
1115 int displayId;
1116 try {
1117 displayId = Integer.parseInt(args[1]);
1118 } catch (NumberFormatException e) {
1119 writer.println("Wrong display id:" + args[1]);
1120 return;
1121 }
1122 mFixedActivityService.stopFixedActivityMode(displayId);
1123 }
1124
Keun young Park9a91efb2019-11-15 18:10:47 -08001125 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1126 if (Binder.getCallingUid() != Process.ROOT_UID) {
1127 writer.println("Only allowed to root/su");
1128 return;
1129 }
1130 String featureName = args[1];
1131 long id = Binder.clearCallingIdentity();
1132 // no permission check here
1133 int r;
1134 if (enable) {
1135 r = mFeatureController.enableFeature(featureName);
1136 } else {
1137 r = mFeatureController.disableFeature(featureName);
1138 }
1139 switch (r) {
1140 case Car.FEATURE_REQUEST_SUCCESS:
1141 if (enable) {
1142 writer.println("Enabled feature:" + featureName);
1143 } else {
1144 writer.println("Disabled feature:" + featureName);
1145 }
1146 break;
1147 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1148 if (enable) {
1149 writer.println("Already enabled:" + featureName);
1150 } else {
1151 writer.println("Already disabled:" + featureName);
1152 }
1153 break;
1154 case Car.FEATURE_REQUEST_MANDATORY:
1155 writer.println("Cannot change mandatory feature:" + featureName);
1156 break;
1157 case Car.FEATURE_REQUEST_NOT_EXISTING:
1158 writer.println("Non-existing feature:" + featureName);
1159 break;
1160 default:
1161 writer.println("Unknown error:" + r);
1162 break;
1163 }
1164 Binder.restoreCallingIdentity(id);
1165 }
1166
Keun young Park313dfaf2019-12-19 11:32:38 -08001167 private void handleInjectKey(String[] args, PrintWriter writer) {
1168 int i = 1; // 0 is command itself
1169 int display = InputHalService.DISPLAY_MAIN;
1170 int delayMs = 0;
1171 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1172 try {
1173 while (i < args.length) {
1174 switch (args[i]) {
1175 case "-d":
1176 i++;
1177 display = Integer.parseInt(args[i]);
1178 break;
1179 case "-t":
1180 i++;
1181 delayMs = Integer.parseInt(args[i]);
1182 break;
1183 default:
1184 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1185 throw new IllegalArgumentException("key_code already set:"
1186 + keyCode);
1187 }
1188 keyCode = Integer.parseInt(args[i]);
1189 }
1190 i++;
1191 }
1192 } catch (Exception e) {
1193 writer.println("Invalid args:" + e);
1194 dumpHelp(writer);
1195 return;
1196 }
1197 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1198 writer.println("Missing key code or invalid keycode");
1199 dumpHelp(writer);
1200 return;
1201 }
1202 if (display != InputHalService.DISPLAY_MAIN
1203 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1204 writer.println("Invalid display:" + display);
1205 dumpHelp(writer);
1206 return;
1207 }
1208 if (delayMs < 0) {
1209 writer.println("Invalid delay:" + delayMs);
1210 dumpHelp(writer);
1211 return;
1212 }
1213 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1214 mCarInputService.onKeyEvent(keyDown, display);
1215 SystemClock.sleep(delayMs);
1216 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1217 mCarInputService.onKeyEvent(keyUp, display);
1218 writer.println("Succeeded");
1219 }
1220
Felipe Lemea5e69382020-02-11 17:53:20 -08001221 private void handleGetInitialUserInfo(String[] args, PrintWriter writer) {
1222 if (args.length < 2) {
1223 writer.println("Insufficient number of args");
1224 return;
1225 }
1226
1227 // Gets the request type
1228 String typeArg = args[1];
1229 int requestType = UserHalHelper.parseInitialUserInfoRequestType(typeArg);
1230
1231 int timeout = 1_000;
1232 for (int i = 2; i < args.length; i++) {
1233 String arg = args[i];
1234 switch (arg) {
1235 case "--timeout":
1236 timeout = Integer.parseInt(args[++i]);
1237 break;
1238 default:
1239 writer.println("Invalid option at index " + i + ": " + arg);
1240 return;
1241
1242 }
1243 }
1244
1245 Log.d(TAG, "handleGetInitialUserInfo(): type=" + requestType + " (" + typeArg
1246 + "), timeout=" + timeout);
1247
1248 UserHalService userHal = mHal.getUserHal();
1249 // TODO(b/146207078): use UserHalHelper to populate it with current users
1250 UsersInfo usersInfo = new UsersInfo();
1251 CountDownLatch latch = new CountDownLatch(1);
1252
1253 userHal.getInitialUserInfo(requestType, timeout, usersInfo, (status, resp) -> {
1254 try {
1255 Log.d(TAG, "GetUserInfoResponse: status=" + status + ", resp=" + resp);
1256 writer.printf("Status: %s\n", UserHalHelper.halCallbackStatusToString(status));
1257 if (status != HalCallback.STATUS_OK) {
1258 return;
1259 }
1260 writer.printf("Request id: %d\n", resp.requestId);
1261 writer.printf("Action: ");
1262 switch (resp.action) {
1263 case InitialUserInfoResponseAction.DEFAULT:
1264 writer.println("default");
1265 break;
1266 case InitialUserInfoResponseAction.SWITCH:
1267 writer.printf("switch to user %d\n", resp.userToSwitchOrCreate.userId);
1268 break;
1269 case InitialUserInfoResponseAction.CREATE:
1270 writer.printf("create user: name=%s, flags=%d\n", resp.userNameToCreate,
1271 resp.userToSwitchOrCreate.flags);
1272 break;
1273 default:
1274 writer.printf("unknown (%d)\n", resp.action);
1275 break;
1276 }
1277 } finally {
1278 latch.countDown();
1279 }
1280 });
1281
1282 try {
1283 if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
1284 writer.printf("HAL didn't respond in %dms\n", timeout);
1285 }
1286 } catch (InterruptedException e) {
1287 Thread.currentThread().interrupt();
1288 writer.println("Interrupted waiting for HAL");
1289 }
1290 return;
1291 }
1292
Yao Chene33f07e2016-07-26 12:02:51 -07001293 private void forceDayNightMode(String arg, PrintWriter writer) {
1294 int mode;
1295 switch (arg) {
1296 case PARAM_DAY_MODE:
1297 mode = CarNightService.FORCED_DAY_MODE;
1298 break;
1299 case PARAM_NIGHT_MODE:
1300 mode = CarNightService.FORCED_NIGHT_MODE;
1301 break;
1302 case PARAM_SENSOR_MODE:
1303 mode = CarNightService.FORCED_SENSOR_MODE;
1304 break;
1305 default:
1306 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1307 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1308 return;
1309 }
1310 int current = mCarNightService.forceDayNightMode(mode);
1311 String currentMode = null;
1312 switch (current) {
1313 case UiModeManager.MODE_NIGHT_AUTO:
1314 currentMode = PARAM_SENSOR_MODE;
1315 break;
1316 case UiModeManager.MODE_NIGHT_YES:
1317 currentMode = PARAM_NIGHT_MODE;
1318 break;
1319 case UiModeManager.MODE_NIGHT_NO:
1320 currentMode = PARAM_DAY_MODE;
1321 break;
1322 }
1323 writer.println("DayNightMode changed to: " + currentMode);
1324 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001325
Bryan Eylera32a7c12018-02-27 15:40:00 -08001326 private void forceGarageMode(String arg, PrintWriter writer) {
1327 switch (arg) {
1328 case PARAM_ON_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001329 mSystemInterface.setDisplayState(false);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001330 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001331 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001332 break;
1333 case PARAM_OFF_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001334 mSystemInterface.setDisplayState(true);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001335 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001336 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001337 break;
1338 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001339 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001340 break;
Jim Kaye9a834ea2020-02-07 15:50:53 -08001341 case PARAM_REBOOT:
1342 mCarPowerManagementService.forceSuspendAndMaybeReboot(true);
1343 writer.println("Entering Garage Mode. Will reboot when it completes.");
1344 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -08001345 default:
1346 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
Jim Kaye9a834ea2020-02-07 15:50:53 -08001347 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE + "|" + PARAM_REBOOT);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001348 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001349 }
1350
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001351 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001352 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001353 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001354 * @param property the Vehicle property Id as defined in the HAL
1355 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001356 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001357 * @param value Data value of the event
1358 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001359 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001360 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001361 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001362 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1363 if (!isPropertyAreaTypeGlobal(property)) {
1364 writer.println("Property area type inconsistent with given zone");
1365 return;
1366 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001367 }
1368 try {
Kai4b098cf2019-06-05 15:03:28 -07001369 if (isErrorEvent) {
1370 mHal.injectOnPropertySetError(property, zone, value);
1371 } else {
1372 mHal.injectVhalEvent(property, zone, value);
1373 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001374 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001375 writer.println("Invalid property Id zone Id or value" + e);
1376 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001377 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001378 }
1379
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001380 // Check if the given property is global
1381 private boolean isPropertyAreaTypeGlobal(String property) {
1382 if (property == null) {
1383 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001384 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001385 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001386 }
Yao Chene33f07e2016-07-26 12:02:51 -07001387 }
kevinjm55822c42018-08-15 11:26:00 -07001388}