blob: 1923d9a40b9ecde52902b321d77d5fd483894f4c [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;
Felipe Lemee2600fc2020-02-26 11:06:04 -080075import com.android.internal.os.IResultReceiver;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070076import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070077
Enrico Granatae8056ca2018-04-03 13:19:52 -070078import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070079import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070080import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080081import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070082import java.util.List;
Felipe Lemea5e69382020-02-11 17:53:20 -080083import java.util.concurrent.CountDownLatch;
84import java.util.concurrent.TimeUnit;
keunyounga3b28d82015-08-25 13:05:15 -070085
keunyoungca515072015-07-10 12:21:47 -070086public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070087
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080088 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070089 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
90 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080091
keunyoungca515072015-07-10 12:21:47 -070092 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080093 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070094
Keun young Park9a91efb2019-11-15 18:10:47 -080095 private final CarFeatureController mFeatureController;
96
Anthony Chen12aec302018-04-25 16:41:48 -070097 private final SystemInterface mSystemInterface;
98
Keun-young Park4727da32016-05-31 10:00:51 -070099 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -0700100 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800101 private final CarPackageManagerService mCarPackageManagerService;
102 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800103 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800104 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800105 private final OccupantAwarenessService mOccupantAwarenessService;
keunyoungd32f4e62015-09-21 11:33:06 -0700106 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800107 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700108 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800109 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700110 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -0700111 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -0800112 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800113 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -0800114 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700115 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800116 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700117 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700118 private final CarDiagnosticService mCarDiagnosticService;
119 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700120 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700121 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800122 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700123 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800124 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700125 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700126 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800127 private final VmsNewBrokerService mVmsBrokerService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700128 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700129 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800130 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
keunyounga74b9ca2015-10-21 13:33:58 -0700131
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700132 private final CarServiceBase[] mAllServices;
133
Steve Paikc302c7c2017-08-04 14:01:58 -0700134 private static final String TAG = "ICarImpl";
135 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700136
137 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700138
keunyoung1ab8e182015-09-24 09:25:22 -0700139 /** Test only service. Populate it only when necessary. */
140 @GuardedBy("this")
141 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700142
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700143 @GuardedBy("this")
144 private ICarServiceHelper mICarServiceHelper;
145
Enrico Granatae8056ca2018-04-03 13:19:52 -0700146 private final String mVehicleInterfaceName;
147
Felipe Lemee93218b2020-02-13 14:10:52 -0800148 private final UserMetrics mUserMetrics = new UserMetrics();
149
Pavel Maltsevec83b632017-01-05 15:10:55 -0800150 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700151 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
Felipe Leme6ef89652019-12-19 16:47:14 -0800152 this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
153 /* carUserService= */ null);
154 }
155
156 @VisibleForTesting
157 ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
158 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
159 @Nullable CarUserService carUserService) {
keunyoungca515072015-07-10 12:21:47 -0700160 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800161 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700162 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800163 Resources res = mContext.getResources();
164 String[] defaultEnabledFeatures = res.getStringArray(
165 R.array.config_allowed_optional_car_features);
166 // Do this before any other service components to allow feature check. It should work
167 // even without init.
168 // TODO (b/144504820) Add vhal plumbing
169 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
170 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
171 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700172 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800173 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Felipe Leme6ef89652019-12-19 16:47:14 -0800174 if (carUserService != null) {
175 mCarUserService = carUserService;
176 } else {
177 UserManager userManager =
178 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
179 int maxRunningUsers = res.getInteger(
180 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Felipe Leme58412202020-01-09 13:45:33 -0800181 mCarUserService = new CarUserService(serviceContext, mHal.getUserHal(),
182 mUserManagerHelper, userManager, ActivityManager.getService(), maxRunningUsers);
Felipe Leme6ef89652019-12-19 16:47:14 -0800183 }
Keun young Parkbb877e22019-08-02 10:38:01 -0700184 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700185 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800186 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700187 systemInterface, mUserManagerHelper);
Keun young Park9a91efb2019-11-15 18:10:47 -0800188 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
189 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
190 } else {
191 mCarUserNoticeService = null;
192 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700193 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
194 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800195 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800196 mCarDrivingStateService, mCarPropertyService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800197 if (mFeatureController.isFeatureEnabled(Car.OCCUPANT_AWARENESS_SERVICE)) {
198 mOccupantAwarenessService = new OccupantAwarenessService(serviceContext);
199 } else {
200 mOccupantAwarenessService = null;
201 }
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800202 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
203 mCarUXRestrictionsService,
Mayank Garg31e73042020-01-23 00:10:38 -0800204 mSystemActivityMonitoringService,
205 mCarUserService);
206 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext, mCarUserService);
Sal Savage703c46f2019-04-15 08:39:25 -0700207 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700208 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800209 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800210 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700211 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700212 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800213 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700214 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700215 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700216 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700217 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700218 mSystemStateControllerService = new SystemStateControllerService(
219 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700220 mCarStatsService = new CarStatsService(serviceContext);
Ruchir Rastogia683d742020-01-22 00:17:51 -0800221 mCarStatsService.init();
Mark Tabry62e749c2020-01-10 19:52:59 -0800222 if (mFeatureController.isFeatureEnabled(Car.VEHICLE_MAP_SERVICE)
223 || mFeatureController.isFeatureEnabled(Car.VMS_SUBSCRIBER_SERVICE)) {
Mark Tabry6fa123d2020-01-10 19:52:59 -0800224 mVmsBrokerService = new VmsNewBrokerService(mContext, mCarStatsService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800225 } else {
226 mVmsBrokerService = null;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800227 }
Keun young Parkc6d80af2020-01-17 18:14:28 -0800228 if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
229 mCarDiagnosticService = new CarDiagnosticService(serviceContext,
230 mHal.getDiagnosticHal());
231 } else {
232 mCarDiagnosticService = null;
233 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800234 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
235 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
236 systemInterface);
237 } else {
238 mCarStorageMonitoringService = null;
239 }
Anthony Chen12aec302018-04-25 16:41:48 -0700240 mCarConfigurationService =
241 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700242 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700243 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Mayank Garg31e73042020-01-23 00:10:38 -0800244 mCarMediaService = new CarMediaService(serviceContext, mCarUserService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700245 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800246 if (!Build.IS_USER) {
247 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
248 serviceContext);
249 } else {
250 mCarExperimentalFeatureServiceController = null;
251 }
keunyounga74b9ca2015-10-21 13:33:58 -0700252
Keun young Parka5fa4782019-04-16 18:56:27 -0700253 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700254 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800255 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700256 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Mayank Gargfc24d572020-02-14 11:32:34 -0800257 CarLocalServices.addService(CarUserNoticeService.class, mCarUserNoticeService);
Gregory Clarka440e812019-02-14 16:05:51 -0800258 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700259 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700260 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700261 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Mark Tabry62e749c2020-01-10 19:52:59 -0800262 CarLocalServices.addService(VmsNewBrokerService.class, mVmsBrokerService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800263
keunyounga3b28d82015-08-25 13:05:15 -0700264 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700265 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800266 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800267 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700268 allServices.add(mSystemActivityMonitoringService);
269 allServices.add(mCarPowerManagementService);
270 allServices.add(mCarPropertyService);
271 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700272 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700273 allServices.add(mCarUXRestrictionsService);
Michael Kellerc0f0bdb2019-12-11 14:58:19 -0800274 addServiceIfNonNull(allServices, mOccupantAwarenessService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700275 allServices.add(mCarPackageManagerService);
276 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700277 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800278 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700279 allServices.add(mAppFocusService);
280 allServices.add(mCarAudioService);
281 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700282 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700283 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700284 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700285 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800286 allServices.add(mCarBluetoothService);
287 allServices.add(mCarProjectionService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800288 addServiceIfNonNull(allServices, mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800289 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700290 allServices.add(mCarConfigurationService);
Mark Tabry6fa123d2020-01-10 19:52:59 -0800291 addServiceIfNonNull(allServices, mVmsBrokerService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700292 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800293 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700294 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700295 allServices.add(mCarBugreportManagerService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800296 // Always put mCarExperimentalFeatureServiceController in last.
297 addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700298 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700299 }
300
Keun young Park9a91efb2019-11-15 18:10:47 -0800301 private void addServiceIfNonNull(List<CarServiceBase> services, CarServiceBase service) {
302 if (service != null) {
303 services.add(service);
304 }
305 }
306
Pavel Maltsevabd47232017-10-10 16:54:57 -0700307 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700308 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700309 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700310 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700311 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700312 traceEnd();
313 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800314 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700315 service.init();
316 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700317 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700318 }
319
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700320 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700321 // release done in opposite order from init
322 for (int i = mAllServices.length - 1; i >= 0; i--) {
323 mAllServices[i].release();
324 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700325 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700326 }
327
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700328 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800329 mHal.vehicleHalReconnected(vehicle);
330 for (CarServiceBase service : mAllServices) {
331 service.vehicleHalReconnected();
332 }
333 }
334
keunyoungca515072015-07-10 12:21:47 -0700335 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700336 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700337 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700338 synchronized (this) {
339 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800340 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700341 }
342 }
343
344 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700345 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700346 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700347 mCarUserService.setUserLockStatus(userId, unlocked == 1);
348 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700349 }
350
351 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700352 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700353 assertCallingFromSystemProcess();
354
Eric Jeong1545f3b2019-09-16 13:56:52 -0700355 Log.i(TAG, "Foreground user switched to " + userId);
356 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700357 }
358
Felipe Lemee93218b2020-02-13 14:10:52 -0800359 // TODO(b/146207078): this method is currently used just for metrics logging purposes, but we
360 // should fold the other too (onSwitchUser() and setUserLockStatus()) onto it.
361 @Override
362 public void onUserLifecycleEvent(int eventType, long timestampMs, int fromUserId,
363 int toUserId) {
364 assertCallingFromSystemProcess();
365 Log.i(TAG, "onUserLifecycleEvent(" + CarUserManager.lifecycleEventTypeToString(eventType)
Felipe Lemee2600fc2020-02-26 11:06:04 -0800366 + ", " + toUserId + ")");
Felipe Lemee93218b2020-02-13 14:10:52 -0800367 mUserMetrics.onEvent(eventType, timestampMs, fromUserId, toUserId);
368 }
369
Keun young Park9a91efb2019-11-15 18:10:47 -0800370 @Override
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800371 public void onFirstUserUnlocked(int userId, long timestampMs, long duration) {
372 mUserMetrics.logFirstUnlockedUser(userId, timestampMs, duration);
373 }
374
375 @Override
Felipe Lemee2600fc2020-02-26 11:06:04 -0800376 public void getInitialUserInfo(int requestType, int timeoutMs, IBinder binder) {
377 IResultReceiver receiver = IResultReceiver.Stub.asInterface(binder);
378 mCarUserService.getInitialUserInfo(requestType, timeoutMs, receiver);
379 }
380
381 @Override
Keun young Park9a91efb2019-11-15 18:10:47 -0800382 public boolean isFeatureEnabled(String featureName) {
383 return mFeatureController.isFeatureEnabled(featureName);
384 }
385
386 @Override
387 public int enableFeature(String featureName) {
388 // permission check inside the controller
389 return mFeatureController.enableFeature(featureName);
390 }
391
392 @Override
393 public int disableFeature(String featureName) {
394 // permission check inside the controller
395 return mFeatureController.disableFeature(featureName);
396 }
397
398 @Override
399 public List<String> getAllEnabledFeatures() {
400 // permission check inside the controller
401 return mFeatureController.getAllEnabledFeatures();
402 }
403
404 @Override
405 public List<String> getAllPendingDisabledFeatures() {
406 // permission check inside the controller
407 return mFeatureController.getAllPendingDisabledFeatures();
408 }
409
410 @Override
411 public List<String> getAllPendingEnabledFeatures() {
412 // permission check inside the controller
413 return mFeatureController.getAllPendingEnabledFeatures();
414 }
415
416 @Override
417 public String getCarManagerClassForFeature(String featureName) {
418 if (mCarExperimentalFeatureServiceController == null) {
419 return null;
420 }
421 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
422 }
423
Jim Kayee5133162019-04-22 12:50:27 -0700424 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800425 int uid = Binder.getCallingUid();
426 if (uid != Process.SYSTEM_UID) {
427 throw new SecurityException("Only allowed from system");
428 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800429 }
430
Keun young Parkaabecd92019-05-03 17:31:27 -0700431 /**
432 * Assert if binder call is coming from system process like system server or if it is called
433 * from its own process even if it is not system. The latter can happen in test environment.
434 * Note that car service runs as system user but test like car service test will not.
435 */
436 static void assertCallingFromSystemProcessOrSelf() {
437 int uid = Binder.getCallingUid();
438 int pid = Binder.getCallingPid();
439 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
440 throw new SecurityException("Only allowed from system or self");
441 }
442 }
443
Keun-young Parkd462a912019-02-11 08:53:42 -0800444 @Override
keunyoungca515072015-07-10 12:21:47 -0700445 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800446 if (!mFeatureController.isFeatureEnabled(serviceName)) {
447 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
448 return null;
449 }
keunyoungca515072015-07-10 12:21:47 -0700450 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800451 case Car.AUDIO_SERVICE:
452 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700453 case Car.APP_FOCUS_SERVICE:
454 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800455 case Car.PACKAGE_SERVICE:
456 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800457 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700458 assertAnyDiagnosticPermission(mContext);
459 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800460 case Car.POWER_SERVICE:
461 assertPowerPermission(mContext);
462 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700463 case Car.CABIN_SERVICE:
464 case Car.HVAC_SERVICE:
465 case Car.INFO_SERVICE:
466 case Car.PROPERTY_SERVICE:
467 case Car.SENSOR_SERVICE:
468 case Car.VENDOR_EXTENSION_SERVICE:
469 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800470 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700471 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700472 IInstrumentClusterNavigation navService =
473 mInstrumentClusterService.getNavigationService();
474 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700475 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
476 assertClusterManagerPermission(mContext);
477 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800478 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800479 return mCarProjectionService;
Mark Tabry6fa123d2020-01-10 19:52:59 -0800480 case Car.VEHICLE_MAP_SERVICE:
481 assertAnyVmsPermission(mContext);
482 return mVmsBrokerService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800483 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700484 assertVmsSubscriberPermission(mContext);
Mark Tabry62e749c2020-01-10 19:52:59 -0800485 return mVmsBrokerService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800486 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700487 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700488 synchronized (this) {
489 if (mCarTestService == null) {
490 mCarTestService = new CarTestService(mContext, this);
491 }
492 return mCarTestService;
493 }
494 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700495 case Car.BLUETOOTH_SERVICE:
496 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700497 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700498 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700499 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800500 case Car.CAR_DRIVING_STATE_SERVICE:
501 assertDrivingStatePermission(mContext);
502 return mCarDrivingStateService;
503 case Car.CAR_UX_RESTRICTION_SERVICE:
504 return mCarUXRestrictionsService;
Michael Kellerac2ed202020-01-30 14:11:16 -0800505 case Car.OCCUPANT_AWARENESS_SERVICE:
506 return mOccupantAwarenessService;
Anthony Chend4203d82018-05-16 16:21:52 -0700507 case Car.CAR_CONFIGURATION_SERVICE:
508 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800509 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
510 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700511 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800512 case Car.CAR_MEDIA_SERVICE:
513 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700514 case Car.CAR_OCCUPANT_ZONE_SERVICE:
515 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700516 case Car.CAR_BUGREPORT_SERVICE:
517 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700518 case Car.CAR_USER_SERVICE:
519 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700520 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800521 IBinder service = null;
522 if (mCarExperimentalFeatureServiceController != null) {
523 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
524 }
525 if (service == null) {
526 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
527 + serviceName);
528 }
529 return service;
keunyoungca515072015-07-10 12:21:47 -0700530 }
531 }
532
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800533 @Override
534 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700535 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800536 }
537
Keun-young Parka28d7b22016-02-29 16:54:29 -0800538 public CarServiceBase getCarInternalService(String serviceName) {
539 switch (serviceName) {
540 case INTERNAL_INPUT_SERVICE:
541 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700542 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
543 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800544 default:
545 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
546 serviceName);
547 return null;
548 }
549 }
550
keunyoung1ab8e182015-09-24 09:25:22 -0700551 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700552 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700553 }
554
Keun-young Parke31a8b22016-03-16 17:34:08 -0700555 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700556 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800557 }
558
Pavel Maltsev905968c2017-07-16 19:48:57 -0700559 public static void assertClusterManagerPermission(Context context) {
560 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
561 }
562
Steve Paik388d7772018-02-12 10:54:51 -0800563 public static void assertPowerPermission(Context context) {
564 assertPermission(context, Car.PERMISSION_CAR_POWER);
565 }
566
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800567 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700568 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
569 }
570
Pavel Maltsev079873b2019-02-25 12:15:09 -0800571 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
572 public static void assertProjectionStatusPermission(Context context) {
573 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
574 }
575
Enrico Granata3c7a6662017-02-23 18:07:59 -0800576 public static void assertAnyDiagnosticPermission(Context context) {
577 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700578 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800579 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
580 }
581
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800582 public static void assertDrivingStatePermission(Context context) {
583 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
584 }
585
Mark Tabry6fa123d2020-01-10 19:52:59 -0800586 /**
587 * Verify the calling context has either {@link Car#PERMISSION_VMS_SUBSCRIBER} or
588 * {@link Car#PERMISSION_VMS_PUBLISHER}
589 */
590 public static void assertAnyVmsPermission(Context context) {
591 assertAnyPermission(context,
592 Car.PERMISSION_VMS_SUBSCRIBER,
593 Car.PERMISSION_VMS_PUBLISHER);
594 }
595
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800596 public static void assertVmsPublisherPermission(Context context) {
597 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
598 }
599
Antonio Cortese4619c72017-02-02 07:53:27 -0800600 public static void assertVmsSubscriberPermission(Context context) {
601 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
602 }
603
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800604 /**
605 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800606 */
607 public static void assertTrustAgentEnrollmentPermission(Context context) {
608 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
609 }
610
Steve Paik461ecc62016-06-08 15:28:32 -0700611 public static void assertPermission(Context context, String permission) {
612 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
613 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800614 }
615 }
616
Steve Paik9ec53d72018-04-27 13:28:31 -0700617 /**
618 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700619 *
620 * @return boolean TRUE if caller has the permission.
621 */
622 public static boolean hasPermission(Context context, String permission) {
623 return context.checkCallingOrSelfPermission(permission)
624 == PackageManager.PERMISSION_GRANTED;
625 }
626
Enrico Granata3c7a6662017-02-23 18:07:59 -0800627 public static void assertAnyPermission(Context context, String... permissions) {
628 for (String permission : permissions) {
629 if (context.checkCallingOrSelfPermission(permission) ==
630 PackageManager.PERMISSION_GRANTED) {
631 return;
632 }
633 }
634 throw new SecurityException("requires any of " + Arrays.toString(permissions));
635 }
636
Enrico Granatae8056ca2018-04-03 13:19:52 -0700637 @Override
638 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
639 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700640 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700641 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700642 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
643 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700644 return;
keunyounga3b28d82015-08-25 13:05:15 -0700645 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700646
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700647 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
648 writer.println("*Dump car service*");
Mark Tabry7e2a7822019-10-17 10:18:32 -0700649 dumpAllServices(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800650 dumpAllHals(writer);
Felipe Lemee93218b2020-02-13 14:10:52 -0800651 mUserMetrics.dump(writer);
felipeal080e5652019-11-02 17:04:04 -0700652 } else if ("--list".equals(args[0])) {
653 dumpListOfServices(writer);
654 return;
655 } else if ("--services".equals(args[0])) {
656 if (args.length < 2) {
657 writer.print("Must pass services to dump when using --services");
658 return;
659 }
660 int length = args.length - 1;
661 String[] services = new String[length];
662 System.arraycopy(args, 1, services, 0, length);
663 dumpIndividualServices(writer, services);
664 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700665 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700666 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800667 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700668 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700669 } else if ("--vms-hal".equals(args[0])) {
670 mHal.getVmsHal().dumpMetrics(fd);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800671 } else if ("--hal".equals(args[0])) {
672 if (args.length == 1) {
673 dumpAllHals(writer);
674 return;
675 }
676 int length = args.length - 1;
677 String[] halNames = new String[length];
678 System.arraycopy(args, 1, halNames, 0, length);
679 mHal.dumpSpecificHals(writer, halNames);
680
681 } else if ("--list-hals".equals(args[0])) {
682 mHal.dumpListHals(writer);
683 return;
Felipe Lemee93218b2020-02-13 14:10:52 -0800684 } else if ("--user-metrics".equals(args[0])) {
685 mUserMetrics.dump(writer);
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800686 } else if ("--first-user-metrics".equals(args[0])) {
687 mUserMetrics.dumpFirstUserUnlockDuration(writer);
Felipe Lemef07c3f92020-02-12 14:26:33 -0800688 } else if ("--help".equals(args[0])) {
689 showDumpHelp(writer);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700690 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
691 execShellCmd(args, writer);
692 } else {
693 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800694 }
695 }
696
Felipe Lemef07c3f92020-02-12 14:26:33 -0800697 private void dumpAllHals(PrintWriter writer) {
698 writer.println("*Dump Vehicle HAL*");
699 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
700 try {
701 // TODO dump all feature flags by creating a dumpable interface
702 mHal.dump(writer);
703 } catch (Exception e) {
704 writer.println("Failed dumping: " + mHal.getClass().getName());
705 e.printStackTrace(writer);
706 }
707 }
708
709 private void showDumpHelp(PrintWriter writer) {
710 writer.println("Car service dump usage:");
711 writer.println("[NO ARG]");
712 writer.println("\t dumps everything (all services and HALs)");
713 writer.println("--help");
714 writer.println("\t shows this help");
715 writer.println("--list");
716 writer.println("\t lists the name of all services");
717 writer.println("--list");
718 writer.println("\t lists the name of all HAls");
719 writer.println("--services <SVC1> [SVC2] [SVCN]");
720 writer.println("\t dumps just the specific services, where SVC is just the service class");
721 writer.println("\t name (like CarUserService)");
722 writer.println("--vms-hal");
723 writer.println("\t dumps the VMS HAL metrics");
724 writer.println("--hal [HAL1] [HAL2] [HALN]");
725 writer.println("\t dumps just the specified HALs (or all of them if none specified),");
726 writer.println("\t where HAL is just the class name (like UserHalService)");
Felipe Lemee93218b2020-02-13 14:10:52 -0800727 writer.println("--user-metrics");
728 writer.println("\t dumps user switching and stopping metrics ");
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800729 writer.println("--first-user-metrics");
730 writer.println("\t dumps how long it took to unlock first user since Android started\n");
731 writer.println("\t (or -1 if not unlocked)");
Felipe Lemef07c3f92020-02-12 14:26:33 -0800732 writer.println("-h");
733 writer.println("\t shows commands usage (NOTE: commands are not available on USER builds");
734 writer.println("[ANYTHING ELSE]");
735 writer.println("\t runs the given command (use --h to see the available commands)");
736 }
737
felipeal72bf6422019-11-02 17:04:04 -0700738 @Override
739 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
740 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
741 throws RemoteException {
742 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
743 }
744
felipeal080e5652019-11-02 17:04:04 -0700745 private void dumpListOfServices(PrintWriter writer) {
746 for (CarServiceBase service : mAllServices) {
747 writer.println(service.getClass().getName());
748 }
749 }
750
Mark Tabry7e2a7822019-10-17 10:18:32 -0700751 private void dumpAllServices(PrintWriter writer) {
Felipe Lemef07c3f92020-02-12 14:26:33 -0800752 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700753 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700754 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700755 }
756 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700757 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700758 }
felipeal080e5652019-11-02 17:04:04 -0700759 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700760
felipeal080e5652019-11-02 17:04:04 -0700761 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
762 for (String serviceName : serviceNames) {
763 writer.println("** Dumping " + serviceName + "\n");
764 CarServiceBase service = getCarServiceBySubstring(serviceName);
765 if (service == null) {
766 writer.println("No such service!");
767 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700768 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700769 }
770 writer.println();
771 }
772 }
773
774 @Nullable
775 private CarServiceBase getCarServiceBySubstring(String className) {
776 return Arrays.asList(mAllServices).stream()
777 .filter(s -> s.getClass().getSimpleName().equals(className))
778 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700779 }
780
Mark Tabry7e2a7822019-10-17 10:18:32 -0700781 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800782 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700783 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800784 } catch (Exception e) {
785 writer.println("Failed dumping: " + service.getClass().getName());
786 e.printStackTrace(writer);
787 }
keunyoungcc449f72015-08-12 10:46:27 -0700788 }
Yao Chene33f07e2016-07-26 12:02:51 -0700789
790 void execShellCmd(String[] args, PrintWriter writer) {
791 new CarShellCommand().exec(args, writer);
792 }
793
Pavel Maltsevabd47232017-10-10 16:54:57 -0700794 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700795 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700796 Slog.i(TAG, name);
797 mBootTiming.traceBegin(name);
798 }
799
Pavel Maltsevabd47232017-10-10 16:54:57 -0700800 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700801 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700802 mBootTiming.traceEnd();
803 }
804
felipeal72bf6422019-11-02 17:04:04 -0700805 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700806 private static final String COMMAND_HELP = "-h";
807 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800808 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700809 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800810 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800811 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800812 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800813 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700814 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700815 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800816 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700817 private static final String COMMAND_RESUME = "resume";
818 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700819 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
820 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700821 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700822 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
823 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800824 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
825 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800826 private static final String COMMAND_INJECT_KEY = "inject-key";
Felipe Lemea5e69382020-02-11 17:53:20 -0800827 private static final String COMMAND_GET_INITIAL_USER_INFO = "get-initial-user-info";
Felipe Leme5bf135b2020-02-07 12:22:14 -0800828
Yao Chene33f07e2016-07-26 12:02:51 -0700829 private static final String PARAM_DAY_MODE = "day";
830 private static final String PARAM_NIGHT_MODE = "night";
831 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800832 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800833 private static final String PARAM_ON_MODE = "on";
834 private static final String PARAM_OFF_MODE = "off";
835 private static final String PARAM_QUERY_MODE = "query";
Jim Kaye9a834ea2020-02-07 15:50:53 -0800836 private static final String PARAM_REBOOT = "reboot";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800837
felipeal72bf6422019-11-02 17:04:04 -0700838 private static final int RESULT_OK = 0;
839 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
840
841
842 @Override
843 public int onCommand(String cmd) {
844 if (cmd == null) {
845 onHelp();
846 return RESULT_ERROR;
847 }
848 ArrayList<String> argsList = new ArrayList<>();
849 argsList.add(cmd);
850 String arg = null;
851 do {
852 arg = getNextArg();
853 if (arg != null) {
854 argsList.add(arg);
855 }
856 } while (arg != null);
857 String[] args = new String[argsList.size()];
858 argsList.toArray(args);
859 return exec(args, getOutPrintWriter());
860 }
861
862 @Override
863 public void onHelp() {
864 dumpHelp(getOutPrintWriter());
865 }
Yao Chene33f07e2016-07-26 12:02:51 -0700866
867 private void dumpHelp(PrintWriter pw) {
868 pw.println("Car service commands:");
869 pw.println("\t-h");
870 pw.println("\t Print this help text.");
871 pw.println("\tday-night-mode [day|night|sensor]");
872 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800873 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800874 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700875 pw.println("\tinject-error-event property zone errorCode");
876 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700877 pw.println("\tenable-uxr true|false");
878 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800879 pw.println("\tgarage-mode [on|off|query|reboot]");
880 pw.println("\t Force into or out of garage mode, or check status.");
881 pw.println("\t With 'reboot', enter garage mode, then reboot when it completes.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800882 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800883 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800884 pw.println("\tget-carpropertyconfig [propertyId]");
885 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700886 pw.println("\tget-property-value [propertyId] [areaId]");
887 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
888 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700889 pw.println("\tsuspend");
890 pw.println("\t Suspend the system to Deep Sleep.");
891 pw.println("\tresume");
892 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700893 pw.println("\tenable-trusted-device true|false");
894 pw.println("\t Enable/Disable Trusted device feature.");
895 pw.println("\tremove-trusted-devices");
896 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700897 pw.println("\tprojection-tethering [true|false]");
898 pw.println("\t Whether tethering should be used when creating access point for"
899 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700900 pw.println("\t--metrics");
901 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700902 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
Jim Kaye9a834ea2020-02-07 15:50:53 -0800903 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700904 pw.println("\tstart-fixed-activity displayId packageName activityName");
905 pw.println("\t Start an Activity the specified display as fixed mode");
906 pw.println("\tstop-fixed-mode displayId");
907 pw.println("\t Stop fixed Activity mode for the given display. "
908 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800909 pw.println("\tenable-feature featureName");
910 pw.println("\t Enable the requested feature. Change will happen after reboot.");
911 pw.println("\t This requires root/su.");
912 pw.println("\tdisable-feature featureName");
913 pw.println("\t Disable the requested feature. Change will happen after reboot");
914 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800915 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
916 pw.println("\t inject key down / up event to car service");
917 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
918 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
919 pw.println("\t it will be 0");
920 pw.println("\t key_code: int key code defined in android KeyEvent");
Felipe Lemea5e69382020-02-11 17:53:20 -0800921 pw.printf("\t%s <REQ_TYPE> [--timeout TIMEOUT_MS]\n", COMMAND_GET_INITIAL_USER_INFO);
922 pw.println("\t Calls the Vehicle HAL to get the initial boot info, passing the given");
923 pw.println("\t REQ_TYPE (which could be either FIRST_BOOT, FIRST_BOOT_AFTER_OTA, ");
924 pw.println("\t COLD_BOOT, RESUME, or any numeric value that would be passed 'as-is')");
925 pw.println("\t and an optional TIMEOUT_MS to wait for the HAL response (if not set,");
926 pw.println("\t it will use a default value).");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700927 }
928
felipeal72bf6422019-11-02 17:04:04 -0700929 private int dumpInvalidArguments(PrintWriter pw) {
930 pw.println("Incorrect number of arguments.");
931 dumpHelp(pw);
932 return RESULT_ERROR;
933 }
934
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700935 private String runSetZoneIdForUid(String zoneString, String uidString) {
936 int uid = Integer.parseInt(uidString);
937 int zoneId = Integer.parseInt(zoneString);
938 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
939 return "zoneid " + zoneId + " not found";
940 }
941 mCarAudioService.setZoneIdForUid(zoneId, uid);
942 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700943 }
944
felipeal72bf6422019-11-02 17:04:04 -0700945 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700946 String arg = args[0];
947 switch (arg) {
948 case COMMAND_HELP:
949 dumpHelp(writer);
950 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800951 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800952 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700953 forceDayNightMode(value, writer);
954 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800955 }
956 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800957 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800958 forceGarageMode(value, writer);
959 break;
960 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800961 case COMMAND_INJECT_VHAL_EVENT:
962 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
963 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800964 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700965 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800966 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800967 // Zoned
968 zone = args[2];
969 data = args[3];
970 } else {
971 // Global
972 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800973 }
Kai4b098cf2019-06-05 15:03:28 -0700974 injectVhalEvent(args[1], zone, data, false, writer);
975 break;
976 case COMMAND_INJECT_ERROR_EVENT:
977 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700978 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700979 }
980 String errorAreaId = args[2];
981 String errorCode = args[3];
982 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800983 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800984 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800985 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700986 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800987 }
988 boolean enableBlocking = Boolean.valueOf(args[1]);
989 if (mCarPackageManagerService != null) {
990 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
991 }
992 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800993 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800994 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700995 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800996 }
997 String pkgName = args[1].toLowerCase();
998 if (mCarPackageManagerService != null) {
999 String[] doActivities =
1000 mCarPackageManagerService.getDistractionOptimizedActivities(
1001 pkgName);
1002 if (doActivities != null) {
1003 writer.println("DO Activities for " + pkgName);
1004 for (String a : doActivities) {
1005 writer.println(a);
1006 }
1007 } else {
1008 writer.println("No DO Activities for " + pkgName);
1009 }
1010 }
1011 break;
Kaidd4ef5a2019-03-04 14:07:26 -08001012 case COMMAND_GET_CARPROPERTYCONFIG:
1013 String propertyId = args.length < 2 ? "" : args[1];
1014 mHal.dumpPropertyConfigs(writer, propertyId);
1015 break;
Kai29fba112019-04-22 18:37:38 -07001016 case COMMAND_GET_PROPERTY_VALUE:
1017 String propId = args.length < 2 ? "" : args[1];
1018 String areaId = args.length < 3 ? "" : args[2];
1019 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
1020 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -08001021 case COMMAND_PROJECTION_UI_MODE:
1022 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001023 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -08001024 }
1025 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -07001026 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001027 case COMMAND_PROJECTION_AP_TETHERING:
1028 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001029 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -07001030 }
1031 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
1032 break;
Jim Kayed76c2742019-04-02 11:33:27 -07001033 case COMMAND_RESUME:
1034 mCarPowerManagementService.forceSimulatedResume();
1035 writer.println("Resume: Simulating resuming from Deep Sleep");
1036 break;
1037 case COMMAND_SUSPEND:
Jim Kaye9a834ea2020-02-07 15:50:53 -08001038 mCarPowerManagementService.forceSuspendAndMaybeReboot(false);
Jim Kayed76c2742019-04-02 11:33:27 -07001039 writer.println("Resume: Simulating powering down to Deep Sleep");
1040 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001041 case COMMAND_ENABLE_TRUSTED_DEVICE:
1042 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -07001043 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001044 }
1045 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
1046 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
1047 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
1048 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
1049 break;
1050 case COMMAND_REMOVE_TRUSTED_DEVICES:
1051 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -07001052 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -07001053 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001054 case COMMAND_SET_UID_TO_ZONE:
1055 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -07001056 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -07001057 }
1058 String results = runSetZoneIdForUid(args[1], args[2]);
1059 if (results != null) {
1060 writer.println(results);
1061 dumpHelp(writer);
1062 }
1063 break;
Keun young Park09ca8492019-10-15 10:34:11 -07001064 case COMMAND_START_FIXED_ACTIVITY_MODE:
1065 handleStartFixedActivity(args, writer);
1066 break;
1067 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
1068 handleStopFixedMode(args, writer);
1069 break;
Keun young Park9a91efb2019-11-15 18:10:47 -08001070 case COMMAND_ENABLE_FEATURE:
1071 if (args.length != 2) {
1072 return dumpInvalidArguments(writer);
1073 }
1074 handleEnableDisableFeature(args, writer, /* enable= */ true);
1075 break;
1076 case COMMAND_DISABLE_FEATURE:
1077 if (args.length != 2) {
1078 return dumpInvalidArguments(writer);
1079 }
1080 handleEnableDisableFeature(args, writer, /* enable= */ false);
1081 break;
Keun young Park313dfaf2019-12-19 11:32:38 -08001082 case COMMAND_INJECT_KEY:
1083 if (args.length < 2) {
1084 return dumpInvalidArguments(writer);
1085 }
1086 handleInjectKey(args, writer);
1087 break;
Felipe Lemea5e69382020-02-11 17:53:20 -08001088 case COMMAND_GET_INITIAL_USER_INFO:
1089 handleGetInitialUserInfo(args, writer);
1090 break;
Yao Chene33f07e2016-07-26 12:02:51 -07001091 default:
Jim Kayef10b7d82019-02-26 12:53:22 -08001092 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -07001093 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -07001094 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -07001095 }
felipeal72bf6422019-11-02 17:04:04 -07001096 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -07001097 }
1098
Keun young Park09ca8492019-10-15 10:34:11 -07001099 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1100 if (args.length != 4) {
1101 writer.println("Incorrect number of arguments");
1102 dumpHelp(writer);
1103 return;
1104 }
1105 int displayId;
1106 try {
1107 displayId = Integer.parseInt(args[1]);
1108 } catch (NumberFormatException e) {
1109 writer.println("Wrong display id:" + args[1]);
1110 return;
1111 }
1112 String packageName = args[2];
1113 String activityName = args[3];
1114 Intent intent = new Intent();
1115 intent.setComponent(new ComponentName(packageName, activityName));
1116 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1117 ActivityOptions options = ActivityOptions.makeBasic();
1118 options.setLaunchDisplayId(displayId);
1119 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1120 displayId, ActivityManager.getCurrentUser())) {
1121 writer.println("Failed to start");
1122 return;
1123 }
1124 writer.println("Succeeded");
1125 }
1126
1127 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1128 if (args.length != 2) {
1129 writer.println("Incorrect number of arguments");
1130 dumpHelp(writer);
1131 return;
1132 }
1133 int displayId;
1134 try {
1135 displayId = Integer.parseInt(args[1]);
1136 } catch (NumberFormatException e) {
1137 writer.println("Wrong display id:" + args[1]);
1138 return;
1139 }
1140 mFixedActivityService.stopFixedActivityMode(displayId);
1141 }
1142
Keun young Park9a91efb2019-11-15 18:10:47 -08001143 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1144 if (Binder.getCallingUid() != Process.ROOT_UID) {
1145 writer.println("Only allowed to root/su");
1146 return;
1147 }
1148 String featureName = args[1];
1149 long id = Binder.clearCallingIdentity();
1150 // no permission check here
1151 int r;
1152 if (enable) {
1153 r = mFeatureController.enableFeature(featureName);
1154 } else {
1155 r = mFeatureController.disableFeature(featureName);
1156 }
1157 switch (r) {
1158 case Car.FEATURE_REQUEST_SUCCESS:
1159 if (enable) {
1160 writer.println("Enabled feature:" + featureName);
1161 } else {
1162 writer.println("Disabled feature:" + featureName);
1163 }
1164 break;
1165 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1166 if (enable) {
1167 writer.println("Already enabled:" + featureName);
1168 } else {
1169 writer.println("Already disabled:" + featureName);
1170 }
1171 break;
1172 case Car.FEATURE_REQUEST_MANDATORY:
1173 writer.println("Cannot change mandatory feature:" + featureName);
1174 break;
1175 case Car.FEATURE_REQUEST_NOT_EXISTING:
1176 writer.println("Non-existing feature:" + featureName);
1177 break;
1178 default:
1179 writer.println("Unknown error:" + r);
1180 break;
1181 }
1182 Binder.restoreCallingIdentity(id);
1183 }
1184
Keun young Park313dfaf2019-12-19 11:32:38 -08001185 private void handleInjectKey(String[] args, PrintWriter writer) {
1186 int i = 1; // 0 is command itself
1187 int display = InputHalService.DISPLAY_MAIN;
1188 int delayMs = 0;
1189 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1190 try {
1191 while (i < args.length) {
1192 switch (args[i]) {
1193 case "-d":
1194 i++;
1195 display = Integer.parseInt(args[i]);
1196 break;
1197 case "-t":
1198 i++;
1199 delayMs = Integer.parseInt(args[i]);
1200 break;
1201 default:
1202 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1203 throw new IllegalArgumentException("key_code already set:"
1204 + keyCode);
1205 }
1206 keyCode = Integer.parseInt(args[i]);
1207 }
1208 i++;
1209 }
1210 } catch (Exception e) {
1211 writer.println("Invalid args:" + e);
1212 dumpHelp(writer);
1213 return;
1214 }
1215 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1216 writer.println("Missing key code or invalid keycode");
1217 dumpHelp(writer);
1218 return;
1219 }
1220 if (display != InputHalService.DISPLAY_MAIN
1221 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1222 writer.println("Invalid display:" + display);
1223 dumpHelp(writer);
1224 return;
1225 }
1226 if (delayMs < 0) {
1227 writer.println("Invalid delay:" + delayMs);
1228 dumpHelp(writer);
1229 return;
1230 }
1231 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1232 mCarInputService.onKeyEvent(keyDown, display);
1233 SystemClock.sleep(delayMs);
1234 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1235 mCarInputService.onKeyEvent(keyUp, display);
1236 writer.println("Succeeded");
1237 }
1238
Felipe Lemea5e69382020-02-11 17:53:20 -08001239 private void handleGetInitialUserInfo(String[] args, PrintWriter writer) {
1240 if (args.length < 2) {
1241 writer.println("Insufficient number of args");
1242 return;
1243 }
1244
1245 // Gets the request type
1246 String typeArg = args[1];
1247 int requestType = UserHalHelper.parseInitialUserInfoRequestType(typeArg);
1248
1249 int timeout = 1_000;
1250 for (int i = 2; i < args.length; i++) {
1251 String arg = args[i];
1252 switch (arg) {
1253 case "--timeout":
1254 timeout = Integer.parseInt(args[++i]);
1255 break;
1256 default:
1257 writer.println("Invalid option at index " + i + ": " + arg);
1258 return;
1259
1260 }
1261 }
1262
1263 Log.d(TAG, "handleGetInitialUserInfo(): type=" + requestType + " (" + typeArg
1264 + "), timeout=" + timeout);
1265
1266 UserHalService userHal = mHal.getUserHal();
1267 // TODO(b/146207078): use UserHalHelper to populate it with current users
1268 UsersInfo usersInfo = new UsersInfo();
1269 CountDownLatch latch = new CountDownLatch(1);
1270
1271 userHal.getInitialUserInfo(requestType, timeout, usersInfo, (status, resp) -> {
1272 try {
1273 Log.d(TAG, "GetUserInfoResponse: status=" + status + ", resp=" + resp);
1274 writer.printf("Status: %s\n", UserHalHelper.halCallbackStatusToString(status));
1275 if (status != HalCallback.STATUS_OK) {
1276 return;
1277 }
1278 writer.printf("Request id: %d\n", resp.requestId);
1279 writer.printf("Action: ");
1280 switch (resp.action) {
1281 case InitialUserInfoResponseAction.DEFAULT:
1282 writer.println("default");
1283 break;
1284 case InitialUserInfoResponseAction.SWITCH:
1285 writer.printf("switch to user %d\n", resp.userToSwitchOrCreate.userId);
1286 break;
1287 case InitialUserInfoResponseAction.CREATE:
1288 writer.printf("create user: name=%s, flags=%d\n", resp.userNameToCreate,
1289 resp.userToSwitchOrCreate.flags);
1290 break;
1291 default:
1292 writer.printf("unknown (%d)\n", resp.action);
1293 break;
1294 }
1295 } finally {
1296 latch.countDown();
1297 }
1298 });
1299
1300 try {
1301 if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
1302 writer.printf("HAL didn't respond in %dms\n", timeout);
1303 }
1304 } catch (InterruptedException e) {
1305 Thread.currentThread().interrupt();
1306 writer.println("Interrupted waiting for HAL");
1307 }
1308 return;
1309 }
1310
Yao Chene33f07e2016-07-26 12:02:51 -07001311 private void forceDayNightMode(String arg, PrintWriter writer) {
1312 int mode;
1313 switch (arg) {
1314 case PARAM_DAY_MODE:
1315 mode = CarNightService.FORCED_DAY_MODE;
1316 break;
1317 case PARAM_NIGHT_MODE:
1318 mode = CarNightService.FORCED_NIGHT_MODE;
1319 break;
1320 case PARAM_SENSOR_MODE:
1321 mode = CarNightService.FORCED_SENSOR_MODE;
1322 break;
1323 default:
1324 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1325 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1326 return;
1327 }
1328 int current = mCarNightService.forceDayNightMode(mode);
1329 String currentMode = null;
1330 switch (current) {
1331 case UiModeManager.MODE_NIGHT_AUTO:
1332 currentMode = PARAM_SENSOR_MODE;
1333 break;
1334 case UiModeManager.MODE_NIGHT_YES:
1335 currentMode = PARAM_NIGHT_MODE;
1336 break;
1337 case UiModeManager.MODE_NIGHT_NO:
1338 currentMode = PARAM_DAY_MODE;
1339 break;
1340 }
1341 writer.println("DayNightMode changed to: " + currentMode);
1342 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001343
Bryan Eylera32a7c12018-02-27 15:40:00 -08001344 private void forceGarageMode(String arg, PrintWriter writer) {
1345 switch (arg) {
1346 case PARAM_ON_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001347 mSystemInterface.setDisplayState(false);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001348 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001349 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001350 break;
1351 case PARAM_OFF_MODE:
Jim Kaye26fc7da2019-10-23 10:46:28 -07001352 mSystemInterface.setDisplayState(true);
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001353 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001354 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001355 break;
1356 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001357 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001358 break;
Jim Kaye9a834ea2020-02-07 15:50:53 -08001359 case PARAM_REBOOT:
1360 mCarPowerManagementService.forceSuspendAndMaybeReboot(true);
1361 writer.println("Entering Garage Mode. Will reboot when it completes.");
1362 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -08001363 default:
1364 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
Jim Kaye9a834ea2020-02-07 15:50:53 -08001365 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE + "|" + PARAM_REBOOT);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001366 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001367 }
1368
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001369 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001370 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001371 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001372 * @param property the Vehicle property Id as defined in the HAL
1373 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001374 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001375 * @param value Data value of the event
1376 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001377 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001378 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001379 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001380 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1381 if (!isPropertyAreaTypeGlobal(property)) {
1382 writer.println("Property area type inconsistent with given zone");
1383 return;
1384 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001385 }
1386 try {
Kai4b098cf2019-06-05 15:03:28 -07001387 if (isErrorEvent) {
1388 mHal.injectOnPropertySetError(property, zone, value);
1389 } else {
1390 mHal.injectVhalEvent(property, zone, value);
1391 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001392 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001393 writer.println("Invalid property Id zone Id or value" + e);
1394 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001395 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001396 }
1397
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001398 // Check if the given property is global
1399 private boolean isPropertyAreaTypeGlobal(String property) {
1400 if (property == null) {
1401 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001402 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001403 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001404 }
Yao Chene33f07e2016-07-26 12:02:51 -07001405 }
kevinjm55822c42018-08-15 11:26:00 -07001406}