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 android.support.car; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.ServiceConnection; 22 import android.os.Handler; 23 import android.os.IBinder; 24 import android.support.car.content.pm.CarPackageManagerEmbedded; 25 import android.support.car.hardware.CarSensorManagerEmbedded; 26 import android.support.car.media.CarAudioManagerEmbedded; 27 import android.support.car.navigation.CarNavigationStatusManagerEmbedded; 28 29 /** 30 * Default CarServiceLoader for system with built-in car service (=embedded). 31 * @hide 32 */ 33 public class CarServiceLoaderEmbedded extends CarServiceLoader { 34 35 private final ServiceConnection mServiceConnection = new ServiceConnection() { 36 37 @Override 38 public void onServiceConnected(ComponentName name, IBinder service) { 39 getConnectionCallback().onConnected(); 40 } 41 42 @Override 43 public void onServiceDisconnected(ComponentName name) { 44 getConnectionCallback().onDisconnected(); 45 } 46 }; 47 48 private final android.car.Car mEmbeddedCar; 49 50 /** @hide */ 51 CarServiceLoaderEmbedded(Context context, CarConnectionCallbackProxy carConnectionCallback, 52 Handler handler) { 53 super(context, carConnectionCallback, handler); 54 mEmbeddedCar = android.car.Car.createCar(context, mServiceConnection, handler); 55 } 56 57 @Override 58 public void connect() throws IllegalStateException { 59 mEmbeddedCar.connect(); 60 } 61 62 @Override 63 public void disconnect() { 64 mEmbeddedCar.disconnect(); 65 } 66 67 @Override 68 public boolean isConnected() { 69 return mEmbeddedCar.isConnected(); 70 } 71 72 @Override 73 public int getCarConnectionType() throws CarNotConnectedException { 74 @android.support.car.Car.ConnectionType 75 int carConnectionType = mEmbeddedCar.getCarConnectionType(); 76 return carConnectionType; 77 } 78 79 @Override 80 public Object getCarManager(String serviceName) throws CarNotConnectedException { 81 Object manager; 82 try { 83 manager = mEmbeddedCar.getCarManager(serviceName); 84 } catch (android.car.CarNotConnectedException e) { 85 throw new CarNotConnectedException(e); 86 } 87 88 if (manager == null) { 89 return null; 90 } 91 // For publicly available versions, return wrapper version. 92 switch (serviceName) { 93 case Car.AUDIO_SERVICE: 94 return new CarAudioManagerEmbedded(manager); 95 case Car.SENSOR_SERVICE: 96 return new CarSensorManagerEmbedded(manager, getContext()); 97 case Car.INFO_SERVICE: 98 return new CarInfoManagerEmbedded(manager); 99 case Car.APP_FOCUS_SERVICE: 100 return new CarAppFocusManagerEmbedded(manager); 101 case Car.PACKAGE_SERVICE: 102 return new CarPackageManagerEmbedded(manager); 103 case Car.CAR_NAVIGATION_SERVICE: 104 return new CarNavigationStatusManagerEmbedded(manager); 105 default: 106 return manager; 107 } 108 } 109 } 110