blob: 014cc4944a7431c3207c47070a064563aa747f03 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
17package com.android.systemui.statusbar.policy;
18
Sudheer Shankab6fc9312016-01-27 19:59:03 +000019import android.app.ActivityManager;
John Spurlockaf8d6c42014-05-07 17:49:08 -040020import android.bluetooth.BluetoothAdapter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040021import android.content.Context;
Jason Monk744cf642015-05-19 12:04:41 -040022import android.os.Handler;
Jason Monk4ae97d32014-12-17 10:14:33 -050023import android.os.Looper;
Jason Monk744cf642015-05-19 12:04:41 -040024import android.os.Message;
Sudheer Shankab6fc9312016-01-27 19:59:03 +000025import android.os.UserHandle;
26import android.os.UserManager;
John Spurlock486b78e2014-07-07 08:37:56 -040027import android.util.Log;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028
Jason Monkbe3c5db2015-02-04 13:00:55 -050029import com.android.settingslib.bluetooth.BluetoothCallback;
30import com.android.settingslib.bluetooth.CachedBluetoothDevice;
31import com.android.settingslib.bluetooth.LocalBluetoothManager;
John Spurlock486b78e2014-07-07 08:37:56 -040032
33import java.io.FileDescriptor;
34import java.io.PrintWriter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040035import java.util.ArrayList;
Jason Monkbe3c5db2015-02-04 13:00:55 -050036import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040037
Jason Monkbe3c5db2015-02-04 13:00:55 -050038public class BluetoothControllerImpl implements BluetoothController, BluetoothCallback,
39 CachedBluetoothDevice.Callback {
John Spurlock486b78e2014-07-07 08:37:56 -040040 private static final String TAG = "BluetoothController";
41 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monk4ae97d32014-12-17 10:14:33 -050042
Jason Monkbe3c5db2015-02-04 13:00:55 -050043 private final LocalBluetoothManager mLocalBluetoothManager;
Sudheer Shankab6fc9312016-01-27 19:59:03 +000044 private final UserManager mUserManager;
45 private final int mCurrentUser;
Jason Monk4ae97d32014-12-17 10:14:33 -050046
John Spurlockd1c86e22014-06-01 00:04:53 -040047 private boolean mEnabled;
Jason Monka7d92b62015-05-27 10:20:37 -040048 private int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
Jason Monkbe3c5db2015-02-04 13:00:55 -050049 private CachedBluetoothDevice mLastDevice;
John Spurlockaf8d6c42014-05-07 17:49:08 -040050
Jason Monk744cf642015-05-19 12:04:41 -040051 private final H mHandler = new H();
52
Jason Monk4ae97d32014-12-17 10:14:33 -050053 public BluetoothControllerImpl(Context context, Looper bgLooper) {
Jason Monkbe3c5db2015-02-04 13:00:55 -050054 mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null);
55 if (mLocalBluetoothManager != null) {
Jason Monk744cf642015-05-19 12:04:41 -040056 mLocalBluetoothManager.getEventManager().setReceiverHandler(new Handler(bgLooper));
Jason Monkbe3c5db2015-02-04 13:00:55 -050057 mLocalBluetoothManager.getEventManager().registerCallback(this);
58 onBluetoothStateChanged(
59 mLocalBluetoothManager.getBluetoothAdapter().getBluetoothState());
John Spurlockaf8d6c42014-05-07 17:49:08 -040060 }
Sudheer Shankab6fc9312016-01-27 19:59:03 +000061 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 Spurlockaf8d6c42014-05-07 17:49:08 -040069 }
70
John Spurlock486b78e2014-07-07 08:37:56 -040071 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
72 pw.println("BluetoothController state:");
Jason Monkbe3c5db2015-02-04 13:00:55 -050073 pw.print(" mLocalBluetoothManager="); pw.println(mLocalBluetoothManager);
Jason Monk09389a92015-05-19 16:06:52 -040074 if (mLocalBluetoothManager == null) {
75 return;
76 }
John Spurlock486b78e2014-07-07 08:37:56 -040077 pw.print(" mEnabled="); pw.println(mEnabled);
Jason Monka7d92b62015-05-27 10:20:37 -040078 pw.print(" mConnectionState="); pw.println(stateToString(mConnectionState));
John Spurlock486b78e2014-07-07 08:37:56 -040079 pw.print(" mLastDevice="); pw.println(mLastDevice);
Adrian Roosa4b54862016-06-17 14:28:27 -070080 pw.print(" mCallbacks.size="); pw.println(mHandler.mCallbacks.size());
Jason Monkbe3c5db2015-02-04 13:00:55 -050081 pw.println(" Bluetooth Devices:");
82 for (CachedBluetoothDevice device :
83 mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) {
84 pw.println(" " + getDeviceString(device));
John Spurlock486b78e2014-07-07 08:37:56 -040085 }
86 }
87
Jason Monka7d92b62015-05-27 10:20:37 -040088 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 Monkbe3c5db2015-02-04 13:00:55 -0500102 private String getDeviceString(CachedBluetoothDevice device) {
103 return device.getName() + " " + device.getBondState() + " " + device.isConnected();
John Spurlock486b78e2014-07-07 08:37:56 -0400104 }
105
Jason Monka7d92b62015-05-27 10:20:37 -0400106 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400107 public void addStateChangedCallback(Callback cb) {
Adrian Roosa4b54862016-06-17 14:28:27 -0700108 mHandler.obtainMessage(H.MSG_ADD_CALLBACK, cb).sendToTarget();
Jason Monk744cf642015-05-19 12:04:41 -0400109 mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 }
111
112 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400113 public void removeStateChangedCallback(Callback cb) {
Adrian Roosa4b54862016-06-17 14:28:27 -0700114 mHandler.obtainMessage(H.MSG_REMOVE_CALLBACK, cb).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 }
116
117 @Override
118 public boolean isBluetoothEnabled() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500119 return mEnabled;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400120 }
121
122 @Override
123 public boolean isBluetoothConnected() {
Jason Monka7d92b62015-05-27 10:20:37 -0400124 return mConnectionState == BluetoothAdapter.STATE_CONNECTED;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400125 }
126
127 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400128 public boolean isBluetoothConnecting() {
Jason Monka7d92b62015-05-27 10:20:37 -0400129 return mConnectionState == BluetoothAdapter.STATE_CONNECTING;
John Spurlockd1c86e22014-06-01 00:04:53 -0400130 }
131
132 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400133 public void setBluetoothEnabled(boolean enabled) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500134 if (mLocalBluetoothManager != null) {
135 mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400136 }
137 }
138
139 @Override
140 public boolean isBluetoothSupported() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500141 return mLocalBluetoothManager != null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400142 }
143
John Spurlock486b78e2014-07-07 08:37:56 -0400144 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500145 public void connect(final CachedBluetoothDevice device) {
146 if (mLocalBluetoothManager == null || device == null) return;
147 device.connect(true);
John Spurlock486b78e2014-07-07 08:37:56 -0400148 }
149
150 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500151 public void disconnect(CachedBluetoothDevice device) {
152 if (mLocalBluetoothManager == null || device == null) return;
153 device.disconnect();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400154 }
155
156 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400157 public String getLastDeviceName() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500158 return mLastDevice != null ? mLastDevice.getName() : null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400159 }
160
Jason Monkbe3c5db2015-02-04 13:00:55 -0500161 @Override
162 public Collection<CachedBluetoothDevice> getDevices() {
163 return mLocalBluetoothManager != null
164 ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
165 : null;
Jason Monk4630c892014-12-08 16:36:16 -0500166 }
167
Jason Monkbe3c5db2015-02-04 13:00:55 -0500168 private void updateConnected() {
Jason Monk90970562015-06-19 10:04:49 -0400169 // 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 Monkbe3c5db2015-02-04 13:00:55 -0500175 if (mLastDevice != null && mLastDevice.isConnected()) {
176 // Our current device is still valid.
177 return;
Jason Monk4ae97d32014-12-17 10:14:33 -0500178 }
Jason Monkbba73172015-08-12 16:17:34 -0400179 mLastDevice = null;
Jason Monkbe3c5db2015-02-04 13:00:55 -0500180 for (CachedBluetoothDevice device : getDevices()) {
181 if (device.isConnected()) {
John Spurlock486b78e2014-07-07 08:37:56 -0400182 mLastDevice = device;
John Spurlock486b78e2014-07-07 08:37:56 -0400183 }
John Spurlock486b78e2014-07-07 08:37:56 -0400184 }
Jason Monkbba73172015-08-12 16:17:34 -0400185 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 Spurlock486b78e2014-07-07 08:37:56 -0400191 }
192
Jason Monkbe3c5db2015-02-04 13:00:55 -0500193 @Override
194 public void onBluetoothStateChanged(int bluetoothState) {
195 mEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
Jason Monk744cf642015-05-19 12:04:41 -0400196 mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
John Spurlock486b78e2014-07-07 08:37:56 -0400197 }
198
Jason Monkbe3c5db2015-02-04 13:00:55 -0500199 @Override
200 public void onScanningStateChanged(boolean started) {
201 // Don't care.
202 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500203
Jason Monkbe3c5db2015-02-04 13:00:55 -0500204 @Override
205 public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
206 cachedDevice.registerCallback(this);
207 updateConnected();
Jason Monk744cf642015-05-19 12:04:41 -0400208 mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
Jason Monkbe3c5db2015-02-04 13:00:55 -0500209 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500210
Jason Monkbe3c5db2015-02-04 13:00:55 -0500211 @Override
212 public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
213 updateConnected();
Jason Monk744cf642015-05-19 12:04:41 -0400214 mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
Jason Monkbe3c5db2015-02-04 13:00:55 -0500215 }
216
217 @Override
218 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
219 updateConnected();
Jason Monk744cf642015-05-19 12:04:41 -0400220 mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
Jason Monkbe3c5db2015-02-04 13:00:55 -0500221 }
222
223 @Override
224 public void onDeviceAttributesChanged() {
225 updateConnected();
Jason Monk744cf642015-05-19 12:04:41 -0400226 mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED);
Jason Monkbe3c5db2015-02-04 13:00:55 -0500227 }
228
229 @Override
230 public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500231 mLastDevice = cachedDevice;
232 updateConnected();
Jason Monk90970562015-06-19 10:04:49 -0400233 mConnectionState = state;
Jason Monk744cf642015-05-19 12:04:41 -0400234 mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
235 }
236
237 private final class H extends Handler {
Adrian Roosa4b54862016-06-17 14:28:27 -0700238 private final ArrayList<BluetoothController.Callback> mCallbacks = new ArrayList<>();
239
Jason Monk744cf642015-05-19 12:04:41 -0400240 private static final int MSG_PAIRED_DEVICES_CHANGED = 1;
241 private static final int MSG_STATE_CHANGED = 2;
Adrian Roosa4b54862016-06-17 14:28:27 -0700242 private static final int MSG_ADD_CALLBACK = 3;
243 private static final int MSG_REMOVE_CALLBACK = 4;
Jason Monk744cf642015-05-19 12:04:41 -0400244
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 Roosa4b54862016-06-17 14:28:27 -0700254 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 Monk744cf642015-05-19 12:04:41 -0400260 }
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 Monka7d92b62015-05-27 10:20:37 -0400276 cb.onBluetoothStateChange(mEnabled);
Jason Monk744cf642015-05-19 12:04:41 -0400277 }
John Spurlock486b78e2014-07-07 08:37:56 -0400278 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400279}