summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/dialer/UiBluetoothMonitor.java
blob: fdc3560cb7e451e08f8f4c33e05ac93b2a116e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.dialer;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Class that responsible for getting status of bluetooth connections.
 */
public class UiBluetoothMonitor {
    private static String TAG = "Em.BtMonitor";

    private List<Listener> mListeners = new CopyOnWriteArrayList<>();
    private final Context mContext;
    private final BluetoothAdapter mBluetoothAdapter;
    private final BluetoothBroadcastReceiver mBluetoothBroadcastReceiver;

    UiBluetoothMonitor(Context context) {
        mContext = context;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
    }

    public void tearDown() {
        mBluetoothBroadcastReceiver.tearDown();
    }

    public boolean isBluetoothEnabled() {
        return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
    }

    public boolean isHfpConnected() {
        if (mBluetoothAdapter == null) {
            return false;
        }
        return mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET_CLIENT)
                == BluetoothProfile.STATE_CONNECTED;
    }
    public boolean isBluetoothPaired() {
        return mBluetoothAdapter != null && mBluetoothAdapter.getBondedDevices().size() > 0;
    }

    public void addListener(Listener listener) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "addListener: " + listener);
        }
        mListeners.add(listener);
    }

    protected void notifyListeners() {
        for (Listener listener : mListeners) {
            listener.onStateChanged();
        }
    }

    public void removeListener(Listener listener) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "removeListener: " + listener);
        }
        mListeners.remove(listener);
    }

    public interface Listener {
        /**
         * Calls when state of Bluetooth was changed, for example when Bluetooth was turned off or
         * on, connection state was changed.
         */
        void onStateChanged();
    }

    private final class BluetoothBroadcastReceiver extends BroadcastReceiver {
        BluetoothBroadcastReceiver() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
            filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
            mContext.registerReceiver(this, filter);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Bluetooth broadcast intent action received: " + intent.getAction());
            }
            notifyListeners();
        }

        void tearDown() {
            mContext.unregisterReceiver(this);
        }
    }
}