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; |
| 21 | import android.os.IBinder; |
| 22 | import android.support.car.Car; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 23 | import android.support.car.ICar; |
| 24 | import android.support.car.ICarConnectionListener; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 27 | import com.android.car.hal.VehicleHal; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
| 29 | |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 30 | import java.io.PrintWriter; |
| 31 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 32 | public 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; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 39 | private final VehicleHal mHal; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 40 | |
| 41 | private final CarSensorService mCarSensorService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 42 | private final CarInfoService mCarInfoService; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame^] | 43 | private final CarAudioService mCarAudioService; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 44 | private final CarServiceBase[] mAllServices; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 45 | |
| 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; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 64 | mHal = VehicleHal.getInstance(); |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 65 | mCarInfoService = new CarInfoService(serviceContext); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 66 | mCarSensorService = new CarSensorService(serviceContext); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame^] | 67 | mCarAudioService = new CarAudioService(serviceContext); |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 68 | // Be careful with order. Service depending on other service should be inited later. |
| 69 | mAllServices = new CarServiceBase[] { |
| 70 | mCarInfoService, |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame^] | 71 | mCarSensorService, |
| 72 | mCarAudioService }; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private void init() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 76 | for (CarServiceBase service: mAllServices) { |
| 77 | service.init(); |
| 78 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private void release() { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 82 | // release done in opposite order from init |
| 83 | for (int i = mAllServices.length - 1; i >= 0; i--) { |
| 84 | mAllServices[i].release(); |
| 85 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 86 | VehicleHal.releaseInstance(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 87 | } |
| 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; |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 99 | case Car.INFO_SERVICE: |
| 100 | return mCarInfoService; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 101 | default: |
| 102 | Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName); |
| 103 | return null; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 108 | 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 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 132 | |
| 133 | void dump(PrintWriter writer) { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 134 | writer.println("*Dump all services*"); |
| 135 | for (CarServiceBase service: mAllServices) { |
| 136 | service.dump(writer); |
| 137 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 138 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 139 | } |