keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.car; |
| 18 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 19 | import android.car.Car; |
| 20 | import android.car.ICar; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 21 | import android.content.Context; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 23 | import android.os.IBinder; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 26 | import com.android.car.cluster.CarNavigationService; |
| 27 | import com.android.car.cluster.InstrumentClusterService; |
| 28 | import com.android.car.cluster.MediaStatusService; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 29 | import com.android.car.hal.VehicleHal; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 30 | import com.android.car.pm.CarPackageManagerService; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 31 | import com.android.internal.annotations.GuardedBy; |
| 32 | |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 33 | import java.io.PrintWriter; |
| 34 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 35 | public class ICarImpl extends ICar.Stub { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 36 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 37 | public static final String INTERNAL_INPUT_SERVICE = "internal_input"; |
| 38 | |
| 39 | // load jni for all services here |
| 40 | static { |
| 41 | System.loadLibrary("jni_car_service"); |
| 42 | } |
| 43 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 44 | @GuardedBy("ICarImpl.class") |
| 45 | private static ICarImpl sInstance = null; |
| 46 | |
| 47 | private final Context mContext; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 48 | private final VehicleHal mHal; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 49 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 50 | private final CarPowerManagementService mCarPowerManagementService; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 51 | private final CarPackageManagerService mCarPackageManagerService; |
| 52 | private final CarInputService mCarInputService; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 53 | private final CarSensorService mCarSensorService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 54 | private final CarInfoService mCarInfoService; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 55 | private final CarAudioService mCarAudioService; |
Vitalii Tomkiv | 6e5ee61 | 2016-03-09 14:57:32 -0800 | [diff] [blame^] | 56 | private final CarProjectionService mCarProjectionService; |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 57 | private final CarCameraService mCarCameraService; |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 58 | private final CarHvacService mCarHvacService; |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 59 | private final CarRadioService mCarRadioService; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 60 | private final CarNightService mCarNightService; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 61 | private final AppContextService mAppContextService; |
Yao Chen | 3a7976d | 2016-01-20 17:27:08 -0800 | [diff] [blame] | 62 | private final GarageModeService mGarageModeService; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 63 | private final CarNavigationService mCarNavigationService; |
| 64 | private final MediaStatusService mMediaStatusService; |
| 65 | private final InstrumentClusterService mInstrumentClusterService; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 66 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 67 | /** Test only service. Populate it only when necessary. */ |
| 68 | @GuardedBy("this") |
| 69 | private CarTestService mCarTestService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 70 | private final CarServiceBase[] mAllServices; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 71 | |
| 72 | public synchronized static ICarImpl getInstance(Context serviceContext) { |
| 73 | if (sInstance == null) { |
| 74 | sInstance = new ICarImpl(serviceContext); |
| 75 | sInstance.init(); |
| 76 | } |
| 77 | return sInstance; |
| 78 | } |
| 79 | |
| 80 | public synchronized static void releaseInstance() { |
| 81 | if (sInstance == null) { |
| 82 | return; |
| 83 | } |
| 84 | sInstance.release(); |
| 85 | sInstance = null; |
| 86 | } |
| 87 | |
| 88 | public ICarImpl(Context serviceContext) { |
| 89 | mContext = serviceContext; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 90 | mHal = VehicleHal.getInstance(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 91 | mCarPowerManagementService = new CarPowerManagementService(serviceContext); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 92 | mCarInputService = new CarInputService(serviceContext); |
Vitalii Tomkiv | 6e5ee61 | 2016-03-09 14:57:32 -0800 | [diff] [blame^] | 93 | mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService); |
Yao Chen | 3a7976d | 2016-01-20 17:27:08 -0800 | [diff] [blame] | 94 | mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService); |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 95 | mCarInfoService = new CarInfoService(serviceContext); |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 96 | mAppContextService = new AppContextService(serviceContext); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 97 | mCarSensorService = new CarSensorService(serviceContext); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 98 | mCarAudioService = new CarAudioService(serviceContext); |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 99 | mCarHvacService = new CarHvacService(serviceContext); |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 100 | mCarRadioService = new CarRadioService(serviceContext); |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 101 | mCarCameraService = new CarCameraService(serviceContext); |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 102 | mCarNightService = new CarNightService(serviceContext); |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 103 | mCarPackageManagerService = new CarPackageManagerService(serviceContext); |
| 104 | mInstrumentClusterService = new InstrumentClusterService(serviceContext); |
| 105 | mMediaStatusService = new MediaStatusService(serviceContext, mInstrumentClusterService); |
| 106 | mCarNavigationService = new CarNavigationService( |
| 107 | serviceContext, mAppContextService, mInstrumentClusterService); |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 108 | |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 109 | // Be careful with order. Service depending on other service should be inited later. |
| 110 | mAllServices = new CarServiceBase[] { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 111 | mCarPowerManagementService, |
Keun-young Park | 45fdcba | 2015-12-08 11:38:58 -0800 | [diff] [blame] | 112 | mCarPackageManagerService, |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 113 | mCarInputService, |
| 114 | mGarageModeService, |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 115 | mCarInfoService, |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 116 | mAppContextService, |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 117 | mCarSensorService, |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 118 | mCarAudioService, |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 119 | mCarHvacService, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 120 | mCarRadioService, |
| 121 | mCarCameraService, |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 122 | mCarNightService, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 123 | mInstrumentClusterService, |
Vitalii Tomkiv | 6e5ee61 | 2016-03-09 14:57:32 -0800 | [diff] [blame^] | 124 | mCarProjectionService, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 125 | mCarNavigationService, |
| 126 | mMediaStatusService |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 127 | }; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private void init() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 131 | for (CarServiceBase service: mAllServices) { |
| 132 | service.init(); |
| 133 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private void release() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 137 | // release done in opposite order from init |
| 138 | for (int i = mAllServices.length - 1; i >= 0; i--) { |
| 139 | mAllServices[i].release(); |
| 140 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 141 | VehicleHal.releaseInstance(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 142 | } |
| 143 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 144 | /** Only for CarTestService */ |
| 145 | void startMocking() { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 146 | reinitServices(); |
| 147 | } |
| 148 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 149 | /** Only for CarTestService */ |
| 150 | void stopMocking() { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 151 | reinitServices(); |
| 152 | } |
| 153 | |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 154 | /** Reset all services when starting / stopping vehicle hal mocking */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 155 | 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 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 164 | @Override |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 165 | public IBinder getCarService(String serviceName) { |
| 166 | switch (serviceName) { |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 167 | case Car.AUDIO_SERVICE: |
| 168 | return mCarAudioService; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 169 | case Car.SENSOR_SERVICE: |
| 170 | return mCarSensorService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 171 | case Car.INFO_SERVICE: |
| 172 | return mCarInfoService; |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 173 | case Car.APP_CONTEXT_SERVICE: |
| 174 | return mAppContextService; |
Keun-young Park | 45fdcba | 2015-12-08 11:38:58 -0800 | [diff] [blame] | 175 | case Car.PACKAGE_SERVICE: |
| 176 | return mCarPackageManagerService; |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 177 | case Car.CAMERA_SERVICE: |
| 178 | assertCameraPermission(mContext); |
| 179 | return mCarCameraService; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 180 | case Car.HVAC_SERVICE: |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 181 | assertHvacPermission(mContext); |
| 182 | return mCarHvacService; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 183 | case Car.RADIO_SERVICE: |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 184 | assertRadioPermission(mContext); |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 185 | return mCarRadioService; |
Pavel Maltsev | 7a948e5 | 2016-02-02 23:30:14 -0800 | [diff] [blame] | 186 | case Car.CAR_NAVIGATION_SERVICE: |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 187 | return mCarNavigationService; |
Vitalii Tomkiv | 6e5ee61 | 2016-03-09 14:57:32 -0800 | [diff] [blame^] | 188 | case Car.PROJECTION_SERVICE: |
| 189 | assertProjectionPermission(mContext); |
| 190 | return mCarProjectionService; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 191 | case Car.TEST_SERVICE: { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 192 | assertVehicleHalMockPermission(mContext); |
| 193 | synchronized (this) { |
| 194 | if (mCarTestService == null) { |
| 195 | mCarTestService = new CarTestService(mContext, this); |
| 196 | } |
| 197 | return mCarTestService; |
| 198 | } |
| 199 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 200 | default: |
| 201 | Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName); |
| 202 | return null; |
| 203 | } |
| 204 | } |
| 205 | |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 206 | @Override |
| 207 | public int getCarConnectionType() { |
| 208 | if (!isInMocking()) { |
| 209 | return Car.CONNECTION_TYPE_EMBEDDED; |
| 210 | } else { |
| 211 | return Car.CONNECTION_TYPE_EMBEDDED_MOCKING; |
| 212 | } |
| 213 | } |
| 214 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 215 | public CarServiceBase getCarInternalService(String serviceName) { |
| 216 | switch (serviceName) { |
| 217 | case INTERNAL_INPUT_SERVICE: |
| 218 | return mCarInputService; |
| 219 | default: |
| 220 | Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" + |
| 221 | serviceName); |
| 222 | return null; |
| 223 | } |
| 224 | } |
| 225 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Whether mocking underlying HAL or not. |
| 228 | * @return |
| 229 | */ |
| 230 | public synchronized boolean isInMocking() { |
| 231 | if (mCarTestService == null) { |
| 232 | return false; |
| 233 | } |
| 234 | return mCarTestService.isInMocking(); |
| 235 | } |
| 236 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 237 | public static void assertVehicleHalMockPermission(Context context) { |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 238 | if (context.checkCallingOrSelfPermission(Car.PERMISSION_MOCK_VEHICLE_HAL) |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 239 | != PackageManager.PERMISSION_GRANTED) { |
| 240 | throw new SecurityException("requires CAR_MOCK_VEHICLE_HAL permission"); |
| 241 | } |
| 242 | } |
| 243 | |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 244 | public static void assertCameraPermission(Context context) { |
| 245 | if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_CAMERA) |
| 246 | != PackageManager.PERMISSION_GRANTED) { |
| 247 | throw new SecurityException( |
| 248 | "requires " + Car.PERMISSION_CAR_CAMERA); |
| 249 | } |
| 250 | } |
| 251 | |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 252 | public static void assertHvacPermission(Context context) { |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 253 | if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_HVAC) |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 254 | != PackageManager.PERMISSION_GRANTED) { |
| 255 | throw new SecurityException( |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 256 | "requires " + Car.PERMISSION_CAR_HVAC); |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
keunyoung | 6b19769 | 2015-11-16 13:54:38 -0800 | [diff] [blame] | 260 | private static void assertRadioPermission(Context context) { |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 261 | if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_RADIO) |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 262 | != PackageManager.PERMISSION_GRANTED) { |
| 263 | throw new SecurityException( |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 264 | "requires permission " + Car.PERMISSION_CAR_RADIO); |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Vitalii Tomkiv | 6e5ee61 | 2016-03-09 14:57:32 -0800 | [diff] [blame^] | 268 | public static void assertProjectionPermission(Context context) { |
| 269 | if (context.checkCallingOrSelfPermission(Car.PERMISSION_CAR_PROJECTION) |
| 270 | != PackageManager.PERMISSION_GRANTED) { |
| 271 | throw new SecurityException( |
| 272 | "requires " + Car.PERMISSION_CAR_PROJECTION); |
| 273 | } |
| 274 | } |
| 275 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 276 | void dump(PrintWriter writer) { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 277 | writer.println("*Dump all services*"); |
| 278 | for (CarServiceBase service: mAllServices) { |
| 279 | service.dump(writer); |
| 280 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 281 | CarTestService testService = mCarTestService; |
| 282 | if (testService != null) { |
| 283 | testService.dump(writer); |
| 284 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 285 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 286 | } |