blob: b6386afd33e4d827c794e08aa34ec8115d07c915 [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;
keunyoungca515072015-07-10 12:21:47 -070025import android.os.IBinder;
keunyoungca515072015-07-10 12:21:47 -070026import android.util.Log;
27
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080028import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070029import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080030import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070031import com.android.internal.annotations.GuardedBy;
32
keunyounga3b28d82015-08-25 13:05:15 -070033import java.io.PrintWriter;
34
keunyoungca515072015-07-10 12:21:47 -070035public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070036
Keun-young Parka28d7b22016-02-29 16:54:29 -080037 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070038 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
39 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080040
41 // load jni for all services here
42 static {
43 System.loadLibrary("jni_car_service");
44 }
45
keunyoungca515072015-07-10 12:21:47 -070046 @GuardedBy("ICarImpl.class")
47 private static ICarImpl sInstance = null;
48
49 private final Context mContext;
keunyoungcc449f72015-08-12 10:46:27 -070050 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070051
Keun-young Park4727da32016-05-31 10:00:51 -070052 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070053 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080054 private final CarPackageManagerService mCarPackageManagerService;
55 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070056 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070057 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070058 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080059 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070060 private final CarCabinService mCarCabinService;
Steve Paik875616c2016-02-05 10:55:59 -080061 private final CarCameraService mCarCameraService;
Steve Paik66481982015-10-27 15:22:38 -070062 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070063 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080064 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070065 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080066 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080067 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070068 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070069 private final CarVendorExtensionService mCarVendorExtensionService;
keunyounga74b9ca2015-10-21 13:33:58 -070070
keunyoung1ab8e182015-09-24 09:25:22 -070071 /** Test only service. Populate it only when necessary. */
72 @GuardedBy("this")
73 private CarTestService mCarTestService;
keunyounga3b28d82015-08-25 13:05:15 -070074 private final CarServiceBase[] mAllServices;
keunyoungca515072015-07-10 12:21:47 -070075
76 public synchronized static ICarImpl getInstance(Context serviceContext) {
77 if (sInstance == null) {
78 sInstance = new ICarImpl(serviceContext);
79 sInstance.init();
80 }
81 return sInstance;
82 }
83
84 public synchronized static void releaseInstance() {
85 if (sInstance == null) {
86 return;
87 }
88 sInstance.release();
89 sInstance = null;
90 }
91
92 public ICarImpl(Context serviceContext) {
93 mContext = serviceContext;
keunyoungfe30ba02015-09-17 17:56:35 -070094 mHal = VehicleHal.getInstance();
Keun-young Park4727da32016-05-31 10:00:51 -070095 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
keunyoung4b0212c2015-10-29 17:11:57 -070096 mCarPowerManagementService = new CarPowerManagementService(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -070097 mCarSensorService = new CarSensorService(serviceContext);
98 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
99 mSystemActivityMonitoringService);
Keun-young Parka28d7b22016-02-29 16:54:29 -0800100 mCarInputService = new CarInputService(serviceContext);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800101 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800102 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
keunyounga3b28d82015-08-25 13:05:15 -0700103 mCarInfoService = new CarInfoService(serviceContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700104 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Yao Chenc4d442f2016-04-08 11:33:47 -0700105 mCarAudioService = new CarAudioService(serviceContext, mCarInputService);
Steve Paik43c04a72016-07-08 19:12:09 -0700106 mCarCabinService = new CarCabinService(serviceContext);
Steve Paik66481982015-10-27 15:22:38 -0700107 mCarHvacService = new CarHvacService(serviceContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700108 mCarRadioService = new CarRadioService(serviceContext);
Steve Paik875616c2016-02-05 10:55:59 -0800109 mCarCameraService = new CarCameraService(serviceContext);
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800110 mCarNightService = new CarNightService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700111 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700112 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700113 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700114 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700115 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700116
keunyounga3b28d82015-08-25 13:05:15 -0700117 // Be careful with order. Service depending on other service should be inited later.
118 mAllServices = new CarServiceBase[] {
Keun-young Park4727da32016-05-31 10:00:51 -0700119 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700120 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700121 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800122 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800123 mCarInputService,
124 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700125 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700126 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700127 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700128 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700129 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800130 mCarRadioService,
131 mCarCameraService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800132 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800133 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800134 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700135 mSystemStateControllerService,
136 mCarVendorExtensionService
keunyounga74b9ca2015-10-21 13:33:58 -0700137 };
keunyoungca515072015-07-10 12:21:47 -0700138 }
139
140 private void init() {
keunyounga3b28d82015-08-25 13:05:15 -0700141 for (CarServiceBase service: mAllServices) {
142 service.init();
143 }
keunyoungca515072015-07-10 12:21:47 -0700144 }
145
146 private void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700147 // release done in opposite order from init
148 for (int i = mAllServices.length - 1; i >= 0; i--) {
149 mAllServices[i].release();
150 }
keunyoungcc449f72015-08-12 10:46:27 -0700151 VehicleHal.releaseInstance();
keunyoungca515072015-07-10 12:21:47 -0700152 }
153
keunyoung4b0212c2015-10-29 17:11:57 -0700154 /** Only for CarTestService */
155 void startMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700156 reinitServices();
157 }
158
keunyoung4b0212c2015-10-29 17:11:57 -0700159 /** Only for CarTestService */
160 void stopMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700161 reinitServices();
162 }
163
keunyoung6b197692015-11-16 13:54:38 -0800164 /** Reset all services when starting / stopping vehicle hal mocking */
keunyoung1ab8e182015-09-24 09:25:22 -0700165 private void reinitServices() {
166 for (int i = mAllServices.length - 1; i >= 0; i--) {
167 mAllServices[i].release();
168 }
Keun-young Park021310d2016-04-25 21:09:39 -0700169 VehicleHal.getInstance().release();
170 VehicleHal.getInstance().init();
keunyoung1ab8e182015-09-24 09:25:22 -0700171 for (CarServiceBase service: mAllServices) {
172 service.init();
173 }
174 }
175
keunyoungca515072015-07-10 12:21:47 -0700176 @Override
keunyoungca515072015-07-10 12:21:47 -0700177 public IBinder getCarService(String serviceName) {
178 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800179 case Car.AUDIO_SERVICE:
180 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700181 case Car.SENSOR_SERVICE:
182 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700183 case Car.INFO_SERVICE:
184 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700185 case Car.APP_FOCUS_SERVICE:
186 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800187 case Car.PACKAGE_SERVICE:
188 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700189 case Car.CABIN_SERVICE:
190 assertCabinPermission(mContext);
191 return mCarCabinService;
Steve Paik875616c2016-02-05 10:55:59 -0800192 case Car.CAMERA_SERVICE:
193 assertCameraPermission(mContext);
194 return mCarCameraService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800195 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700196 assertHvacPermission(mContext);
197 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800198 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800199 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700200 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800201 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700202 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700203 IInstrumentClusterNavigation navService =
204 mInstrumentClusterService.getNavigationService();
205 return navService == null ? null : navService.asBinder();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800206 case Car.PROJECTION_SERVICE:
207 assertProjectionPermission(mContext);
208 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700209 case Car.VENDOR_EXTENSION_SERVICE:
210 assertVendorExtensionPermission(mContext);
211 return mCarVendorExtensionService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800212 case Car.TEST_SERVICE: {
keunyoung1ab8e182015-09-24 09:25:22 -0700213 assertVehicleHalMockPermission(mContext);
214 synchronized (this) {
215 if (mCarTestService == null) {
216 mCarTestService = new CarTestService(mContext, this);
217 }
218 return mCarTestService;
219 }
220 }
keunyoungca515072015-07-10 12:21:47 -0700221 default:
222 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
223 return null;
224 }
225 }
226
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800227 @Override
228 public int getCarConnectionType() {
229 if (!isInMocking()) {
230 return Car.CONNECTION_TYPE_EMBEDDED;
231 } else {
232 return Car.CONNECTION_TYPE_EMBEDDED_MOCKING;
233 }
234 }
235
Keun-young Parka28d7b22016-02-29 16:54:29 -0800236 public CarServiceBase getCarInternalService(String serviceName) {
237 switch (serviceName) {
238 case INTERNAL_INPUT_SERVICE:
239 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700240 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
241 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800242 default:
243 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
244 serviceName);
245 return null;
246 }
247 }
248
keunyoung4b0212c2015-10-29 17:11:57 -0700249 /**
250 * Whether mocking underlying HAL or not.
251 * @return
252 */
253 public synchronized boolean isInMocking() {
254 if (mCarTestService == null) {
255 return false;
256 }
257 return mCarTestService.isInMocking();
258 }
259
keunyoung1ab8e182015-09-24 09:25:22 -0700260 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700261 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700262 }
263
Steve Paik43c04a72016-07-08 19:12:09 -0700264 public static void assertCabinPermission(Context context) {
265 assertPermission(context, Car.PERMISSION_CAR_CABIN);
266 }
267
Steve Paik875616c2016-02-05 10:55:59 -0800268 public static void assertCameraPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700269 assertPermission(context, Car.PERMISSION_CAR_CAMERA);
Steve Paik875616c2016-02-05 10:55:59 -0800270 }
271
Keun-young Parke31a8b22016-03-16 17:34:08 -0700272 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700273 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800274 }
275
Steve Paik66481982015-10-27 15:22:38 -0700276 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700277 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700278 }
279
keunyoung6b197692015-11-16 13:54:38 -0800280 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700281 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700282 }
283
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800284 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700285 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
286 }
287
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700288 public static void assertVendorExtensionPermission(Context context) {
289 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
290 }
291
Steve Paik461ecc62016-06-08 15:28:32 -0700292 public static void assertPermission(Context context, String permission) {
293 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
294 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800295 }
296 }
297
keunyoungcc449f72015-08-12 10:46:27 -0700298 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700299 writer.println("*Dump all services*");
300 for (CarServiceBase service: mAllServices) {
301 service.dump(writer);
302 }
keunyoung1ab8e182015-09-24 09:25:22 -0700303 CarTestService testService = mCarTestService;
304 if (testService != null) {
305 testService.dump(writer);
306 }
keunyoungcc449f72015-08-12 10:46:27 -0700307 }
Yao Chene33f07e2016-07-26 12:02:51 -0700308
309 void execShellCmd(String[] args, PrintWriter writer) {
310 new CarShellCommand().exec(args, writer);
311 }
312
313 private class CarShellCommand {
314 private static final String COMMAND_HELP = "-h";
315 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
316 private static final String PARAM_DAY_MODE = "day";
317 private static final String PARAM_NIGHT_MODE = "night";
318 private static final String PARAM_SENSOR_MODE = "sensor";
319
320 private void dumpHelp(PrintWriter pw) {
321 pw.println("Car service commands:");
322 pw.println("\t-h");
323 pw.println("\t Print this help text.");
324 pw.println("\tday-night-mode [day|night|sensor]");
325 pw.println("\t Force into day/night mode or restore to auto.");
326 }
327
328 public void exec(String[] args, PrintWriter writer) {
329 String arg = args[0];
330 switch (arg) {
331 case COMMAND_HELP:
332 dumpHelp(writer);
333 break;
334 case COMMAND_DAY_NIGHT_MODE:
335 String value = args.length < 1 ? "" : args[1];
336 forceDayNightMode(value, writer);
337 break;
338 default:
339 writer.println("Unknown command.");
340 dumpHelp(writer);
341 }
342 }
343
344 private void forceDayNightMode(String arg, PrintWriter writer) {
345 int mode;
346 switch (arg) {
347 case PARAM_DAY_MODE:
348 mode = CarNightService.FORCED_DAY_MODE;
349 break;
350 case PARAM_NIGHT_MODE:
351 mode = CarNightService.FORCED_NIGHT_MODE;
352 break;
353 case PARAM_SENSOR_MODE:
354 mode = CarNightService.FORCED_SENSOR_MODE;
355 break;
356 default:
357 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
358 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
359 return;
360 }
361 int current = mCarNightService.forceDayNightMode(mode);
362 String currentMode = null;
363 switch (current) {
364 case UiModeManager.MODE_NIGHT_AUTO:
365 currentMode = PARAM_SENSOR_MODE;
366 break;
367 case UiModeManager.MODE_NIGHT_YES:
368 currentMode = PARAM_NIGHT_MODE;
369 break;
370 case UiModeManager.MODE_NIGHT_NO:
371 currentMode = PARAM_DAY_MODE;
372 break;
373 }
374 writer.println("DayNightMode changed to: " + currentMode);
375 }
376 }
377}