1 /* 2 * Copyright (C) 2019 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.systemui.car; 18 19 import android.car.Car; 20 import android.content.Context; 21 22 import androidx.annotation.AnyThread; 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.internal.annotations.GuardedBy; 26 import com.android.systemui.car.dagger.CarSysUIDumpable; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 import javax.inject.Inject; 32 import javax.inject.Singleton; 33 34 /** Provides a common connection to the car service that can be shared. */ 35 // It needs to be @Singleton scoped because it is used by a number of components in @WMSingleton & 36 // @SysUISingleton scopes. 37 @Singleton 38 public class CarServiceProvider { 39 40 private final Context mContext; 41 /** 42 * mListeners is guarded by itself - when requiring locks on both mListeners and mCar, always 43 * obtain the car lock before the listeners. 44 */ 45 @GuardedBy("mListeners") 46 private final List<CarServiceOnConnectedListener> mListeners = new ArrayList<>(); 47 private final Object mCarLock = new Object(); 48 /** 49 * mCar is guarded by mCarLock - when requiring locks on both mListeners and mCar, always 50 * obtain the car lock before the listeners. 51 */ 52 @GuardedBy("mCarLock") 53 private Car mCar; 54 55 @Inject CarServiceProvider(@arSysUIDumpable Context context)56 public CarServiceProvider(@CarSysUIDumpable Context context) { 57 mContext = context; 58 mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT, 59 (car, ready) -> { 60 synchronized (mCarLock) { 61 synchronized (mListeners) { 62 mCar = car; 63 if (ready) { 64 for (CarServiceOnConnectedListener listener : mListeners) { 65 listener.onConnected(mCar); 66 } 67 } 68 } 69 } 70 }); 71 } 72 73 @VisibleForTesting CarServiceProvider(Context context, Car car)74 public CarServiceProvider(Context context, Car car) { 75 mContext = context; 76 mCar = car; 77 } 78 79 /** 80 * Let's other components hook into the connection to the car service. If we're already 81 * connected to the car service, the callback is immediately triggered. 82 */ 83 @AnyThread addListener(CarServiceOnConnectedListener listener)84 public void addListener(CarServiceOnConnectedListener listener) { 85 synchronized (mCarLock) { 86 if (mCar.isConnected()) { 87 listener.onConnected(mCar); 88 } 89 } 90 synchronized (mListeners) { 91 mListeners.add(listener); 92 } 93 } 94 95 /** 96 * Remove a car service connection listener. 97 */ 98 @AnyThread removeListener(CarServiceOnConnectedListener listener)99 public void removeListener(CarServiceOnConnectedListener listener) { 100 synchronized (mListeners) { 101 mListeners.remove(listener); 102 } 103 } 104 105 /** 106 * Listener which is triggered when Car Service is connected. 107 */ 108 public interface CarServiceOnConnectedListener { 109 /** This will be called when the car service has successfully been connected. */ onConnected(Car car)110 void onConnected(Car car); 111 } 112 } 113