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 @GuardedBy("mCarLock") 55 private boolean mIsCarReady; 56 57 @Inject CarServiceProvider(@arSysUIDumpable Context context)58 public CarServiceProvider(@CarSysUIDumpable Context context) { 59 mContext = context; 60 mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT, 61 (car, ready) -> { 62 synchronized (mCarLock) { 63 synchronized (mListeners) { 64 mIsCarReady = ready; 65 mCar = car; 66 if (ready) { 67 for (CarServiceOnConnectedListener listener : mListeners) { 68 listener.onConnected(mCar); 69 } 70 } 71 } 72 } 73 }); 74 } 75 76 @VisibleForTesting CarServiceProvider(Context context, Car car)77 public CarServiceProvider(Context context, Car car) { 78 mContext = context; 79 mCar = car; 80 } 81 82 /** 83 * Let's other components hook into the connection to the car service. If we're already 84 * connected to the car service, the callback is immediately triggered. 85 */ 86 @AnyThread addListener(CarServiceOnConnectedListener listener)87 public void addListener(CarServiceOnConnectedListener listener) { 88 synchronized (mCarLock) { 89 if (mIsCarReady) { 90 listener.onConnected(mCar); 91 } 92 synchronized (mListeners) { 93 mListeners.add(listener); 94 } 95 } 96 } 97 98 /** 99 * Remove a car service connection listener. 100 */ 101 @AnyThread removeListener(CarServiceOnConnectedListener listener)102 public void removeListener(CarServiceOnConnectedListener listener) { 103 synchronized (mListeners) { 104 mListeners.remove(listener); 105 } 106 } 107 108 /** 109 * Listener which is triggered when Car Service is connected. 110 */ 111 public interface CarServiceOnConnectedListener { 112 /** This will be called when the car service has successfully been connected. */ onConnected(Car car)113 void onConnected(Car car); 114 } 115 } 116