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