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 | |
| 19 | import android.bluetooth.BluetoothAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 21 | import android.os.Handler; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 22 | import android.os.Looper; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 23 | import android.os.Message; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 24 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 26 | import com.android.settingslib.bluetooth.BluetoothCallback; |
| 27 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 28 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 29 | |
| 30 | import java.io.FileDescriptor; |
| 31 | import java.io.PrintWriter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import java.util.ArrayList; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 33 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 34 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 35 | public class BluetoothControllerImpl implements BluetoothController, BluetoothCallback, |
| 36 | CachedBluetoothDevice.Callback { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 37 | private static final String TAG = "BluetoothController"; |
| 38 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 39 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 40 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 41 | private final LocalBluetoothManager mLocalBluetoothManager; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 42 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 43 | private boolean mEnabled; |
| 44 | private boolean mConnecting; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 45 | private CachedBluetoothDevice mLastDevice; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 46 | |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 47 | private final H mHandler = new H(); |
| 48 | |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 49 | public BluetoothControllerImpl(Context context, Looper bgLooper) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 50 | mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null); |
| 51 | if (mLocalBluetoothManager != null) { |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 52 | mLocalBluetoothManager.getEventManager().setReceiverHandler(new Handler(bgLooper)); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 53 | mLocalBluetoothManager.getEventManager().registerCallback(this); |
| 54 | onBluetoothStateChanged( |
| 55 | mLocalBluetoothManager.getBluetoothAdapter().getBluetoothState()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 57 | } |
| 58 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 59 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 60 | pw.println("BluetoothController state:"); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 61 | pw.print(" mLocalBluetoothManager="); pw.println(mLocalBluetoothManager); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 62 | pw.print(" mEnabled="); pw.println(mEnabled); |
| 63 | pw.print(" mConnecting="); pw.println(mConnecting); |
| 64 | pw.print(" mLastDevice="); pw.println(mLastDevice); |
| 65 | pw.print(" mCallbacks.size="); pw.println(mCallbacks.size()); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 66 | pw.println(" Bluetooth Devices:"); |
| 67 | for (CachedBluetoothDevice device : |
| 68 | mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) { |
| 69 | pw.println(" " + getDeviceString(device)); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 73 | private String getDeviceString(CachedBluetoothDevice device) { |
| 74 | return device.getName() + " " + device.getBondState() + " " + device.isConnected(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 75 | } |
| 76 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 77 | public void addStateChangedCallback(Callback cb) { |
| 78 | mCallbacks.add(cb); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 79 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 83 | public void removeStateChangedCallback(Callback cb) { |
| 84 | mCallbacks.remove(cb); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean isBluetoothEnabled() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 89 | return mEnabled; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean isBluetoothConnected() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 94 | return mLocalBluetoothManager != null |
| 95 | && mLocalBluetoothManager.getBluetoothAdapter().getConnectionState() |
| 96 | == BluetoothAdapter.STATE_CONNECTED; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 100 | public boolean isBluetoothConnecting() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 101 | return mConnecting; |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 105 | public void setBluetoothEnabled(boolean enabled) { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 106 | if (mLocalBluetoothManager != null) { |
| 107 | mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean isBluetoothSupported() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 113 | return mLocalBluetoothManager != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 114 | } |
| 115 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 116 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 117 | public void connect(final CachedBluetoothDevice device) { |
| 118 | if (mLocalBluetoothManager == null || device == null) return; |
| 119 | device.connect(true); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 123 | public void disconnect(CachedBluetoothDevice device) { |
| 124 | if (mLocalBluetoothManager == null || device == null) return; |
| 125 | device.disconnect(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 129 | public String getLastDeviceName() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 130 | return mLastDevice != null ? mLastDevice.getName() : null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 133 | @Override |
| 134 | public Collection<CachedBluetoothDevice> getDevices() { |
| 135 | return mLocalBluetoothManager != null |
| 136 | ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy() |
| 137 | : null; |
Jason Monk | 4630c89 | 2014-12-08 16:36:16 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 140 | private void updateConnected() { |
| 141 | if (mLastDevice != null && mLastDevice.isConnected()) { |
| 142 | // Our current device is still valid. |
| 143 | return; |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 144 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 145 | for (CachedBluetoothDevice device : getDevices()) { |
| 146 | if (device.isConnected()) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 147 | mLastDevice = device; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 148 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 152 | @Override |
| 153 | public void onBluetoothStateChanged(int bluetoothState) { |
| 154 | mEnabled = bluetoothState == BluetoothAdapter.STATE_ON; |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 155 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 158 | @Override |
| 159 | public void onScanningStateChanged(boolean started) { |
| 160 | // Don't care. |
| 161 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 162 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 163 | @Override |
| 164 | public void onDeviceAdded(CachedBluetoothDevice cachedDevice) { |
| 165 | cachedDevice.registerCallback(this); |
| 166 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 167 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 168 | } |
Jason Monk | 4ae97d3 | 2014-12-17 10:14:33 -0500 | [diff] [blame] | 169 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 170 | @Override |
| 171 | public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) { |
| 172 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 173 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { |
| 178 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 179 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void onDeviceAttributesChanged() { |
| 184 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 185 | mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { |
| 190 | mConnecting = state == BluetoothAdapter.STATE_CONNECTING; |
| 191 | mLastDevice = cachedDevice; |
| 192 | updateConnected(); |
Jason Monk | 744cf64 | 2015-05-19 12:04:41 -0400 | [diff] [blame^] | 193 | mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); |
| 194 | } |
| 195 | |
| 196 | private final class H extends Handler { |
| 197 | private static final int MSG_PAIRED_DEVICES_CHANGED = 1; |
| 198 | private static final int MSG_STATE_CHANGED = 2; |
| 199 | |
| 200 | @Override |
| 201 | public void handleMessage(Message msg) { |
| 202 | switch (msg.what) { |
| 203 | case MSG_PAIRED_DEVICES_CHANGED: |
| 204 | firePairedDevicesChanged(); |
| 205 | break; |
| 206 | case MSG_STATE_CHANGED: |
| 207 | fireStateChange(); |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | private void firePairedDevicesChanged() { |
| 213 | for (BluetoothController.Callback cb : mCallbacks) { |
| 214 | cb.onBluetoothDevicesChanged(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private void fireStateChange() { |
| 219 | for (BluetoothController.Callback cb : mCallbacks) { |
| 220 | fireStateChange(cb); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private void fireStateChange(BluetoothController.Callback cb) { |
| 225 | cb.onBluetoothStateChange(mEnabled, mConnecting); |
| 226 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 227 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 228 | } |