blob: e05efaa3ebceb3d1918cd0998091224a84c18366 [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
Yao Chene33f07e2016-07-26 12:02:51 -070019import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080020import android.car.Car;
21import android.car.ICar;
Pavel Maltsev0477e292016-05-27 12:22:36 -070022import android.car.cluster.renderer.IInstrumentClusterNavigation;
keunyoungca515072015-07-10 12:21:47 -070023import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070024import android.content.pm.PackageManager;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070025import android.hardware.vehicle.V2_0.IVehicle;
keunyoungca515072015-07-10 12:21:47 -070026import android.os.IBinder;
keunyoungca515072015-07-10 12:21:47 -070027import android.util.Log;
28
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080029import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070030import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080031import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070032import com.android.internal.annotations.GuardedBy;
33
keunyounga3b28d82015-08-25 13:05:15 -070034import java.io.PrintWriter;
35
keunyoungca515072015-07-10 12:21:47 -070036public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070037
Keun-young Parka28d7b22016-02-29 16:54:29 -080038 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070039 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
40 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080041
42 // load jni for all services here
43 static {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070044 try {
45 System.loadLibrary("jni_car_service");
46 } catch (UnsatisfiedLinkError ex) {
47 // Unable to load native library when loaded from the testing framework.
48 Log.e(CarLog.TAG_SERVICE, "Failed to load jni_car_service library: " + ex.getMessage());
49 }
Keun-young Parka28d7b22016-02-29 16:54:29 -080050 }
51
keunyoungca515072015-07-10 12:21:47 -070052 private final Context mContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070053 private VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070054
Keun-young Park4727da32016-05-31 10:00:51 -070055 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070056 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080057 private final CarPackageManagerService mCarPackageManagerService;
58 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070059 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070060 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070061 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080062 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070063 private final CarCabinService mCarCabinService;
Steve Paik875616c2016-02-05 10:55:59 -080064 private final CarCameraService mCarCameraService;
Steve Paik66481982015-10-27 15:22:38 -070065 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070066 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080067 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070068 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080069 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080070 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070071 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070072 private final CarVendorExtensionService mCarVendorExtensionService;
keunyounga74b9ca2015-10-21 13:33:58 -070073
Pavel Maltsev0d07c762016-11-03 16:40:15 -070074 private final CarServiceBase[] mAllServices;
75
keunyoung1ab8e182015-09-24 09:25:22 -070076 /** Test only service. Populate it only when necessary. */
77 @GuardedBy("this")
78 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070079
Pavel Maltsev0d07c762016-11-03 16:40:15 -070080 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface) {
keunyoungca515072015-07-10 12:21:47 -070081 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070082 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -070083 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070084 mCarPowerManagementService = new CarPowerManagementService(
85 mHal.getPowerHal(), systemInterface);
86 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Keun-young Park4727da32016-05-31 10:00:51 -070087 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
88 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070089 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080090 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -080091 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070092 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -070093 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070094 mCarAudioService = new CarAudioService(serviceContext, mHal.getAudioHal(),
95 mCarInputService);
96 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
97 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
98 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Steve Paik875616c2016-02-05 10:55:59 -080099 mCarCameraService = new CarCameraService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700100 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700101 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700102 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700103 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700104 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700105 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
106 mHal.getVendorExtensionHal());
keunyounga74b9ca2015-10-21 13:33:58 -0700107
keunyounga3b28d82015-08-25 13:05:15 -0700108 // Be careful with order. Service depending on other service should be inited later.
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700109 mAllServices = new CarServiceBase[]{
Keun-young Park4727da32016-05-31 10:00:51 -0700110 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700111 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700112 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800113 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800114 mCarInputService,
115 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700116 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700117 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700118 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700119 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700120 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800121 mCarRadioService,
122 mCarCameraService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800123 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800124 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800125 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700126 mSystemStateControllerService,
127 mCarVendorExtensionService
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700128 };
keunyoungca515072015-07-10 12:21:47 -0700129 }
130
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700131 public void init() {
132 mHal.init();
keunyounga3b28d82015-08-25 13:05:15 -0700133 for (CarServiceBase service: mAllServices) {
134 service.init();
135 }
keunyoungca515072015-07-10 12:21:47 -0700136 }
137
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700138 public void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700139 // release done in opposite order from init
140 for (int i = mAllServices.length - 1; i >= 0; i--) {
141 mAllServices[i].release();
142 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700143 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700144 }
145
keunyoungca515072015-07-10 12:21:47 -0700146 @Override
keunyoungca515072015-07-10 12:21:47 -0700147 public IBinder getCarService(String serviceName) {
148 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800149 case Car.AUDIO_SERVICE:
150 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700151 case Car.SENSOR_SERVICE:
152 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700153 case Car.INFO_SERVICE:
154 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700155 case Car.APP_FOCUS_SERVICE:
156 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800157 case Car.PACKAGE_SERVICE:
158 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700159 case Car.CABIN_SERVICE:
160 assertCabinPermission(mContext);
161 return mCarCabinService;
Steve Paik875616c2016-02-05 10:55:59 -0800162 case Car.CAMERA_SERVICE:
163 assertCameraPermission(mContext);
164 return mCarCameraService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800165 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700166 assertHvacPermission(mContext);
167 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800168 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800169 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700170 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800171 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700172 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700173 IInstrumentClusterNavigation navService =
174 mInstrumentClusterService.getNavigationService();
175 return navService == null ? null : navService.asBinder();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800176 case Car.PROJECTION_SERVICE:
177 assertProjectionPermission(mContext);
178 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700179 case Car.VENDOR_EXTENSION_SERVICE:
180 assertVendorExtensionPermission(mContext);
181 return mCarVendorExtensionService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800182 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700183 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700184 synchronized (this) {
185 if (mCarTestService == null) {
186 mCarTestService = new CarTestService(mContext, this);
187 }
188 return mCarTestService;
189 }
190 }
keunyoungca515072015-07-10 12:21:47 -0700191 default:
192 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
193 return null;
194 }
195 }
196
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800197 @Override
198 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700199 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800200 }
201
Keun-young Parka28d7b22016-02-29 16:54:29 -0800202 public CarServiceBase getCarInternalService(String serviceName) {
203 switch (serviceName) {
204 case INTERNAL_INPUT_SERVICE:
205 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700206 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
207 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800208 default:
209 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
210 serviceName);
211 return null;
212 }
213 }
214
keunyoung1ab8e182015-09-24 09:25:22 -0700215 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700216 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700217 }
218
Steve Paik43c04a72016-07-08 19:12:09 -0700219 public static void assertCabinPermission(Context context) {
220 assertPermission(context, Car.PERMISSION_CAR_CABIN);
221 }
222
Steve Paik875616c2016-02-05 10:55:59 -0800223 public static void assertCameraPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700224 assertPermission(context, Car.PERMISSION_CAR_CAMERA);
Steve Paik875616c2016-02-05 10:55:59 -0800225 }
226
Keun-young Parke31a8b22016-03-16 17:34:08 -0700227 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700228 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800229 }
230
Steve Paik66481982015-10-27 15:22:38 -0700231 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700232 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700233 }
234
keunyoung6b197692015-11-16 13:54:38 -0800235 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700236 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700237 }
238
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800239 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700240 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
241 }
242
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700243 public static void assertVendorExtensionPermission(Context context) {
244 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
245 }
246
Steve Paik461ecc62016-06-08 15:28:32 -0700247 public static void assertPermission(Context context, String permission) {
248 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
249 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800250 }
251 }
252
keunyoungcc449f72015-08-12 10:46:27 -0700253 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700254 writer.println("*Dump all services*");
255 for (CarServiceBase service: mAllServices) {
256 service.dump(writer);
257 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700258 if (mCarTestService != null) {
259 mCarTestService.dump(writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700260 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700261 writer.println("*Dump Vehicle HAL*");
262 mHal.dump(writer);
keunyoungcc449f72015-08-12 10:46:27 -0700263 }
Yao Chene33f07e2016-07-26 12:02:51 -0700264
265 void execShellCmd(String[] args, PrintWriter writer) {
266 new CarShellCommand().exec(args, writer);
267 }
268
269 private class CarShellCommand {
270 private static final String COMMAND_HELP = "-h";
271 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
272 private static final String PARAM_DAY_MODE = "day";
273 private static final String PARAM_NIGHT_MODE = "night";
274 private static final String PARAM_SENSOR_MODE = "sensor";
275
276 private void dumpHelp(PrintWriter pw) {
277 pw.println("Car service commands:");
278 pw.println("\t-h");
279 pw.println("\t Print this help text.");
280 pw.println("\tday-night-mode [day|night|sensor]");
281 pw.println("\t Force into day/night mode or restore to auto.");
282 }
283
284 public void exec(String[] args, PrintWriter writer) {
285 String arg = args[0];
286 switch (arg) {
287 case COMMAND_HELP:
288 dumpHelp(writer);
289 break;
290 case COMMAND_DAY_NIGHT_MODE:
291 String value = args.length < 1 ? "" : args[1];
292 forceDayNightMode(value, writer);
293 break;
294 default:
295 writer.println("Unknown command.");
296 dumpHelp(writer);
297 }
298 }
299
300 private void forceDayNightMode(String arg, PrintWriter writer) {
301 int mode;
302 switch (arg) {
303 case PARAM_DAY_MODE:
304 mode = CarNightService.FORCED_DAY_MODE;
305 break;
306 case PARAM_NIGHT_MODE:
307 mode = CarNightService.FORCED_NIGHT_MODE;
308 break;
309 case PARAM_SENSOR_MODE:
310 mode = CarNightService.FORCED_SENSOR_MODE;
311 break;
312 default:
313 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
314 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
315 return;
316 }
317 int current = mCarNightService.forceDayNightMode(mode);
318 String currentMode = null;
319 switch (current) {
320 case UiModeManager.MODE_NIGHT_AUTO:
321 currentMode = PARAM_SENSOR_MODE;
322 break;
323 case UiModeManager.MODE_NIGHT_YES:
324 currentMode = PARAM_NIGHT_MODE;
325 break;
326 case UiModeManager.MODE_NIGHT_NO:
327 currentMode = PARAM_DAY_MODE;
328 break;
329 }
330 writer.println("DayNightMode changed to: " + currentMode);
331 }
332 }
333}