John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.systemui.statusbar.policy; |
| 18 | |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 19 | import android.app.ActivityManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothProfile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 23 | import android.content.Context; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 25 | import android.os.Looper; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 26 | import android.os.Message; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 29 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 31 | import com.android.settingslib.bluetooth.BluetoothCallback; |
| 32 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 33 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 34 | import com.android.systemui.Dependency; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 35 | |
| 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 38 | import java.lang.ref.WeakReference; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | import java.util.ArrayList; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 40 | import java.util.Collection; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 41 | import java.util.WeakHashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 43 | public class BluetoothControllerImpl implements BluetoothController, BluetoothCallback, |
| 44 | CachedBluetoothDevice.Callback { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 45 | private static final String TAG = "BluetoothController"; |
| 46 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 47 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 48 | private final LocalBluetoothManager mLocalBluetoothManager; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 49 | private final UserManager mUserManager; |
| 50 | private final int mCurrentUser; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 51 | private final WeakHashMap<CachedBluetoothDevice, ActuallyCachedState> mCachedState = |
| 52 | new WeakHashMap<>(); |
| 53 | private final Handler mBgHandler; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 54 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 55 | private boolean mEnabled; |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 56 | private int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 57 | private CachedBluetoothDevice mLastDevice; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 59 | private final H mHandler = new H(Looper.getMainLooper()); |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 60 | private int mState; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 61 | |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 62 | public BluetoothControllerImpl(Context context, Looper bgLooper) { |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 63 | mLocalBluetoothManager = Dependency.get(LocalBluetoothManager.class); |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 64 | mBgHandler = new Handler(bgLooper); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 65 | if (mLocalBluetoothManager != null) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 66 | mLocalBluetoothManager.getEventManager().setReceiverHandler(mBgHandler); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 67 | mLocalBluetoothManager.getEventManager().registerCallback(this); |
| 68 | onBluetoothStateChanged( |
| 69 | mLocalBluetoothManager.getBluetoothAdapter().getBluetoothState()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 70 | } |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 71 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 72 | mCurrentUser = ActivityManager.getCurrentUser(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean canConfigBluetooth() { |
| 77 | return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, |
phweiss | bd8d033 | 2017-06-12 11:38:09 +0200 | [diff] [blame] | 78 | UserHandle.of(mCurrentUser)) |
| 79 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_BLUETOOTH, |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 80 | UserHandle.of(mCurrentUser)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 81 | } |
| 82 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 83 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 84 | pw.println("BluetoothController state:"); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 85 | pw.print(" mLocalBluetoothManager="); pw.println(mLocalBluetoothManager); |
Jason Monk | 09389a9 | 2015-05-19 16:06:52 -0400 | [diff] [blame] | 86 | if (mLocalBluetoothManager == null) { |
| 87 | return; |
| 88 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 89 | pw.print(" mEnabled="); pw.println(mEnabled); |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 90 | pw.print(" mConnectionState="); pw.println(stateToString(mConnectionState)); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 91 | pw.print(" mLastDevice="); pw.println(mLastDevice); |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 92 | pw.print(" mCallbacks.size="); pw.println(mHandler.mCallbacks.size()); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 93 | pw.println(" Bluetooth Devices:"); |
| 94 | for (CachedBluetoothDevice device : |
| 95 | mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) { |
| 96 | pw.println(" " + getDeviceString(device)); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 100 | private static String stateToString(int state) { |
| 101 | switch (state) { |
| 102 | case BluetoothAdapter.STATE_CONNECTED: |
| 103 | return "CONNECTED"; |
| 104 | case BluetoothAdapter.STATE_CONNECTING: |
| 105 | return "CONNECTING"; |
| 106 | case BluetoothAdapter.STATE_DISCONNECTED: |
| 107 | return "DISCONNECTED"; |
| 108 | case BluetoothAdapter.STATE_DISCONNECTING: |
| 109 | return "DISCONNECTING"; |
| 110 | } |
| 111 | return "UNKNOWN(" + state + ")"; |
| 112 | } |
| 113 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 114 | private String getDeviceString(CachedBluetoothDevice device) { |
| 115 | return device.getName() + " " + device.getBondState() + " " + device.isConnected(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 118 | @Override |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 119 | public int getBondState(CachedBluetoothDevice device) { |
| 120 | return getCachedState(device).mBondState; |
| 121 | } |
| 122 | |
| 123 | @Override |
Jason Monk | 3fd0b14 | 2017-08-30 18:11:21 -0400 | [diff] [blame] | 124 | public CachedBluetoothDevice getLastDevice() { |
| 125 | return mLastDevice; |
| 126 | } |
| 127 | |
| 128 | @Override |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 129 | public int getMaxConnectionState(CachedBluetoothDevice device) { |
| 130 | return getCachedState(device).mMaxConnectionState; |
| 131 | } |
| 132 | |
| 133 | @Override |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 134 | public void addCallback(Callback cb) { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 135 | mHandler.obtainMessage(H.MSG_ADD_CALLBACK, cb).sendToTarget(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 136 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 140 | public void removeCallback(Callback cb) { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 141 | mHandler.obtainMessage(H.MSG_REMOVE_CALLBACK, cb).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean isBluetoothEnabled() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 146 | return mEnabled; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 150 | public int getBluetoothState() { |
| 151 | return mState; |
| 152 | } |
| 153 | |
| 154 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 155 | public boolean isBluetoothConnected() { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 156 | return mConnectionState == BluetoothAdapter.STATE_CONNECTED; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 160 | public boolean isBluetoothConnecting() { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 161 | return mConnectionState == BluetoothAdapter.STATE_CONNECTING; |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 165 | public void setBluetoothEnabled(boolean enabled) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 166 | if (mLocalBluetoothManager != null) { |
| 167 | mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean isBluetoothSupported() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 173 | return mLocalBluetoothManager != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 174 | } |
| 175 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 176 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 177 | public void connect(final CachedBluetoothDevice device) { |
| 178 | if (mLocalBluetoothManager == null || device == null) return; |
| 179 | device.connect(true); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 183 | public void disconnect(CachedBluetoothDevice device) { |
| 184 | if (mLocalBluetoothManager == null || device == null) return; |
| 185 | device.disconnect(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 189 | public String getLastDeviceName() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 190 | return mLastDevice != null ? mLastDevice.getName() : null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 193 | @Override |
| 194 | public Collection<CachedBluetoothDevice> getDevices() { |
| 195 | return mLocalBluetoothManager != null |
| 196 | ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy() |
| 197 | : null; |
Jason Monk | 4630c89 | 2014-12-08 16:36:16 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 200 | private void updateConnected() { |
Jason Monk | 9097056 | 2015-06-19 10:04:49 -0400 | [diff] [blame] | 201 | // Make sure our connection state is up to date. |
| 202 | int state = mLocalBluetoothManager.getBluetoothAdapter().getConnectionState(); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 203 | if (mLastDevice != null && !mLastDevice.isConnected()) { |
| 204 | // Clear out last device if no longer connected. |
| 205 | mLastDevice = null; |
Jason Monk | 9097056 | 2015-06-19 10:04:49 -0400 | [diff] [blame] | 206 | } |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 207 | // If any of the devices are in a higher state than the adapter, move the adapter into |
| 208 | // that state. |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 209 | for (CachedBluetoothDevice device : getDevices()) { |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 210 | int maxDeviceState = device.getMaxConnectionState(); |
| 211 | if (maxDeviceState > state) { |
| 212 | state = maxDeviceState; |
| 213 | } |
| 214 | if (mLastDevice == null && device.isConnected()) { |
| 215 | // Set as last connected device only if we don't have one. |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 216 | mLastDevice = device; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 217 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 218 | } |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 219 | |
| 220 | if (mLastDevice == null && state == BluetoothAdapter.STATE_CONNECTED) { |
Jason Monk | bba7317 | 2015-08-12 16:17:34 -0400 | [diff] [blame] | 221 | // If somehow we think we are connected, but have no connected devices, we aren't |
| 222 | // connected. |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 223 | state = BluetoothAdapter.STATE_DISCONNECTED; |
| 224 | } |
| 225 | if (state != mConnectionState) { |
| 226 | mConnectionState = state; |
Jason Monk | bba7317 | 2015-08-12 16:17:34 -0400 | [diff] [blame] | 227 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 228 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 231 | @Override |
| 232 | public void onBluetoothStateChanged(int bluetoothState) { |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 233 | mEnabled = bluetoothState == BluetoothAdapter.STATE_ON |
| 234 | || bluetoothState == BluetoothAdapter.STATE_TURNING_ON; |
| 235 | mState = bluetoothState; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 236 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 239 | @Override |
| 240 | public void onScanningStateChanged(boolean started) { |
| 241 | // Don't care. |
| 242 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 243 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 244 | @Override |
| 245 | public void onDeviceAdded(CachedBluetoothDevice cachedDevice) { |
| 246 | cachedDevice.registerCallback(this); |
| 247 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 248 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 249 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 250 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 251 | @Override |
| 252 | public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 253 | mCachedState.remove(cachedDevice); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 254 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 255 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 260 | mCachedState.remove(cachedDevice); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 261 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 262 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onDeviceAttributesChanged() { |
| 267 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 268 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 273 | mCachedState.remove(cachedDevice); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 274 | mLastDevice = cachedDevice; |
| 275 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 276 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 277 | } |
| 278 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame^] | 279 | @Override |
| 280 | public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {} |
| 281 | |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 282 | private ActuallyCachedState getCachedState(CachedBluetoothDevice device) { |
| 283 | ActuallyCachedState state = mCachedState.get(device); |
| 284 | if (state == null) { |
| 285 | state = new ActuallyCachedState(device, mHandler); |
| 286 | mBgHandler.post(state); |
| 287 | mCachedState.put(device, state); |
| 288 | return state; |
| 289 | } |
| 290 | return state; |
| 291 | } |
| 292 | |
| 293 | private static class ActuallyCachedState implements Runnable { |
| 294 | |
| 295 | private final WeakReference<CachedBluetoothDevice> mDevice; |
| 296 | private final Handler mUiHandler; |
| 297 | private int mBondState = BluetoothDevice.BOND_NONE; |
| 298 | private int mMaxConnectionState = BluetoothProfile.STATE_DISCONNECTED; |
| 299 | |
| 300 | private ActuallyCachedState(CachedBluetoothDevice device, Handler uiHandler) { |
| 301 | mDevice = new WeakReference<>(device); |
| 302 | mUiHandler = uiHandler; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void run() { |
Jason Monk | 0f5d402 | 2017-08-15 14:29:49 -0400 | [diff] [blame] | 307 | CachedBluetoothDevice device = mDevice.get(); |
| 308 | if (device != null) { |
| 309 | mBondState = device.getBondState(); |
| 310 | mMaxConnectionState = device.getMaxConnectionState(); |
| 311 | mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED); |
| 312 | mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
| 313 | } |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 317 | private final class H extends Handler { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 318 | private final ArrayList<BluetoothController.Callback> mCallbacks = new ArrayList<>(); |
| 319 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 320 | private static final int MSG_PAIRED_DEVICES_CHANGED = 1; |
| 321 | private static final int MSG_STATE_CHANGED = 2; |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 322 | private static final int MSG_ADD_CALLBACK = 3; |
| 323 | private static final int MSG_REMOVE_CALLBACK = 4; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 324 | |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 325 | public H(Looper looper) { |
| 326 | super(looper); |
| 327 | } |
| 328 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 329 | @Override |
| 330 | public void handleMessage(Message msg) { |
| 331 | switch (msg.what) { |
| 332 | case MSG_PAIRED_DEVICES_CHANGED: |
| 333 | firePairedDevicesChanged(); |
| 334 | break; |
| 335 | case MSG_STATE_CHANGED: |
| 336 | fireStateChange(); |
| 337 | break; |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame] | 338 | case MSG_ADD_CALLBACK: |
| 339 | mCallbacks.add((BluetoothController.Callback) msg.obj); |
| 340 | break; |
| 341 | case MSG_REMOVE_CALLBACK: |
| 342 | mCallbacks.remove((BluetoothController.Callback) msg.obj); |
| 343 | break; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | private void firePairedDevicesChanged() { |
| 348 | for (BluetoothController.Callback cb : mCallbacks) { |
| 349 | cb.onBluetoothDevicesChanged(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private void fireStateChange() { |
| 354 | for (BluetoothController.Callback cb : mCallbacks) { |
| 355 | fireStateChange(cb); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private void fireStateChange(BluetoothController.Callback cb) { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 360 | cb.onBluetoothStateChange(mEnabled); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 361 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 362 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 363 | } |