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