1 /*
2  * Copyright (C) 2018 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.livedata;
18 
19 import android.bluetooth.BluetoothAdapter;
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 
27 import com.android.car.dialer.log.L;
28 
29 import androidx.lifecycle.LiveData;
30 
31 /**
32  * Provides the connectivity state of HFP Bluetooth profile. States can be one of:
33  * <ul>
34  * <li>{@link BluetoothProfile#STATE_DISCONNECTED},
35  * <li>{@link BluetoothProfile#STATE_CONNECTING},
36  * <li>{@link BluetoothProfile#STATE_CONNECTED},
37  * <li>{@link BluetoothProfile#STATE_DISCONNECTING}
38  * </ul>
39  */
40 public class BluetoothHfpStateLiveData extends LiveData<Integer> {
41     private static final String TAG = "CD.BluetoothHfpStateLiveData";
42 
43     private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
44     private final Context mContext;
45     private final IntentFilter mIntentFilter = new IntentFilter();
46 
47     private BroadcastReceiver mBluetoothStateReceiver = new BroadcastReceiver() {
48         @Override
49         public void onReceive(Context context, Intent intent) {
50             updateState();
51         }
52     };
53 
54     /** Creates a new {@link BluetoothHfpStateLiveData}. Call on main thread. */
BluetoothHfpStateLiveData(Context context)55     public BluetoothHfpStateLiveData(Context context) {
56         mContext = context;
57         mIntentFilter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
58     }
59 
60     @Override
onActive()61     protected void onActive() {
62         if (mBluetoothAdapter != null) {
63             updateState();
64             mContext.registerReceiver(mBluetoothStateReceiver, mIntentFilter);
65         }
66     }
67 
68     @Override
onInactive()69     protected void onInactive() {
70         if (mBluetoothAdapter != null) {
71             mContext.unregisterReceiver(mBluetoothStateReceiver);
72         }
73     }
74 
updateState()75     private void updateState() {
76         int state = mBluetoothAdapter.getProfileConnectionState(
77                 BluetoothProfile.HEADSET_CLIENT);
78         if (getValue() == null || state != getValue()) {
79             L.d(TAG, "updateState to %s", state);
80             setValue(state);
81         }
82     }
83 }
84