1 /*
2  * Copyright (C) 2016 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 package com.android.car.dialer.bluetooth;
17 
18 import com.android.car.dialer.ClassFactory;
19 
20 import android.util.Log;
21 
22 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 
25 /**
26  * Class that responsible for getting status of bluetooth connections.
27  */
28 public abstract class UiBluetoothMonitor {
29     private List<Listener> mListeners = new CopyOnWriteArrayList<>();
30 
31     private static String TAG = "Em.BtMonitor";
32 
33     private static UiBluetoothMonitor sInstance;
34     private static Object sInstanceLock = new Object();
35 
isBluetoothEnabled()36     public abstract boolean isBluetoothEnabled();
isHfpConnected()37     public abstract boolean isHfpConnected();
isBluetoothPaired()38     public abstract boolean isBluetoothPaired();
39 
getInstance()40     public static UiBluetoothMonitor getInstance() {
41         if (sInstance == null) {
42             synchronized (sInstanceLock) {
43                 if (sInstance == null) {
44                     if (Log.isLoggable(TAG, Log.DEBUG)) {
45                         Log.d(TAG, "Creating an instance of UiBluetoothMonitor");
46                     }
47                     sInstance = ClassFactory.getFactory().createBluetoothMonitor();
48                 }
49             }
50         }
51         return sInstance;
52     }
53 
addListener(Listener listener)54     public void addListener(Listener listener) {
55         if (Log.isLoggable(TAG, Log.DEBUG)) {
56             Log.d(TAG, "addListener: " + listener);
57         }
58         mListeners.add(listener);
59     }
60 
notifyListeners()61     protected void notifyListeners() {
62         for (Listener listener : mListeners) {
63             listener.onStateChanged();
64         }
65     }
66 
removeListener(Listener listener)67     public void removeListener(Listener listener) {
68         if (Log.isLoggable(TAG, Log.DEBUG)) {
69             Log.d(TAG, "removeListener: " + listener);
70         }
71         mListeners.remove(listener);
72     }
73 
74     public interface Listener {
75         /**
76          * Calls when state of Bluetooth was changed, for example when Bluetooth was turned off or
77          * on, connection state was changed.
78          */
onStateChanged()79         void onStateChanged();
80     }
81 }
82