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