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; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 22 | import android.os.Handler; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 23 | import android.os.Looper; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 24 | import android.os.Message; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 27 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 28 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 29 | import com.android.settingslib.bluetooth.BluetoothCallback; |
| 30 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 31 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 32 | |
| 33 | import java.io.FileDescriptor; |
| 34 | import java.io.PrintWriter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | import java.util.ArrayList; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 36 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 38 | public class BluetoothControllerImpl implements BluetoothController, BluetoothCallback, |
| 39 | CachedBluetoothDevice.Callback { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 40 | private static final String TAG = "BluetoothController"; |
| 41 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 42 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 43 | private final LocalBluetoothManager mLocalBluetoothManager; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 44 | private final UserManager mUserManager; |
| 45 | private final int mCurrentUser; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 46 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 47 | private boolean mEnabled; |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 48 | private int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 49 | private CachedBluetoothDevice mLastDevice; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 51 | private final H mHandler = new H(); |
| 52 | |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 53 | public BluetoothControllerImpl(Context context, Looper bgLooper) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 54 | mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null); |
| 55 | if (mLocalBluetoothManager != null) { |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 56 | mLocalBluetoothManager.getEventManager().setReceiverHandler(new Handler(bgLooper)); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 57 | mLocalBluetoothManager.getEventManager().registerCallback(this); |
| 58 | onBluetoothStateChanged( |
| 59 | mLocalBluetoothManager.getBluetoothAdapter().getBluetoothState()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | } |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 61 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 62 | mCurrentUser = ActivityManager.getCurrentUser(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean canConfigBluetooth() { |
| 67 | return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, |
| 68 | UserHandle.of(mCurrentUser)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | } |
| 70 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 71 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 72 | pw.println("BluetoothController state:"); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 73 | pw.print(" mLocalBluetoothManager="); pw.println(mLocalBluetoothManager); |
Jason Monk | 09389a9 | 2015-05-19 16:06:52 -0400 | [diff] [blame] | 74 | if (mLocalBluetoothManager == null) { |
| 75 | return; |
| 76 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 77 | pw.print(" mEnabled="); pw.println(mEnabled); |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 78 | pw.print(" mConnectionState="); pw.println(stateToString(mConnectionState)); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 79 | pw.print(" mLastDevice="); pw.println(mLastDevice); |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 80 | pw.print(" mCallbacks.size="); pw.println(mHandler.mCallbacks.size()); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 81 | pw.println(" Bluetooth Devices:"); |
| 82 | for (CachedBluetoothDevice device : |
| 83 | mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) { |
| 84 | pw.println(" " + getDeviceString(device)); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 88 | private static String stateToString(int state) { |
| 89 | switch (state) { |
| 90 | case BluetoothAdapter.STATE_CONNECTED: |
| 91 | return "CONNECTED"; |
| 92 | case BluetoothAdapter.STATE_CONNECTING: |
| 93 | return "CONNECTING"; |
| 94 | case BluetoothAdapter.STATE_DISCONNECTED: |
| 95 | return "DISCONNECTED"; |
| 96 | case BluetoothAdapter.STATE_DISCONNECTING: |
| 97 | return "DISCONNECTING"; |
| 98 | } |
| 99 | return "UNKNOWN(" + state + ")"; |
| 100 | } |
| 101 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 102 | private String getDeviceString(CachedBluetoothDevice device) { |
| 103 | return device.getName() + " " + device.getBondState() + " " + device.isConnected(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 106 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 107 | public void addStateChangedCallback(Callback cb) { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 108 | mHandler.obtainMessage(H.MSG_ADD_CALLBACK, cb).sendToTarget(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 109 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 113 | public void removeStateChangedCallback(Callback cb) { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 114 | mHandler.obtainMessage(H.MSG_REMOVE_CALLBACK, cb).sendToTarget(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean isBluetoothEnabled() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 119 | return mEnabled; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean isBluetoothConnected() { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 124 | return mConnectionState == BluetoothAdapter.STATE_CONNECTED; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 128 | public boolean isBluetoothConnecting() { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 129 | return mConnectionState == BluetoothAdapter.STATE_CONNECTING; |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 133 | public void setBluetoothEnabled(boolean enabled) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 134 | if (mLocalBluetoothManager != null) { |
| 135 | mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean isBluetoothSupported() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 141 | return mLocalBluetoothManager != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 142 | } |
| 143 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 144 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 145 | public void connect(final CachedBluetoothDevice device) { |
| 146 | if (mLocalBluetoothManager == null || device == null) return; |
| 147 | device.connect(true); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 151 | public void disconnect(CachedBluetoothDevice device) { |
| 152 | if (mLocalBluetoothManager == null || device == null) return; |
| 153 | device.disconnect(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 157 | public String getLastDeviceName() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 158 | return mLastDevice != null ? mLastDevice.getName() : null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 161 | @Override |
| 162 | public Collection<CachedBluetoothDevice> getDevices() { |
| 163 | return mLocalBluetoothManager != null |
| 164 | ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy() |
| 165 | : null; |
Jason Monk | 4630c89 | 2014-12-08 16:36:16 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 168 | private void updateConnected() { |
Jason Monk | 9097056 | 2015-06-19 10:04:49 -0400 | [diff] [blame] | 169 | // Make sure our connection state is up to date. |
| 170 | int state = mLocalBluetoothManager.getBluetoothAdapter().getConnectionState(); |
| 171 | if (state != mConnectionState) { |
| 172 | mConnectionState = state; |
| 173 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 174 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 175 | if (mLastDevice != null && mLastDevice.isConnected()) { |
| 176 | // Our current device is still valid. |
| 177 | return; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 178 | } |
Jason Monk | bba7317 | 2015-08-12 16:17:34 -0400 | [diff] [blame] | 179 | mLastDevice = null; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 180 | for (CachedBluetoothDevice device : getDevices()) { |
| 181 | if (device.isConnected()) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 182 | mLastDevice = device; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 183 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 184 | } |
Jason Monk | bba7317 | 2015-08-12 16:17:34 -0400 | [diff] [blame] | 185 | if (mLastDevice == null && mConnectionState == BluetoothAdapter.STATE_CONNECTED) { |
| 186 | // If somehow we think we are connected, but have no connected devices, we aren't |
| 187 | // connected. |
| 188 | mConnectionState = BluetoothAdapter.STATE_DISCONNECTED; |
| 189 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 190 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 193 | @Override |
| 194 | public void onBluetoothStateChanged(int bluetoothState) { |
| 195 | mEnabled = bluetoothState == BluetoothAdapter.STATE_ON; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 196 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 199 | @Override |
| 200 | public void onScanningStateChanged(boolean started) { |
| 201 | // Don't care. |
| 202 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 203 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 204 | @Override |
| 205 | public void onDeviceAdded(CachedBluetoothDevice cachedDevice) { |
| 206 | cachedDevice.registerCallback(this); |
| 207 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 208 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 209 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 210 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 211 | @Override |
| 212 | public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) { |
| 213 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 214 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { |
| 219 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 220 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onDeviceAttributesChanged() { |
| 225 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 226 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 231 | mLastDevice = cachedDevice; |
| 232 | updateConnected(); |
Jason Monk | 9097056 | 2015-06-19 10:04:49 -0400 | [diff] [blame] | 233 | mConnectionState = state; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 234 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 235 | } |
| 236 | |
| 237 | private final class H extends Handler { |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 238 | private final ArrayList<BluetoothController.Callback> mCallbacks = new ArrayList<>(); |
| 239 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 240 | private static final int MSG_PAIRED_DEVICES_CHANGED = 1; |
| 241 | private static final int MSG_STATE_CHANGED = 2; |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 242 | private static final int MSG_ADD_CALLBACK = 3; |
| 243 | private static final int MSG_REMOVE_CALLBACK = 4; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 244 | |
| 245 | @Override |
| 246 | public void handleMessage(Message msg) { |
| 247 | switch (msg.what) { |
| 248 | case MSG_PAIRED_DEVICES_CHANGED: |
| 249 | firePairedDevicesChanged(); |
| 250 | break; |
| 251 | case MSG_STATE_CHANGED: |
| 252 | fireStateChange(); |
| 253 | break; |
Adrian Roos | a4b5486 | 2016-06-17 14:28:27 -0700 | [diff] [blame^] | 254 | case MSG_ADD_CALLBACK: |
| 255 | mCallbacks.add((BluetoothController.Callback) msg.obj); |
| 256 | break; |
| 257 | case MSG_REMOVE_CALLBACK: |
| 258 | mCallbacks.remove((BluetoothController.Callback) msg.obj); |
| 259 | break; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | private void firePairedDevicesChanged() { |
| 264 | for (BluetoothController.Callback cb : mCallbacks) { |
| 265 | cb.onBluetoothDevicesChanged(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private void fireStateChange() { |
| 270 | for (BluetoothController.Callback cb : mCallbacks) { |
| 271 | fireStateChange(cb); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private void fireStateChange(BluetoothController.Callback cb) { |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 276 | cb.onBluetoothStateChange(mEnabled); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame] | 277 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 278 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 279 | } |