blob: b02bce61d05d936a1eba9a14431ea2b1bbcf8f93 [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;
21import android.os.IBinder;
22import android.support.car.Car;
keunyoungca515072015-07-10 12:21:47 -070023import android.support.car.ICar;
24import android.support.car.ICarConnectionListener;
keunyoungca515072015-07-10 12:21:47 -070025import android.util.Log;
26
keunyoungcc449f72015-08-12 10:46:27 -070027import com.android.car.hal.VehicleHal;
keunyoungca515072015-07-10 12:21:47 -070028import com.android.internal.annotations.GuardedBy;
29
keunyounga3b28d82015-08-25 13:05:15 -070030import java.io.PrintWriter;
31
keunyoungca515072015-07-10 12:21:47 -070032public class ICarImpl extends ICar.Stub {
33 private static final int VERSION = 1;
34
35 @GuardedBy("ICarImpl.class")
36 private static ICarImpl sInstance = null;
37
38 private final Context mContext;
keunyoungcc449f72015-08-12 10:46:27 -070039 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070040
41 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070042 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070043 private final CarAudioService mCarAudioService;
keunyounga3b28d82015-08-25 13:05:15 -070044 private final CarServiceBase[] mAllServices;
keunyoungca515072015-07-10 12:21:47 -070045
46 public synchronized static ICarImpl getInstance(Context serviceContext) {
47 if (sInstance == null) {
48 sInstance = new ICarImpl(serviceContext);
49 sInstance.init();
50 }
51 return sInstance;
52 }
53
54 public synchronized static void releaseInstance() {
55 if (sInstance == null) {
56 return;
57 }
58 sInstance.release();
59 sInstance = null;
60 }
61
62 public ICarImpl(Context serviceContext) {
63 mContext = serviceContext;
keunyoungfe30ba02015-09-17 17:56:35 -070064 mHal = VehicleHal.getInstance();
keunyounga3b28d82015-08-25 13:05:15 -070065 mCarInfoService = new CarInfoService(serviceContext);
keunyoungca515072015-07-10 12:21:47 -070066 mCarSensorService = new CarSensorService(serviceContext);
keunyoungd32f4e62015-09-21 11:33:06 -070067 mCarAudioService = new CarAudioService(serviceContext);
keunyounga3b28d82015-08-25 13:05:15 -070068 // Be careful with order. Service depending on other service should be inited later.
69 mAllServices = new CarServiceBase[] {
70 mCarInfoService,
keunyoungd32f4e62015-09-21 11:33:06 -070071 mCarSensorService,
72 mCarAudioService };
keunyoungca515072015-07-10 12:21:47 -070073 }
74
75 private void init() {
keunyounga3b28d82015-08-25 13:05:15 -070076 for (CarServiceBase service: mAllServices) {
77 service.init();
78 }
keunyoungca515072015-07-10 12:21:47 -070079 }
80
81 private void release() {
keunyounga3b28d82015-08-25 13:05:15 -070082 // release done in opposite order from init
83 for (int i = mAllServices.length - 1; i >= 0; i--) {
84 mAllServices[i].release();
85 }
keunyoungcc449f72015-08-12 10:46:27 -070086 VehicleHal.releaseInstance();
keunyoungca515072015-07-10 12:21:47 -070087 }
88
89 @Override
90 public int getVersion() {
91 return VERSION;
92 }
93
94 @Override
95 public IBinder getCarService(String serviceName) {
96 switch (serviceName) {
97 case Car.SENSOR_SERVICE:
98 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070099 case Car.INFO_SERVICE:
100 return mCarInfoService;
keunyoungca515072015-07-10 12:21:47 -0700101 default:
102 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
103 return null;
104 }
105 }
106
107 @Override
keunyoungca515072015-07-10 12:21:47 -0700108 public boolean isConnectedToCar() {
109 return true; // always connected in embedded
110 }
111
112 @Override
113 public int getCarConnectionType() {
114 return Car.CONNECTION_TYPE_EMBEDDED;
115 }
116
117 @Override
118 public void registerCarConnectionListener(int clientVersion, ICarConnectionListener listener) {
119 //TODO
120 }
121
122 @Override
123 public void unregisterCarConnectionListener(ICarConnectionListener listener) {
124 //TODO
125 }
126
127 @Override
128 public boolean startCarActivity(Intent intent) {
129 //TODO
130 return false;
131 }
keunyoungcc449f72015-08-12 10:46:27 -0700132
133 void dump(PrintWriter writer) {
keunyounga3b28d82015-08-25 13:05:15 -0700134 writer.println("*Dump all services*");
135 for (CarServiceBase service: mAllServices) {
136 service.dump(writer);
137 }
keunyoungcc449f72015-08-12 10:46:27 -0700138 }
keunyoungca515072015-07-10 12:21:47 -0700139}