blob: 779a48ff95a76f3b8d308a037493fe0fe652552a [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
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
17package com.android.car;
18
19import android.content.Context;
20import android.content.Intent;
keunyoung1ab8e182015-09-24 09:25:22 -070021import android.content.pm.PackageManager;
keunyoungca515072015-07-10 12:21:47 -070022import android.os.IBinder;
23import android.support.car.Car;
keunyoungca515072015-07-10 12:21:47 -070024import android.support.car.ICar;
25import android.support.car.ICarConnectionListener;
keunyoungca515072015-07-10 12:21:47 -070026import android.util.Log;
27
keunyoungcc449f72015-08-12 10:46:27 -070028import com.android.car.hal.VehicleHal;
keunyoungca515072015-07-10 12:21:47 -070029import com.android.internal.annotations.GuardedBy;
30
keunyounga3b28d82015-08-25 13:05:15 -070031import java.io.PrintWriter;
32
keunyoungca515072015-07-10 12:21:47 -070033public 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;
keunyoungcc449f72015-08-12 10:46:27 -070040 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070041
42 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070043 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070044 private final CarAudioService mCarAudioService;
keunyoung1ab8e182015-09-24 09:25:22 -070045 /** Test only service. Populate it only when necessary. */
46 @GuardedBy("this")
47 private CarTestService mCarTestService;
keunyounga3b28d82015-08-25 13:05:15 -070048 private final CarServiceBase[] mAllServices;
keunyoungca515072015-07-10 12:21:47 -070049
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;
keunyoungfe30ba02015-09-17 17:56:35 -070068 mHal = VehicleHal.getInstance();
keunyounga3b28d82015-08-25 13:05:15 -070069 mCarInfoService = new CarInfoService(serviceContext);
keunyoungca515072015-07-10 12:21:47 -070070 mCarSensorService = new CarSensorService(serviceContext);
keunyoungd32f4e62015-09-21 11:33:06 -070071 mCarAudioService = new CarAudioService(serviceContext);
keunyounga3b28d82015-08-25 13:05:15 -070072 // Be careful with order. Service depending on other service should be inited later.
73 mAllServices = new CarServiceBase[] {
74 mCarInfoService,
keunyoungd32f4e62015-09-21 11:33:06 -070075 mCarSensorService,
76 mCarAudioService };
keunyoungca515072015-07-10 12:21:47 -070077 }
78
79 private void init() {
keunyounga3b28d82015-08-25 13:05:15 -070080 for (CarServiceBase service: mAllServices) {
81 service.init();
82 }
keunyoungca515072015-07-10 12:21:47 -070083 }
84
85 private void release() {
keunyounga3b28d82015-08-25 13:05:15 -070086 // release done in opposite order from init
87 for (int i = mAllServices.length - 1; i >= 0; i--) {
88 mAllServices[i].release();
89 }
keunyoungcc449f72015-08-12 10:46:27 -070090 VehicleHal.releaseInstance();
keunyoungca515072015-07-10 12:21:47 -070091 }
92
keunyoung1ab8e182015-09-24 09:25:22 -070093 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
keunyoungca515072015-07-10 12:21:47 -0700110 @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;
keunyounga3b28d82015-08-25 13:05:15 -0700120 case Car.INFO_SERVICE:
121 return mCarInfoService;
keunyoung1ab8e182015-09-24 09:25:22 -0700122 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 }
keunyoungca515072015-07-10 12:21:47 -0700131 default:
132 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
133 return null;
134 }
135 }
136
137 @Override
keunyoungca515072015-07-10 12:21:47 -0700138 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 }
keunyoungcc449f72015-08-12 10:46:27 -0700162
keunyoung1ab8e182015-09-24 09:25:22 -0700163 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
keunyoungcc449f72015-08-12 10:46:27 -0700170 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700171 writer.println("*Dump all services*");
172 for (CarServiceBase service: mAllServices) {
173 service.dump(writer);
174 }
keunyoung1ab8e182015-09-24 09:25:22 -0700175 CarTestService testService = mCarTestService;
176 if (testService != null) {
177 testService.dump(writer);
178 }
keunyoungcc449f72015-08-12 10:46:27 -0700179 }
keunyoungca515072015-07-10 12:21:47 -0700180}