blob: 893fe3fdd024ec8ee918ac4a8d3bdb2b21ce8829 [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car;
18
Pavel Maltsevabd47232017-10-10 16:54:57 -070019import android.annotation.MainThread;
felipeal080e5652019-11-02 17:04:04 -070020import android.annotation.Nullable;
Keun young Parkf3523cd2019-04-08 10:09:17 -070021import android.app.ActivityManager;
Keun young Park09ca8492019-10-15 10:34:11 -070022import android.app.ActivityOptions;
Yao Chene33f07e2016-07-26 12:02:51 -070023import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080024import android.car.Car;
Keun young Park9a91efb2019-11-15 18:10:47 -080025import android.car.CarFeatures;
Keun-young Parke54ac272016-02-16 19:02:18 -080026import android.car.ICar;
Pavel Maltsev0477e292016-05-27 12:22:36 -070027import android.car.cluster.renderer.IInstrumentClusterNavigation;
Ying Zheng9fc99402018-09-19 14:23:59 -070028import android.car.userlib.CarUserManagerHelper;
Keun young Park09ca8492019-10-15 10:34:11 -070029import android.content.ComponentName;
keunyoungca515072015-07-10 12:21:47 -070030import android.content.Context;
Keun young Park09ca8492019-10-15 10:34:11 -070031import android.content.Intent;
keunyoung1ab8e182015-09-24 09:25:22 -070032import android.content.pm.PackageManager;
Keun young Parkf3523cd2019-04-08 10:09:17 -070033import android.content.res.Resources;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080034import android.hardware.automotive.vehicle.V2_0.IVehicle;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080035import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070036import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070037import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070038import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070039import android.os.Process;
felipeal72bf6422019-11-02 17:04:04 -070040import android.os.RemoteException;
41import android.os.ResultReceiver;
42import android.os.ShellCallback;
43import android.os.ShellCommand;
Keun young Park313dfaf2019-12-19 11:32:38 -080044import android.os.SystemClock;
Steve Paikc302c7c2017-08-04 14:01:58 -070045import android.os.Trace;
Anthony Hugh9932a252019-06-12 16:19:56 -070046import android.os.UserManager;
keunyoungca515072015-07-10 12:21:47 -070047import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070048import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070049import android.util.TimingsTraceLog;
Keun young Park313dfaf2019-12-19 11:32:38 -080050import android.view.KeyEvent;
Gregory Clark26fa6012018-03-14 18:38:56 -070051
Keun young Parka4d12202019-10-03 10:51:12 -070052import com.android.car.am.FixedActivityService;
Hongwei Wang38549092018-08-22 12:32:42 -070053import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080054import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070055import com.android.car.garagemode.GarageModeService;
Keun young Park313dfaf2019-12-19 11:32:38 -080056import com.android.car.hal.InputHalService;
keunyoungcc449f72015-08-12 10:46:27 -070057import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080058import com.android.car.pm.CarPackageManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -070059import com.android.car.stats.CarStatsService;
Enrico Granatab19bc322017-10-12 12:25:06 -070060import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070061import com.android.car.trust.CarTrustedDeviceService;
Keun young Park7af7d6c2019-09-12 10:31:48 -070062import com.android.car.user.CarUserNoticeService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070063import com.android.car.user.CarUserService;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080064import com.android.car.vms.VmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080065import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070066import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000067import com.android.internal.car.ICarServiceHelper;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070068import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070069
Enrico Granatae8056ca2018-04-03 13:19:52 -070070import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070071import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070072import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080073import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070074import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070075
keunyoungca515072015-07-10 12:21:47 -070076public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070077
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080078 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070079 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
80 "system_activity_monitoring";
Yan Zhub07585d2019-10-29 14:16:40 -070081 public static final String INTERNAL_VMS_MANAGER = "vms_manager";
Keun-young Parka28d7b22016-02-29 16:54:29 -080082
keunyoungca515072015-07-10 12:21:47 -070083 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080084 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070085
Keun young Park9a91efb2019-11-15 18:10:47 -080086 private final CarFeatureController mFeatureController;
87
Anthony Chen12aec302018-04-25 16:41:48 -070088 private final SystemInterface mSystemInterface;
89
Keun-young Park4727da32016-05-31 10:00:51 -070090 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070091 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080092 private final CarPackageManagerService mCarPackageManagerService;
93 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080094 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080095 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070096 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080097 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070098 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080099 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700100 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -0700101 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -0800102 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800103 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -0800104 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700105 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800106 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700107 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700108 private final CarDiagnosticService mCarDiagnosticService;
109 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700110 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700111 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800112 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700113 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800114 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700115 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700116 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800117 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800118 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800119 private final VmsSubscriberService mVmsSubscriberService;
120 private final VmsPublisherService mVmsPublisherService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700121 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700122 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800123 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
keunyounga74b9ca2015-10-21 13:33:58 -0700124
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700125 private final CarServiceBase[] mAllServices;
126
Steve Paikc302c7c2017-08-04 14:01:58 -0700127 private static final String TAG = "ICarImpl";
128 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700129
130 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700131
keunyoung1ab8e182015-09-24 09:25:22 -0700132 /** Test only service. Populate it only when necessary. */
133 @GuardedBy("this")
134 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700135
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700136 @GuardedBy("this")
137 private ICarServiceHelper mICarServiceHelper;
138
Enrico Granatae8056ca2018-04-03 13:19:52 -0700139 private final String mVehicleInterfaceName;
140
Pavel Maltsevec83b632017-01-05 15:10:55 -0800141 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700142 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700143 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800144 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700145 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800146 Resources res = mContext.getResources();
147 String[] defaultEnabledFeatures = res.getStringArray(
148 R.array.config_allowed_optional_car_features);
149 // Do this before any other service components to allow feature check. It should work
150 // even without init.
151 // TODO (b/144504820) Add vhal plumbing
152 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
153 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
154 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700155 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800156 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Anthony Hugh9932a252019-06-12 16:19:56 -0700157 UserManager userManager =
158 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
Keun young Park9a91efb2019-11-15 18:10:47 -0800159 int maxRunningUsers = res.getInteger(
Keun young Parkf3523cd2019-04-08 10:09:17 -0700160 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Eric Jeong3a793b02019-09-30 16:12:53 -0700161 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper, userManager,
Keun young Parkf3523cd2019-04-08 10:09:17 -0700162 ActivityManager.getService(), maxRunningUsers);
Keun young Parkbb877e22019-08-02 10:38:01 -0700163 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700164 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800165 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700166 systemInterface, mUserManagerHelper);
Keun young Park9a91efb2019-11-15 18:10:47 -0800167 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
168 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
169 } else {
170 mCarUserNoticeService = null;
171 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700172 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
173 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800174 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800175 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800176 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
177 mCarUXRestrictionsService,
Anthony Hughfbb67762019-10-15 12:54:54 -0700178 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800179 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Sal Savage703c46f2019-04-15 08:39:25 -0700180 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700181 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800182 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800183 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700184 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700185 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800186 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700187 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700188 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700189 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700190 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700191 mSystemStateControllerService = new SystemStateControllerService(
192 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700193 mCarStatsService = new CarStatsService(serviceContext);
Mark Tabry61a06c72019-06-17 10:37:49 -0700194 mVmsBrokerService = new VmsBrokerService();
Mark Tabry1534c062019-03-29 01:30:49 -0700195 mVmsClientManager = new VmsClientManager(
Mark Tabry7e2a7822019-10-17 10:18:32 -0700196 // CarStatsService needs to be passed to the constructor due to HAL init order
197 serviceContext, mCarStatsService, mCarUserService, mVmsBrokerService,
198 mHal.getVmsHal());
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800199 mVmsSubscriberService = new VmsSubscriberService(
Mark Tabry61a06c72019-06-17 10:37:49 -0700200 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800201 mVmsPublisherService = new VmsPublisherService(
Mark Tabry7e2a7822019-10-17 10:18:32 -0700202 serviceContext, mCarStatsService, mVmsBrokerService, mVmsClientManager);
Enrico Granatab3634e22017-05-05 18:02:31 -0700203 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Keun young Park9a91efb2019-11-15 18:10:47 -0800204 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
205 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
206 systemInterface);
207 } else {
208 mCarStorageMonitoringService = null;
209 }
Anthony Chen12aec302018-04-25 16:41:48 -0700210 mCarConfigurationService =
211 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700212 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700213 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800214 mCarMediaService = new CarMediaService(serviceContext);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700215 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800216 if (!Build.IS_USER) {
217 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
218 serviceContext);
219 } else {
220 mCarExperimentalFeatureServiceController = null;
221 }
keunyounga74b9ca2015-10-21 13:33:58 -0700222
Keun young Parka5fa4782019-04-16 18:56:27 -0700223 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700224 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800225 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700226 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800227 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700228 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700229 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700230 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800231
keunyounga3b28d82015-08-25 13:05:15 -0700232 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700233 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800234 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800235 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700236 allServices.add(mSystemActivityMonitoringService);
237 allServices.add(mCarPowerManagementService);
238 allServices.add(mCarPropertyService);
239 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700240 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700241 allServices.add(mCarUXRestrictionsService);
242 allServices.add(mCarPackageManagerService);
243 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700244 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800245 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700246 allServices.add(mAppFocusService);
247 allServices.add(mCarAudioService);
248 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700249 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700250 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700251 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700252 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800253 allServices.add(mCarBluetoothService);
254 allServices.add(mCarProjectionService);
255 allServices.add(mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800256 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700257 allServices.add(mCarConfigurationService);
Mark Tabrya8fce562019-01-16 16:24:01 -0800258 allServices.add(mVmsClientManager);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700259 allServices.add(mVmsSubscriberService);
260 allServices.add(mVmsPublisherService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700261 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800262 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700263 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700264 allServices.add(mCarBugreportManagerService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800265 // Always put mCarExperimentalFeatureServiceController in last.
266 addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700267 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700268 }
269
Keun young Park9a91efb2019-11-15 18:10:47 -0800270 private void addServiceIfNonNull(List<CarServiceBase> services, CarServiceBase service) {
271 if (service != null) {
272 services.add(service);
273 }
274 }
275
Pavel Maltsevabd47232017-10-10 16:54:57 -0700276 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700277 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700278 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700279 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700280 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700281 traceEnd();
282 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800283 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700284 service.init();
285 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700286 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700287 }
288
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700289 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700290 // release done in opposite order from init
291 for (int i = mAllServices.length - 1; i >= 0; i--) {
292 mAllServices[i].release();
293 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700294 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700295 }
296
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700297 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800298 mHal.vehicleHalReconnected(vehicle);
299 for (CarServiceBase service : mAllServices) {
300 service.vehicleHalReconnected();
301 }
302 }
303
keunyoungca515072015-07-10 12:21:47 -0700304 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700305 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700306 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700307 synchronized (this) {
308 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800309 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700310 }
311 }
312
313 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700314 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700315 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700316 mCarUserService.setUserLockStatus(userId, unlocked == 1);
317 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700318 }
319
320 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700321 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700322 assertCallingFromSystemProcess();
323
Eric Jeong1545f3b2019-09-16 13:56:52 -0700324 Log.i(TAG, "Foreground user switched to " + userId);
325 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700326 }
327
Keun young Park9a91efb2019-11-15 18:10:47 -0800328 @Override
329 public boolean isFeatureEnabled(String featureName) {
330 return mFeatureController.isFeatureEnabled(featureName);
331 }
332
333 @Override
334 public int enableFeature(String featureName) {
335 // permission check inside the controller
336 return mFeatureController.enableFeature(featureName);
337 }
338
339 @Override
340 public int disableFeature(String featureName) {
341 // permission check inside the controller
342 return mFeatureController.disableFeature(featureName);
343 }
344
345 @Override
346 public List<String> getAllEnabledFeatures() {
347 // permission check inside the controller
348 return mFeatureController.getAllEnabledFeatures();
349 }
350
351 @Override
352 public List<String> getAllPendingDisabledFeatures() {
353 // permission check inside the controller
354 return mFeatureController.getAllPendingDisabledFeatures();
355 }
356
357 @Override
358 public List<String> getAllPendingEnabledFeatures() {
359 // permission check inside the controller
360 return mFeatureController.getAllPendingEnabledFeatures();
361 }
362
363 @Override
364 public String getCarManagerClassForFeature(String featureName) {
365 if (mCarExperimentalFeatureServiceController == null) {
366 return null;
367 }
368 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
369 }
370
Jim Kayee5133162019-04-22 12:50:27 -0700371 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800372 int uid = Binder.getCallingUid();
373 if (uid != Process.SYSTEM_UID) {
374 throw new SecurityException("Only allowed from system");
375 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800376 }
377
Keun young Parkaabecd92019-05-03 17:31:27 -0700378 /**
379 * Assert if binder call is coming from system process like system server or if it is called
380 * from its own process even if it is not system. The latter can happen in test environment.
381 * Note that car service runs as system user but test like car service test will not.
382 */
383 static void assertCallingFromSystemProcessOrSelf() {
384 int uid = Binder.getCallingUid();
385 int pid = Binder.getCallingPid();
386 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
387 throw new SecurityException("Only allowed from system or self");
388 }
389 }
390
Keun-young Parkd462a912019-02-11 08:53:42 -0800391 @Override
keunyoungca515072015-07-10 12:21:47 -0700392 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800393 if (!mFeatureController.isFeatureEnabled(serviceName)) {
394 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
395 return null;
396 }
keunyoungca515072015-07-10 12:21:47 -0700397 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800398 case Car.AUDIO_SERVICE:
399 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700400 case Car.APP_FOCUS_SERVICE:
401 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800402 case Car.PACKAGE_SERVICE:
403 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800404 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700405 assertAnyDiagnosticPermission(mContext);
406 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800407 case Car.POWER_SERVICE:
408 assertPowerPermission(mContext);
409 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700410 case Car.CABIN_SERVICE:
411 case Car.HVAC_SERVICE:
412 case Car.INFO_SERVICE:
413 case Car.PROPERTY_SERVICE:
414 case Car.SENSOR_SERVICE:
415 case Car.VENDOR_EXTENSION_SERVICE:
416 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800417 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700418 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700419 IInstrumentClusterNavigation navService =
420 mInstrumentClusterService.getNavigationService();
421 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700422 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
423 assertClusterManagerPermission(mContext);
424 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800425 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800426 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800427 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700428 assertVmsSubscriberPermission(mContext);
429 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800430 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700431 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700432 synchronized (this) {
433 if (mCarTestService == null) {
434 mCarTestService = new CarTestService(mContext, this);
435 }
436 return mCarTestService;
437 }
438 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700439 case Car.BLUETOOTH_SERVICE:
440 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700441 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700442 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700443 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800444 case Car.CAR_DRIVING_STATE_SERVICE:
445 assertDrivingStatePermission(mContext);
446 return mCarDrivingStateService;
447 case Car.CAR_UX_RESTRICTION_SERVICE:
448 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700449 case Car.CAR_CONFIGURATION_SERVICE:
450 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800451 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
452 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700453 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800454 case Car.CAR_MEDIA_SERVICE:
455 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700456 case Car.CAR_OCCUPANT_ZONE_SERVICE:
457 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700458 case Car.CAR_BUGREPORT_SERVICE:
459 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700460 case Car.CAR_USER_SERVICE:
461 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700462 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800463 IBinder service = null;
464 if (mCarExperimentalFeatureServiceController != null) {
465 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
466 }
467 if (service == null) {
468 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
469 + serviceName);
470 }
471 return service;
keunyoungca515072015-07-10 12:21:47 -0700472 }
473 }
474
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800475 @Override
476 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700477 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800478 }
479
Keun-young Parka28d7b22016-02-29 16:54:29 -0800480 public CarServiceBase getCarInternalService(String serviceName) {
481 switch (serviceName) {
482 case INTERNAL_INPUT_SERVICE:
483 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700484 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
485 return mSystemActivityMonitoringService;
Yan Zhub07585d2019-10-29 14:16:40 -0700486 // TODO(b/144027497): temporary until tests are refactored to not use it
487 case INTERNAL_VMS_MANAGER:
488 return mVmsClientManager;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800489 default:
490 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
491 serviceName);
492 return null;
493 }
494 }
495
Mark Tabryf7319ae2019-11-07 11:26:35 -0800496 CarStatsService getStatsService() {
497 return mCarStatsService;
498 }
499
keunyoung1ab8e182015-09-24 09:25:22 -0700500 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700501 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700502 }
503
Keun-young Parke31a8b22016-03-16 17:34:08 -0700504 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700505 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800506 }
507
Pavel Maltsev905968c2017-07-16 19:48:57 -0700508 public static void assertClusterManagerPermission(Context context) {
509 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
510 }
511
Steve Paik388d7772018-02-12 10:54:51 -0800512 public static void assertPowerPermission(Context context) {
513 assertPermission(context, Car.PERMISSION_CAR_POWER);
514 }
515
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800516 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700517 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
518 }
519
Pavel Maltsev079873b2019-02-25 12:15:09 -0800520 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
521 public static void assertProjectionStatusPermission(Context context) {
522 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
523 }
524
Enrico Granata3c7a6662017-02-23 18:07:59 -0800525 public static void assertAnyDiagnosticPermission(Context context) {
526 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700527 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800528 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
529 }
530
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800531 public static void assertDrivingStatePermission(Context context) {
532 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
533 }
534
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800535 public static void assertVmsPublisherPermission(Context context) {
536 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
537 }
538
Antonio Cortese4619c72017-02-02 07:53:27 -0800539 public static void assertVmsSubscriberPermission(Context context) {
540 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
541 }
542
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800543 /**
544 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800545 */
546 public static void assertTrustAgentEnrollmentPermission(Context context) {
547 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
548 }
549
Steve Paik461ecc62016-06-08 15:28:32 -0700550 public static void assertPermission(Context context, String permission) {
551 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
552 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800553 }
554 }
555
Steve Paik9ec53d72018-04-27 13:28:31 -0700556 /**
557 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700558 *
559 * @return boolean TRUE if caller has the permission.
560 */
561 public static boolean hasPermission(Context context, String permission) {
562 return context.checkCallingOrSelfPermission(permission)
563 == PackageManager.PERMISSION_GRANTED;
564 }
565
Enrico Granata3c7a6662017-02-23 18:07:59 -0800566 public static void assertAnyPermission(Context context, String... permissions) {
567 for (String permission : permissions) {
568 if (context.checkCallingOrSelfPermission(permission) ==
569 PackageManager.PERMISSION_GRANTED) {
570 return;
571 }
572 }
573 throw new SecurityException("requires any of " + Arrays.toString(permissions));
574 }
575
Enrico Granatae8056ca2018-04-03 13:19:52 -0700576 @Override
577 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
578 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700579 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700580 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700581 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
582 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700583 return;
keunyounga3b28d82015-08-25 13:05:15 -0700584 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700585
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700586 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
587 writer.println("*Dump car service*");
Enrico Granatae8056ca2018-04-03 13:19:52 -0700588 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700589
Mark Tabry7e2a7822019-10-17 10:18:32 -0700590 dumpAllServices(writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700591
Enrico Granatae8056ca2018-04-03 13:19:52 -0700592 writer.println("*Dump Vehicle HAL*");
593 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
594 try {
595 // TODO dump all feature flags by creating a dumpable interface
596 mHal.dump(writer);
597 } catch (Exception e) {
598 writer.println("Failed dumping: " + mHal.getClass().getName());
599 e.printStackTrace(writer);
600 }
felipeal080e5652019-11-02 17:04:04 -0700601 } else if ("--list".equals(args[0])) {
602 dumpListOfServices(writer);
603 return;
604 } else if ("--services".equals(args[0])) {
605 if (args.length < 2) {
606 writer.print("Must pass services to dump when using --services");
607 return;
608 }
609 int length = args.length - 1;
610 String[] services = new String[length];
611 System.arraycopy(args, 1, services, 0, length);
612 dumpIndividualServices(writer, services);
613 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700614 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700615 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800616 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700617 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700618 } else if ("--vms-hal".equals(args[0])) {
619 mHal.getVmsHal().dumpMetrics(fd);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700620 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
621 execShellCmd(args, writer);
622 } else {
623 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800624 }
625 }
626
felipeal72bf6422019-11-02 17:04:04 -0700627 @Override
628 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
629 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
630 throws RemoteException {
631 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
632 }
633
felipeal080e5652019-11-02 17:04:04 -0700634 private void dumpListOfServices(PrintWriter writer) {
635 for (CarServiceBase service : mAllServices) {
636 writer.println(service.getClass().getName());
637 }
638 }
639
Mark Tabry7e2a7822019-10-17 10:18:32 -0700640 private void dumpAllServices(PrintWriter writer) {
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700641 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700642 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700643 }
644 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700645 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700646 }
felipeal080e5652019-11-02 17:04:04 -0700647 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700648
felipeal080e5652019-11-02 17:04:04 -0700649 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
650 for (String serviceName : serviceNames) {
651 writer.println("** Dumping " + serviceName + "\n");
652 CarServiceBase service = getCarServiceBySubstring(serviceName);
653 if (service == null) {
654 writer.println("No such service!");
655 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700656 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700657 }
658 writer.println();
659 }
660 }
661
662 @Nullable
663 private CarServiceBase getCarServiceBySubstring(String className) {
664 return Arrays.asList(mAllServices).stream()
665 .filter(s -> s.getClass().getSimpleName().equals(className))
666 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700667 }
668
Mark Tabry7e2a7822019-10-17 10:18:32 -0700669 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800670 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700671 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800672 } catch (Exception e) {
673 writer.println("Failed dumping: " + service.getClass().getName());
674 e.printStackTrace(writer);
675 }
keunyoungcc449f72015-08-12 10:46:27 -0700676 }
Yao Chene33f07e2016-07-26 12:02:51 -0700677
678 void execShellCmd(String[] args, PrintWriter writer) {
679 new CarShellCommand().exec(args, writer);
680 }
681
Pavel Maltsevabd47232017-10-10 16:54:57 -0700682 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700683 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700684 Slog.i(TAG, name);
685 mBootTiming.traceBegin(name);
686 }
687
Pavel Maltsevabd47232017-10-10 16:54:57 -0700688 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700689 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700690 mBootTiming.traceEnd();
691 }
692
felipeal72bf6422019-11-02 17:04:04 -0700693 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700694 private static final String COMMAND_HELP = "-h";
695 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800696 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700697 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800698 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800699 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800700 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800701 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700702 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700703 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800704 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700705 private static final String COMMAND_RESUME = "resume";
706 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700707 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
708 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700709 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700710 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
711 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800712 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
713 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800714 private static final String COMMAND_INJECT_KEY = "inject-key";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800715
Yao Chene33f07e2016-07-26 12:02:51 -0700716 private static final String PARAM_DAY_MODE = "day";
717 private static final String PARAM_NIGHT_MODE = "night";
718 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800719 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800720 private static final String PARAM_ON_MODE = "on";
721 private static final String PARAM_OFF_MODE = "off";
722 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800723
felipeal72bf6422019-11-02 17:04:04 -0700724 private static final int RESULT_OK = 0;
725 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
726
727
728 @Override
729 public int onCommand(String cmd) {
730 if (cmd == null) {
731 onHelp();
732 return RESULT_ERROR;
733 }
734 ArrayList<String> argsList = new ArrayList<>();
735 argsList.add(cmd);
736 String arg = null;
737 do {
738 arg = getNextArg();
739 if (arg != null) {
740 argsList.add(arg);
741 }
742 } while (arg != null);
743 String[] args = new String[argsList.size()];
744 argsList.toArray(args);
745 return exec(args, getOutPrintWriter());
746 }
747
748 @Override
749 public void onHelp() {
750 dumpHelp(getOutPrintWriter());
751 }
Yao Chene33f07e2016-07-26 12:02:51 -0700752
753 private void dumpHelp(PrintWriter pw) {
754 pw.println("Car service commands:");
755 pw.println("\t-h");
756 pw.println("\t Print this help text.");
757 pw.println("\tday-night-mode [day|night|sensor]");
758 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800759 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800760 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700761 pw.println("\tinject-error-event property zone errorCode");
762 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700763 pw.println("\tenable-uxr true|false");
764 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800765 pw.println("\tgarage-mode [on|off|query]");
766 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800767 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800768 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800769 pw.println("\tget-carpropertyconfig [propertyId]");
770 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700771 pw.println("\tget-property-value [propertyId] [areaId]");
772 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
773 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700774 pw.println("\tsuspend");
775 pw.println("\t Suspend the system to Deep Sleep.");
776 pw.println("\tresume");
777 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700778 pw.println("\tenable-trusted-device true|false");
779 pw.println("\t Enable/Disable Trusted device feature.");
780 pw.println("\tremove-trusted-devices");
781 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700782 pw.println("\tprojection-tethering [true|false]");
783 pw.println("\t Whether tethering should be used when creating access point for"
784 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700785 pw.println("\t--metrics");
786 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700787 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
788 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700789 pw.println("\tstart-fixed-activity displayId packageName activityName");
790 pw.println("\t Start an Activity the specified display as fixed mode");
791 pw.println("\tstop-fixed-mode displayId");
792 pw.println("\t Stop fixed Activity mode for the given display. "
793 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800794 pw.println("\tenable-feature featureName");
795 pw.println("\t Enable the requested feature. Change will happen after reboot.");
796 pw.println("\t This requires root/su.");
797 pw.println("\tdisable-feature featureName");
798 pw.println("\t Disable the requested feature. Change will happen after reboot");
799 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800800 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
801 pw.println("\t inject key down / up event to car service");
802 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
803 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
804 pw.println("\t it will be 0");
805 pw.println("\t key_code: int key code defined in android KeyEvent");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700806 }
807
felipeal72bf6422019-11-02 17:04:04 -0700808 private int dumpInvalidArguments(PrintWriter pw) {
809 pw.println("Incorrect number of arguments.");
810 dumpHelp(pw);
811 return RESULT_ERROR;
812 }
813
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700814 private String runSetZoneIdForUid(String zoneString, String uidString) {
815 int uid = Integer.parseInt(uidString);
816 int zoneId = Integer.parseInt(zoneString);
817 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
818 return "zoneid " + zoneId + " not found";
819 }
820 mCarAudioService.setZoneIdForUid(zoneId, uid);
821 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700822 }
823
felipeal72bf6422019-11-02 17:04:04 -0700824 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700825 String arg = args[0];
826 switch (arg) {
827 case COMMAND_HELP:
828 dumpHelp(writer);
829 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800830 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800831 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700832 forceDayNightMode(value, writer);
833 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800834 }
835 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800836 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800837 forceGarageMode(value, writer);
838 break;
839 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800840 case COMMAND_INJECT_VHAL_EVENT:
841 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
842 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800843 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700844 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800845 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800846 // Zoned
847 zone = args[2];
848 data = args[3];
849 } else {
850 // Global
851 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800852 }
Kai4b098cf2019-06-05 15:03:28 -0700853 injectVhalEvent(args[1], zone, data, false, writer);
854 break;
855 case COMMAND_INJECT_ERROR_EVENT:
856 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700857 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700858 }
859 String errorAreaId = args[2];
860 String errorCode = args[3];
861 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800862 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800863 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800864 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700865 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800866 }
867 boolean enableBlocking = Boolean.valueOf(args[1]);
868 if (mCarPackageManagerService != null) {
869 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
870 }
871 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800872 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800873 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700874 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800875 }
876 String pkgName = args[1].toLowerCase();
877 if (mCarPackageManagerService != null) {
878 String[] doActivities =
879 mCarPackageManagerService.getDistractionOptimizedActivities(
880 pkgName);
881 if (doActivities != null) {
882 writer.println("DO Activities for " + pkgName);
883 for (String a : doActivities) {
884 writer.println(a);
885 }
886 } else {
887 writer.println("No DO Activities for " + pkgName);
888 }
889 }
890 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800891 case COMMAND_GET_CARPROPERTYCONFIG:
892 String propertyId = args.length < 2 ? "" : args[1];
893 mHal.dumpPropertyConfigs(writer, propertyId);
894 break;
Kai29fba112019-04-22 18:37:38 -0700895 case COMMAND_GET_PROPERTY_VALUE:
896 String propId = args.length < 2 ? "" : args[1];
897 String areaId = args.length < 3 ? "" : args[2];
898 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
899 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800900 case COMMAND_PROJECTION_UI_MODE:
901 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700902 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -0800903 }
904 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700905 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700906 case COMMAND_PROJECTION_AP_TETHERING:
907 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700908 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700909 }
910 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
911 break;
Jim Kayed76c2742019-04-02 11:33:27 -0700912 case COMMAND_RESUME:
913 mCarPowerManagementService.forceSimulatedResume();
914 writer.println("Resume: Simulating resuming from Deep Sleep");
915 break;
916 case COMMAND_SUSPEND:
917 mCarPowerManagementService.forceSimulatedSuspend();
918 writer.println("Resume: Simulating powering down to Deep Sleep");
919 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700920 case COMMAND_ENABLE_TRUSTED_DEVICE:
921 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700922 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700923 }
924 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
925 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
926 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
927 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
928 break;
929 case COMMAND_REMOVE_TRUSTED_DEVICES:
930 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -0700931 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700932 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700933 case COMMAND_SET_UID_TO_ZONE:
934 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -0700935 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700936 }
937 String results = runSetZoneIdForUid(args[1], args[2]);
938 if (results != null) {
939 writer.println(results);
940 dumpHelp(writer);
941 }
942 break;
Keun young Park09ca8492019-10-15 10:34:11 -0700943 case COMMAND_START_FIXED_ACTIVITY_MODE:
944 handleStartFixedActivity(args, writer);
945 break;
946 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
947 handleStopFixedMode(args, writer);
948 break;
Keun young Park9a91efb2019-11-15 18:10:47 -0800949 case COMMAND_ENABLE_FEATURE:
950 if (args.length != 2) {
951 return dumpInvalidArguments(writer);
952 }
953 handleEnableDisableFeature(args, writer, /* enable= */ true);
954 break;
955 case COMMAND_DISABLE_FEATURE:
956 if (args.length != 2) {
957 return dumpInvalidArguments(writer);
958 }
959 handleEnableDisableFeature(args, writer, /* enable= */ false);
960 break;
Keun young Park313dfaf2019-12-19 11:32:38 -0800961 case COMMAND_INJECT_KEY:
962 if (args.length < 2) {
963 return dumpInvalidArguments(writer);
964 }
965 handleInjectKey(args, writer);
966 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700967 default:
Jim Kayef10b7d82019-02-26 12:53:22 -0800968 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -0700969 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -0700970 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -0700971 }
felipeal72bf6422019-11-02 17:04:04 -0700972 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -0700973 }
974
Keun young Park09ca8492019-10-15 10:34:11 -0700975 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
976 if (args.length != 4) {
977 writer.println("Incorrect number of arguments");
978 dumpHelp(writer);
979 return;
980 }
981 int displayId;
982 try {
983 displayId = Integer.parseInt(args[1]);
984 } catch (NumberFormatException e) {
985 writer.println("Wrong display id:" + args[1]);
986 return;
987 }
988 String packageName = args[2];
989 String activityName = args[3];
990 Intent intent = new Intent();
991 intent.setComponent(new ComponentName(packageName, activityName));
992 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
993 ActivityOptions options = ActivityOptions.makeBasic();
994 options.setLaunchDisplayId(displayId);
995 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
996 displayId, ActivityManager.getCurrentUser())) {
997 writer.println("Failed to start");
998 return;
999 }
1000 writer.println("Succeeded");
1001 }
1002
1003 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1004 if (args.length != 2) {
1005 writer.println("Incorrect number of arguments");
1006 dumpHelp(writer);
1007 return;
1008 }
1009 int displayId;
1010 try {
1011 displayId = Integer.parseInt(args[1]);
1012 } catch (NumberFormatException e) {
1013 writer.println("Wrong display id:" + args[1]);
1014 return;
1015 }
1016 mFixedActivityService.stopFixedActivityMode(displayId);
1017 }
1018
Keun young Park9a91efb2019-11-15 18:10:47 -08001019 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1020 if (Binder.getCallingUid() != Process.ROOT_UID) {
1021 writer.println("Only allowed to root/su");
1022 return;
1023 }
1024 String featureName = args[1];
1025 long id = Binder.clearCallingIdentity();
1026 // no permission check here
1027 int r;
1028 if (enable) {
1029 r = mFeatureController.enableFeature(featureName);
1030 } else {
1031 r = mFeatureController.disableFeature(featureName);
1032 }
1033 switch (r) {
1034 case Car.FEATURE_REQUEST_SUCCESS:
1035 if (enable) {
1036 writer.println("Enabled feature:" + featureName);
1037 } else {
1038 writer.println("Disabled feature:" + featureName);
1039 }
1040 break;
1041 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1042 if (enable) {
1043 writer.println("Already enabled:" + featureName);
1044 } else {
1045 writer.println("Already disabled:" + featureName);
1046 }
1047 break;
1048 case Car.FEATURE_REQUEST_MANDATORY:
1049 writer.println("Cannot change mandatory feature:" + featureName);
1050 break;
1051 case Car.FEATURE_REQUEST_NOT_EXISTING:
1052 writer.println("Non-existing feature:" + featureName);
1053 break;
1054 default:
1055 writer.println("Unknown error:" + r);
1056 break;
1057 }
1058 Binder.restoreCallingIdentity(id);
1059 }
1060
Keun young Park313dfaf2019-12-19 11:32:38 -08001061 private void handleInjectKey(String[] args, PrintWriter writer) {
1062 int i = 1; // 0 is command itself
1063 int display = InputHalService.DISPLAY_MAIN;
1064 int delayMs = 0;
1065 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1066 try {
1067 while (i < args.length) {
1068 switch (args[i]) {
1069 case "-d":
1070 i++;
1071 display = Integer.parseInt(args[i]);
1072 break;
1073 case "-t":
1074 i++;
1075 delayMs = Integer.parseInt(args[i]);
1076 break;
1077 default:
1078 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1079 throw new IllegalArgumentException("key_code already set:"
1080 + keyCode);
1081 }
1082 keyCode = Integer.parseInt(args[i]);
1083 }
1084 i++;
1085 }
1086 } catch (Exception e) {
1087 writer.println("Invalid args:" + e);
1088 dumpHelp(writer);
1089 return;
1090 }
1091 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1092 writer.println("Missing key code or invalid keycode");
1093 dumpHelp(writer);
1094 return;
1095 }
1096 if (display != InputHalService.DISPLAY_MAIN
1097 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1098 writer.println("Invalid display:" + display);
1099 dumpHelp(writer);
1100 return;
1101 }
1102 if (delayMs < 0) {
1103 writer.println("Invalid delay:" + delayMs);
1104 dumpHelp(writer);
1105 return;
1106 }
1107 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1108 mCarInputService.onKeyEvent(keyDown, display);
1109 SystemClock.sleep(delayMs);
1110 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1111 mCarInputService.onKeyEvent(keyUp, display);
1112 writer.println("Succeeded");
1113 }
1114
Yao Chene33f07e2016-07-26 12:02:51 -07001115 private void forceDayNightMode(String arg, PrintWriter writer) {
1116 int mode;
1117 switch (arg) {
1118 case PARAM_DAY_MODE:
1119 mode = CarNightService.FORCED_DAY_MODE;
1120 break;
1121 case PARAM_NIGHT_MODE:
1122 mode = CarNightService.FORCED_NIGHT_MODE;
1123 break;
1124 case PARAM_SENSOR_MODE:
1125 mode = CarNightService.FORCED_SENSOR_MODE;
1126 break;
1127 default:
1128 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1129 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1130 return;
1131 }
1132 int current = mCarNightService.forceDayNightMode(mode);
1133 String currentMode = null;
1134 switch (current) {
1135 case UiModeManager.MODE_NIGHT_AUTO:
1136 currentMode = PARAM_SENSOR_MODE;
1137 break;
1138 case UiModeManager.MODE_NIGHT_YES:
1139 currentMode = PARAM_NIGHT_MODE;
1140 break;
1141 case UiModeManager.MODE_NIGHT_NO:
1142 currentMode = PARAM_DAY_MODE;
1143 break;
1144 }
1145 writer.println("DayNightMode changed to: " + currentMode);
1146 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001147
Bryan Eylera32a7c12018-02-27 15:40:00 -08001148 private void forceGarageMode(String arg, PrintWriter writer) {
1149 switch (arg) {
1150 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001151 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001152 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001153 break;
1154 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001155 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001156 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001157 break;
1158 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001159 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001160 break;
1161 default:
1162 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
1163 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001164 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001165 }
1166
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001167 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001168 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001169 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001170 * @param property the Vehicle property Id as defined in the HAL
1171 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001172 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001173 * @param value Data value of the event
1174 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001175 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001176 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001177 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001178 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1179 if (!isPropertyAreaTypeGlobal(property)) {
1180 writer.println("Property area type inconsistent with given zone");
1181 return;
1182 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001183 }
1184 try {
Kai4b098cf2019-06-05 15:03:28 -07001185 if (isErrorEvent) {
1186 mHal.injectOnPropertySetError(property, zone, value);
1187 } else {
1188 mHal.injectVhalEvent(property, zone, value);
1189 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001190 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001191 writer.println("Invalid property Id zone Id or value" + e);
1192 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001193 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001194 }
1195
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001196 // Check if the given property is global
1197 private boolean isPropertyAreaTypeGlobal(String property) {
1198 if (property == null) {
1199 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001200 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001201 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001202 }
Yao Chene33f07e2016-07-26 12:02:51 -07001203 }
kevinjm55822c42018-08-15 11:26:00 -07001204}