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.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 
25 import androidx.annotation.IntDef;
26 import androidx.lifecycle.LiveData;
27 
28 import com.android.car.dialer.log.L;
29 
30 /**
31  * Provides the device Bluetooth availability. Updates client with {@link BluetoothState}.
32  */
33 public class BluetoothStateLiveData extends LiveData<Integer> {
34     private static final String TAG = "CD.BluetoothStateLiveData";
35 
36     @IntDef({
37             BluetoothState.UNKNOWN,
38             BluetoothState.DISABLED,
39             BluetoothState.ENABLED,
40     })
41     public @interface BluetoothState {
42         /** Bluetooth is not supported on the current device */
43         int UNKNOWN = 0;
44         /** Bluetooth is disabled */
45         int DISABLED = 1;
46         /** Bluetooth is enabled */
47         int ENABLED = 2;
48     }
49 
50     private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
51     private final Context mContext;
52     private final IntentFilter mIntentFilter = new IntentFilter();
53 
54     private BroadcastReceiver mBluetoothStateReceiver = new BroadcastReceiver() {
55         @Override
56         public void onReceive(Context context, Intent intent) {
57             updateState();
58         }
59     };
60 
61     /** Creates a new {@link BluetoothStateLiveData}. Call on main thread. */
BluetoothStateLiveData(Context context)62     public BluetoothStateLiveData(Context context) {
63         mContext = context;
64         mIntentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
65     }
66 
67     @Override
onActive()68     protected void onActive() {
69         if (mBluetoothAdapter != null) {
70             updateState();
71             mContext.registerReceiver(mBluetoothStateReceiver, mIntentFilter);
72         }
73 
74     }
75 
76     @Override
onInactive()77     protected void onInactive() {
78         if (mBluetoothAdapter != null) {
79             mContext.unregisterReceiver(mBluetoothStateReceiver);
80         }
81     }
82 
updateState()83     private void updateState() {
84         @BluetoothState int state = BluetoothState.UNKNOWN;
85         if (mBluetoothAdapter != null) {
86             state = mBluetoothAdapter.isEnabled() ? BluetoothState.ENABLED
87                     : BluetoothState.DISABLED;
88         }
89 
90         if (getValue() == null || state != getValue()) {
91             L.d(TAG, "updateState to %s", state);
92             setValue(state);
93         }
94     }
95 }
96