• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.settings.bluetooth;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Context;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.R;
27 import com.android.settings.widget.SummaryUpdater;
28 import com.android.settingslib.bluetooth.BluetoothCallback;
29 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
30 import com.android.settingslib.bluetooth.LocalBluetoothManager;
31 
32 import java.util.Set;
33 
34 /**
35  * Helper class that listeners to bluetooth callback and notify client when there is update in
36  * bluetooth summary info.
37  */
38 public final class BluetoothSummaryUpdater extends SummaryUpdater implements BluetoothCallback {
39     private static final String TAG = "BluetoothSummaryUpdater";
40 
41     private final BluetoothAdapter mBluetoothAdapter;
42     private final LocalBluetoothManager mBluetoothManager;
43 
BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener, LocalBluetoothManager bluetoothManager)44     public BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener,
45             LocalBluetoothManager bluetoothManager) {
46         super(context, listener);
47         mBluetoothManager = bluetoothManager;
48         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
49     }
50 
51     @Override
onBluetoothStateChanged(int bluetoothState)52     public void onBluetoothStateChanged(int bluetoothState) {
53         notifyChangeIfNeeded();
54     }
55 
56     @Override
onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state)57     public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
58         notifyChangeIfNeeded();
59     }
60 
61     @Override
register(boolean listening)62     public void register(boolean listening) {
63         if (mBluetoothAdapter == null) {
64             return;
65         }
66         if (listening) {
67             notifyChangeIfNeeded();
68             mBluetoothManager.getEventManager().registerCallback(this);
69         } else {
70             mBluetoothManager.getEventManager().unregisterCallback(this);
71         }
72     }
73 
74     @Override
getSummary()75     public String getSummary() {
76         if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
77             return mContext.getString(R.string.bluetooth_disabled);
78         }
79         switch (mBluetoothAdapter.getConnectionState()) {
80             case BluetoothAdapter.STATE_CONNECTED:
81                 return getConnectedDeviceSummary();
82             case BluetoothAdapter.STATE_CONNECTING:
83                 return mContext.getString(R.string.bluetooth_connecting);
84             case BluetoothAdapter.STATE_DISCONNECTING:
85                 return mContext.getString(R.string.bluetooth_disconnecting);
86             default:
87                 return mContext.getString(R.string.disconnected);
88         }
89     }
90 
91     @VisibleForTesting
getConnectedDeviceSummary()92     String getConnectedDeviceSummary() {
93         String deviceName = null;
94         int count = 0;
95         final Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
96         if (devices == null) {
97             Log.e(TAG, "getConnectedDeviceSummary, bonded devices are null");
98             return mContext.getString(R.string.bluetooth_disabled);
99         } else if (devices.isEmpty()) {
100             Log.e(TAG, "getConnectedDeviceSummary, no bonded devices");
101             return mContext.getString(R.string.disconnected);
102         }
103         for (BluetoothDevice device : devices) {
104             if (device.isConnected()) {
105                 deviceName = device.getName();
106                 count++;
107                 if (count > 1) {
108                     break;
109                 }
110             }
111         }
112         if (deviceName == null) {
113             Log.e(TAG, "getConnectedDeviceSummary, deviceName is null, numBondedDevices="
114                     + devices.size());
115             for (BluetoothDevice device : devices) {
116                 Log.e(TAG, "getConnectedDeviceSummary, device=" + device.getName() + "["
117                         + device.getAddress() + "]" + ", isConnected=" + device.isConnected());
118             }
119             return mContext.getString(R.string.disconnected);
120         }
121         return count > 1 ? mContext.getString(R.string.bluetooth_connected_multiple_devices_summary)
122                 : mContext.getString(R.string.bluetooth_connected_summary, deviceName);
123     }
124 
125 }
126