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.telecom; 18 19 import android.content.Context; 20 21 import com.android.car.dialer.livedata.BluetoothHfpStateLiveData; 22 import com.android.car.dialer.livedata.BluetoothPairListLiveData; 23 import com.android.car.dialer.livedata.BluetoothStateLiveData; 24 25 /** 26 * Class that responsible for getting status of bluetooth connections. 27 */ 28 public class UiBluetoothMonitor { 29 private static String TAG = "Em.BtMonitor"; 30 31 private static UiBluetoothMonitor sUiBluetoothMonitor; 32 33 private final Context mContext; 34 35 private BluetoothHfpStateLiveData mHfpStateLiveData; 36 private BluetoothPairListLiveData mPairListLiveData; 37 private BluetoothStateLiveData mBluetoothStateLiveData; 38 39 /** 40 * Initialized a globally accessible {@link UiBluetoothMonitor} which can be retrieved by 41 * {@link #get}. If this function is called a second time before calling {@link #tearDown()}, 42 * an exception will be thrown. 43 * 44 * @param applicationContext Application context. 45 */ 46 // TODO: Create a singleton abstract class for Dialer common service. init(Context applicationContext)47 public static UiBluetoothMonitor init(Context applicationContext) { 48 if (sUiBluetoothMonitor == null) { 49 sUiBluetoothMonitor = new UiBluetoothMonitor(applicationContext); 50 } else { 51 throw new IllegalStateException("UiBluetoothMonitor has been initialized."); 52 } 53 54 return sUiBluetoothMonitor; 55 } 56 get()57 public static UiBluetoothMonitor get() { 58 return sUiBluetoothMonitor; 59 } 60 UiBluetoothMonitor(Context applicationContext)61 private UiBluetoothMonitor(Context applicationContext) { 62 mContext = applicationContext; 63 } 64 65 /** 66 * Stops the {@link UiBluetoothMonitor}. Call this function when Dialer goes to background. 67 * {@link #get()} won't return a valid {@link UiBluetoothMonitor} after calling this function. 68 */ tearDown()69 public void tearDown() { 70 sUiBluetoothMonitor = null; 71 } 72 73 /** 74 * Returns a LiveData which monitors the HFP profile state changes. 75 */ getHfpStateLiveData()76 public BluetoothHfpStateLiveData getHfpStateLiveData() { 77 if (mHfpStateLiveData == null) { 78 mHfpStateLiveData = new BluetoothHfpStateLiveData(mContext); 79 } 80 return mHfpStateLiveData; 81 } 82 83 /** 84 * Returns a LiveData which monitors the paired device list changes. 85 */ getPairListLiveData()86 public BluetoothPairListLiveData getPairListLiveData() { 87 if (mPairListLiveData == null) { 88 mPairListLiveData = new BluetoothPairListLiveData(mContext); 89 } 90 return mPairListLiveData; 91 } 92 93 /** 94 * Returns a LiveData which monitors the Bluetooth state changes. 95 */ getBluetoothStateLiveData()96 public BluetoothStateLiveData getBluetoothStateLiveData() { 97 if (mBluetoothStateLiveData == null) { 98 mBluetoothStateLiveData = new BluetoothStateLiveData(mContext); 99 } 100 return mBluetoothStateLiveData; 101 } 102 } 103