blob: 0ff4bc0c283967aeae1a55660187825666a1b266 [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
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800369 public void onFirstUserUnlocked(int userId, long timestampMs, long duration) {
370 mUserMetrics.logFirstUnlockedUser(userId, timestampMs, duration);
371 }
372
373 @Override
Keun young Park9a91efb2019-11-15 18:10:47 -0800374 public boolean isFeatureEnabled(String featureName) {
375 return mFeatureController.isFeatureEnabled(featureName);
376 }
377
378 @Override
379 public int enableFeature(String featureName) {
380 // permission check inside the controller
381 return mFeatureController.enableFeature(featureName);
382 }
383
384 @Override
385 public int disableFeature(String featureName) {
386 // permission check inside the controller
387 return mFeatureController.disableFeature(featureName);
388 }
389
390 @Override
391 public List<String> getAllEnabledFeatures() {
392 // permission check inside the controller
393 return mFeatureController.getAllEnabledFeatures();
394 }
395
396 @Override
397 public List<String> getAllPendingDisabledFeatures() {
398 // permission check inside the controller
399 return mFeatureController.getAllPendingDisabledFeatures();
400 }
401
402 @Override
403 public List<String> getAllPendingEnabledFeatures() {
404 // permission check inside the controller
405 return mFeatureController.getAllPendingEnabledFeatures();
406 }
407
408 @Override
409 public String getCarManagerClassForFeature(String featureName) {
410 if (mCarExperimentalFeatureServiceController == null) {
411 return null;
412 }
413 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
414 }
415
Jim Kayee5133162019-04-22 12:50:27 -0700416 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800417 int uid = Binder.getCallingUid();
418 if (uid != Process.SYSTEM_UID) {
419 throw new SecurityException("Only allowed from system");
420 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800421 }
422
Keun young Parkaabecd92019-05-03 17:31:27 -0700423 /**
424 * Assert if binder call is coming from system process like system server or if it is called
425 * from its own process even if it is not system. The latter can happen in test environment.
426 * Note that car service runs as system user but test like car service test will not.
427 */
428 static void assertCallingFromSystemProcessOrSelf() {
429 int uid = Binder.getCallingUid();
430 int pid = Binder.getCallingPid();
431 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
432 throw new SecurityException("Only allowed from system or self");
433 }
434 }
435
Keun-young Parkd462a912019-02-11 08:53:42 -0800436 @Override
keunyoungca515072015-07-10 12:21:47 -0700437 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800438 if (!mFeatureController.isFeatureEnabled(serviceName)) {
439 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
440 return null;
441 }
keunyoungca515072015-07-10 12:21:47 -0700442 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800443 case Car.AUDIO_SERVICE:
444 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700445 case Car.APP_FOCUS_SERVICE:
446 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800447 case Car.PACKAGE_SERVICE:
448 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800449 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700450 assertAnyDiagnosticPermission(mContext);
451 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800452 case Car.POWER_SERVICE:
453 assertPowerPermission(mContext);
454 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700455 case Car.CABIN_SERVICE:
456 case Car.HVAC_SERVICE:
457 case Car.INFO_SERVICE:
458 case Car.PROPERTY_SERVICE:
459 case Car.SENSOR_SERVICE:
460 case Car.VENDOR_EXTENSION_SERVICE:
461 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800462 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700463 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700464 IInstrumentClusterNavigation navService =
465 mInstrumentClusterService.getNavigationService();
466 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700467 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
468 assertClusterManagerPermission(mContext);
469 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800470 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800471 return mCarProjectionService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800472 case Car.VEHICLE_MAP_SERVICE:
473 assertAnyVmsPermission(mContext);
474 return mVmsBrokerService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800475 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700476 assertVmsSubscriberPermission(mContext);
Mark Tabry62e749c2020-01-10 19:52:59 -0800477 return mVmsBrokerService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800478 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700479 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700480 synchronized (this) {
481 if (mCarTestService == null) {
482 mCarTestService = new CarTestService(mContext, this);
483 }
484 return mCarTestService;
485 }
486 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700487 case Car.BLUETOOTH_SERVICE:
488 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700489 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700490 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700491 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800492 case Car.CAR_DRIVING_STATE_SERVICE:
493 assertDrivingStatePermission(mContext);
494 return mCarDrivingStateService;
495 case Car.CAR_UX_RESTRICTION_SERVICE:
496 return mCarUXRestrictionsService;
Michael Kellerac2ed202020-01-30 14:11:16 -0800497 case Car.OCCUPANT_AWARENESS_SERVICE:
498 return mOccupantAwarenessService;
Anthony Chend4203d82018-05-16 16:21:52 -0700499 case Car.CAR_CONFIGURATION_SERVICE:
500 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800501 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
502 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700503 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800504 case Car.CAR_MEDIA_SERVICE:
505 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700506 case Car.CAR_OCCUPANT_ZONE_SERVICE:
507 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700508 case Car.CAR_BUGREPORT_SERVICE:
509 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700510 case Car.CAR_USER_SERVICE:
511 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700512 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800513 IBinder service = null;
514 if (mCarExperimentalFeatureServiceController != null) {
515 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
516 }
517 if (service == null) {
518 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
519 + serviceName);
520 }
521 return service;
keunyoungca515072015-07-10 12:21:47 -0700522 }
523 }
524
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800525 @Override
526 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700527 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800528 }
529
Keun-young Parka28d7b22016-02-29 16:54:29 -0800530 public CarServiceBase getCarInternalService(String serviceName) {
531 switch (serviceName) {
532 case INTERNAL_INPUT_SERVICE:
533 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700534 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
535 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800536 default:
537 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
538 serviceName);
539 return null;
540 }
541 }
542
keunyoung1ab8e182015-09-24 09:25:22 -0700543 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700544 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700545 }
546
Keun-young Parke31a8b22016-03-16 17:34:08 -0700547 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700548 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800549 }
550
Pavel Maltsev905968c2017-07-16 19:48:57 -0700551 public static void assertClusterManagerPermission(Context context) {
552 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
553 }
554
Steve Paik388d7772018-02-12 10:54:51 -0800555 public static void assertPowerPermission(Context context) {
556 assertPermission(context, Car.PERMISSION_CAR_POWER);
557 }
558
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800559 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700560 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
561 }
562
Pavel Maltsev079873b2019-02-25 12:15:09 -0800563 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
564 public static void assertProjectionStatusPermission(Context context) {
565 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
566 }
567
Enrico Granata3c7a6662017-02-23 18:07:59 -0800568 public static void assertAnyDiagnosticPermission(Context context) {
569 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700570 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800571 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
572 }
573
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800574 public static void assertDrivingStatePermission(Context context) {
575 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
576 }
577
Mark Tabry6fa123d2020-01-10 19:52:59 -0800578 /**
579 * Verify the calling context has either {@link Car#PERMISSION_VMS_SUBSCRIBER} or
580 * {@link Car#PERMISSION_VMS_PUBLISHER}
581 */
582 public static void assertAnyVmsPermission(Context context) {
583 assertAnyPermission(context,
584 Car.PERMISSION_VMS_SUBSCRIBER,
585 Car.PERMISSION_VMS_PUBLISHER);
586 }
587
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800588 public static void assertVmsPublisherPermission(Context context) {
589 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
590 }
591
Antonio Cortese4619c72017-02-02 07:53:27 -0800592 public static void assertVmsSubscriberPermission(Context context) {
593 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
594 }
595
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800596 /**
597 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800598 */
599 public static void assertTrustAgentEnrollmentPermission(Context context) {
600 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
601 }
602
Steve Paik461ecc62016-06-08 15:28:32 -0700603 public static void assertPermission(Context context, String permission) {
604 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
605 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800606 }
607 }
608
Steve Paik9ec53d72018-04-27 13:28:31 -0700609 /**
610 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700611 *
612 * @return boolean TRUE if caller has the permission.
613 */
614 public static boolean hasPermission(Context context, String permission) {
615 return context.checkCallingOrSelfPermission(permission)
616 == PackageManager.PERMISSION_GRANTED;
617 }
618
Enrico Granata3c7a6662017-02-23 18:07:59 -0800619 public static void assertAnyPermission(Context context, String... permissions) {
620 for (String permission : permissions) {
621 if (context.checkCallingOrSelfPermission(permission) ==
622 PackageManager.PERMISSION_GRANTED) {
623 return;
624 }
625 }
626 throw new SecurityException("requires any of " + Arrays.toString(permissions));
627 }
628
Enrico Granatae8056ca2018-04-03 13:19:52 -0700629 @Override
630 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
631 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700632 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700633 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700634 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
635 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700636 return;
keunyounga3b28d82015-08-25 13:05:15 -0700637 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700638
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700639 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
640 writer.println("*Dump car service*");
Mark Tabry7e2a7822019-10-17 10:18:32 -0700641 dumpAllServices(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800642 dumpAllHals(writer);
Felipe Lemee93218b2020-02-13 14:10:52 -0800643 mUserMetrics.dump(writer);
felipeal080e5652019-11-02 17:04:04 -0700644 } else if ("--list".equals(args[0])) {
645 dumpListOfServices(writer);
646 return;
647 } else if ("--services".equals(args[0])) {
648 if (args.length < 2) {
649 writer.print("Must pass services to dump when using --services");
650 return;
651 }
652 int length = args.length - 1;
653 String[] services = new String[length];
654 System.arraycopy(args, 1, services, 0, length);
655 dumpIndividualServices(writer, services);
656 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700657 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700658 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800659 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700660 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700661 } else if ("--vms-hal".equals(args[0])) {
662 mHal.getVmsHal().dumpMetrics(fd);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800663 } else if ("--hal".equals(args[0])) {
664 if (args.length == 1) {
665 dumpAllHals(writer);
666 return;
667 }
668 int length = args.length - 1;
669 String[] halNames = new String[length];
670 System.arraycopy(args, 1, halNames, 0, length);
671 mHal.dumpSpecificHals(writer, halNames);
672
673 } else if ("--list-hals".equals(args[0])) {
674 mHal.dumpListHals(writer);
675 return;
Felipe Lemee93218b2020-02-13 14:10:52 -0800676 } else if ("--user-metrics".equals(args[0])) {
677 mUserMetrics.dump(writer);
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800678 } else if ("--first-user-metrics".equals(args[0])) {
679 mUserMetrics.dumpFirstUserUnlockDuration(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800680 } else if ("--help".equals(args[0])) {
681 showDumpHelp(writer);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700682 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
683 execShellCmd(args, writer);
684 } else {
685 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800686 }
687 }
688
Felipe Lemef07c3f92020-02-12 14:26:33 -0800689 private void dumpAllHals(PrintWriter writer) {
690 writer.println("*Dump Vehicle HAL*");
691 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
692 try {
693 // TODO dump all feature flags by creating a dumpable interface
694 mHal.dump(writer);
695 } catch (Exception e) {
696 writer.println("Failed dumping: " + mHal.getClass().getName());
697 e.printStackTrace(writer);
698 }
699 }
700
701 private void showDumpHelp(PrintWriter writer) {
702 writer.println("Car service dump usage:");
703 writer.println("[NO ARG]");
704 writer.println("\t dumps everything (all services and HALs)");
705 writer.println("--help");
706 writer.println("\t shows this help");
707 writer.println("--list");
708 writer.println("\t lists the name of all services");
709 writer.println("--list");
710 writer.println("\t lists the name of all HAls");
711 writer.println("--services <SVC1> [SVC2] [SVCN]");
712 writer.println("\t dumps just the specific services, where SVC is just the service class");
713 writer.println("\t name (like CarUserService)");
714 writer.println("--vms-hal");
715 writer.println("\t dumps the VMS HAL metrics");
716 writer.println("--hal [HAL1] [HAL2] [HALN]");
717 writer.println("\t dumps just the specified HALs (or all of them if none specified),");
718 writer.println("\t where HAL is just the class name (like UserHalService)");
Felipe Lemee93218b2020-02-13 14:10:52 -0800719 writer.println("--user-metrics");
720 writer.println("\t dumps user switching and stopping metrics ");
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800721 writer.println("--first-user-metrics");
722 writer.println("\t dumps how long it took to unlock first user since Android started\n");
723 writer.println("\t (or -1 if not unlocked)");
Felipe Lemef07c3f92020-02-12 14:26:33 -0800724 writer.println("-h");
725 writer.println("\t shows commands usage (NOTE: commands are not available on USER builds");
726 writer.println("[ANYTHING ELSE]");
727 writer.println("\t runs the given command (use --h to see the available commands)");
728 }
729
felipeal72bf6422019-11-02 17:04:04 -0700730 @Override
731 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
732 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
733 throws RemoteException {
734 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
735 }
736
felipeal080e5652019-11-02 17:04:04 -0700737 private void dumpListOfServices(PrintWriter writer) {
738 for (CarServiceBase service : mAllServices) {
739 writer.println(service.getClass().getName());
740 }
741 }
742
Mark Tabry7e2a7822019-10-17 10:18:32 -0700743 private void dumpAllServices(PrintWriter writer) {
Felipe Lemef07c3f92020-02-12 14:26:33 -0800744 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700745 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700746 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700747 }
748 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700749 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700750 }
felipeal080e5652019-11-02 17:04:04 -0700751 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700752
felipeal080e5652019-11-02 17:04:04 -0700753 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
754 for (String serviceName : serviceNames) {
755 writer.println("** Dumping " + serviceName + "\n");
756 CarServiceBase service = getCarServiceBySubstring(serviceName);
757 if (service == null) {
758 writer.println("No such service!");
759 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700760 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700761 }
762 writer.println();
763 }
764 }
765
766 @Nullable
767 private CarServiceBase getCarServiceBySubstring(String className) {
768 return Arrays.asList(mAllServices).stream()
769 .filter(s -> s.getClass().getSimpleName().equals(className))
770 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700771 }
772
Mark Tabry7e2a7822019-10-17 10:18:32 -0700773 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800774 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700775 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800776 } catch (Exception e) {
777 writer.println("Failed dumping: " + service.getClass().getName());
778 e.printStackTrace(writer);
779 }
keunyoungcc449f72015-08-12 10:46:27 -0700780 }
Yao Chene33f07e2016-07-26 12:02:51 -0700781
782 void execShellCmd(String[] args, PrintWriter writer) {
783 new CarShellCommand().exec(args, writer);
784 }
785
Pavel Maltsevabd47232017-10-10 16:54:57 -0700786 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700787 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700788 Slog.i(TAG, name);
789 mBootTiming.traceBegin(name);
790 }
791
Pavel Maltsevabd47232017-10-10 16:54:57 -0700792 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700793 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700794 mBootTiming.traceEnd();
795 }
796
felipeal72bf6422019-11-02 17:04:04 -0700797 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700798 private static final String COMMAND_HELP = "-h";
799 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800800 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700801 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800802 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800803 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800804 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800805 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700806 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700807 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800808 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700809 private static final String COMMAND_RESUME = "resume";
810 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700811 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
812 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700813 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700814 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
815 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800816 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
817 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800818 private static final String COMMAND_INJECT_KEY = "inject-key";
Felipe Lemea5e69382020-02-11 17:53:20 -0800819 private static final String COMMAND_GET_INITIAL_USER_INFO = "get-initial-user-info";
Felipe Leme5bf135b2020-02-07 12:22:14 -0800820
Yao Chene33f07e2016-07-26 12:02:51 -0700821 private static final String PARAM_DAY_MODE = "day";
822 private static final String PARAM_NIGHT_MODE = "night";
823 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800824 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800825 private static final String PARAM_ON_MODE = "on";
826 private static final String PARAM_OFF_MODE = "off";
827 private static final String PARAM_QUERY_MODE = "query";
Jim Kaye9a834ea2020-02-07 15:50:53 -0800828 private static final String PARAM_REBOOT = "reboot";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800829
felipeal72bf6422019-11-02 17:04:04 -0700830 private static final int RESULT_OK = 0;
831 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
832
833
834 @Override
835 public int onCommand(String cmd) {
836 if (cmd == null) {
837 onHelp();
838 return RESULT_ERROR;
839 }
840 ArrayList<String> argsList = new ArrayList<>();
841 argsList.add(cmd);
842 String arg = null;
843 do {
844 arg = getNextArg();
845 if (arg != null) {
846 argsList.add(arg);
847 }
848 } while (arg != null);
849 String[] args = new String[argsList.size()];
850 argsList.toArray(args);
851 return exec(args, getOutPrintWriter());
852 }
853
854 @Override
855 public void onHelp() {
856 dumpHelp(getOutPrintWriter());
857 }
Yao Chene33f07e2016-07-26 12:02:51 -0700858
859 private void dumpHelp(PrintWriter pw) {
860 pw.println("Car service commands:");
861 pw.println("\t-h");
862 pw.println("\t Print this help text.");
863 pw.println("\tday-night-mode [day|night|sensor]");
864 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800865 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800866 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700867 pw.println("\tinject-error-event property zone errorCode");
868 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700869 pw.println("\tenable-uxr true|false");
870 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800871 pw.println("\tgarage-mode [on|off|query|reboot]");
872 pw.println("\t Force into or out of garage mode, or check status.");
873 pw.println("\t With 'reboot', enter garage mode, then reboot when it completes.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800874 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800875 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800876 pw.println("\tget-carpropertyconfig [propertyId]");
877 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700878 pw.println("\tget-property-value [propertyId] [areaId]");
879 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
880 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700881 pw.println("\tsuspend");
882 pw.println("\t Suspend the system to Deep Sleep.");
883 pw.println("\tresume");
884 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700885 pw.println("\tenable-trusted-device true|false");
886 pw.println("\t Enable/Disable Trusted device feature.");
887 pw.println("\tremove-trusted-devices");
888 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700889 pw.println("\tprojection-tethering [true|false]");
890 pw.println("\t Whether tethering should be used when creating access point for"
891 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700892 pw.println("\t--metrics");
893 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700894 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800895 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700896 pw.println("\tstart-fixed-activity displayId packageName activityName");
897 pw.println("\t Start an Activity the specified display as fixed mode");
898 pw.println("\tstop-fixed-mode displayId");
899 pw.println("\t Stop fixed Activity mode for the given display. "
900 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800901 pw.println("\tenable-feature featureName");
902 pw.println("\t Enable the requested feature. Change will happen after reboot.");
903 pw.println("\t This requires root/su.");
904 pw.println("\tdisable-feature featureName");
905 pw.println("\t Disable the requested feature. Change will happen after reboot");
906 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800907 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
908 pw.println("\t inject key down / up event to car service");
909 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
910 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
911 pw.println("\t it will be 0");
912 pw.println("\t key_code: int key code defined in android KeyEvent");
Felipe Lemea5e69382020-02-11 17:53:20 -0800913 pw.printf("\t%s <REQ_TYPE> [--timeout TIMEOUT_MS]\n", COMMAND_GET_INITIAL_USER_INFO);
914 pw.println("\t Calls the Vehicle HAL to get the initial boot info, passing the given");
915 pw.println("\t REQ_TYPE (which could be either FIRST_BOOT, FIRST_BOOT_AFTER_OTA, ");
916 pw.println("\t COLD_BOOT, RESUME, or any numeric value that would be passed 'as-is')");
917 pw.println("\t and an optional TIMEOUT_MS to wait for the HAL response (if not set,");
918 pw.println("\t it will use a default value).");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700919 }
920
felipeal72bf6422019-11-02 17:04:04 -0700921 private int dumpInvalidArguments(PrintWriter pw) {
922 pw.println("Incorrect number of arguments.");
923 dumpHelp(pw);
924 return RESULT_ERROR;
925 }
926
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700927 private String runSetZoneIdForUid(String zoneString, String uidString) {
928 int uid = Integer.parseInt(uidString);
929 int zoneId = Integer.parseInt(zoneString);
930 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
931 return "zoneid " + zoneId + " not found";
932 }
933 mCarAudioService.setZoneIdForUid(zoneId, uid);
934 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700935 }
936
felipeal72bf6422019-11-02 17:04:04 -0700937 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700938 String arg = args[0];
939 switch (arg) {
940 case COMMAND_HELP:
941 dumpHelp(writer);
942 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800943 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800944 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700945 forceDayNightMode(value, writer);
946 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800947 }
948 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800949 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800950 forceGarageMode(value, writer);
951 break;
952 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800953 case COMMAND_INJECT_VHAL_EVENT:
954 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
955 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800956 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700957 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800958 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800959 // Zoned
960 zone = args[2];
961 data = args[3];
962 } else {
963 // Global
964 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800965 }
Kai4b098cf2019-06-05 15:03:28 -0700966 injectVhalEvent(args[1], zone, data, false, writer);
967 break;
968 case COMMAND_INJECT_ERROR_EVENT:
969 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700970 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700971 }
972 String errorAreaId = args[2];
973 String errorCode = args[3];
974 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800975 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800976 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800977 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700978 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800979 }
980 boolean enableBlocking = Boolean.valueOf(args[1]);
981 if (mCarPackageManagerService != null) {
982 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
983 }
984 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800985 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800986 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700987 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800988 }
989 String pkgName = args[1].toLowerCase();
990 if (mCarPackageManagerService != null) {
991 String[] doActivities =
992 mCarPackageManagerService.getDistractionOptimizedActivities(
993 pkgName);
994 if (doActivities != null) {
995 writer.println("DO Activities for " + pkgName);
996 for (String a : doActivities) {
997 writer.println(a);
998 }
999 } else {
1000 writer.println("No DO Activities for " + pkgName);
1001 }
1002 }
1003 break;
Kaidd4ef5a2019-03-04 14:07:26 -08001004 case COMMAND_GET_CARPROPERTYCONFIG:
1005 String propertyId = args.length < 2 ? "" : args[1];
1006 mHal.dumpPropertyConfigs(writer, propertyId);
1007 break;
Kai29fba112019-04-22 18:37:38 -07001008 case COMMAND_GET_PROPERTY_VALUE:
1009 String propId = args.length < 2 ? "" : args[1];
1010 String areaId = args.length < 3 ? "" : args[2];
1011 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
1012 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -08001013 case COMMAND_PROJECTION_UI_MODE:
1014 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001015 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -08001016 }
1017 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -07001018 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001019 case COMMAND_PROJECTION_AP_TETHERING:
1020 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001021 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001022 }
1023 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
1024 break;
Jim Kayed76c2742019-04-02 11:33:27 -07001025 case COMMAND_RESUME:
1026 mCarPowerManagementService.forceSimulatedResume();
1027 writer.println("Resume: Simulating resuming from Deep Sleep");
1028 break;
1029 case COMMAND_SUSPEND:
Jim Kaye9a834ea2020-02-07 15:50:53 -08001030 mCarPowerManagementService.forceSuspendAndMaybeReboot(false);
Jim Kayed76c2742019-04-02 11:33:27 -07001031 writer.println("Resume: Simulating powering down to Deep Sleep");
1032 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001033 case COMMAND_ENABLE_TRUSTED_DEVICE:
1034 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001035 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001036 }
1037 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
1038 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
1039 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
1040 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
1041 break;
1042 case COMMAND_REMOVE_TRUSTED_DEVICES:
1043 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -07001044 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001045 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001046 case COMMAND_SET_UID_TO_ZONE:
1047 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -07001048 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001049 }
1050 String results = runSetZoneIdForUid(args[1], args[2]);
1051 if (results != null) {
1052 writer.println(results);
1053 dumpHelp(writer);
1054 }
1055 break;
Keun young Park09ca8492019-10-15 10:34:11 -07001056 case COMMAND_START_FIXED_ACTIVITY_MODE:
1057 handleStartFixedActivity(args, writer);
1058 break;
1059 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
1060 handleStopFixedMode(args, writer);
1061 break;
Keun young Park9a91efb2019-11-15 18:10:47 -08001062 case COMMAND_ENABLE_FEATURE:
1063 if (args.length != 2) {
1064 return dumpInvalidArguments(writer);
1065 }
1066 handleEnableDisableFeature(args, writer, /* enable= */ true);
1067 break;
1068 case COMMAND_DISABLE_FEATURE:
1069 if (args.length != 2) {
1070 return dumpInvalidArguments(writer);
1071 }
1072 handleEnableDisableFeature(args, writer, /* enable= */ false);
1073 break;
Keun young Park313dfaf2019-12-19 11:32:38 -08001074 case COMMAND_INJECT_KEY:
1075 if (args.length < 2) {
1076 return dumpInvalidArguments(writer);
1077 }
1078 handleInjectKey(args, writer);
1079 break;
Felipe Lemea5e69382020-02-11 17:53:20 -08001080 case COMMAND_GET_INITIAL_USER_INFO:
1081 handleGetInitialUserInfo(args, writer);
1082 break;
Yao Chene33f07e2016-07-26 12:02:51 -07001083 default:
Jim Kayef10b7d82019-02-26 12:53:22 -08001084 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -07001085 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -07001086 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -07001087 }
felipeal72bf6422019-11-02 17:04:04 -07001088 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -07001089 }
1090
Keun young Park09ca8492019-10-15 10:34:11 -07001091 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1092 if (args.length != 4) {
1093 writer.println("Incorrect number of arguments");
1094 dumpHelp(writer);
1095 return;
1096 }
1097 int displayId;
1098 try {
1099 displayId = Integer.parseInt(args[1]);
1100 } catch (NumberFormatException e) {
1101 writer.println("Wrong display id:" + args[1]);
1102 return;
1103 }
1104 String packageName = args[2];
1105 String activityName = args[3];
1106 Intent intent = new Intent();
1107 intent.setComponent(new ComponentName(packageName, activityName));
1108 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1109 ActivityOptions options = ActivityOptions.makeBasic();
1110 options.setLaunchDisplayId(displayId);
1111 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1112 displayId, ActivityManager.getCurrentUser())) {
1113 writer.println("Failed to start");
1114 return;
1115 }
1116 writer.println("Succeeded");
1117 }
1118
1119 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1120 if (args.length != 2) {
1121 writer.println("Incorrect number of arguments");
1122 dumpHelp(writer);
1123 return;
1124 }
1125 int displayId;
1126 try {
1127 displayId = Integer.parseInt(args[1]);
1128 } catch (NumberFormatException e) {
1129 writer.println("Wrong display id:" + args[1]);
1130 return;
1131 }
1132 mFixedActivityService.stopFixedActivityMode(displayId);
1133 }
1134
Keun young Park9a91efb2019-11-15 18:10:47 -08001135 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1136 if (Binder.getCallingUid() != Process.ROOT_UID) {
1137 writer.println("Only allowed to root/su");
1138 return;
1139 }
1140 String featureName = args[1];
1141 long id = Binder.clearCallingIdentity();
1142 // no permission check here
1143 int r;
1144 if (enable) {
1145 r = mFeatureController.enableFeature(featureName);
1146 } else {
1147 r = mFeatureController.disableFeature(featureName);
1148 }
1149 switch (r) {
1150 case Car.FEATURE_REQUEST_SUCCESS:
1151 if (enable) {
1152 writer.println("Enabled feature:" + featureName);
1153 } else {
1154 writer.println("Disabled feature:" + featureName);
1155 }
1156 break;
1157 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1158 if (enable) {
1159 writer.println("Already enabled:" + featureName);
1160 } else {
1161 writer.println("Already disabled:" + featureName);
1162 }
1163 break;
1164 case Car.FEATURE_REQUEST_MANDATORY:
1165 writer.println("Cannot change mandatory feature:" + featureName);
1166 break;
1167 case Car.FEATURE_REQUEST_NOT_EXISTING:
1168 writer.println("Non-existing feature:" + featureName);
1169 break;
1170 default:
1171 writer.println("Unknown error:" + r);
1172 break;
1173 }
1174 Binder.restoreCallingIdentity(id);
1175 }
1176
Keun young Park313dfaf2019-12-19 11:32:38 -08001177 private void handleInjectKey(String[] args, PrintWriter writer) {
1178 int i = 1; // 0 is command itself
1179 int display = InputHalService.DISPLAY_MAIN;
1180 int delayMs = 0;
1181 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1182 try {
1183 while (i < args.length) {
1184 switch (args[i]) {
1185 case "-d":
1186 i++;
1187 display = Integer.parseInt(args[i]);
1188 break;
1189 case "-t":
1190 i++;
1191 delayMs = Integer.parseInt(args[i]);
1192 break;
1193 default:
1194 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1195 throw new IllegalArgumentException("key_code already set:"
1196 + keyCode);
1197 }
1198 keyCode = Integer.parseInt(args[i]);
1199 }
1200 i++;
1201 }
1202 } catch (Exception e) {
1203 writer.println("Invalid args:" + e);
1204 dumpHelp(writer);
1205 return;
1206 }
1207 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1208 writer.println("Missing key code or invalid keycode");
1209 dumpHelp(writer);
1210 return;
1211 }
1212 if (display != InputHalService.DISPLAY_MAIN
1213 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1214 writer.println("Invalid display:" + display);
1215 dumpHelp(writer);
1216 return;
1217 }
1218 if (delayMs < 0) {
1219 writer.println("Invalid delay:" + delayMs);
1220 dumpHelp(writer);
1221 return;
1222 }
1223 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1224 mCarInputService.onKeyEvent(keyDown, display);
1225 SystemClock.sleep(delayMs);
1226 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1227 mCarInputService.onKeyEvent(keyUp, display);
1228 writer.println("Succeeded");
1229 }
1230
Felipe Lemea5e69382020-02-11 17:53:20 -08001231 private void handleGetInitialUserInfo(String[] args, PrintWriter writer) {
1232 if (args.length < 2) {
1233 writer.println("Insufficient number of args");
1234 return;
1235 }
1236
1237 // Gets the request type
1238 String typeArg = args[1];
1239 int requestType = UserHalHelper.parseInitialUserInfoRequestType(typeArg);
1240
1241 int timeout = 1_000;
1242 for (int i = 2; i < args.length; i++) {
1243 String arg = args[i];
1244 switch (arg) {
1245 case "--timeout":
1246 timeout = Integer.parseInt(args[++i]);
1247 break;
1248 default:
1249 writer.println("Invalid option at index " + i + ": " + arg);
1250 return;
1251
1252 }
1253 }
1254
1255 Log.d(TAG, "handleGetInitialUserInfo(): type=" + requestType + " (" + typeArg
1256 + "), timeout=" + timeout);
1257
1258 UserHalService userHal = mHal.getUserHal();
1259 // TODO(b/146207078): use UserHalHelper to populate it with current users
1260 UsersInfo usersInfo = new UsersInfo();
1261 CountDownLatch latch = new CountDownLatch(1);
1262
1263 userHal.getInitialUserInfo(requestType, timeout, usersInfo, (status, resp) -> {
1264 try {
1265 Log.d(TAG, "GetUserInfoResponse: status=" + status + ", resp=" + resp);
1266 writer.printf("Status: %s\n", UserHalHelper.halCallbackStatusToString(status));
1267 if (status != HalCallback.STATUS_OK) {
1268 return;
1269 }
1270 writer.printf("Request id: %d\n", resp.requestId);
1271 writer.printf("Action: ");
1272 switch (resp.action) {
1273 case InitialUserInfoResponseAction.DEFAULT:
1274 writer.println("default");
1275 break;
1276 case InitialUserInfoResponseAction.SWITCH:
1277 writer.printf("switch to user %d\n", resp.userToSwitchOrCreate.userId);
1278 break;
1279 case InitialUserInfoResponseAction.CREATE:
1280 writer.printf("create user: name=%s, flags=%d\n", resp.userNameToCreate,
1281 resp.userToSwitchOrCreate.flags);
1282 break;
1283 default:
1284 writer.printf("unknown (%d)\n", resp.action);
1285 break;
1286 }
1287 } finally {
1288 latch.countDown();
1289 }
1290 });
1291
1292 try {
1293 if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
1294 writer.printf("HAL didn't respond in %dms\n", timeout);
1295 }
1296 } catch (InterruptedException e) {
1297 Thread.currentThread().interrupt();
1298 writer.println("Interrupted waiting for HAL");
1299 }
1300 return;
1301 }
1302
Yao Chene33f07e2016-07-26 12:02:51 -07001303 private void forceDayNightMode(String arg, PrintWriter writer) {
1304 int mode;
1305 switch (arg) {
1306 case PARAM_DAY_MODE:
1307 mode = CarNightService.FORCED_DAY_MODE;
1308 break;
1309 case PARAM_NIGHT_MODE:
1310 mode = CarNightService.FORCED_NIGHT_MODE;
1311 break;
1312 case PARAM_SENSOR_MODE:
1313 mode = CarNightService.FORCED_SENSOR_MODE;
1314 break;
1315 default:
1316 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1317 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1318 return;
1319 }
1320 int current = mCarNightService.forceDayNightMode(mode);
1321 String currentMode = null;
1322 switch (current) {
1323 case UiModeManager.MODE_NIGHT_AUTO:
1324 currentMode = PARAM_SENSOR_MODE;
1325 break;
1326 case UiModeManager.MODE_NIGHT_YES:
1327 currentMode = PARAM_NIGHT_MODE;
1328 break;
1329 case UiModeManager.MODE_NIGHT_NO:
1330 currentMode = PARAM_DAY_MODE;
1331 break;
1332 }
1333 writer.println("DayNightMode changed to: " + currentMode);
1334 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001335
Bryan Eylera32a7c12018-02-27 15:40:00 -08001336 private void forceGarageMode(String arg, PrintWriter writer) {
1337 switch (arg) {
1338 case PARAM_ON_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001339 mSystemInterface.setDisplayState(false);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001340 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001341 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001342 break;
1343 case PARAM_OFF_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001344 mSystemInterface.setDisplayState(true);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001345 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001346 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001347 break;
1348 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001349 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001350 break;
Jim Kaye9a834ea2020-02-07 15:50:53 -08001351 case PARAM_REBOOT:
1352 mCarPowerManagementService.forceSuspendAndMaybeReboot(true);
1353 writer.println("Entering Garage Mode. Will reboot when it completes.");
1354 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -08001355 default:
1356 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
Jim Kaye9a834ea2020-02-07 15:50:53 -08001357 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE + "|" + PARAM_REBOOT);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001358 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001359 }
1360
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001361 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001362 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001363 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001364 * @param property the Vehicle property Id as defined in the HAL
1365 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001366 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001367 * @param value Data value of the event
1368 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001369 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001370 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001371 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001372 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1373 if (!isPropertyAreaTypeGlobal(property)) {
1374 writer.println("Property area type inconsistent with given zone");
1375 return;
1376 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001377 }
1378 try {
Kai4b098cf2019-06-05 15:03:28 -07001379 if (isErrorEvent) {
1380 mHal.injectOnPropertySetError(property, zone, value);
1381 } else {
1382 mHal.injectVhalEvent(property, zone, value);
1383 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001384 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001385 writer.println("Invalid property Id zone Id or value" + e);
1386 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001387 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001388 }
1389
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001390 // Check if the given property is global
1391 private boolean isPropertyAreaTypeGlobal(String property) {
1392 if (property == null) {
1393 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001394 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001395 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001396 }
Yao Chene33f07e2016-07-26 12:02:51 -07001397 }
kevinjm55822c42018-08-15 11:26:00 -07001398}