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