blob: 2de5d5ca6238a981139480ad1a84bba962f6138a [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;
25import android.car.ICar;
Pavel Maltsev0477e292016-05-27 12:22:36 -070026import android.car.cluster.renderer.IInstrumentClusterNavigation;
Ying Zheng9fc99402018-09-19 14:23:59 -070027import android.car.userlib.CarUserManagerHelper;
Keun young Park09ca8492019-10-15 10:34:11 -070028import android.content.ComponentName;
keunyoungca515072015-07-10 12:21:47 -070029import android.content.Context;
Keun young Park09ca8492019-10-15 10:34:11 -070030import android.content.Intent;
keunyoung1ab8e182015-09-24 09:25:22 -070031import android.content.pm.PackageManager;
Keun young Parkf3523cd2019-04-08 10:09:17 -070032import android.content.res.Resources;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080033import android.hardware.automotive.vehicle.V2_0.IVehicle;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080034import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070035import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070036import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070037import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070038import android.os.Process;
felipeal72bf6422019-11-02 17:04:04 -070039import android.os.RemoteException;
40import android.os.ResultReceiver;
41import android.os.ShellCallback;
42import android.os.ShellCommand;
Steve Paikc302c7c2017-08-04 14:01:58 -070043import android.os.Trace;
Anthony Hugh9932a252019-06-12 16:19:56 -070044import android.os.UserManager;
keunyoungca515072015-07-10 12:21:47 -070045import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070046import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070047import android.util.TimingsTraceLog;
Gregory Clark26fa6012018-03-14 18:38:56 -070048
Keun young Parka4d12202019-10-03 10:51:12 -070049import com.android.car.am.FixedActivityService;
Hongwei Wang38549092018-08-22 12:32:42 -070050import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080051import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070052import com.android.car.garagemode.GarageModeService;
keunyoungcc449f72015-08-12 10:46:27 -070053import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080054import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080055import com.android.car.pm.CarPackageManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -070056import com.android.car.stats.CarStatsService;
Enrico Granatab19bc322017-10-12 12:25:06 -070057import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070058import com.android.car.trust.CarTrustedDeviceService;
Keun young Park7af7d6c2019-09-12 10:31:48 -070059import com.android.car.user.CarUserNoticeService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070060import com.android.car.user.CarUserService;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080061import com.android.car.vms.VmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080062import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070063import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000064import com.android.internal.car.ICarServiceHelper;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -070065import com.android.internal.util.ArrayUtils;
Gregory Clark26fa6012018-03-14 18:38:56 -070066
Enrico Granatae8056ca2018-04-03 13:19:52 -070067import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070068import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070069import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080070import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070071import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070072
keunyoungca515072015-07-10 12:21:47 -070073public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070074
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080075 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070076 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
77 "system_activity_monitoring";
Yan Zhub07585d2019-10-29 14:16:40 -070078 public static final String INTERNAL_VMS_MANAGER = "vms_manager";
Keun-young Parka28d7b22016-02-29 16:54:29 -080079
keunyoungca515072015-07-10 12:21:47 -070080 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080081 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070082
Anthony Chen12aec302018-04-25 16:41:48 -070083 private final SystemInterface mSystemInterface;
84
Keun-young Park4727da32016-05-31 10:00:51 -070085 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070086 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080087 private final CarPackageManagerService mCarPackageManagerService;
88 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080089 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080090 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070091 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080092 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070093 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080094 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070095 private final AppFocusService mAppFocusService;
Keun young Parka4d12202019-10-03 10:51:12 -070096 private final FixedActivityService mFixedActivityService;
Yao Chen3a7976d2016-01-20 17:27:08 -080097 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080098 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080099 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -0700100 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800101 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700102 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -0700103 private final CarDiagnosticService mCarDiagnosticService;
104 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -0700105 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700106 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -0800107 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700108 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -0800109 private final CarUserService mCarUserService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700110 private final CarOccupantZoneService mCarOccupantZoneService;
Keun young Park7af7d6c2019-09-12 10:31:48 -0700111 private final CarUserNoticeService mCarUserNoticeService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800112 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800113 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -0800114 private final VmsSubscriberService mVmsSubscriberService;
115 private final VmsPublisherService mVmsPublisherService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700116 private final CarBugreportManagerService mCarBugreportManagerService;
Mark Tabry7e2a7822019-10-17 10:18:32 -0700117 private final CarStatsService mCarStatsService;
keunyounga74b9ca2015-10-21 13:33:58 -0700118
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700119 private final CarServiceBase[] mAllServices;
120
Steve Paikc302c7c2017-08-04 14:01:58 -0700121 private static final String TAG = "ICarImpl";
122 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700123
124 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700125
keunyoung1ab8e182015-09-24 09:25:22 -0700126 /** Test only service. Populate it only when necessary. */
127 @GuardedBy("this")
128 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700129
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700130 @GuardedBy("this")
131 private ICarServiceHelper mICarServiceHelper;
132
Enrico Granatae8056ca2018-04-03 13:19:52 -0700133 private final String mVehicleInterfaceName;
134
Pavel Maltsevec83b632017-01-05 15:10:55 -0800135 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700136 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700137 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800138 mSystemInterface = systemInterface;
Mark Tabryb588d2e2019-09-12 10:50:11 -0700139 mHal = new VehicleHal(serviceContext, vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700140 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800141 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Anthony Hugh9932a252019-06-12 16:19:56 -0700142 UserManager userManager =
143 (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
Keun young Parkf3523cd2019-04-08 10:09:17 -0700144 final Resources res = mContext.getResources();
145 final int maxRunningUsers = res.getInteger(
146 com.android.internal.R.integer.config_multiuserMaxRunningUsers);
Eric Jeong3a793b02019-09-30 16:12:53 -0700147 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper, userManager,
Keun young Parkf3523cd2019-04-08 10:09:17 -0700148 ActivityManager.getService(), maxRunningUsers);
Keun young Parkbb877e22019-08-02 10:38:01 -0700149 mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700150 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800151 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700152 systemInterface, mUserManagerHelper);
Keun young Park7af7d6c2019-09-12 10:31:48 -0700153 mCarUserNoticeService = new CarUserNoticeService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700154 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
155 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800156 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800157 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800158 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
159 mCarUXRestrictionsService,
Anthony Hughfbb67762019-10-15 12:54:54 -0700160 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800161 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Sal Savage703c46f2019-04-15 08:39:25 -0700162 mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700163 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800164 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800165 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700166 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700167 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800168 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700169 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Keun young Parka4d12202019-10-03 10:51:12 -0700170 mFixedActivityService = new FixedActivityService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700171 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700172 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700173 mSystemStateControllerService = new SystemStateControllerService(
174 serviceContext, mCarAudioService, this);
Mark Tabry7e2a7822019-10-17 10:18:32 -0700175 mCarStatsService = new CarStatsService(serviceContext);
Mark Tabry61a06c72019-06-17 10:37:49 -0700176 mVmsBrokerService = new VmsBrokerService();
Mark Tabry1534c062019-03-29 01:30:49 -0700177 mVmsClientManager = new VmsClientManager(
Mark Tabry7e2a7822019-10-17 10:18:32 -0700178 // CarStatsService needs to be passed to the constructor due to HAL init order
179 serviceContext, mCarStatsService, mCarUserService, mVmsBrokerService,
180 mHal.getVmsHal());
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800181 mVmsSubscriberService = new VmsSubscriberService(
Mark Tabry61a06c72019-06-17 10:37:49 -0700182 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800183 mVmsPublisherService = new VmsPublisherService(
Mark Tabry7e2a7822019-10-17 10:18:32 -0700184 serviceContext, mCarStatsService, mVmsBrokerService, mVmsClientManager);
Enrico Granatab3634e22017-05-05 18:02:31 -0700185 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700186 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
187 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700188 mCarConfigurationService =
189 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Anthony Hugh5f2465e2019-10-08 15:07:01 -0700190 mCarLocationService = new CarLocationService(serviceContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700191 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800192 mCarMediaService = new CarMediaService(serviceContext);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700193 mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700194
Keun young Parka5fa4782019-04-16 18:56:27 -0700195 CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700196 CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800197 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700198 CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800199 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Gregory Clark18a0ef92019-05-23 20:00:49 -0700200 CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
Gregory Clark55620c12019-06-03 17:42:58 -0700201 CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
Keun young Parka4d12202019-10-03 10:51:12 -0700202 CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
Keun-young Parkd462a912019-02-11 08:53:42 -0800203
keunyounga3b28d82015-08-25 13:05:15 -0700204 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700205 List<CarServiceBase> allServices = new ArrayList<>();
Keun-young Parkd462a912019-02-11 08:53:42 -0800206 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700207 allServices.add(mSystemActivityMonitoringService);
208 allServices.add(mCarPowerManagementService);
209 allServices.add(mCarPropertyService);
210 allServices.add(mCarDrivingStateService);
Keun young Parkbb877e22019-08-02 10:38:01 -0700211 allServices.add(mCarOccupantZoneService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700212 allServices.add(mCarUXRestrictionsService);
213 allServices.add(mCarPackageManagerService);
214 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700215 allServices.add(mGarageModeService);
Keun young Park7af7d6c2019-09-12 10:31:48 -0700216 allServices.add(mCarUserNoticeService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700217 allServices.add(mAppFocusService);
218 allServices.add(mCarAudioService);
219 allServices.add(mCarNightService);
Keun young Parka4d12202019-10-03 10:51:12 -0700220 allServices.add(mFixedActivityService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700221 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700222 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700223 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800224 allServices.add(mCarBluetoothService);
225 allServices.add(mCarProjectionService);
226 allServices.add(mCarDiagnosticService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700227 allServices.add(mCarStorageMonitoringService);
228 allServices.add(mCarConfigurationService);
Mark Tabrya8fce562019-01-16 16:24:01 -0800229 allServices.add(mVmsClientManager);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700230 allServices.add(mVmsSubscriberService);
231 allServices.add(mVmsPublisherService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700232 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800233 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700234 allServices.add(mCarLocationService);
Selim Gurunc8c82f62019-04-17 14:07:30 -0700235 allServices.add(mCarBugreportManagerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700236 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700237 }
238
Pavel Maltsevabd47232017-10-10 16:54:57 -0700239 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700240 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700241 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700242 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700243 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700244 traceEnd();
245 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800246 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700247 service.init();
248 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700249 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700250 }
251
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700252 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700253 // release done in opposite order from init
254 for (int i = mAllServices.length - 1; i >= 0; i--) {
255 mAllServices[i].release();
256 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700257 mHal.release();
Keun-young Parkd462a912019-02-11 08:53:42 -0800258 CarLocalServices.removeAllServices();
keunyoung1ab8e182015-09-24 09:25:22 -0700259 }
260
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700261 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800262 mHal.vehicleHalReconnected(vehicle);
263 for (CarServiceBase service : mAllServices) {
264 service.vehicleHalReconnected();
265 }
266 }
267
keunyoungca515072015-07-10 12:21:47 -0700268 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700269 public void setCarServiceHelper(IBinder helper) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700270 assertCallingFromSystemProcess();
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700271 synchronized (this) {
272 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800273 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700274 }
275 }
276
277 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700278 public void setUserLockStatus(int userId, int unlocked) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700279 assertCallingFromSystemProcess();
Eric Jeong1545f3b2019-09-16 13:56:52 -0700280 mCarUserService.setUserLockStatus(userId, unlocked == 1);
281 mCarMediaService.setUserLockStatus(userId, unlocked == 1);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700282 }
283
284 @Override
Eric Jeong1545f3b2019-09-16 13:56:52 -0700285 public void onSwitchUser(int userId) {
Pavel Maltsev17e81832019-04-04 14:38:41 -0700286 assertCallingFromSystemProcess();
287
Eric Jeong1545f3b2019-09-16 13:56:52 -0700288 Log.i(TAG, "Foreground user switched to " + userId);
289 mCarUserService.onSwitchUser(userId);
Pavel Maltsev17e81832019-04-04 14:38:41 -0700290 }
291
Jim Kayee5133162019-04-22 12:50:27 -0700292 static void assertCallingFromSystemProcess() {
Keun-young Parkd462a912019-02-11 08:53:42 -0800293 int uid = Binder.getCallingUid();
294 if (uid != Process.SYSTEM_UID) {
295 throw new SecurityException("Only allowed from system");
296 }
Keun-young Parkd462a912019-02-11 08:53:42 -0800297 }
298
Keun young Parkaabecd92019-05-03 17:31:27 -0700299 /**
300 * Assert if binder call is coming from system process like system server or if it is called
301 * from its own process even if it is not system. The latter can happen in test environment.
302 * Note that car service runs as system user but test like car service test will not.
303 */
304 static void assertCallingFromSystemProcessOrSelf() {
305 int uid = Binder.getCallingUid();
306 int pid = Binder.getCallingPid();
307 if (uid != Process.SYSTEM_UID && pid != Process.myPid()) {
308 throw new SecurityException("Only allowed from system or self");
309 }
310 }
311
Keun-young Parkd462a912019-02-11 08:53:42 -0800312 @Override
keunyoungca515072015-07-10 12:21:47 -0700313 public IBinder getCarService(String serviceName) {
314 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800315 case Car.AUDIO_SERVICE:
316 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700317 case Car.APP_FOCUS_SERVICE:
318 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800319 case Car.PACKAGE_SERVICE:
320 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800321 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700322 assertAnyDiagnosticPermission(mContext);
323 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800324 case Car.POWER_SERVICE:
325 assertPowerPermission(mContext);
326 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700327 case Car.CABIN_SERVICE:
328 case Car.HVAC_SERVICE:
329 case Car.INFO_SERVICE:
330 case Car.PROPERTY_SERVICE:
331 case Car.SENSOR_SERVICE:
332 case Car.VENDOR_EXTENSION_SERVICE:
333 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800334 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700335 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700336 IInstrumentClusterNavigation navService =
337 mInstrumentClusterService.getNavigationService();
338 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700339 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
340 assertClusterManagerPermission(mContext);
341 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800342 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800343 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800344 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700345 assertVmsSubscriberPermission(mContext);
346 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800347 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700348 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700349 synchronized (this) {
350 if (mCarTestService == null) {
351 mCarTestService = new CarTestService(mContext, this);
352 }
353 return mCarTestService;
354 }
355 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700356 case Car.BLUETOOTH_SERVICE:
357 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700358 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700359 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700360 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800361 case Car.CAR_DRIVING_STATE_SERVICE:
362 assertDrivingStatePermission(mContext);
363 return mCarDrivingStateService;
364 case Car.CAR_UX_RESTRICTION_SERVICE:
365 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700366 case Car.CAR_CONFIGURATION_SERVICE:
367 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800368 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
369 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700370 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800371 case Car.CAR_MEDIA_SERVICE:
372 return mCarMediaService;
Keun young Parkbb877e22019-08-02 10:38:01 -0700373 case Car.CAR_OCCUPANT_ZONE_SERVICE:
374 return mCarOccupantZoneService;
Selim Gurunc8c82f62019-04-17 14:07:30 -0700375 case Car.CAR_BUGREPORT_SERVICE:
376 return mCarBugreportManagerService;
Eric Jeong1545f3b2019-09-16 13:56:52 -0700377 case Car.CAR_USER_SERVICE:
378 return mCarUserService;
keunyoungca515072015-07-10 12:21:47 -0700379 default:
380 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
381 return null;
382 }
383 }
384
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800385 @Override
386 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700387 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800388 }
389
Keun-young Parka28d7b22016-02-29 16:54:29 -0800390 public CarServiceBase getCarInternalService(String serviceName) {
391 switch (serviceName) {
392 case INTERNAL_INPUT_SERVICE:
393 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700394 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
395 return mSystemActivityMonitoringService;
Yan Zhub07585d2019-10-29 14:16:40 -0700396 // TODO(b/144027497): temporary until tests are refactored to not use it
397 case INTERNAL_VMS_MANAGER:
398 return mVmsClientManager;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800399 default:
400 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
401 serviceName);
402 return null;
403 }
404 }
405
keunyoung1ab8e182015-09-24 09:25:22 -0700406 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700407 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700408 }
409
Keun-young Parke31a8b22016-03-16 17:34:08 -0700410 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700411 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800412 }
413
Pavel Maltsev905968c2017-07-16 19:48:57 -0700414 public static void assertClusterManagerPermission(Context context) {
415 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
416 }
417
Steve Paik388d7772018-02-12 10:54:51 -0800418 public static void assertPowerPermission(Context context) {
419 assertPermission(context, Car.PERMISSION_CAR_POWER);
420 }
421
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800422 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700423 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
424 }
425
Pavel Maltsev079873b2019-02-25 12:15:09 -0800426 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
427 public static void assertProjectionStatusPermission(Context context) {
428 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
429 }
430
Enrico Granata3c7a6662017-02-23 18:07:59 -0800431 public static void assertAnyDiagnosticPermission(Context context) {
432 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700433 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800434 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
435 }
436
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800437 public static void assertDrivingStatePermission(Context context) {
438 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
439 }
440
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800441 public static void assertVmsPublisherPermission(Context context) {
442 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
443 }
444
Antonio Cortese4619c72017-02-02 07:53:27 -0800445 public static void assertVmsSubscriberPermission(Context context) {
446 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
447 }
448
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800449 /**
450 * Ensures the caller has the permission to enroll a Trust Agent.
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800451 */
452 public static void assertTrustAgentEnrollmentPermission(Context context) {
453 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
454 }
455
Steve Paik461ecc62016-06-08 15:28:32 -0700456 public static void assertPermission(Context context, String permission) {
457 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
458 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800459 }
460 }
461
Steve Paik9ec53d72018-04-27 13:28:31 -0700462 /**
463 * Checks to see if the caller has a permission.
Steve Paik9ec53d72018-04-27 13:28:31 -0700464 *
465 * @return boolean TRUE if caller has the permission.
466 */
467 public static boolean hasPermission(Context context, String permission) {
468 return context.checkCallingOrSelfPermission(permission)
469 == PackageManager.PERMISSION_GRANTED;
470 }
471
Enrico Granata3c7a6662017-02-23 18:07:59 -0800472 public static void assertAnyPermission(Context context, String... permissions) {
473 for (String permission : permissions) {
474 if (context.checkCallingOrSelfPermission(permission) ==
475 PackageManager.PERMISSION_GRANTED) {
476 return;
477 }
478 }
479 throw new SecurityException("requires any of " + Arrays.toString(permissions));
480 }
481
Enrico Granatae8056ca2018-04-03 13:19:52 -0700482 @Override
483 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
484 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700485 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700486 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700487 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
488 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700489 return;
keunyounga3b28d82015-08-25 13:05:15 -0700490 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700491
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700492 if (args == null || args.length == 0 || (args.length > 0 && "-a".equals(args[0]))) {
493 writer.println("*Dump car service*");
Enrico Granatae8056ca2018-04-03 13:19:52 -0700494 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
495 writer.println("*Dump all services*");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700496
Mark Tabry7e2a7822019-10-17 10:18:32 -0700497 dumpAllServices(writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700498
Enrico Granatae8056ca2018-04-03 13:19:52 -0700499 writer.println("*Dump Vehicle HAL*");
500 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
501 try {
502 // TODO dump all feature flags by creating a dumpable interface
503 mHal.dump(writer);
504 } catch (Exception e) {
505 writer.println("Failed dumping: " + mHal.getClass().getName());
506 e.printStackTrace(writer);
507 }
felipeal080e5652019-11-02 17:04:04 -0700508 } else if ("--list".equals(args[0])) {
509 dumpListOfServices(writer);
510 return;
511 } else if ("--services".equals(args[0])) {
512 if (args.length < 2) {
513 writer.print("Must pass services to dump when using --services");
514 return;
515 }
516 int length = args.length - 1;
517 String[] services = new String[length];
518 System.arraycopy(args, 1, services, 0, length);
519 dumpIndividualServices(writer, services);
520 return;
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700521 } else if ("--metrics".equals(args[0])) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700522 // Strip the --metrics flag when passing dumpsys arguments to CarStatsService
523 mCarStatsService.dump(fd, writer, Arrays.copyOfRange(args, 1, args.length));
Mark Tabryb588d2e2019-09-12 10:50:11 -0700524 } else if ("--vms-hal".equals(args[0])) {
525 mHal.getVmsHal().dumpMetrics(fd);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700526 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
527 execShellCmd(args, writer);
528 } else {
529 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800530 }
531 }
532
felipeal72bf6422019-11-02 17:04:04 -0700533 @Override
534 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
535 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
536 throws RemoteException {
537 new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
538 }
539
felipeal080e5652019-11-02 17:04:04 -0700540 private void dumpListOfServices(PrintWriter writer) {
541 for (CarServiceBase service : mAllServices) {
542 writer.println(service.getClass().getName());
543 }
544 }
545
Mark Tabry7e2a7822019-10-17 10:18:32 -0700546 private void dumpAllServices(PrintWriter writer) {
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700547 for (CarServiceBase service : mAllServices) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700548 dumpService(service, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700549 }
550 if (mCarTestService != null) {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700551 dumpService(mCarTestService, writer);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700552 }
felipeal080e5652019-11-02 17:04:04 -0700553 }
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700554
felipeal080e5652019-11-02 17:04:04 -0700555 private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
556 for (String serviceName : serviceNames) {
557 writer.println("** Dumping " + serviceName + "\n");
558 CarServiceBase service = getCarServiceBySubstring(serviceName);
559 if (service == null) {
560 writer.println("No such service!");
561 } else {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700562 dumpService(service, writer);
felipeal080e5652019-11-02 17:04:04 -0700563 }
564 writer.println();
565 }
566 }
567
568 @Nullable
569 private CarServiceBase getCarServiceBySubstring(String className) {
570 return Arrays.asList(mAllServices).stream()
571 .filter(s -> s.getClass().getSimpleName().equals(className))
572 .findFirst().orElse(null);
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700573 }
574
Mark Tabry7e2a7822019-10-17 10:18:32 -0700575 private void dumpService(CarServiceBase service, PrintWriter writer) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800576 try {
Mark Tabry7e2a7822019-10-17 10:18:32 -0700577 service.dump(writer);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800578 } catch (Exception e) {
579 writer.println("Failed dumping: " + service.getClass().getName());
580 e.printStackTrace(writer);
581 }
keunyoungcc449f72015-08-12 10:46:27 -0700582 }
Yao Chene33f07e2016-07-26 12:02:51 -0700583
584 void execShellCmd(String[] args, PrintWriter writer) {
585 new CarShellCommand().exec(args, writer);
586 }
587
Pavel Maltsevabd47232017-10-10 16:54:57 -0700588 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700589 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700590 Slog.i(TAG, name);
591 mBootTiming.traceBegin(name);
592 }
593
Pavel Maltsevabd47232017-10-10 16:54:57 -0700594 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700595 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700596 mBootTiming.traceEnd();
597 }
598
felipeal72bf6422019-11-02 17:04:04 -0700599 private final class CarShellCommand extends ShellCommand {
Yao Chene33f07e2016-07-26 12:02:51 -0700600 private static final String COMMAND_HELP = "-h";
601 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800602 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Kai4b098cf2019-06-05 15:03:28 -0700603 private static final String COMMAND_INJECT_ERROR_EVENT = "inject-error-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800604 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800605 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800606 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800607 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Kai29fba112019-04-22 18:37:38 -0700608 private static final String COMMAND_GET_PROPERTY_VALUE = "get-property-value";
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700609 private static final String COMMAND_PROJECTION_AP_TETHERING = "projection-tethering";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800610 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700611 private static final String COMMAND_RESUME = "resume";
612 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700613 private static final String COMMAND_ENABLE_TRUSTED_DEVICE = "enable-trusted-device";
614 private static final String COMMAND_REMOVE_TRUSTED_DEVICES = "remove-trusted-devices";
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700615 private static final String COMMAND_SET_UID_TO_ZONE = "set-zoneid-for-uid";
Keun young Park09ca8492019-10-15 10:34:11 -0700616 private static final String COMMAND_START_FIXED_ACTIVITY_MODE = "start-fixed-activity-mode";
617 private static final String COMMAND_STOP_FIXED_ACTIVITY_MODE = "stop-fixed-activity-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800618
Yao Chene33f07e2016-07-26 12:02:51 -0700619 private static final String PARAM_DAY_MODE = "day";
620 private static final String PARAM_NIGHT_MODE = "night";
621 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800622 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800623 private static final String PARAM_ON_MODE = "on";
624 private static final String PARAM_OFF_MODE = "off";
625 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800626
felipeal72bf6422019-11-02 17:04:04 -0700627 private static final int RESULT_OK = 0;
628 private static final int RESULT_ERROR = -1; // Arbitrary value, any non-0 is fine
629
630
631 @Override
632 public int onCommand(String cmd) {
633 if (cmd == null) {
634 onHelp();
635 return RESULT_ERROR;
636 }
637 ArrayList<String> argsList = new ArrayList<>();
638 argsList.add(cmd);
639 String arg = null;
640 do {
641 arg = getNextArg();
642 if (arg != null) {
643 argsList.add(arg);
644 }
645 } while (arg != null);
646 String[] args = new String[argsList.size()];
647 argsList.toArray(args);
648 return exec(args, getOutPrintWriter());
649 }
650
651 @Override
652 public void onHelp() {
653 dumpHelp(getOutPrintWriter());
654 }
Yao Chene33f07e2016-07-26 12:02:51 -0700655
656 private void dumpHelp(PrintWriter pw) {
657 pw.println("Car service commands:");
658 pw.println("\t-h");
659 pw.println("\t Print this help text.");
660 pw.println("\tday-night-mode [day|night|sensor]");
661 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800662 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800663 pw.println("\t Inject a vehicle property for testing.");
Kai4b098cf2019-06-05 15:03:28 -0700664 pw.println("\tinject-error-event property zone errorCode");
665 pw.println("\t Inject an error event from VHAL for testing.");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700666 pw.println("\tenable-uxr true|false");
667 pw.println("\t Enable/Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800668 pw.println("\tgarage-mode [on|off|query]");
669 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800670 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800671 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800672 pw.println("\tget-carpropertyconfig [propertyId]");
673 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Kai29fba112019-04-22 18:37:38 -0700674 pw.println("\tget-property-value [propertyId] [areaId]");
675 pw.println("\t Get a vehicle property value by property id in Hex and areaId");
676 pw.println("\t or list all property values for all areaId");
Jim Kayed76c2742019-04-02 11:33:27 -0700677 pw.println("\tsuspend");
678 pw.println("\t Suspend the system to Deep Sleep.");
679 pw.println("\tresume");
680 pw.println("\t Wake the system up after a 'suspend.'");
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700681 pw.println("\tenable-trusted-device true|false");
682 pw.println("\t Enable/Disable Trusted device feature.");
683 pw.println("\tremove-trusted-devices");
684 pw.println("\t Remove all trusted devices for the current foreground user.");
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700685 pw.println("\tprojection-tethering [true|false]");
686 pw.println("\t Whether tethering should be used when creating access point for"
687 + " wireless projection");
Andrew T Nguyena9228c72019-06-12 11:28:42 -0700688 pw.println("\t--metrics");
689 pw.println("\t When used with dumpsys, only metrics will be in the dumpsys output.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700690 pw.println("\tset-zoneid-for-uid [zoneid] [uid]");
691 pw.println("\t Maps the audio zoneid to uid.");
Keun young Park09ca8492019-10-15 10:34:11 -0700692 pw.println("\tstart-fixed-activity displayId packageName activityName");
693 pw.println("\t Start an Activity the specified display as fixed mode");
694 pw.println("\tstop-fixed-mode displayId");
695 pw.println("\t Stop fixed Activity mode for the given display. "
696 + "The Activity will not be restarted upon crash.");
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700697 }
698
felipeal72bf6422019-11-02 17:04:04 -0700699 private int dumpInvalidArguments(PrintWriter pw) {
700 pw.println("Incorrect number of arguments.");
701 dumpHelp(pw);
702 return RESULT_ERROR;
703 }
704
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700705 private String runSetZoneIdForUid(String zoneString, String uidString) {
706 int uid = Integer.parseInt(uidString);
707 int zoneId = Integer.parseInt(zoneString);
708 if (!ArrayUtils.contains(mCarAudioService.getAudioZoneIds(), zoneId)) {
709 return "zoneid " + zoneId + " not found";
710 }
711 mCarAudioService.setZoneIdForUid(zoneId, uid);
712 return null;
Yao Chene33f07e2016-07-26 12:02:51 -0700713 }
714
felipeal72bf6422019-11-02 17:04:04 -0700715 public int exec(String[] args, PrintWriter writer) {
Yao Chene33f07e2016-07-26 12:02:51 -0700716 String arg = args[0];
717 switch (arg) {
718 case COMMAND_HELP:
719 dumpHelp(writer);
720 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800721 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800722 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700723 forceDayNightMode(value, writer);
724 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800725 }
726 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800727 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800728 forceGarageMode(value, writer);
729 break;
730 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800731 case COMMAND_INJECT_VHAL_EVENT:
732 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
733 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800734 if (args.length != 3 && args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700735 return dumpInvalidArguments(writer);
Jim Kayef10b7d82019-02-26 12:53:22 -0800736 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800737 // Zoned
738 zone = args[2];
739 data = args[3];
740 } else {
741 // Global
742 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800743 }
Kai4b098cf2019-06-05 15:03:28 -0700744 injectVhalEvent(args[1], zone, data, false, writer);
745 break;
746 case COMMAND_INJECT_ERROR_EVENT:
747 if (args.length != 4) {
felipeal72bf6422019-11-02 17:04:04 -0700748 return dumpInvalidArguments(writer);
Kai4b098cf2019-06-05 15:03:28 -0700749 }
750 String errorAreaId = args[2];
751 String errorCode = args[3];
752 injectVhalEvent(args[1], errorAreaId, errorCode, true, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800753 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800754 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800755 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700756 return dumpInvalidArguments(writer);
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800757 }
758 boolean enableBlocking = Boolean.valueOf(args[1]);
759 if (mCarPackageManagerService != null) {
760 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
761 }
762 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800763 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800764 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700765 return dumpInvalidArguments(writer);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800766 }
767 String pkgName = args[1].toLowerCase();
768 if (mCarPackageManagerService != null) {
769 String[] doActivities =
770 mCarPackageManagerService.getDistractionOptimizedActivities(
771 pkgName);
772 if (doActivities != null) {
773 writer.println("DO Activities for " + pkgName);
774 for (String a : doActivities) {
775 writer.println(a);
776 }
777 } else {
778 writer.println("No DO Activities for " + pkgName);
779 }
780 }
781 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800782 case COMMAND_GET_CARPROPERTYCONFIG:
783 String propertyId = args.length < 2 ? "" : args[1];
784 mHal.dumpPropertyConfigs(writer, propertyId);
785 break;
Kai29fba112019-04-22 18:37:38 -0700786 case COMMAND_GET_PROPERTY_VALUE:
787 String propId = args.length < 2 ? "" : args[1];
788 String areaId = args.length < 3 ? "" : args[2];
789 mHal.dumpPropertyValueByCommend(writer, propId, areaId);
790 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800791 case COMMAND_PROJECTION_UI_MODE:
792 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700793 return dumpInvalidArguments(writer);
Pavel Maltsev48a31942019-03-03 22:08:28 -0800794 }
795 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700796 break;
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700797 case COMMAND_PROJECTION_AP_TETHERING:
798 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700799 return dumpInvalidArguments(writer);
Pavel Maltsev371d97f2019-05-20 14:51:31 -0700800 }
801 mCarProjectionService.setAccessPointTethering(Boolean.valueOf(args[1]));
802 break;
Jim Kayed76c2742019-04-02 11:33:27 -0700803 case COMMAND_RESUME:
804 mCarPowerManagementService.forceSimulatedResume();
805 writer.println("Resume: Simulating resuming from Deep Sleep");
806 break;
807 case COMMAND_SUSPEND:
808 mCarPowerManagementService.forceSimulatedSuspend();
809 writer.println("Resume: Simulating powering down to Deep Sleep");
810 break;
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700811 case COMMAND_ENABLE_TRUSTED_DEVICE:
812 if (args.length != 2) {
felipeal72bf6422019-11-02 17:04:04 -0700813 return dumpInvalidArguments(writer);
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700814 }
815 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
816 .setTrustedDeviceEnrollmentEnabled(Boolean.valueOf(args[1]));
817 mCarTrustedDeviceService.getCarTrustAgentUnlockService()
818 .setTrustedDeviceUnlockEnabled(Boolean.valueOf(args[1]));
819 break;
820 case COMMAND_REMOVE_TRUSTED_DEVICES:
821 mCarTrustedDeviceService.getCarTrustAgentEnrollmentService()
Anthony Hughfbb67762019-10-15 12:54:54 -0700822 .removeAllTrustedDevices(ActivityManager.getCurrentUser());
Ram Periathiruvadid26a3fb2019-04-15 14:48:23 -0700823 break;
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700824 case COMMAND_SET_UID_TO_ZONE:
825 if (args.length != 3) {
felipeal72bf6422019-11-02 17:04:04 -0700826 return dumpInvalidArguments(writer);
Oscar Azucena3f3a25e2019-07-12 14:28:24 -0700827 }
828 String results = runSetZoneIdForUid(args[1], args[2]);
829 if (results != null) {
830 writer.println(results);
831 dumpHelp(writer);
832 }
833 break;
Keun young Park09ca8492019-10-15 10:34:11 -0700834 case COMMAND_START_FIXED_ACTIVITY_MODE:
835 handleStartFixedActivity(args, writer);
836 break;
837 case COMMAND_STOP_FIXED_ACTIVITY_MODE:
838 handleStopFixedMode(args, writer);
839 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700840 default:
Jim Kayef10b7d82019-02-26 12:53:22 -0800841 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -0700842 dumpHelp(writer);
felipeal72bf6422019-11-02 17:04:04 -0700843 return RESULT_ERROR;
Yao Chene33f07e2016-07-26 12:02:51 -0700844 }
felipeal72bf6422019-11-02 17:04:04 -0700845 return RESULT_OK;
Yao Chene33f07e2016-07-26 12:02:51 -0700846 }
847
Keun young Park09ca8492019-10-15 10:34:11 -0700848 private void handleStartFixedActivity(String[] args, PrintWriter writer) {
849 if (args.length != 4) {
850 writer.println("Incorrect number of arguments");
851 dumpHelp(writer);
852 return;
853 }
854 int displayId;
855 try {
856 displayId = Integer.parseInt(args[1]);
857 } catch (NumberFormatException e) {
858 writer.println("Wrong display id:" + args[1]);
859 return;
860 }
861 String packageName = args[2];
862 String activityName = args[3];
863 Intent intent = new Intent();
864 intent.setComponent(new ComponentName(packageName, activityName));
865 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
866 ActivityOptions options = ActivityOptions.makeBasic();
867 options.setLaunchDisplayId(displayId);
868 if (!mFixedActivityService.startFixedActivityModeForDisplayAndUser(intent, options,
869 displayId, ActivityManager.getCurrentUser())) {
870 writer.println("Failed to start");
871 return;
872 }
873 writer.println("Succeeded");
874 }
875
876 private void handleStopFixedMode(String[] args, PrintWriter writer) {
877 if (args.length != 2) {
878 writer.println("Incorrect number of arguments");
879 dumpHelp(writer);
880 return;
881 }
882 int displayId;
883 try {
884 displayId = Integer.parseInt(args[1]);
885 } catch (NumberFormatException e) {
886 writer.println("Wrong display id:" + args[1]);
887 return;
888 }
889 mFixedActivityService.stopFixedActivityMode(displayId);
890 }
891
Yao Chene33f07e2016-07-26 12:02:51 -0700892 private void forceDayNightMode(String arg, PrintWriter writer) {
893 int mode;
894 switch (arg) {
895 case PARAM_DAY_MODE:
896 mode = CarNightService.FORCED_DAY_MODE;
897 break;
898 case PARAM_NIGHT_MODE:
899 mode = CarNightService.FORCED_NIGHT_MODE;
900 break;
901 case PARAM_SENSOR_MODE:
902 mode = CarNightService.FORCED_SENSOR_MODE;
903 break;
904 default:
905 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
906 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
907 return;
908 }
909 int current = mCarNightService.forceDayNightMode(mode);
910 String currentMode = null;
911 switch (current) {
912 case UiModeManager.MODE_NIGHT_AUTO:
913 currentMode = PARAM_SENSOR_MODE;
914 break;
915 case UiModeManager.MODE_NIGHT_YES:
916 currentMode = PARAM_NIGHT_MODE;
917 break;
918 case UiModeManager.MODE_NIGHT_NO:
919 currentMode = PARAM_DAY_MODE;
920 break;
921 }
922 writer.println("DayNightMode changed to: " + currentMode);
923 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800924
Bryan Eylera32a7c12018-02-27 15:40:00 -0800925 private void forceGarageMode(String arg, PrintWriter writer) {
926 switch (arg) {
927 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700928 mGarageModeService.forceStartGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -0700929 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800930 break;
931 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700932 mGarageModeService.stopAndResetGarageMode();
Jim Kaye1b5e6182019-05-24 15:48:32 -0700933 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800934 break;
935 case PARAM_QUERY_MODE:
Jim Kaye1b5e6182019-05-24 15:48:32 -0700936 mGarageModeService.dump(writer);
Bryan Eylera32a7c12018-02-27 15:40:00 -0800937 break;
938 default:
939 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
940 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
Bryan Eylera32a7c12018-02-27 15:40:00 -0800941 }
Bryan Eylera32a7c12018-02-27 15:40:00 -0800942 }
943
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800944 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800945 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800946 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800947 * @param property the Vehicle property Id as defined in the HAL
948 * @param zone Zone that this event services
Kai4b098cf2019-06-05 15:03:28 -0700949 * @param isErrorEvent indicates the type of event
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800950 * @param value Data value of the event
951 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800952 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800953 private void injectVhalEvent(String property, String zone, String value,
Kai4b098cf2019-06-05 15:03:28 -0700954 boolean isErrorEvent, PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800955 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
956 if (!isPropertyAreaTypeGlobal(property)) {
957 writer.println("Property area type inconsistent with given zone");
958 return;
959 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800960 }
961 try {
Kai4b098cf2019-06-05 15:03:28 -0700962 if (isErrorEvent) {
963 mHal.injectOnPropertySetError(property, zone, value);
964 } else {
965 mHal.injectVhalEvent(property, zone, value);
966 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800967 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800968 writer.println("Invalid property Id zone Id or value" + e);
969 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800970 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800971 }
972
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800973 // Check if the given property is global
974 private boolean isPropertyAreaTypeGlobal(String property) {
975 if (property == null) {
976 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800977 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800978 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800979 }
Yao Chene33f07e2016-07-26 12:02:51 -0700980 }
kevinjm55822c42018-08-15 11:26:00 -0700981}