Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.settingslib.bluetooth; |
| 18 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 19 | import android.bluetooth.BluetoothA2dp; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 20 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 21 | import android.bluetooth.BluetoothDevice; |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 22 | import android.bluetooth.BluetoothHeadset; |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 23 | import android.bluetooth.BluetoothHearingAid; |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 24 | import android.bluetooth.BluetoothProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 30 | import android.telephony.TelephonyManager; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 33 | import androidx.annotation.Nullable; |
| 34 | import androidx.annotation.VisibleForTesting; |
| 35 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 36 | import com.android.settingslib.R; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Collection; |
| 40 | import java.util.HashMap; |
| 41 | import java.util.Map; |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 42 | import java.util.Objects; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | |
| 45 | /** |
| 46 | * BluetoothEventManager receives broadcasts and callbacks from the Bluetooth |
| 47 | * API and dispatches the event on the UI thread to the right class in the |
| 48 | * Settings. |
| 49 | */ |
Fan Zhang | 82dd3b0 | 2016-12-27 13:13:00 -0800 | [diff] [blame] | 50 | public class BluetoothEventManager { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 51 | private static final String TAG = "BluetoothEventManager"; |
| 52 | |
| 53 | private final LocalBluetoothAdapter mLocalAdapter; |
| 54 | private final CachedBluetoothDeviceManager mDeviceManager; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 55 | private final IntentFilter mAdapterIntentFilter, mProfileIntentFilter; |
| 56 | private final Map<String, Handler> mHandlerMap; |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 57 | private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver(); |
| 58 | private final BroadcastReceiver mProfileBroadcastReceiver = new BluetoothBroadcastReceiver(); |
| 59 | private final Collection<BluetoothCallback> mCallbacks = new ArrayList<>(); |
Amin Shaikh | 947f76d | 2018-08-27 15:55:57 -0400 | [diff] [blame] | 60 | private final android.os.Handler mReceiverHandler; |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 61 | private final UserHandle mUserHandle; |
Amin Shaikh | 947f76d | 2018-08-27 15:55:57 -0400 | [diff] [blame] | 62 | private final Context mContext; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 63 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 64 | interface Handler { |
| 65 | void onReceive(Context context, Intent intent, BluetoothDevice device); |
| 66 | } |
| 67 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Creates BluetoothEventManager with the ability to pass in {@link UserHandle} that tells it to |
| 70 | * listen for bluetooth events for that particular userHandle. |
| 71 | * |
| 72 | * <p> If passing in userHandle that's different from the user running the process, |
| 73 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission is required. If |
| 74 | * userHandle passed in is {@code null}, we register event receiver for the |
| 75 | * {@code context.getUser()} handle. |
| 76 | */ |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 77 | BluetoothEventManager(LocalBluetoothAdapter adapter, |
Amin Shaikh | 947f76d | 2018-08-27 15:55:57 -0400 | [diff] [blame] | 78 | CachedBluetoothDeviceManager deviceManager, Context context, |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 79 | android.os.Handler handler, @Nullable UserHandle userHandle) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 80 | mLocalAdapter = adapter; |
| 81 | mDeviceManager = deviceManager; |
| 82 | mAdapterIntentFilter = new IntentFilter(); |
| 83 | mProfileIntentFilter = new IntentFilter(); |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 84 | mHandlerMap = new HashMap<>(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 85 | mContext = context; |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 86 | mUserHandle = userHandle; |
Amin Shaikh | 947f76d | 2018-08-27 15:55:57 -0400 | [diff] [blame] | 87 | mReceiverHandler = handler; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 88 | |
| 89 | // Bluetooth on/off broadcasts |
| 90 | addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler()); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 91 | // Generic connected/not broadcast |
| 92 | addHandler(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED, |
| 93 | new ConnectionStateChangedHandler()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 94 | |
| 95 | // Discovery broadcasts |
| 96 | addHandler(BluetoothAdapter.ACTION_DISCOVERY_STARTED, new ScanningStateChangedHandler(true)); |
| 97 | addHandler(BluetoothAdapter.ACTION_DISCOVERY_FINISHED, new ScanningStateChangedHandler(false)); |
| 98 | addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 99 | addHandler(BluetoothDevice.ACTION_NAME_CHANGED, new NameChangedHandler()); |
Jason Monk | 8c495be | 2015-06-03 16:00:24 -0400 | [diff] [blame] | 100 | addHandler(BluetoothDevice.ACTION_ALIAS_CHANGED, new NameChangedHandler()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 101 | |
| 102 | // Pairing broadcasts |
| 103 | addHandler(BluetoothDevice.ACTION_BOND_STATE_CHANGED, new BondStateChangedHandler()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 104 | |
| 105 | // Fine-grained state broadcasts |
| 106 | addHandler(BluetoothDevice.ACTION_CLASS_CHANGED, new ClassChangedHandler()); |
| 107 | addHandler(BluetoothDevice.ACTION_UUID, new UuidChangedHandler()); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 108 | addHandler(BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED, new BatteryLevelChangedHandler()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 109 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 110 | // Active device broadcasts |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 111 | addHandler(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED, new ActiveDeviceChangedHandler()); |
| 112 | addHandler(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED, new ActiveDeviceChangedHandler()); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 113 | addHandler(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED, |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 114 | new ActiveDeviceChangedHandler()); |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 115 | |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 116 | // Headset state changed broadcasts |
| 117 | addHandler(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED, |
| 118 | new AudioModeChangedHandler()); |
| 119 | addHandler(TelephonyManager.ACTION_PHONE_STATE_CHANGED, |
| 120 | new AudioModeChangedHandler()); |
| 121 | |
jackqdyulei | 2948963 | 2018-10-30 11:27:30 -0700 | [diff] [blame] | 122 | // ACL connection changed broadcasts |
| 123 | addHandler(BluetoothDevice.ACTION_ACL_CONNECTED, new AclStateChangedHandler()); |
| 124 | addHandler(BluetoothDevice.ACTION_ACL_DISCONNECTED, new AclStateChangedHandler()); |
| 125 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 126 | registerAdapterIntentReceiver(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** Register to start receiving callbacks for Bluetooth events. */ |
| 130 | public void registerCallback(BluetoothCallback callback) { |
| 131 | synchronized (mCallbacks) { |
| 132 | mCallbacks.add(callback); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** Unregister to stop receiving callbacks for Bluetooth events. */ |
| 137 | public void unregisterCallback(BluetoothCallback callback) { |
| 138 | synchronized (mCallbacks) { |
| 139 | mCallbacks.remove(callback); |
| 140 | } |
| 141 | } |
| 142 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 143 | @VisibleForTesting |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 144 | void registerProfileIntentReceiver() { |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 145 | registerIntentReceiver(mProfileBroadcastReceiver, mProfileIntentFilter); |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 148 | @VisibleForTesting |
| 149 | void registerAdapterIntentReceiver() { |
| 150 | registerIntentReceiver(mBroadcastReceiver, mAdapterIntentFilter); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Registers the provided receiver to receive the broadcasts that correspond to the |
| 155 | * passed intent filter, in the context of the provided handler. |
| 156 | */ |
| 157 | private void registerIntentReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 158 | if (mUserHandle == null) { |
| 159 | // If userHandle has not been provided, simply call registerReceiver. |
| 160 | mContext.registerReceiver(receiver, filter, null, mReceiverHandler); |
| 161 | } else { |
| 162 | // userHandle was explicitly specified, so need to call multi-user aware API. |
| 163 | mContext.registerReceiverAsUser(receiver, mUserHandle, filter, null, mReceiverHandler); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @VisibleForTesting |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 168 | void addProfileHandler(String action, Handler handler) { |
| 169 | mHandlerMap.put(action, handler); |
| 170 | mProfileIntentFilter.addAction(action); |
| 171 | } |
| 172 | |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 173 | boolean readPairedDevices() { |
| 174 | Set<BluetoothDevice> bondedDevices = mLocalAdapter.getBondedDevices(); |
| 175 | if (bondedDevices == null) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | boolean deviceAdded = false; |
| 180 | for (BluetoothDevice device : bondedDevices) { |
| 181 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 182 | if (cachedDevice == null) { |
jackqdyulei | cc22949 | 2018-08-20 14:02:18 -0700 | [diff] [blame] | 183 | mDeviceManager.addDevice(device); |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 184 | deviceAdded = true; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return deviceAdded; |
| 189 | } |
| 190 | |
| 191 | void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) { |
| 192 | synchronized (mCallbacks) { |
| 193 | for (BluetoothCallback callback : mCallbacks) { |
| 194 | callback.onDeviceAdded(cachedDevice); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void dispatchDeviceRemoved(CachedBluetoothDevice cachedDevice) { |
| 200 | synchronized (mCallbacks) { |
| 201 | for (BluetoothCallback callback : mCallbacks) { |
| 202 | callback.onDeviceDeleted(cachedDevice); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void dispatchProfileConnectionStateChanged(CachedBluetoothDevice device, int state, |
| 208 | int bluetoothProfile) { |
| 209 | synchronized (mCallbacks) { |
| 210 | for (BluetoothCallback callback : mCallbacks) { |
| 211 | callback.onProfileConnectionStateChanged(device, state, bluetoothProfile); |
| 212 | } |
| 213 | } |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private void dispatchConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { |
| 217 | synchronized (mCallbacks) { |
| 218 | for (BluetoothCallback callback : mCallbacks) { |
| 219 | callback.onConnectionStateChanged(cachedDevice, state); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private void dispatchAudioModeChanged() { |
| 225 | mDeviceManager.dispatchAudioModeChanged(); |
| 226 | synchronized (mCallbacks) { |
| 227 | for (BluetoothCallback callback : mCallbacks) { |
| 228 | callback.onAudioModeChanged(); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private void dispatchActiveDeviceChanged(CachedBluetoothDevice activeDevice, |
| 234 | int bluetoothProfile) { |
| 235 | mDeviceManager.onActiveDeviceChanged(activeDevice, bluetoothProfile); |
| 236 | synchronized (mCallbacks) { |
| 237 | for (BluetoothCallback callback : mCallbacks) { |
| 238 | callback.onActiveDeviceChanged(activeDevice, bluetoothProfile); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
jackqdyulei | 2948963 | 2018-10-30 11:27:30 -0700 | [diff] [blame] | 243 | private void dispatchAclStateChanged(CachedBluetoothDevice activeDevice, |
| 244 | int state) { |
| 245 | synchronized (mCallbacks) { |
| 246 | for (BluetoothCallback callback : mCallbacks) { |
| 247 | callback.onAclConnectionStateChanged(activeDevice, state); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
jovanak | 206d6e4 | 2018-10-17 10:08:20 -0700 | [diff] [blame] | 252 | @VisibleForTesting |
| 253 | void addHandler(String action, Handler handler) { |
jackqdyulei | c58e976 | 2018-07-24 16:59:23 -0700 | [diff] [blame] | 254 | mHandlerMap.put(action, handler); |
| 255 | mAdapterIntentFilter.addAction(action); |
| 256 | } |
| 257 | |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 258 | private class BluetoothBroadcastReceiver extends BroadcastReceiver { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 259 | @Override |
| 260 | public void onReceive(Context context, Intent intent) { |
| 261 | String action = intent.getAction(); |
| 262 | BluetoothDevice device = intent |
| 263 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 264 | |
| 265 | Handler handler = mHandlerMap.get(action); |
| 266 | if (handler != null) { |
| 267 | handler.onReceive(context, intent, device); |
| 268 | } |
| 269 | } |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 270 | } |
Manu Viswanadhan | d199295 | 2016-10-25 20:38:23 +0530 | [diff] [blame] | 271 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 272 | private class AdapterStateChangedHandler implements Handler { |
| 273 | public void onReceive(Context context, Intent intent, |
| 274 | BluetoothDevice device) { |
| 275 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 276 | BluetoothAdapter.ERROR); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 277 | // update local profiles and get paired devices |
| 278 | mLocalAdapter.setBluetoothStateInt(state); |
| 279 | // send callback to update UI and possibly start scanning |
| 280 | synchronized (mCallbacks) { |
| 281 | for (BluetoothCallback callback : mCallbacks) { |
| 282 | callback.onBluetoothStateChanged(state); |
| 283 | } |
| 284 | } |
| 285 | // Inform CachedDeviceManager that the adapter state has changed |
| 286 | mDeviceManager.onBluetoothStateChanged(state); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private class ScanningStateChangedHandler implements Handler { |
| 291 | private final boolean mStarted; |
| 292 | |
| 293 | ScanningStateChangedHandler(boolean started) { |
| 294 | mStarted = started; |
| 295 | } |
| 296 | public void onReceive(Context context, Intent intent, |
| 297 | BluetoothDevice device) { |
| 298 | synchronized (mCallbacks) { |
| 299 | for (BluetoothCallback callback : mCallbacks) { |
| 300 | callback.onScanningStateChanged(mStarted); |
| 301 | } |
| 302 | } |
| 303 | mDeviceManager.onScanningStateChanged(mStarted); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | private class DeviceFoundHandler implements Handler { |
| 308 | public void onReceive(Context context, Intent intent, |
| 309 | BluetoothDevice device) { |
| 310 | short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 311 | String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); |
| 312 | // TODO Pick up UUID. They should be available for 2.1 devices. |
| 313 | // Skip for now, there's a bluez problem and we are not getting uuids even for 2.1. |
| 314 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 315 | if (cachedDevice == null) { |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 316 | cachedDevice = mDeviceManager.addDevice(device); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 317 | Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: " |
| 318 | + cachedDevice); |
Ash_Chen | 26fda9a | 2018-08-31 21:48:13 +0800 | [diff] [blame] | 319 | } else if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED |
| 320 | &&!cachedDevice.getDevice().isConnected()) { |
| 321 | // Dispatch device add callback to show bonded but |
| 322 | // not connected devices in discovery mode |
Ash_Chen | ff54d93 | 2018-08-17 22:20:37 +0800 | [diff] [blame] | 323 | dispatchDeviceAdded(cachedDevice); |
Chienyuan | 559b7f1 | 2018-12-17 15:30:20 +0800 | [diff] [blame] | 324 | Log.d(TAG, "DeviceFoundHandler found bonded and not connected device:" |
| 325 | + cachedDevice); |
| 326 | } else { |
| 327 | Log.d(TAG, "DeviceFoundHandler found existing CachedBluetoothDevice:" |
| 328 | + cachedDevice); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 329 | } |
| 330 | cachedDevice.setRssi(rssi); |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 331 | cachedDevice.setJustDiscovered(true); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 335 | private class ConnectionStateChangedHandler implements Handler { |
| 336 | @Override |
| 337 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 338 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 339 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, |
| 340 | BluetoothAdapter.ERROR); |
| 341 | dispatchConnectionStateChanged(cachedDevice, state); |
| 342 | } |
| 343 | } |
| 344 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 345 | private class NameChangedHandler implements Handler { |
| 346 | public void onReceive(Context context, Intent intent, |
| 347 | BluetoothDevice device) { |
| 348 | mDeviceManager.onDeviceNameUpdated(device); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | private class BondStateChangedHandler implements Handler { |
| 353 | public void onReceive(Context context, Intent intent, |
| 354 | BluetoothDevice device) { |
| 355 | if (device == null) { |
| 356 | Log.e(TAG, "ACTION_BOND_STATE_CHANGED with no EXTRA_DEVICE"); |
| 357 | return; |
| 358 | } |
| 359 | int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 360 | BluetoothDevice.ERROR); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 361 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 362 | if (cachedDevice == null) { |
jackqdyulei | cc22949 | 2018-08-20 14:02:18 -0700 | [diff] [blame] | 363 | Log.w(TAG, "Got bonding state changed for " + device + |
| 364 | ", but we have no record of that device."); |
| 365 | cachedDevice = mDeviceManager.addDevice(device); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | synchronized (mCallbacks) { |
| 369 | for (BluetoothCallback callback : mCallbacks) { |
| 370 | callback.onDeviceBondStateChanged(cachedDevice, bondState); |
| 371 | } |
| 372 | } |
| 373 | cachedDevice.onBondingStateChanged(bondState); |
| 374 | |
| 375 | if (bondState == BluetoothDevice.BOND_NONE) { |
Isha Bobra | c3d9413 | 2018-02-08 16:04:36 -0800 | [diff] [blame] | 376 | /* Check if we need to remove other Hearing Aid devices */ |
| 377 | if (cachedDevice.getHiSyncId() != BluetoothHearingAid.HI_SYNC_ID_INVALID) { |
| 378 | mDeviceManager.onDeviceUnpaired(cachedDevice); |
| 379 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 380 | int reason = intent.getIntExtra(BluetoothDevice.EXTRA_REASON, |
| 381 | BluetoothDevice.ERROR); |
| 382 | |
| 383 | showUnbondMessage(context, cachedDevice.getName(), reason); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Called when we have reached the unbonded state. |
| 389 | * |
| 390 | * @param reason one of the error reasons from |
| 391 | * BluetoothDevice.UNBOND_REASON_* |
| 392 | */ |
| 393 | private void showUnbondMessage(Context context, String name, int reason) { |
| 394 | int errorMsg; |
| 395 | |
| 396 | switch(reason) { |
jackqdyulei | c877589 | 2018-07-24 15:41:42 -0700 | [diff] [blame] | 397 | case BluetoothDevice.UNBOND_REASON_AUTH_FAILED: |
| 398 | errorMsg = R.string.bluetooth_pairing_pin_error_message; |
| 399 | break; |
| 400 | case BluetoothDevice.UNBOND_REASON_AUTH_REJECTED: |
| 401 | errorMsg = R.string.bluetooth_pairing_rejected_error_message; |
| 402 | break; |
| 403 | case BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN: |
| 404 | errorMsg = R.string.bluetooth_pairing_device_down_error_message; |
| 405 | break; |
| 406 | case BluetoothDevice.UNBOND_REASON_DISCOVERY_IN_PROGRESS: |
| 407 | case BluetoothDevice.UNBOND_REASON_AUTH_TIMEOUT: |
| 408 | case BluetoothDevice.UNBOND_REASON_REPEATED_ATTEMPTS: |
| 409 | case BluetoothDevice.UNBOND_REASON_REMOTE_AUTH_CANCELED: |
| 410 | errorMsg = R.string.bluetooth_pairing_error_message; |
| 411 | break; |
| 412 | default: |
| 413 | Log.w(TAG, "showUnbondMessage: Not displaying any message for reason: " + reason); |
| 414 | return; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 415 | } |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 416 | BluetoothUtils.showError(context, name, errorMsg); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | private class ClassChangedHandler implements Handler { |
| 421 | public void onReceive(Context context, Intent intent, |
| 422 | BluetoothDevice device) { |
| 423 | mDeviceManager.onBtClassChanged(device); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private class UuidChangedHandler implements Handler { |
| 428 | public void onReceive(Context context, Intent intent, |
| 429 | BluetoothDevice device) { |
| 430 | mDeviceManager.onUuidChanged(device); |
| 431 | } |
| 432 | } |
| 433 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 434 | private class BatteryLevelChangedHandler implements Handler { |
| 435 | public void onReceive(Context context, Intent intent, |
| 436 | BluetoothDevice device) { |
| 437 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 438 | if (cachedDevice != null) { |
| 439 | cachedDevice.refresh(); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 444 | private class ActiveDeviceChangedHandler implements Handler { |
| 445 | @Override |
| 446 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 447 | String action = intent.getAction(); |
| 448 | if (action == null) { |
| 449 | Log.w(TAG, "ActiveDeviceChangedHandler: action is null"); |
| 450 | return; |
| 451 | } |
| 452 | CachedBluetoothDevice activeDevice = mDeviceManager.findDevice(device); |
| 453 | int bluetoothProfile = 0; |
| 454 | if (Objects.equals(action, BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) { |
| 455 | bluetoothProfile = BluetoothProfile.A2DP; |
| 456 | } else if (Objects.equals(action, BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED)) { |
| 457 | bluetoothProfile = BluetoothProfile.HEADSET; |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 458 | } else if (Objects.equals(action, BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED)) { |
| 459 | bluetoothProfile = BluetoothProfile.HEARING_AID; |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 460 | } else { |
| 461 | Log.w(TAG, "ActiveDeviceChangedHandler: unknown action " + action); |
| 462 | return; |
| 463 | } |
| 464 | dispatchActiveDeviceChanged(activeDevice, bluetoothProfile); |
| 465 | } |
| 466 | } |
| 467 | |
jackqdyulei | 2948963 | 2018-10-30 11:27:30 -0700 | [diff] [blame] | 468 | private class AclStateChangedHandler implements Handler { |
| 469 | @Override |
| 470 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
wengsu | 5194558 | 2018-12-17 12:41:04 +0800 | [diff] [blame] | 471 | if (device == null) { |
| 472 | Log.w(TAG, "AclStateChangedHandler: device is null"); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | // Avoid to notify Settings UI for Hearing Aid sub device. |
| 477 | if (mDeviceManager.isSubDevice(device)) { |
| 478 | return; |
| 479 | } |
| 480 | |
jackqdyulei | 2948963 | 2018-10-30 11:27:30 -0700 | [diff] [blame] | 481 | final String action = intent.getAction(); |
| 482 | if (action == null) { |
| 483 | Log.w(TAG, "AclStateChangedHandler: action is null"); |
| 484 | return; |
| 485 | } |
| 486 | final CachedBluetoothDevice activeDevice = mDeviceManager.findDevice(device); |
Chienyuan | c4218061 | 2018-12-27 15:50:15 +0800 | [diff] [blame] | 487 | if (activeDevice == null) { |
| 488 | Log.w(TAG, "AclStateChangedHandler: activeDevice is null"); |
| 489 | return; |
| 490 | } |
jackqdyulei | 2948963 | 2018-10-30 11:27:30 -0700 | [diff] [blame] | 491 | final int state; |
| 492 | switch (action) { |
| 493 | case BluetoothDevice.ACTION_ACL_CONNECTED: |
| 494 | state = BluetoothAdapter.STATE_CONNECTED; |
| 495 | break; |
| 496 | case BluetoothDevice.ACTION_ACL_DISCONNECTED: |
| 497 | state = BluetoothAdapter.STATE_DISCONNECTED; |
| 498 | break; |
| 499 | default: |
| 500 | Log.w(TAG, "ActiveDeviceChangedHandler: unknown action " + action); |
| 501 | return; |
| 502 | |
| 503 | } |
| 504 | dispatchAclStateChanged(activeDevice, state); |
| 505 | } |
| 506 | } |
| 507 | |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 508 | private class AudioModeChangedHandler implements Handler { |
| 509 | |
| 510 | @Override |
| 511 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 512 | final String action = intent.getAction(); |
| 513 | if (action == null) { |
| 514 | Log.w(TAG, "AudioModeChangedHandler() action is null"); |
| 515 | return; |
| 516 | } |
| 517 | dispatchAudioModeChanged(); |
| 518 | } |
| 519 | } |
Chienyuan | 804e7c1 | 2018-07-30 19:46:33 +0800 | [diff] [blame] | 520 | } |