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 | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 24 | import android.support.car.Car; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 25 | import android.support.car.ICar; |
| 26 | import android.support.car.ICarConnectionListener; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 29 | import com.android.car.CarSystem; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 30 | import com.android.car.hal.VehicleHal; |
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 { |
| 36 | private static final int VERSION = 1; |
| 37 | |
| 38 | @GuardedBy("ICarImpl.class") |
| 39 | private static ICarImpl sInstance = null; |
| 40 | |
| 41 | private final Context mContext; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 42 | private final VehicleHal mHal; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 43 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 44 | private final CarPowerManagementService mCarPowerManagementService; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 45 | private final CarSensorService mCarSensorService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 46 | private final CarInfoService mCarInfoService; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 47 | private final CarAudioService mCarAudioService; |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 48 | private final CarRadioService mCarRadioService; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 49 | private final AppContextService mAppContextService; |
| 50 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 51 | /** Test only service. Populate it only when necessary. */ |
| 52 | @GuardedBy("this") |
| 53 | private CarTestService mCarTestService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 54 | private final CarServiceBase[] mAllServices; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 55 | |
| 56 | public synchronized static ICarImpl getInstance(Context serviceContext) { |
| 57 | if (sInstance == null) { |
| 58 | sInstance = new ICarImpl(serviceContext); |
| 59 | sInstance.init(); |
| 60 | } |
| 61 | return sInstance; |
| 62 | } |
| 63 | |
| 64 | public synchronized static void releaseInstance() { |
| 65 | if (sInstance == null) { |
| 66 | return; |
| 67 | } |
| 68 | sInstance.release(); |
| 69 | sInstance = null; |
| 70 | } |
| 71 | |
| 72 | public ICarImpl(Context serviceContext) { |
| 73 | mContext = serviceContext; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 74 | mHal = VehicleHal.getInstance(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 75 | mCarPowerManagementService = new CarPowerManagementService(serviceContext); |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 76 | mCarInfoService = new CarInfoService(serviceContext); |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 77 | mAppContextService = new AppContextService(serviceContext); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 78 | mCarSensorService = new CarSensorService(serviceContext); |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 79 | mCarAudioService = new CarAudioService(serviceContext, mAppContextService); |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 80 | mCarRadioService = new CarRadioService(serviceContext); |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 81 | |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 82 | // Be careful with order. Service depending on other service should be inited later. |
| 83 | mAllServices = new CarServiceBase[] { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 84 | mCarPowerManagementService, |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 85 | mCarInfoService, |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 86 | mAppContextService, |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 87 | mCarSensorService, |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 88 | mCarAudioService, |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 89 | mCarRadioService, |
| 90 | }; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private void init() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 94 | for (CarServiceBase service: mAllServices) { |
| 95 | service.init(); |
| 96 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private void release() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 100 | // release done in opposite order from init |
| 101 | for (int i = mAllServices.length - 1; i >= 0; i--) { |
| 102 | mAllServices[i].release(); |
| 103 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 104 | VehicleHal.releaseInstance(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 105 | } |
| 106 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 107 | /** Only for CarTestService */ |
| 108 | void startMocking() { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 109 | reinitServices(); |
| 110 | } |
| 111 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 112 | /** Only for CarTestService */ |
| 113 | void stopMocking() { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 114 | reinitServices(); |
| 115 | } |
| 116 | |
| 117 | private void reinitServices() { |
| 118 | for (int i = mAllServices.length - 1; i >= 0; i--) { |
| 119 | mAllServices[i].release(); |
| 120 | } |
| 121 | for (CarServiceBase service: mAllServices) { |
| 122 | service.init(); |
| 123 | } |
| 124 | } |
| 125 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public int getVersion() { |
| 128 | return VERSION; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public IBinder getCarService(String serviceName) { |
| 133 | switch (serviceName) { |
| 134 | case Car.SENSOR_SERVICE: |
| 135 | return mCarSensorService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 136 | case Car.INFO_SERVICE: |
| 137 | return mCarInfoService; |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 138 | case CarSystem.RADIO_SERVICE: |
| 139 | assertSystemUidOrPermission(mContext); |
| 140 | return mCarRadioService; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 141 | case CarSystemTest.TEST_SERVICE: { |
| 142 | assertVehicleHalMockPermission(mContext); |
| 143 | synchronized (this) { |
| 144 | if (mCarTestService == null) { |
| 145 | mCarTestService = new CarTestService(mContext, this); |
| 146 | } |
| 147 | return mCarTestService; |
| 148 | } |
| 149 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 150 | default: |
| 151 | Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName); |
| 152 | return null; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 157 | public boolean isConnectedToCar() { |
| 158 | return true; // always connected in embedded |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public int getCarConnectionType() { |
| 163 | return Car.CONNECTION_TYPE_EMBEDDED; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void registerCarConnectionListener(int clientVersion, ICarConnectionListener listener) { |
| 168 | //TODO |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void unregisterCarConnectionListener(ICarConnectionListener listener) { |
| 173 | //TODO |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean startCarActivity(Intent intent) { |
| 178 | //TODO |
| 179 | return false; |
| 180 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 181 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame^] | 182 | /** |
| 183 | * Whether mocking underlying HAL or not. |
| 184 | * @return |
| 185 | */ |
| 186 | public synchronized boolean isInMocking() { |
| 187 | if (mCarTestService == null) { |
| 188 | return false; |
| 189 | } |
| 190 | return mCarTestService.isInMocking(); |
| 191 | } |
| 192 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 193 | public static void assertVehicleHalMockPermission(Context context) { |
| 194 | if (context.checkCallingOrSelfPermission(CarSystemTest.PERMISSION_MOCK_VEHICLE_HAL) |
| 195 | != PackageManager.PERMISSION_GRANTED) { |
| 196 | throw new SecurityException("requires CAR_MOCK_VEHICLE_HAL permission"); |
| 197 | } |
| 198 | } |
| 199 | |
Sanket Agarwal | 3cf096a | 2015-10-13 14:46:31 -0700 | [diff] [blame] | 200 | private static void assertSystemUidOrPermission(Context context) { |
| 201 | if (getCallingUid() != Process.SYSTEM_UID && |
| 202 | context.checkCallingOrSelfPermission(CarSystem.PERMISSION_RADIO_VEHICLE_HAL) |
| 203 | != PackageManager.PERMISSION_GRANTED) { |
| 204 | throw new SecurityException( |
| 205 | "requires system app or " + CarSystem.PERMISSION_RADIO_VEHICLE_HAL); |
| 206 | } |
| 207 | } |
| 208 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 209 | void dump(PrintWriter writer) { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 210 | writer.println("*Dump all services*"); |
| 211 | for (CarServiceBase service: mAllServices) { |
| 212 | service.dump(writer); |
| 213 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 214 | CarTestService testService = mCarTestService; |
| 215 | if (testService != null) { |
| 216 | testService.dump(writer); |
| 217 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 218 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 219 | } |