blob: 315b24ec25558957cf1d9f5ac4b22c88bf63a3b4 [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;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070060 private final AppFocusService mAppFocusService;
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);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070095 mAppFocusService = new AppFocusService(serviceContext);
keunyoungca515072015-07-10 12:21:47 -070096 mCarSensorService = new CarSensorService(serviceContext);
Yao Chenc4d442f2016-04-08 11:33:47 -070097 mCarAudioService = new CarAudioService(serviceContext, mCarInputService);
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(
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700105 mAppFocusService, mInstrumentClusterService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700106 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700107 mCarPowerManagementService, mCarAudioService, this);
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,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700116 mAppFocusService,
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 }
Keun-young Park021310d2016-04-25 21:09:39 -0700159 VehicleHal.getInstance().release();
160 VehicleHal.getInstance().init();
keunyoung1ab8e182015-09-24 09:25:22 -0700161 for (CarServiceBase service: mAllServices) {
162 service.init();
163 }
164 }
165
keunyoungca515072015-07-10 12:21:47 -0700166 @Override
keunyoungca515072015-07-10 12:21:47 -0700167 public IBinder getCarService(String serviceName) {
168 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800169 case Car.AUDIO_SERVICE:
170 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700171 case Car.SENSOR_SERVICE:
172 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700173 case Car.INFO_SERVICE:
174 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700175 case Car.APP_FOCUS_SERVICE:
176 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800177 case Car.PACKAGE_SERVICE:
178 return mCarPackageManagerService;
Steve Paik875616c2016-02-05 10:55:59 -0800179 case Car.CAMERA_SERVICE:
180 assertCameraPermission(mContext);
181 return mCarCameraService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800182 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700183 assertHvacPermission(mContext);
184 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800185 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800186 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700187 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800188 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700189 assertNavigationManagerPermission(mContext);
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800190 return mCarNavigationService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800191 case Car.PROJECTION_SERVICE:
192 assertProjectionPermission(mContext);
193 return mCarProjectionService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800194 case Car.TEST_SERVICE: {
keunyoung1ab8e182015-09-24 09:25:22 -0700195 assertVehicleHalMockPermission(mContext);
196 synchronized (this) {
197 if (mCarTestService == null) {
198 mCarTestService = new CarTestService(mContext, this);
199 }
200 return mCarTestService;
201 }
202 }
keunyoungca515072015-07-10 12:21:47 -0700203 default:
204 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
205 return null;
206 }
207 }
208
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800209 @Override
210 public int getCarConnectionType() {
211 if (!isInMocking()) {
212 return Car.CONNECTION_TYPE_EMBEDDED;
213 } else {
214 return Car.CONNECTION_TYPE_EMBEDDED_MOCKING;
215 }
216 }
217
Keun-young Parka28d7b22016-02-29 16:54:29 -0800218 public CarServiceBase getCarInternalService(String serviceName) {
219 switch (serviceName) {
220 case INTERNAL_INPUT_SERVICE:
221 return mCarInputService;
222 default:
223 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
224 serviceName);
225 return null;
226 }
227 }
228
keunyoung4b0212c2015-10-29 17:11:57 -0700229 /**
230 * Whether mocking underlying HAL or not.
231 * @return
232 */
233 public synchronized boolean isInMocking() {
234 if (mCarTestService == null) {
235 return false;
236 }
237 return mCarTestService.isInMocking();
238 }
239
keunyoung1ab8e182015-09-24 09:25:22 -0700240 public static void assertVehicleHalMockPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800241 if (context.checkCallingOrSelfPermission(Car.PERMISSION_MOCK_VEHICLE_HAL)
keunyoung1ab8e182015-09-24 09:25:22 -0700242 != PackageManager.PERMISSION_GRANTED) {
243 throw new SecurityException("requires CAR_MOCK_VEHICLE_HAL permission");
244 }
245 }
246
Steve Paik875616c2016-02-05 10:55:59 -0800247 public static void assertCameraPermission(Context context) {
248 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_CAMERA)
249 != PackageManager.PERMISSION_GRANTED) {
250 throw new SecurityException(
251 "requires " + Car.PERMISSION_CAR_CAMERA);
252 }
253 }
254
Keun-young Parke31a8b22016-03-16 17:34:08 -0700255 public static void assertNavigationManagerPermission(Context context) {
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800256 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_NAVIGATION_MANAGER)
257 != PackageManager.PERMISSION_GRANTED) {
258 throw new SecurityException(
259 "requires " + Car.PERMISSION_CAR_NAVIGATION_MANAGER);
260 }
261 }
262
Steve Paik66481982015-10-27 15:22:38 -0700263 public static void assertHvacPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800264 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_HVAC)
Steve Paik66481982015-10-27 15:22:38 -0700265 != PackageManager.PERMISSION_GRANTED) {
266 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800267 "requires " + Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700268 }
269 }
270
keunyoung6b197692015-11-16 13:54:38 -0800271 private static void assertRadioPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800272 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_RADIO)
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700273 != PackageManager.PERMISSION_GRANTED) {
274 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800275 "requires permission " + Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700276 }
277 }
278
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800279 public static void assertProjectionPermission(Context context) {
280 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_PROJECTION)
281 != PackageManager.PERMISSION_GRANTED) {
282 throw new SecurityException(
283 "requires " + Car.PERMISSION_CAR_PROJECTION);
284 }
285 }
286
keunyoungcc449f72015-08-12 10:46:27 -0700287 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700288 writer.println("*Dump all services*");
289 for (CarServiceBase service: mAllServices) {
290 service.dump(writer);
291 }
keunyoung1ab8e182015-09-24 09:25:22 -0700292 CarTestService testService = mCarTestService;
293 if (testService != null) {
294 testService.dump(writer);
295 }
keunyoungcc449f72015-08-12 10:46:27 -0700296 }
keunyoungca515072015-07-10 12:21:47 -0700297}