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