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