blob: af5e0f415346e345779f77375e0f35e20cf93c5d [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
Keun-young Parke54ac272016-02-16 19:02:18 -080019import android.car.Car;
20import android.car.ICar;
keunyoungca515072015-07-10 12:21:47 -070021import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070022import android.content.pm.PackageManager;
keunyoungca515072015-07-10 12:21:47 -070023import android.os.IBinder;
keunyoungca515072015-07-10 12:21:47 -070024import android.util.Log;
25
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080026import com.android.car.cluster.CarNavigationService;
27import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070028import com.android.car.hal.VehicleHal;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080029import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070030import com.android.internal.annotations.GuardedBy;
31
keunyounga3b28d82015-08-25 13:05:15 -070032import java.io.PrintWriter;
33
keunyoungca515072015-07-10 12:21:47 -070034public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070035
Keun-young Parka28d7b22016-02-29 16:54:29 -080036 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
37
38 // load jni for all services here
39 static {
40 System.loadLibrary("jni_car_service");
41 }
42
keunyoungca515072015-07-10 12:21:47 -070043 @GuardedBy("ICarImpl.class")
44 private static ICarImpl sInstance = null;
45
46 private final Context mContext;
keunyoungcc449f72015-08-12 10:46:27 -070047 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070048
keunyoung4b0212c2015-10-29 17:11:57 -070049 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080050 private final CarPackageManagerService mCarPackageManagerService;
51 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070052 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070053 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070054 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080055 private final CarProjectionService mCarProjectionService;
Steve Paik875616c2016-02-05 10:55:59 -080056 private final CarCameraService mCarCameraService;
Steve Paik66481982015-10-27 15:22:38 -070057 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070058 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080059 private final CarNightService mCarNightService;
keunyounga74b9ca2015-10-21 13:33:58 -070060 private final AppContextService mAppContextService;
Yao Chen3a7976d2016-01-20 17:27:08 -080061 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080062 private final CarNavigationService mCarNavigationService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080063 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070064 private final SystemStateControllerService mSystemStateControllerService;
keunyounga74b9ca2015-10-21 13:33:58 -070065
keunyoung1ab8e182015-09-24 09:25:22 -070066 /** Test only service. Populate it only when necessary. */
67 @GuardedBy("this")
68 private CarTestService mCarTestService;
keunyounga3b28d82015-08-25 13:05:15 -070069 private final CarServiceBase[] mAllServices;
keunyoungca515072015-07-10 12:21:47 -070070
71 public synchronized static ICarImpl getInstance(Context serviceContext) {
72 if (sInstance == null) {
73 sInstance = new ICarImpl(serviceContext);
74 sInstance.init();
75 }
76 return sInstance;
77 }
78
79 public synchronized static void releaseInstance() {
80 if (sInstance == null) {
81 return;
82 }
83 sInstance.release();
84 sInstance = null;
85 }
86
87 public ICarImpl(Context serviceContext) {
88 mContext = serviceContext;
keunyoungfe30ba02015-09-17 17:56:35 -070089 mHal = VehicleHal.getInstance();
keunyoung4b0212c2015-10-29 17:11:57 -070090 mCarPowerManagementService = new CarPowerManagementService(serviceContext);
Keun-young Parka28d7b22016-02-29 16:54:29 -080091 mCarInputService = new CarInputService(serviceContext);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080092 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -080093 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
keunyounga3b28d82015-08-25 13:05:15 -070094 mCarInfoService = new CarInfoService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -070095 mAppContextService = new AppContextService(serviceContext);
keunyoungca515072015-07-10 12:21:47 -070096 mCarSensorService = new CarSensorService(serviceContext);
Keun-young Parka28d7b22016-02-29 16:54:29 -080097 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik66481982015-10-27 15:22:38 -070098 mCarHvacService = new CarHvacService(serviceContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070099 mCarRadioService = new CarRadioService(serviceContext);
Steve Paik875616c2016-02-05 10:55:59 -0800100 mCarCameraService = new CarCameraService(serviceContext);
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800101 mCarNightService = new CarNightService(serviceContext);
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800102 mCarPackageManagerService = new CarPackageManagerService(serviceContext);
103 mInstrumentClusterService = new InstrumentClusterService(serviceContext);
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800104 mCarNavigationService = new CarNavigationService(
105 serviceContext, mAppContextService, mInstrumentClusterService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700106 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
107 mCarPowerManagementService);
keunyounga74b9ca2015-10-21 13:33:58 -0700108
keunyounga3b28d82015-08-25 13:05:15 -0700109 // Be careful with order. Service depending on other service should be inited later.
110 mAllServices = new CarServiceBase[] {
keunyoung4b0212c2015-10-29 17:11:57 -0700111 mCarPowerManagementService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800112 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800113 mCarInputService,
114 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700115 mCarInfoService,
keunyounga74b9ca2015-10-21 13:33:58 -0700116 mAppContextService,
keunyoungd32f4e62015-09-21 11:33:06 -0700117 mCarSensorService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700118 mCarAudioService,
Steve Paik66481982015-10-27 15:22:38 -0700119 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800120 mCarRadioService,
121 mCarCameraService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800122 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800123 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800124 mCarProjectionService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800125 mCarNavigationService,
Keun-young Parkd73afae2016-04-08 20:03:32 -0700126 mSystemStateControllerService
keunyounga74b9ca2015-10-21 13:33:58 -0700127 };
keunyoungca515072015-07-10 12:21:47 -0700128 }
129
130 private void init() {
keunyounga3b28d82015-08-25 13:05:15 -0700131 for (CarServiceBase service: mAllServices) {
132 service.init();
133 }
keunyoungca515072015-07-10 12:21:47 -0700134 }
135
136 private void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700137 // release done in opposite order from init
138 for (int i = mAllServices.length - 1; i >= 0; i--) {
139 mAllServices[i].release();
140 }
keunyoungcc449f72015-08-12 10:46:27 -0700141 VehicleHal.releaseInstance();
keunyoungca515072015-07-10 12:21:47 -0700142 }
143
keunyoung4b0212c2015-10-29 17:11:57 -0700144 /** Only for CarTestService */
145 void startMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700146 reinitServices();
147 }
148
keunyoung4b0212c2015-10-29 17:11:57 -0700149 /** Only for CarTestService */
150 void stopMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700151 reinitServices();
152 }
153
keunyoung6b197692015-11-16 13:54:38 -0800154 /** Reset all services when starting / stopping vehicle hal mocking */
keunyoung1ab8e182015-09-24 09:25:22 -0700155 private void reinitServices() {
156 for (int i = mAllServices.length - 1; i >= 0; i--) {
157 mAllServices[i].release();
158 }
159 for (CarServiceBase service: mAllServices) {
160 service.init();
161 }
162 }
163
keunyoungca515072015-07-10 12:21:47 -0700164 @Override
keunyoungca515072015-07-10 12:21:47 -0700165 public IBinder getCarService(String serviceName) {
166 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800167 case Car.AUDIO_SERVICE:
168 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700169 case Car.SENSOR_SERVICE:
170 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700171 case Car.INFO_SERVICE:
172 return mCarInfoService;
keunyounge4c90c42015-11-16 18:42:52 -0800173 case Car.APP_CONTEXT_SERVICE:
174 return mAppContextService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800175 case Car.PACKAGE_SERVICE:
176 return mCarPackageManagerService;
Steve Paik875616c2016-02-05 10:55:59 -0800177 case Car.CAMERA_SERVICE:
178 assertCameraPermission(mContext);
179 return mCarCameraService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800180 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700181 assertHvacPermission(mContext);
182 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800183 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800184 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700185 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800186 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700187 assertNavigationManagerPermission(mContext);
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800188 return mCarNavigationService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800189 case Car.PROJECTION_SERVICE:
190 assertProjectionPermission(mContext);
191 return mCarProjectionService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800192 case Car.TEST_SERVICE: {
keunyoung1ab8e182015-09-24 09:25:22 -0700193 assertVehicleHalMockPermission(mContext);
194 synchronized (this) {
195 if (mCarTestService == null) {
196 mCarTestService = new CarTestService(mContext, this);
197 }
198 return mCarTestService;
199 }
200 }
keunyoungca515072015-07-10 12:21:47 -0700201 default:
202 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
203 return null;
204 }
205 }
206
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800207 @Override
208 public int getCarConnectionType() {
209 if (!isInMocking()) {
210 return Car.CONNECTION_TYPE_EMBEDDED;
211 } else {
212 return Car.CONNECTION_TYPE_EMBEDDED_MOCKING;
213 }
214 }
215
Keun-young Parka28d7b22016-02-29 16:54:29 -0800216 public CarServiceBase getCarInternalService(String serviceName) {
217 switch (serviceName) {
218 case INTERNAL_INPUT_SERVICE:
219 return mCarInputService;
220 default:
221 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
222 serviceName);
223 return null;
224 }
225 }
226
keunyoung4b0212c2015-10-29 17:11:57 -0700227 /**
228 * Whether mocking underlying HAL or not.
229 * @return
230 */
231 public synchronized boolean isInMocking() {
232 if (mCarTestService == null) {
233 return false;
234 }
235 return mCarTestService.isInMocking();
236 }
237
keunyoung1ab8e182015-09-24 09:25:22 -0700238 public static void assertVehicleHalMockPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800239 if (context.checkCallingOrSelfPermission(Car.PERMISSION_MOCK_VEHICLE_HAL)
keunyoung1ab8e182015-09-24 09:25:22 -0700240 != PackageManager.PERMISSION_GRANTED) {
241 throw new SecurityException("requires CAR_MOCK_VEHICLE_HAL permission");
242 }
243 }
244
Steve Paik875616c2016-02-05 10:55:59 -0800245 public static void assertCameraPermission(Context context) {
246 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_CAMERA)
247 != PackageManager.PERMISSION_GRANTED) {
248 throw new SecurityException(
249 "requires " + Car.PERMISSION_CAR_CAMERA);
250 }
251 }
252
Keun-young Parke31a8b22016-03-16 17:34:08 -0700253 public static void assertNavigationManagerPermission(Context context) {
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800254 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_NAVIGATION_MANAGER)
255 != PackageManager.PERMISSION_GRANTED) {
256 throw new SecurityException(
257 "requires " + Car.PERMISSION_CAR_NAVIGATION_MANAGER);
258 }
259 }
260
Steve Paik66481982015-10-27 15:22:38 -0700261 public static void assertHvacPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800262 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_HVAC)
Steve Paik66481982015-10-27 15:22:38 -0700263 != PackageManager.PERMISSION_GRANTED) {
264 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800265 "requires " + Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700266 }
267 }
268
keunyoung6b197692015-11-16 13:54:38 -0800269 private static void assertRadioPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800270 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_RADIO)
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700271 != PackageManager.PERMISSION_GRANTED) {
272 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800273 "requires permission " + Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700274 }
275 }
276
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800277 public static void assertProjectionPermission(Context context) {
278 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_PROJECTION)
279 != PackageManager.PERMISSION_GRANTED) {
280 throw new SecurityException(
281 "requires " + Car.PERMISSION_CAR_PROJECTION);
282 }
283 }
284
keunyoungcc449f72015-08-12 10:46:27 -0700285 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700286 writer.println("*Dump all services*");
287 for (CarServiceBase service: mAllServices) {
288 service.dump(writer);
289 }
keunyoung1ab8e182015-09-24 09:25:22 -0700290 CarTestService testService = mCarTestService;
291 if (testService != null) {
292 testService.dump(writer);
293 }
keunyoungcc449f72015-08-12 10:46:27 -0700294 }
keunyoungca515072015-07-10 12:21:47 -0700295}