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 17 package com.android.car.dialer.bluetooth; 18 19 import android.bluetooth.BluetoothDevice; 20 21 import androidx.lifecycle.LiveData; 22 import androidx.lifecycle.Observer; 23 24 import com.android.car.dialer.log.L; 25 26 import java.util.List; 27 import java.util.Set; 28 29 import javax.inject.Inject; 30 import javax.inject.Singleton; 31 32 /** 33 * Class that responsible for getting status of bluetooth connections. 34 */ 35 @Singleton 36 public final class UiBluetoothMonitor { 37 private static final String TAG = "CD.BtMonitor"; 38 39 private BluetoothPairListLiveData mPairListLiveData; 40 private BluetoothStateLiveData mBluetoothStateLiveData; 41 private HfpDeviceListLiveData mHfpDeviceListLiveData; 42 43 private Observer mPairListObserver; 44 private Observer mBluetoothStateObserver; 45 private Observer<List<BluetoothDevice>> mHfpDeviceListObserver; 46 47 @Inject UiBluetoothMonitor( BluetoothPairListLiveData bluetoothPairListLiveData, BluetoothStateLiveData bluetoothStateLiveData, HfpDeviceListLiveData hfpDeviceListLiveData)48 public UiBluetoothMonitor( 49 BluetoothPairListLiveData bluetoothPairListLiveData, 50 BluetoothStateLiveData bluetoothStateLiveData, 51 HfpDeviceListLiveData hfpDeviceListLiveData) { 52 mPairListLiveData = bluetoothPairListLiveData; 53 mBluetoothStateLiveData = bluetoothStateLiveData; 54 mHfpDeviceListLiveData = hfpDeviceListLiveData; 55 56 mPairListObserver = o -> L.i(TAG, "PairList is updated"); 57 mBluetoothStateObserver = o -> L.i(TAG, "BluetoothState is updated"); 58 mHfpDeviceListObserver = deviceList -> L.i(TAG, "HfpDeviceList is updated"); 59 60 mPairListLiveData.observeForever(mPairListObserver); 61 mBluetoothStateLiveData.observeForever(mBluetoothStateObserver); 62 mHfpDeviceListLiveData.observeForever(mHfpDeviceListObserver); 63 } 64 65 /** 66 * Stops the {@link UiBluetoothMonitor}. Call this function when Dialer goes to background. 67 */ tearDown()68 public void tearDown() { 69 removeObserver(mPairListLiveData, mPairListObserver); 70 removeObserver(mBluetoothStateLiveData, mBluetoothStateObserver); 71 removeObserver(mHfpDeviceListLiveData, mHfpDeviceListObserver); 72 } 73 74 /** 75 * Returns a LiveData which monitors the paired device list changes. 76 */ getPairListLiveData()77 public LiveData<Set<BluetoothDevice>> getPairListLiveData() { 78 return mPairListLiveData; 79 } 80 81 /** 82 * Returns a LiveData which monitors the Bluetooth state changes. 83 */ getBluetoothStateLiveData()84 public LiveData<Integer> getBluetoothStateLiveData() { 85 return mBluetoothStateLiveData; 86 } 87 88 /** 89 * Returns a SingleLiveEvent which monitors whether to refresh Dialer. 90 */ getHfpDeviceListLiveData()91 public LiveData<List<BluetoothDevice>> getHfpDeviceListLiveData() { 92 return mHfpDeviceListLiveData; 93 } 94 removeObserver(LiveData liveData, Observer observer)95 private void removeObserver(LiveData liveData, Observer observer) { 96 if (liveData != null && liveData.hasObservers()) { 97 liveData.removeObserver(observer); 98 } 99 } 100 } 101