blob: d2329ba5df41e6bddfe6fa9d7de53278a9501216 [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;
Pavel Maltsev0477e292016-05-27 12:22:36 -070021import android.car.cluster.renderer.IInstrumentClusterNavigation;
keunyoungca515072015-07-10 12:21:47 -070022import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070023import android.content.pm.PackageManager;
keunyoungca515072015-07-10 12:21:47 -070024import android.os.IBinder;
keunyoungca515072015-07-10 12:21:47 -070025import android.util.Log;
26
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080027import 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 InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070063 private final SystemStateControllerService mSystemStateControllerService;
keunyounga74b9ca2015-10-21 13:33:58 -070064
keunyoung1ab8e182015-09-24 09:25:22 -070065 /** Test only service. Populate it only when necessary. */
66 @GuardedBy("this")
67 private CarTestService mCarTestService;
keunyounga3b28d82015-08-25 13:05:15 -070068 private final CarServiceBase[] mAllServices;
keunyoungca515072015-07-10 12:21:47 -070069
70 public synchronized static ICarImpl getInstance(Context serviceContext) {
71 if (sInstance == null) {
72 sInstance = new ICarImpl(serviceContext);
73 sInstance.init();
74 }
75 return sInstance;
76 }
77
78 public synchronized static void releaseInstance() {
79 if (sInstance == null) {
80 return;
81 }
82 sInstance.release();
83 sInstance = null;
84 }
85
86 public ICarImpl(Context serviceContext) {
87 mContext = serviceContext;
keunyoungfe30ba02015-09-17 17:56:35 -070088 mHal = VehicleHal.getInstance();
keunyoung4b0212c2015-10-29 17:11:57 -070089 mCarPowerManagementService = new CarPowerManagementService(serviceContext);
Keun-young Parka28d7b22016-02-29 16:54:29 -080090 mCarInputService = new CarInputService(serviceContext);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080091 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -080092 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
keunyounga3b28d82015-08-25 13:05:15 -070093 mCarInfoService = new CarInfoService(serviceContext);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070094 mAppFocusService = new AppFocusService(serviceContext);
keunyoungca515072015-07-10 12:21:47 -070095 mCarSensorService = new CarSensorService(serviceContext);
Yao Chenc4d442f2016-04-08 11:33:47 -070096 mCarAudioService = new CarAudioService(serviceContext, mCarInputService);
Steve Paik66481982015-10-27 15:22:38 -070097 mCarHvacService = new CarHvacService(serviceContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070098 mCarRadioService = new CarRadioService(serviceContext);
Steve Paik875616c2016-02-05 10:55:59 -080099 mCarCameraService = new CarCameraService(serviceContext);
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800100 mCarNightService = new CarNightService(serviceContext);
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800101 mCarPackageManagerService = new CarPackageManagerService(serviceContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700102 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
103 mAppFocusService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700104 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700105 mCarPowerManagementService, mCarAudioService, this);
keunyounga74b9ca2015-10-21 13:33:58 -0700106
keunyounga3b28d82015-08-25 13:05:15 -0700107 // Be careful with order. Service depending on other service should be inited later.
108 mAllServices = new CarServiceBase[] {
keunyoung4b0212c2015-10-29 17:11:57 -0700109 mCarPowerManagementService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800110 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800111 mCarInputService,
112 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700113 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700114 mAppFocusService,
keunyoungd32f4e62015-09-21 11:33:06 -0700115 mCarSensorService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700116 mCarAudioService,
Steve Paik66481982015-10-27 15:22:38 -0700117 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800118 mCarRadioService,
119 mCarCameraService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800120 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800121 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800122 mCarProjectionService,
Keun-young Parkd73afae2016-04-08 20:03:32 -0700123 mSystemStateControllerService
keunyounga74b9ca2015-10-21 13:33:58 -0700124 };
keunyoungca515072015-07-10 12:21:47 -0700125 }
126
127 private void init() {
keunyounga3b28d82015-08-25 13:05:15 -0700128 for (CarServiceBase service: mAllServices) {
129 service.init();
130 }
keunyoungca515072015-07-10 12:21:47 -0700131 }
132
133 private void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700134 // release done in opposite order from init
135 for (int i = mAllServices.length - 1; i >= 0; i--) {
136 mAllServices[i].release();
137 }
keunyoungcc449f72015-08-12 10:46:27 -0700138 VehicleHal.releaseInstance();
keunyoungca515072015-07-10 12:21:47 -0700139 }
140
keunyoung4b0212c2015-10-29 17:11:57 -0700141 /** Only for CarTestService */
142 void startMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700143 reinitServices();
144 }
145
keunyoung4b0212c2015-10-29 17:11:57 -0700146 /** Only for CarTestService */
147 void stopMocking() {
keunyoung1ab8e182015-09-24 09:25:22 -0700148 reinitServices();
149 }
150
keunyoung6b197692015-11-16 13:54:38 -0800151 /** Reset all services when starting / stopping vehicle hal mocking */
keunyoung1ab8e182015-09-24 09:25:22 -0700152 private void reinitServices() {
153 for (int i = mAllServices.length - 1; i >= 0; i--) {
154 mAllServices[i].release();
155 }
Keun-young Park021310d2016-04-25 21:09:39 -0700156 VehicleHal.getInstance().release();
157 VehicleHal.getInstance().init();
keunyoung1ab8e182015-09-24 09:25:22 -0700158 for (CarServiceBase service: mAllServices) {
159 service.init();
160 }
161 }
162
keunyoungca515072015-07-10 12:21:47 -0700163 @Override
keunyoungca515072015-07-10 12:21:47 -0700164 public IBinder getCarService(String serviceName) {
165 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800166 case Car.AUDIO_SERVICE:
167 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700168 case Car.SENSOR_SERVICE:
169 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700170 case Car.INFO_SERVICE:
171 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700172 case Car.APP_FOCUS_SERVICE:
173 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800174 case Car.PACKAGE_SERVICE:
175 return mCarPackageManagerService;
Steve Paik875616c2016-02-05 10:55:59 -0800176 case Car.CAMERA_SERVICE:
177 assertCameraPermission(mContext);
178 return mCarCameraService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800179 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700180 assertHvacPermission(mContext);
181 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800182 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800183 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700184 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800185 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700186 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700187 IInstrumentClusterNavigation navService =
188 mInstrumentClusterService.getNavigationService();
189 return navService == null ? null : navService.asBinder();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800190 case Car.PROJECTION_SERVICE:
191 assertProjectionPermission(mContext);
192 return mCarProjectionService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800193 case Car.TEST_SERVICE: {
keunyoung1ab8e182015-09-24 09:25:22 -0700194 assertVehicleHalMockPermission(mContext);
195 synchronized (this) {
196 if (mCarTestService == null) {
197 mCarTestService = new CarTestService(mContext, this);
198 }
199 return mCarTestService;
200 }
201 }
keunyoungca515072015-07-10 12:21:47 -0700202 default:
203 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
204 return null;
205 }
206 }
207
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800208 @Override
209 public int getCarConnectionType() {
210 if (!isInMocking()) {
211 return Car.CONNECTION_TYPE_EMBEDDED;
212 } else {
213 return Car.CONNECTION_TYPE_EMBEDDED_MOCKING;
214 }
215 }
216
Keun-young Parka28d7b22016-02-29 16:54:29 -0800217 public CarServiceBase getCarInternalService(String serviceName) {
218 switch (serviceName) {
219 case INTERNAL_INPUT_SERVICE:
220 return mCarInputService;
221 default:
222 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
223 serviceName);
224 return null;
225 }
226 }
227
keunyoung4b0212c2015-10-29 17:11:57 -0700228 /**
229 * Whether mocking underlying HAL or not.
230 * @return
231 */
232 public synchronized boolean isInMocking() {
233 if (mCarTestService == null) {
234 return false;
235 }
236 return mCarTestService.isInMocking();
237 }
238
keunyoung1ab8e182015-09-24 09:25:22 -0700239 public static void assertVehicleHalMockPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800240 if (context.checkCallingOrSelfPermission(Car.PERMISSION_MOCK_VEHICLE_HAL)
keunyoung1ab8e182015-09-24 09:25:22 -0700241 != PackageManager.PERMISSION_GRANTED) {
242 throw new SecurityException("requires CAR_MOCK_VEHICLE_HAL permission");
243 }
244 }
245
Steve Paik875616c2016-02-05 10:55:59 -0800246 public static void assertCameraPermission(Context context) {
247 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_CAMERA)
248 != PackageManager.PERMISSION_GRANTED) {
249 throw new SecurityException(
250 "requires " + Car.PERMISSION_CAR_CAMERA);
251 }
252 }
253
Keun-young Parke31a8b22016-03-16 17:34:08 -0700254 public static void assertNavigationManagerPermission(Context context) {
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800255 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_NAVIGATION_MANAGER)
256 != PackageManager.PERMISSION_GRANTED) {
257 throw new SecurityException(
258 "requires " + Car.PERMISSION_CAR_NAVIGATION_MANAGER);
259 }
260 }
261
Steve Paik66481982015-10-27 15:22:38 -0700262 public static void assertHvacPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800263 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_HVAC)
Steve Paik66481982015-10-27 15:22:38 -0700264 != PackageManager.PERMISSION_GRANTED) {
265 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800266 "requires " + Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700267 }
268 }
269
keunyoung6b197692015-11-16 13:54:38 -0800270 private static void assertRadioPermission(Context context) {
Keun-young Parke54ac272016-02-16 19:02:18 -0800271 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_RADIO)
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700272 != PackageManager.PERMISSION_GRANTED) {
273 throw new SecurityException(
Keun-young Parke54ac272016-02-16 19:02:18 -0800274 "requires permission " + Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700275 }
276 }
277
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800278 public static void assertProjectionPermission(Context context) {
279 if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_PROJECTION)
280 != PackageManager.PERMISSION_GRANTED) {
281 throw new SecurityException(
282 "requires " + Car.PERMISSION_CAR_PROJECTION);
283 }
284 }
285
keunyoungcc449f72015-08-12 10:46:27 -0700286 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700287 writer.println("*Dump all services*");
288 for (CarServiceBase service: mAllServices) {
289 service.dump(writer);
290 }
keunyoung1ab8e182015-09-24 09:25:22 -0700291 CarTestService testService = mCarTestService;
292 if (testService != null) {
293 testService.dump(writer);
294 }
keunyoungcc449f72015-08-12 10:46:27 -0700295 }
keunyoungca515072015-07-10 12:21:47 -0700296}