• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.VisibleForTesting;
23 
24 import com.android.systemui.dagger.SysUISingleton;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 import javax.inject.Inject;
30 
31 /** Provides a common connection to the car service that can be shared. */
32 @SysUISingleton
33 public class CarServiceProvider {
34 
35     private final Context mContext;
36     private final List<CarServiceOnConnectedListener> mListeners = new ArrayList<>();
37     private Car mCar;
38 
39     @Inject
CarServiceProvider(Context context)40     public CarServiceProvider(Context context) {
41         mContext = context;
42         mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
43                 (car, ready) -> {
44                     mCar = car;
45 
46                     synchronized (mListeners) {
47                         for (CarServiceOnConnectedListener listener : mListeners) {
48                             if (ready) {
49                                 listener.onConnected(mCar);
50                             }
51                         }
52                     }
53                 });
54     }
55 
56     @VisibleForTesting
CarServiceProvider(Context context, Car car)57     public CarServiceProvider(Context context, Car car) {
58         mContext = context;
59         mCar = car;
60     }
61 
62     /**
63      * Let's other components hook into the connection to the car service. If we're already
64      * connected to the car service, the callback is immediately triggered.
65      */
addListener(CarServiceOnConnectedListener listener)66     public void addListener(CarServiceOnConnectedListener listener) {
67         if (mCar.isConnected()) {
68             listener.onConnected(mCar);
69         }
70         mListeners.add(listener);
71     }
72 
73     /**
74      * Listener which is triggered when Car Service is connected.
75      */
76     public interface CarServiceOnConnectedListener {
77         /** This will be called when the car service has successfully been connected. */
onConnected(Car car)78         void onConnected(Car car);
79     }
80 }
81