• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bluetooth;
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.Nullable;
26 import androidx.lifecycle.LiveData;
27 
28 import com.android.car.dialer.log.L;
29 
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 
33 import dagger.hilt.android.qualifiers.ApplicationContext;
34 
35 /**
36  * Provides the device Bluetooth availability. Updates client with {@link BluetoothState}.
37  */
38 @Singleton
39 class BluetoothStateLiveData extends LiveData<Integer> {
40     private static final String TAG = "CD.BluetoothStateLiveData";
41 
42     private final BluetoothAdapter mBluetoothAdapter;
43     private final Context mContext;
44     private final IntentFilter mIntentFilter = new IntentFilter();
45 
46     private BroadcastReceiver mBluetoothStateReceiver = new BroadcastReceiver() {
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             updateState();
50         }
51     };
52 
53     /** Creates a new {@link BluetoothStateLiveData}. Call on main thread. */
54     @Inject
BluetoothStateLiveData( @pplicationContext Context context, @Nullable BluetoothAdapter bluetoothAdapter)55     BluetoothStateLiveData(
56             @ApplicationContext Context context,
57             @Nullable BluetoothAdapter bluetoothAdapter) {
58         mContext = context;
59         mBluetoothAdapter = bluetoothAdapter;
60         mIntentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
61     }
62 
63     @Override
onActive()64     protected void onActive() {
65         if (mBluetoothAdapter != null) {
66             updateState();
67             mContext.registerReceiver(mBluetoothStateReceiver, mIntentFilter);
68         }
69     }
70 
71     @Override
onInactive()72     protected void onInactive() {
73         if (mBluetoothAdapter != null) {
74             mContext.unregisterReceiver(mBluetoothStateReceiver);
75         }
76     }
77 
updateState()78     private void updateState() {
79         @BluetoothState int state = BluetoothState.UNKNOWN;
80         if (mBluetoothAdapter != null) {
81             state = mBluetoothAdapter.isEnabled() ? BluetoothState.ENABLED
82                     : BluetoothState.DISABLED;
83         }
84 
85         if (getValue() == null || state != getValue()) {
86             L.d(TAG, "updateState to %s", state);
87             setValue(state);
88         }
89     }
90 }
91