blob: bcdc64005eb740c090b9c3b0541d84ec48aec33c [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;
Felipe Leme6ef89652019-12-19 16:47:14 -080067import com.android.internal.annotations.VisibleForTesting;
Steve Paik1d8bea22018-02-03 01:54:06 +000068import com.android.internal.car.ICarServiceHelper;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070069import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070070
Enrico Granatae8056ca2018-04-03 13:19:52 -070071import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070072import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070073import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080074import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070075import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070076
keunyoungca515072015-07-10 12:21:47 -070077public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070078
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080079 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070080 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
81 "system_activity_monitoring";
Yan Zhub07585d2019-10-29 14:16:40 -070082 public static final String INTERNAL_VMS_MANAGER = "vms_manager";
Keun-young Parka28d7b22016-02-29 16:54:29 -080083
keunyoungca515072015-07-10 12:21:47 -070084 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080085 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070086
Keun young Park9a91efb2019-11-15 18:10:47 -080087 private final CarFeatureController mFeatureController;
88
Anthony Chen12aec302018-04-25 16:41:48 -070089 private final SystemInterface mSystemInterface;
90
Keun-young Park4727da32016-05-31 10:00:51 -070091 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070092 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080093 private final CarPackageManagerService mCarPackageManagerService;
94 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080095 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080096 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070097 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080098 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070099 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800100 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700101 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -0700102 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -0800103 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800104 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -0800105 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700106 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800107 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700108 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700109 private final CarDiagnosticService mCarDiagnosticService;
110 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700111 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700112 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800113 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700114 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800115 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700116 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700117 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800118 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800119 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800120 private final VmsSubscriberService mVmsSubscriberService;
121 private final VmsPublisherService mVmsPublisherService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700122 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700123 private final CarStatsService mCarStatsService;
Keun young Park9a91efb2019-11-15 18:10:47 -0800124 private final CarExperimentalFeatureServiceController mCarExperimentalFeatureServiceController;
keunyounga74b9ca2015-10-21 13:33:58 -0700125
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700126 private final CarServiceBase[] mAllServices;
127
Steve Paikc302c7c2017-08-04 14:01:58 -0700128 private static final String TAG = "ICarImpl";
129 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700130
131 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700132
keunyoung1ab8e182015-09-24 09:25:22 -0700133 /** Test only service. Populate it only when necessary. */
134 @GuardedBy("this")
135 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700136
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700137 @GuardedBy("this")
138 private ICarServiceHelper mICarServiceHelper;
139
Enrico Granatae8056ca2018-04-03 13:19:52 -0700140 private final String mVehicleInterfaceName;
141
Pavel Maltsevec83b632017-01-05 15:10:55 -0800142 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700143 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
Felipe Leme6ef89652019-12-19 16:47:14 -0800144 this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
145 /* carUserService= */ null);
146 }
147
148 @VisibleForTesting
149 ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
150 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
151 @Nullable CarUserService carUserService) {
keunyoungca515072015-07-10 12:21:47 -0700152 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800153 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700154 mHal = new VehicleHal(serviceContext, vehicle);
Keun young Park9a91efb2019-11-15 18:10:47 -0800155 Resources res = mContext.getResources();
156 String[] defaultEnabledFeatures = res.getStringArray(
157 R.array.config_allowed_optional_car_features);
158 // Do this before any other service components to allow feature check. It should work
159 // even without init.
160 // TODO (b/144504820) Add vhal plumbing
161 mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
162 /* disabledFeaturesFromVhal= */ new String[0], mSystemInterface.getSystemCarDir());
163 CarLocalServices.addService(CarFeatureController.class, mFeatureController);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700164 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800165 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Felipe Leme6ef89652019-12-19 16:47:14 -0800166 if (carUserService != null) {
167 mCarUserService = carUserService;
168 } else {
169 UserManager userManager =
170 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
171 int maxRunningUsers = res.getInteger(
172 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
173 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper, userManager,
174 ActivityManager.getService(), maxRunningUsers);
175 }
Keun young Parkbb877e22019-08-02 10:38:01 -0700176 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700177 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800178 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700179 systemInterface, mUserManagerHelper);
Keun young Park9a91efb2019-11-15 18:10:47 -0800180 if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
181 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
182 } else {
183 mCarUserNoticeService = null;
184 }
Steve Paik9ec53d72018-04-27 13:28:31 -0700185 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
186 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800187 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800188 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800189 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
190 mCarUXRestrictionsService,
Anthony Hughfbb67762019-10-15 12:54:54 -0700191 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800192 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Sal Savage703c46f2019-04-15 08:39:25 -0700193 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700194 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800195 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800196 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700197 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700198 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800199 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700200 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700201 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700202 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700203 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700204 mSystemStateControllerService = new SystemStateControllerService(
205 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700206 mCarStatsService = new CarStatsService(serviceContext);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800207 if (mFeatureController.isFeatureEnabled(Car.VMS_SUBSCRIBER_SERVICE)) {
208 mVmsBrokerService = new VmsBrokerService();
209 mVmsClientManager = new VmsClientManager(
210 // CarStatsService needs to be passed to the constructor due to HAL init order
211 serviceContext, mCarStatsService, mCarUserService, mVmsBrokerService,
212 mHal.getVmsHal());
213 mVmsSubscriberService = new VmsSubscriberService(
214 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
215 mVmsPublisherService = new VmsPublisherService(
216 serviceContext, mCarStatsService, mVmsBrokerService, mVmsClientManager);
217 } else {
218 mVmsBrokerService = null;
219 mVmsClientManager = null;
220 mVmsSubscriberService = null;
221 mVmsPublisherService = null;
222 }
223 if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
224 mCarDiagnosticService = new CarDiagnosticService(serviceContext,
225 mHal.getDiagnosticHal());
226 } else {
227 mCarDiagnosticService = null;
228 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800229 if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
230 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
231 systemInterface);
232 } else {
233 mCarStorageMonitoringService = null;
234 }
Anthony Chen12aec302018-04-25 16:41:48 -0700235 mCarConfigurationService =
236 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700237 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700238 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800239 mCarMediaService = new CarMediaService(serviceContext);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700240 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
Keun young Park9a91efb2019-11-15 18:10:47 -0800241 if (!Build.IS_USER) {
242 mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
243 serviceContext);
244 } else {
245 mCarExperimentalFeatureServiceController = null;
246 }
keunyounga74b9ca2015-10-21 13:33:58 -0700247
Keun young Parka5fa4782019-04-16 18:56:27 -0700248 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700249 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800250 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700251 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800252 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700253 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700254 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700255 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800256
keunyounga3b28d82015-08-25 13:05:15 -0700257 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700258 List<CarServiceBase> allServices = new ArrayList<>();
Keun young Park9a91efb2019-11-15 18:10:47 -0800259 allServices.add(mFeatureController);
Keun-young Parkd462a912019-02-11 08:53:42 -0800260 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700261 allServices.add(mSystemActivityMonitoringService);
262 allServices.add(mCarPowerManagementService);
263 allServices.add(mCarPropertyService);
264 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700265 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700266 allServices.add(mCarUXRestrictionsService);
267 allServices.add(mCarPackageManagerService);
268 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700269 allServices.add(mGarageModeService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800270 addServiceIfNonNull(allServices, mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700271 allServices.add(mAppFocusService);
272 allServices.add(mCarAudioService);
273 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700274 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700275 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700276 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700277 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800278 allServices.add(mCarBluetoothService);
279 allServices.add(mCarProjectionService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800280 addServiceIfNonNull(allServices, mCarDiagnosticService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800281 addServiceIfNonNull(allServices, mCarStorageMonitoringService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700282 allServices.add(mCarConfigurationService);
Keun young Parkc6d80af2020-01-17 18:14:28 -0800283 addServiceIfNonNull(allServices, mVmsClientManager);
284 addServiceIfNonNull(allServices, mVmsSubscriberService);
285 addServiceIfNonNull(allServices, mVmsPublisherService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700286 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800287 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700288 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700289 allServices.add(mCarBugreportManagerService);
Keun young Park9a91efb2019-11-15 18:10:47 -0800290 // Always put mCarExperimentalFeatureServiceController in last.
291 addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700292 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700293 }
294
Keun young Park9a91efb2019-11-15 18:10:47 -0800295 private void addServiceIfNonNull(List<CarServiceBase> services, CarServiceBase service) {
296 if (service != null) {
297 services.add(service);
298 }
299 }
300
Pavel Maltsevabd47232017-10-10 16:54:57 -0700301 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700302 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700303 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700304 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700305 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700306 traceEnd();
307 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800308 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700309 service.init();
310 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700311 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700312 }
313
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700314 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700315 // release done in opposite order from init
316 for (int i = mAllServices.length - 1; i >= 0; i--) {
317 mAllServices[i].release();
318 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700319 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700320 }
321
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700322 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800323 mHal.vehicleHalReconnected(vehicle);
324 for (CarServiceBase service : mAllServices) {
325 service.vehicleHalReconnected();
326 }
327 }
328
keunyoungca515072015-07-10 12:21:47 -0700329 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700330 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700331 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700332 synchronized (this) {
333 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800334 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700335 }
336 }
337
338 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700339 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700340 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700341 mCarUserService.setUserLockStatus(userId, unlocked == 1);
342 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700343 }
344
345 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700346 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700347 assertCallingFromSystemProcess();
348
Eric Jeong1545f3b2019-09-16 13:56:52 -0700349 Log.i(TAG, "Foreground user switched to " + userId);
350 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700351 }
352
Keun young Park9a91efb2019-11-15 18:10:47 -0800353 @Override
354 public boolean isFeatureEnabled(String featureName) {
355 return mFeatureController.isFeatureEnabled(featureName);
356 }
357
358 @Override
359 public int enableFeature(String featureName) {
360 // permission check inside the controller
361 return mFeatureController.enableFeature(featureName);
362 }
363
364 @Override
365 public int disableFeature(String featureName) {
366 // permission check inside the controller
367 return mFeatureController.disableFeature(featureName);
368 }
369
370 @Override
371 public List<String> getAllEnabledFeatures() {
372 // permission check inside the controller
373 return mFeatureController.getAllEnabledFeatures();
374 }
375
376 @Override
377 public List<String> getAllPendingDisabledFeatures() {
378 // permission check inside the controller
379 return mFeatureController.getAllPendingDisabledFeatures();
380 }
381
382 @Override
383 public List<String> getAllPendingEnabledFeatures() {
384 // permission check inside the controller
385 return mFeatureController.getAllPendingEnabledFeatures();
386 }
387
388 @Override
389 public String getCarManagerClassForFeature(String featureName) {
390 if (mCarExperimentalFeatureServiceController == null) {
391 return null;
392 }
393 return mCarExperimentalFeatureServiceController.getCarManagerClassForFeature(featureName);
394 }
395
Jim Kayee5133162019-04-22 12:50:27 -0700396 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800397 int uid = Binder.getCallingUid();
398 if (uid != Process.SYSTEM_UID) {
399 throw new SecurityException("Only allowed from system");
400 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800401 }
402
Keun young Parkaabecd92019-05-03 17:31:27 -0700403 /**
404 * Assert if binder call is coming from system process like system server or if it is called
405 * from its own process even if it is not system. The latter can happen in test environment.
406 * Note that car service runs as system user but test like car service test will not.
407 */
408 static void assertCallingFromSystemProcessOrSelf() {
409 int uid = Binder.getCallingUid();
410 int pid = Binder.getCallingPid();
411 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
412 throw new SecurityException("Only allowed from system or self");
413 }
414 }
415
Keun-young Parkd462a912019-02-11 08:53:42 -0800416 @Override
keunyoungca515072015-07-10 12:21:47 -0700417 public IBinder getCarService(String serviceName) {
Keun young Park9a91efb2019-11-15 18:10:47 -0800418 if (!mFeatureController.isFeatureEnabled(serviceName)) {
419 Log.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);
420 return null;
421 }
keunyoungca515072015-07-10 12:21:47 -0700422 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800423 case Car.AUDIO_SERVICE:
424 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700425 case Car.APP_FOCUS_SERVICE:
426 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800427 case Car.PACKAGE_SERVICE:
428 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800429 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700430 assertAnyDiagnosticPermission(mContext);
431 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800432 case Car.POWER_SERVICE:
433 assertPowerPermission(mContext);
434 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700435 case Car.CABIN_SERVICE:
436 case Car.HVAC_SERVICE:
437 case Car.INFO_SERVICE:
438 case Car.PROPERTY_SERVICE:
439 case Car.SENSOR_SERVICE:
440 case Car.VENDOR_EXTENSION_SERVICE:
441 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800442 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700443 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700444 IInstrumentClusterNavigation navService =
445 mInstrumentClusterService.getNavigationService();
446 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700447 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
448 assertClusterManagerPermission(mContext);
449 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800450 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800451 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800452 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700453 assertVmsSubscriberPermission(mContext);
454 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800455 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700456 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700457 synchronized (this) {
458 if (mCarTestService == null) {
459 mCarTestService = new CarTestService(mContext, this);
460 }
461 return mCarTestService;
462 }
463 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700464 case Car.BLUETOOTH_SERVICE:
465 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700466 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700467 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700468 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800469 case Car.CAR_DRIVING_STATE_SERVICE:
470 assertDrivingStatePermission(mContext);
471 return mCarDrivingStateService;
472 case Car.CAR_UX_RESTRICTION_SERVICE:
473 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700474 case Car.CAR_CONFIGURATION_SERVICE:
475 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800476 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
477 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700478 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800479 case Car.CAR_MEDIA_SERVICE:
480 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700481 case Car.CAR_OCCUPANT_ZONE_SERVICE:
482 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700483 case Car.CAR_BUGREPORT_SERVICE:
484 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700485 case Car.CAR_USER_SERVICE:
486 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700487 default:
Keun young Park9a91efb2019-11-15 18:10:47 -0800488 IBinder service = null;
489 if (mCarExperimentalFeatureServiceController != null) {
490 service = mCarExperimentalFeatureServiceController.getCarService(serviceName);
491 }
492 if (service == null) {
493 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"
494 + serviceName);
495 }
496 return service;
keunyoungca515072015-07-10 12:21:47 -0700497 }
498 }
499
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800500 @Override
501 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700502 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800503 }
504
Keun-young Parka28d7b22016-02-29 16:54:29 -0800505 public CarServiceBase getCarInternalService(String serviceName) {
506 switch (serviceName) {
507 case INTERNAL_INPUT_SERVICE:
508 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700509 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
510 return mSystemActivityMonitoringService;
Yan Zhub07585d2019-10-29 14:16:40 -0700511 // TODO(b/144027497): temporary until tests are refactored to not use it
512 case INTERNAL_VMS_MANAGER:
513 return mVmsClientManager;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800514 default:
515 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
516 serviceName);
517 return null;
518 }
519 }
520
Mark Tabryf7319ae2019-11-07 11:26:35 -0800521 CarStatsService getStatsService() {
522 return mCarStatsService;
523 }
524
keunyoung1ab8e182015-09-24 09:25:22 -0700525 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700526 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700527 }
528
Keun-young Parke31a8b22016-03-16 17:34:08 -0700529 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700530 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800531 }
532
Pavel Maltsev905968c2017-07-16 19:48:57 -0700533 public static void assertClusterManagerPermission(Context context) {
534 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
535 }
536
Steve Paik388d7772018-02-12 10:54:51 -0800537 public static void assertPowerPermission(Context context) {
538 assertPermission(context, Car.PERMISSION_CAR_POWER);
539 }
540
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800541 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700542 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
543 }
544
Pavel Maltsev079873b2019-02-25 12:15:09 -0800545 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
546 public static void assertProjectionStatusPermission(Context context) {
547 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
548 }
549
Enrico Granata3c7a6662017-02-23 18:07:59 -0800550 public static void assertAnyDiagnosticPermission(Context context) {
551 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700552 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800553 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
554 }
555
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800556 public static void assertDrivingStatePermission(Context context) {
557 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
558 }
559
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800560 public static void assertVmsPublisherPermission(Context context) {
561 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
562 }
563
Antonio Cortese4619c72017-02-02 07:53:27 -0800564 public static void assertVmsSubscriberPermission(Context context) {
565 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
566 }
567
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800568 /**
569 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800570 */
571 public static void assertTrustAgentEnrollmentPermission(Context context) {
572 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
573 }
574
Steve Paik461ecc62016-06-08 15:28:32 -0700575 public static void assertPermission(Context context, String permission) {
576 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
577 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800578 }
579 }
580
Steve Paik9ec53d72018-04-27 13:28:31 -0700581 /**
582 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700583 *
584 * @return boolean TRUE if caller has the permission.
585 */
586 public static boolean hasPermission(Context context, String permission) {
587 return context.checkCallingOrSelfPermission(permission)
588 == PackageManager.PERMISSION_GRANTED;
589 }
590
Enrico Granata3c7a6662017-02-23 18:07:59 -0800591 public static void assertAnyPermission(Context context, String... permissions) {
592 for (String permission : permissions) {
593 if (context.checkCallingOrSelfPermission(permission) ==
594 PackageManager.PERMISSION_GRANTED) {
595 return;
596 }
597 }
598 throw new SecurityException("requires any of " + Arrays.toString(permissions));
599 }
600
Enrico Granatae8056ca2018-04-03 13:19:52 -0700601 @Override
602 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
603 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700604 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700605 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700606 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
607 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700608 return;
keunyounga3b28d82015-08-25 13:05:15 -0700609 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700610
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700611 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
612 writer.println("*Dump car service*");
Enrico Granatae8056ca2018-04-03 13:19:52 -0700613 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700614
Mark Tabry7e2a7822019-10-17 10:18:32 -0700615 dumpAllServices(writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700616
Enrico Granatae8056ca2018-04-03 13:19:52 -0700617 writer.println("*Dump Vehicle HAL*");
618 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
619 try {
620 // TODO dump all feature flags by creating a dumpable interface
621 mHal.dump(writer);
622 } catch (Exception e) {
623 writer.println("Failed dumping: " + mHal.getClass().getName());
624 e.printStackTrace(writer);
625 }
felipeal080e5652019-11-02 17:04:04 -0700626 } else if ("--list".equals(args[0])) {
627 dumpListOfServices(writer);
628 return;
629 } else if ("--services".equals(args[0])) {
630 if (args.length < 2) {
631 writer.print("Must pass services to dump when using --services");
632 return;
633 }
634 int length = args.length - 1;
635 String[] services = new String[length];
636 System.arraycopy(args, 1, services, 0, length);
637 dumpIndividualServices(writer, services);
638 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700639 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700640 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
Mark Tabryc98f9952019-12-03 11:03:48 -0800641 // allowing for nested flag selection
Mark Tabry7e2a7822019-10-17 10:18:32 -0700642 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700643 } else if ("--vms-hal".equals(args[0])) {
644 mHal.getVmsHal().dumpMetrics(fd);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700645 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
646 execShellCmd(args, writer);
647 } else {
648 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800649 }
650 }
651
felipeal72bf6422019-11-02 17:04:04 -0700652 @Override
653 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
654 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
655 throws RemoteException {
656 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
657 }
658
felipeal080e5652019-11-02 17:04:04 -0700659 private void dumpListOfServices(PrintWriter writer) {
660 for (CarServiceBase service : mAllServices) {
661 writer.println(service.getClass().getName());
662 }
663 }
664
Mark Tabry7e2a7822019-10-17 10:18:32 -0700665 private void dumpAllServices(PrintWriter writer) {
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700666 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700667 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700668 }
669 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700670 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700671 }
felipeal080e5652019-11-02 17:04:04 -0700672 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700673
felipeal080e5652019-11-02 17:04:04 -0700674 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
675 for (String serviceName : serviceNames) {
676 writer.println("** Dumping " + serviceName + "\n");
677 CarServiceBase service = getCarServiceBySubstring(serviceName);
678 if (service == null) {
679 writer.println("No such service!");
680 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700681 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700682 }
683 writer.println();
684 }
685 }
686
687 @Nullable
688 private CarServiceBase getCarServiceBySubstring(String className) {
689 return Arrays.asList(mAllServices).stream()
690 .filter(s -> s.getClass().getSimpleName().equals(className))
691 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700692 }
693
Mark Tabry7e2a7822019-10-17 10:18:32 -0700694 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800695 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700696 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800697 } catch (Exception e) {
698 writer.println("Failed dumping: " + service.getClass().getName());
699 e.printStackTrace(writer);
700 }
keunyoungcc449f72015-08-12 10:46:27 -0700701 }
Yao Chene33f07e2016-07-26 12:02:51 -0700702
703 void execShellCmd(String[] args, PrintWriter writer) {
704 new CarShellCommand().exec(args, writer);
705 }
706
Pavel Maltsevabd47232017-10-10 16:54:57 -0700707 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700708 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700709 Slog.i(TAG, name);
710 mBootTiming.traceBegin(name);
711 }
712
Pavel Maltsevabd47232017-10-10 16:54:57 -0700713 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700714 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700715 mBootTiming.traceEnd();
716 }
717
felipeal72bf6422019-11-02 17:04:04 -0700718 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700719 private static final String COMMAND_HELP = "-h";
720 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800721 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700722 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800723 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800724 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800725 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800726 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700727 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700728 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800729 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700730 private static final String COMMAND_RESUME = "resume";
731 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700732 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
733 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700734 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700735 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
736 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Keun young Park9a91efb2019-11-15 18:10:47 -0800737 private static final String COMMAND_ENABLE_FEATURE = "enable-feature";
738 private static final String COMMAND_DISABLE_FEATURE = "disable-feature";
Keun young Park313dfaf2019-12-19 11:32:38 -0800739 private static final String COMMAND_INJECT_KEY = "inject-key";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800740
Yao Chene33f07e2016-07-26 12:02:51 -0700741 private static final String PARAM_DAY_MODE = "day";
742 private static final String PARAM_NIGHT_MODE = "night";
743 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800744 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800745 private static final String PARAM_ON_MODE = "on";
746 private static final String PARAM_OFF_MODE = "off";
747 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800748
felipeal72bf6422019-11-02 17:04:04 -0700749 private static final int RESULT_OK = 0;
750 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
751
752
753 @Override
754 public int onCommand(String cmd) {
755 if (cmd == null) {
756 onHelp();
757 return RESULT_ERROR;
758 }
759 ArrayList<String> argsList = new ArrayList<>();
760 argsList.add(cmd);
761 String arg = null;
762 do {
763 arg = getNextArg();
764 if (arg != null) {
765 argsList.add(arg);
766 }
767 } while (arg != null);
768 String[] args = new String[argsList.size()];
769 argsList.toArray(args);
770 return exec(args, getOutPrintWriter());
771 }
772
773 @Override
774 public void onHelp() {
775 dumpHelp(getOutPrintWriter());
776 }
Yao Chene33f07e2016-07-26 12:02:51 -0700777
778 private void dumpHelp(PrintWriter pw) {
779 pw.println("Car service commands:");
780 pw.println("\t-h");
781 pw.println("\t Print this help text.");
782 pw.println("\tday-night-mode [day|night|sensor]");
783 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800784 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800785 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700786 pw.println("\tinject-error-event property zone errorCode");
787 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700788 pw.println("\tenable-uxr true|false");
789 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800790 pw.println("\tgarage-mode [on|off|query]");
791 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800792 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800793 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800794 pw.println("\tget-carpropertyconfig [propertyId]");
795 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700796 pw.println("\tget-property-value [propertyId] [areaId]");
797 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
798 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700799 pw.println("\tsuspend");
800 pw.println("\t Suspend the system to Deep Sleep.");
801 pw.println("\tresume");
802 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700803 pw.println("\tenable-trusted-device true|false");
804 pw.println("\t Enable/Disable Trusted device feature.");
805 pw.println("\tremove-trusted-devices");
806 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700807 pw.println("\tprojection-tethering [true|false]");
808 pw.println("\t Whether tethering should be used when creating access point for"
809 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700810 pw.println("\t--metrics");
811 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700812 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
813 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700814 pw.println("\tstart-fixed-activity displayId packageName activityName");
815 pw.println("\t Start an Activity the specified display as fixed mode");
816 pw.println("\tstop-fixed-mode displayId");
817 pw.println("\t Stop fixed Activity mode for the given display. "
818 + "The Activity will not be restarted upon crash.");
Keun young Park9a91efb2019-11-15 18:10:47 -0800819 pw.println("\tenable-feature featureName");
820 pw.println("\t Enable the requested feature. Change will happen after reboot.");
821 pw.println("\t This requires root/su.");
822 pw.println("\tdisable-feature featureName");
823 pw.println("\t Disable the requested feature. Change will happen after reboot");
824 pw.println("\t This requires root/su.");
Keun young Park313dfaf2019-12-19 11:32:38 -0800825 pw.println("\tinject-key [-d display] [-t down_delay_ms] key_code");
826 pw.println("\t inject key down / up event to car service");
827 pw.println("\t display: 0 for main, 1 for cluster. If not specified, it will be 0.");
828 pw.println("\t down_delay_ms: delay from down to up key event. If not specified,");
829 pw.println("\t it will be 0");
830 pw.println("\t key_code: int key code defined in android KeyEvent");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700831 }
832
felipeal72bf6422019-11-02 17:04:04 -0700833 private int dumpInvalidArguments(PrintWriter pw) {
834 pw.println("Incorrect number of arguments.");
835 dumpHelp(pw);
836 return RESULT_ERROR;
837 }
838
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700839 private String runSetZoneIdForUid(String zoneString, String uidString) {
840 int uid = Integer.parseInt(uidString);
841 int zoneId = Integer.parseInt(zoneString);
842 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
843 return "zoneid " + zoneId + " not found";
844 }
845 mCarAudioService.setZoneIdForUid(zoneId, uid);
846 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700847 }
848
felipeal72bf6422019-11-02 17:04:04 -0700849 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700850 String arg = args[0];
851 switch (arg) {
852 case COMMAND_HELP:
853 dumpHelp(writer);
854 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800855 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800856 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700857 forceDayNightMode(value, writer);
858 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800859 }
860 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800861 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800862 forceGarageMode(value, writer);
863 break;
864 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800865 case COMMAND_INJECT_VHAL_EVENT:
866 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
867 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800868 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700869 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800870 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800871 // Zoned
872 zone = args[2];
873 data = args[3];
874 } else {
875 // Global
876 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800877 }
Kai4b098cf2019-06-05 15:03:28 -0700878 injectVhalEvent(args[1], zone, data, false, writer);
879 break;
880 case COMMAND_INJECT_ERROR_EVENT:
881 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700882 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700883 }
884 String errorAreaId = args[2];
885 String errorCode = args[3];
886 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800887 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800888 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800889 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700890 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800891 }
892 boolean enableBlocking = Boolean.valueOf(args[1]);
893 if (mCarPackageManagerService != null) {
894 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
895 }
896 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800897 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800898 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700899 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800900 }
901 String pkgName = args[1].toLowerCase();
902 if (mCarPackageManagerService != null) {
903 String[] doActivities =
904 mCarPackageManagerService.getDistractionOptimizedActivities(
905 pkgName);
906 if (doActivities != null) {
907 writer.println("DO Activities for " + pkgName);
908 for (String a : doActivities) {
909 writer.println(a);
910 }
911 } else {
912 writer.println("No DO Activities for " + pkgName);
913 }
914 }
915 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800916 case COMMAND_GET_CARPROPERTYCONFIG:
917 String propertyId = args.length < 2 ? "" : args[1];
918 mHal.dumpPropertyConfigs(writer, propertyId);
919 break;
Kai29fba112019-04-22 18:37:38 -0700920 case COMMAND_GET_PROPERTY_VALUE:
921 String propId = args.length < 2 ? "" : args[1];
922 String areaId = args.length < 3 ? "" : args[2];
923 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
924 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800925 case COMMAND_PROJECTION_UI_MODE:
926 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700927 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -0800928 }
929 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700930 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700931 case COMMAND_PROJECTION_AP_TETHERING:
932 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700933 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700934 }
935 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
936 break;
Jim Kayed76c2742019-04-02 11:33:27 -0700937 case COMMAND_RESUME:
938 mCarPowerManagementService.forceSimulatedResume();
939 writer.println("Resume: Simulating resuming from Deep Sleep");
940 break;
941 case COMMAND_SUSPEND:
942 mCarPowerManagementService.forceSimulatedSuspend();
943 writer.println("Resume: Simulating powering down to Deep Sleep");
944 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700945 case COMMAND_ENABLE_TRUSTED_DEVICE:
946 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700947 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700948 }
949 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
950 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
951 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
952 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
953 break;
954 case COMMAND_REMOVE_TRUSTED_DEVICES:
955 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -0700956 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700957 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700958 case COMMAND_SET_UID_TO_ZONE:
959 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -0700960 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700961 }
962 String results = runSetZoneIdForUid(args[1], args[2]);
963 if (results != null) {
964 writer.println(results);
965 dumpHelp(writer);
966 }
967 break;
Keun young Park09ca8492019-10-15 10:34:11 -0700968 case COMMAND_START_FIXED_ACTIVITY_MODE:
969 handleStartFixedActivity(args, writer);
970 break;
971 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
972 handleStopFixedMode(args, writer);
973 break;
Keun young Park9a91efb2019-11-15 18:10:47 -0800974 case COMMAND_ENABLE_FEATURE:
975 if (args.length != 2) {
976 return dumpInvalidArguments(writer);
977 }
978 handleEnableDisableFeature(args, writer, /* enable= */ true);
979 break;
980 case COMMAND_DISABLE_FEATURE:
981 if (args.length != 2) {
982 return dumpInvalidArguments(writer);
983 }
984 handleEnableDisableFeature(args, writer, /* enable= */ false);
985 break;
Keun young Park313dfaf2019-12-19 11:32:38 -0800986 case COMMAND_INJECT_KEY:
987 if (args.length < 2) {
988 return dumpInvalidArguments(writer);
989 }
990 handleInjectKey(args, writer);
991 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700992 default:
Jim Kayef10b7d82019-02-26 12:53:22 -0800993 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -0700994 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -0700995 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -0700996 }
felipeal72bf6422019-11-02 17:04:04 -0700997 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -0700998 }
999
Keun young Park09ca8492019-10-15 10:34:11 -07001000 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
1001 if (args.length != 4) {
1002 writer.println("Incorrect number of arguments");
1003 dumpHelp(writer);
1004 return;
1005 }
1006 int displayId;
1007 try {
1008 displayId = Integer.parseInt(args[1]);
1009 } catch (NumberFormatException e) {
1010 writer.println("Wrong display id:" + args[1]);
1011 return;
1012 }
1013 String packageName = args[2];
1014 String activityName = args[3];
1015 Intent intent = new Intent();
1016 intent.setComponent(new ComponentName(packageName, activityName));
1017 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1018 ActivityOptions options = ActivityOptions.makeBasic();
1019 options.setLaunchDisplayId(displayId);
1020 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
1021 displayId, ActivityManager.getCurrentUser())) {
1022 writer.println("Failed to start");
1023 return;
1024 }
1025 writer.println("Succeeded");
1026 }
1027
1028 private void handleStopFixedMode(String[] args, PrintWriter writer) {
1029 if (args.length != 2) {
1030 writer.println("Incorrect number of arguments");
1031 dumpHelp(writer);
1032 return;
1033 }
1034 int displayId;
1035 try {
1036 displayId = Integer.parseInt(args[1]);
1037 } catch (NumberFormatException e) {
1038 writer.println("Wrong display id:" + args[1]);
1039 return;
1040 }
1041 mFixedActivityService.stopFixedActivityMode(displayId);
1042 }
1043
Keun young Park9a91efb2019-11-15 18:10:47 -08001044 private void handleEnableDisableFeature(String[] args, PrintWriter writer, boolean enable) {
1045 if (Binder.getCallingUid() != Process.ROOT_UID) {
1046 writer.println("Only allowed to root/su");
1047 return;
1048 }
1049 String featureName = args[1];
1050 long id = Binder.clearCallingIdentity();
1051 // no permission check here
1052 int r;
1053 if (enable) {
1054 r = mFeatureController.enableFeature(featureName);
1055 } else {
1056 r = mFeatureController.disableFeature(featureName);
1057 }
1058 switch (r) {
1059 case Car.FEATURE_REQUEST_SUCCESS:
1060 if (enable) {
1061 writer.println("Enabled feature:" + featureName);
1062 } else {
1063 writer.println("Disabled feature:" + featureName);
1064 }
1065 break;
1066 case Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE:
1067 if (enable) {
1068 writer.println("Already enabled:" + featureName);
1069 } else {
1070 writer.println("Already disabled:" + featureName);
1071 }
1072 break;
1073 case Car.FEATURE_REQUEST_MANDATORY:
1074 writer.println("Cannot change mandatory feature:" + featureName);
1075 break;
1076 case Car.FEATURE_REQUEST_NOT_EXISTING:
1077 writer.println("Non-existing feature:" + featureName);
1078 break;
1079 default:
1080 writer.println("Unknown error:" + r);
1081 break;
1082 }
1083 Binder.restoreCallingIdentity(id);
1084 }
1085
Keun young Park313dfaf2019-12-19 11:32:38 -08001086 private void handleInjectKey(String[] args, PrintWriter writer) {
1087 int i = 1; // 0 is command itself
1088 int display = InputHalService.DISPLAY_MAIN;
1089 int delayMs = 0;
1090 int keyCode = KeyEvent.KEYCODE_UNKNOWN;
1091 try {
1092 while (i < args.length) {
1093 switch (args[i]) {
1094 case "-d":
1095 i++;
1096 display = Integer.parseInt(args[i]);
1097 break;
1098 case "-t":
1099 i++;
1100 delayMs = Integer.parseInt(args[i]);
1101 break;
1102 default:
1103 if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
1104 throw new IllegalArgumentException("key_code already set:"
1105 + keyCode);
1106 }
1107 keyCode = Integer.parseInt(args[i]);
1108 }
1109 i++;
1110 }
1111 } catch (Exception e) {
1112 writer.println("Invalid args:" + e);
1113 dumpHelp(writer);
1114 return;
1115 }
1116 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
1117 writer.println("Missing key code or invalid keycode");
1118 dumpHelp(writer);
1119 return;
1120 }
1121 if (display != InputHalService.DISPLAY_MAIN
1122 && display != InputHalService.DISPLAY_INSTRUMENT_CLUSTER) {
1123 writer.println("Invalid display:" + display);
1124 dumpHelp(writer);
1125 return;
1126 }
1127 if (delayMs < 0) {
1128 writer.println("Invalid delay:" + delayMs);
1129 dumpHelp(writer);
1130 return;
1131 }
1132 KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
1133 mCarInputService.onKeyEvent(keyDown, display);
1134 SystemClock.sleep(delayMs);
1135 KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
1136 mCarInputService.onKeyEvent(keyUp, display);
1137 writer.println("Succeeded");
1138 }
1139
Yao Chene33f07e2016-07-26 12:02:51 -07001140 private void forceDayNightMode(String arg, PrintWriter writer) {
1141 int mode;
1142 switch (arg) {
1143 case PARAM_DAY_MODE:
1144 mode = CarNightService.FORCED_DAY_MODE;
1145 break;
1146 case PARAM_NIGHT_MODE:
1147 mode = CarNightService.FORCED_NIGHT_MODE;
1148 break;
1149 case PARAM_SENSOR_MODE:
1150 mode = CarNightService.FORCED_SENSOR_MODE;
1151 break;
1152 default:
1153 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
1154 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
1155 return;
1156 }
1157 int current = mCarNightService.forceDayNightMode(mode);
1158 String currentMode = null;
1159 switch (current) {
1160 case UiModeManager.MODE_NIGHT_AUTO:
1161 currentMode = PARAM_SENSOR_MODE;
1162 break;
1163 case UiModeManager.MODE_NIGHT_YES:
1164 currentMode = PARAM_NIGHT_MODE;
1165 break;
1166 case UiModeManager.MODE_NIGHT_NO:
1167 currentMode = PARAM_DAY_MODE;
1168 break;
1169 }
1170 writer.println("DayNightMode changed to: " + currentMode);
1171 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001172
Bryan Eylera32a7c12018-02-27 15:40:00 -08001173 private void forceGarageMode(String arg, PrintWriter writer) {
1174 switch (arg) {
1175 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001176 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001177 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001178 break;
1179 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -07001180 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -07001181 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -08001182 break;
1183 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -07001184 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001185 break;
1186 default:
1187 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
1188 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
Bryan Eylera32a7c12018-02-27 15:40:00 -08001189 }
Bryan Eylera32a7c12018-02-27 15:40:00 -08001190 }
1191
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001192 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001193 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001194 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001195 * @param property the Vehicle property Id as defined in the HAL
1196 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -07001197 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001198 * @param value Data value of the event
1199 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001200 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001201 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -07001202 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001203 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
1204 if (!isPropertyAreaTypeGlobal(property)) {
1205 writer.println("Property area type inconsistent with given zone");
1206 return;
1207 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001208 }
1209 try {
Kai4b098cf2019-06-05 15:03:28 -07001210 if (isErrorEvent) {
1211 mHal.injectOnPropertySetError(property, zone, value);
1212 } else {
1213 mHal.injectVhalEvent(property, zone, value);
1214 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001215 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001216 writer.println("Invalid property Id zone Id or value" + e);
1217 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001218 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -08001219 }
1220
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001221 // Check if the given property is global
1222 private boolean isPropertyAreaTypeGlobal(String property) {
1223 if (property == null) {
1224 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001225 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -08001226 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -08001227 }
Yao Chene33f07e2016-07-26 12:02:51 -07001228 }
kevinjm55822c42018-08-15 11:26:00 -07001229}