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; 17 18 import android.bluetooth.BluetoothAdapter; 19 import android.bluetooth.BluetoothDevice; 20 import android.bluetooth.BluetoothHeadsetClient; 21 import android.bluetooth.BluetoothProfile; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.util.Log; 27 28 import java.util.List; 29 import java.util.concurrent.CopyOnWriteArrayList; 30 31 /** 32 * Class that responsible for getting status of bluetooth connections. 33 */ 34 public class UiBluetoothMonitor { 35 private static String TAG = "Em.BtMonitor"; 36 37 private List<Listener> mListeners = new CopyOnWriteArrayList<>(); 38 private final Context mContext; 39 private final BluetoothAdapter mBluetoothAdapter; 40 private final BluetoothBroadcastReceiver mBluetoothBroadcastReceiver; 41 UiBluetoothMonitor(Context context)42 UiBluetoothMonitor(Context context) { 43 mContext = context; 44 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 45 mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver(); 46 } 47 tearDown()48 public void tearDown() { 49 mBluetoothBroadcastReceiver.tearDown(); 50 } 51 isBluetoothEnabled()52 public boolean isBluetoothEnabled() { 53 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); 54 } 55 isHfpConnected()56 public boolean isHfpConnected() { 57 if (mBluetoothAdapter == null) { 58 return false; 59 } 60 return mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET_CLIENT) 61 == BluetoothProfile.STATE_CONNECTED; 62 } isBluetoothPaired()63 public boolean isBluetoothPaired() { 64 return mBluetoothAdapter != null && mBluetoothAdapter.getBondedDevices().size() > 0; 65 } 66 addListener(Listener listener)67 public void addListener(Listener listener) { 68 if (Log.isLoggable(TAG, Log.DEBUG)) { 69 Log.d(TAG, "addListener: " + listener); 70 } 71 mListeners.add(listener); 72 } 73 notifyListeners()74 protected void notifyListeners() { 75 for (Listener listener : mListeners) { 76 listener.onStateChanged(); 77 } 78 } 79 removeListener(Listener listener)80 public void removeListener(Listener listener) { 81 if (Log.isLoggable(TAG, Log.DEBUG)) { 82 Log.d(TAG, "removeListener: " + listener); 83 } 84 mListeners.remove(listener); 85 } 86 87 public interface Listener { 88 /** 89 * Calls when state of Bluetooth was changed, for example when Bluetooth was turned off or 90 * on, connection state was changed. 91 */ onStateChanged()92 void onStateChanged(); 93 } 94 95 private final class BluetoothBroadcastReceiver extends BroadcastReceiver { BluetoothBroadcastReceiver()96 BluetoothBroadcastReceiver() { 97 IntentFilter filter = new IntentFilter(); 98 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 99 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 100 filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED); 101 mContext.registerReceiver(this, filter); 102 } 103 104 @Override onReceive(Context context, Intent intent)105 public void onReceive(Context context, Intent intent) { 106 if (Log.isLoggable(TAG, Log.DEBUG)) { 107 Log.d(TAG, "Bluetooth broadcast intent action received: " + intent.getAction()); 108 } 109 notifyListeners(); 110 } 111 tearDown()112 void tearDown() { 113 mContext.unregisterReceiver(this); 114 } 115 } 116 } 117