blob: f1d5cb404ad62a9a18a71d0d1ccf4ab88ac7a172 [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
19import android.bluetooth.BluetoothAdapter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040020import android.content.Context;
Jason Monk4ae97d32014-12-17 10:14:33 -050021import android.os.Looper;
John Spurlock486b78e2014-07-07 08:37:56 -040022import android.util.Log;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023
Jason Monkbe3c5db2015-02-04 13:00:55 -050024import com.android.settingslib.bluetooth.BluetoothCallback;
25import com.android.settingslib.bluetooth.CachedBluetoothDevice;
26import com.android.settingslib.bluetooth.LocalBluetoothManager;
John Spurlock486b78e2014-07-07 08:37:56 -040027
28import java.io.FileDescriptor;
29import java.io.PrintWriter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import java.util.ArrayList;
Jason Monkbe3c5db2015-02-04 13:00:55 -050031import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032
Jason Monkbe3c5db2015-02-04 13:00:55 -050033public class BluetoothControllerImpl implements BluetoothController, BluetoothCallback,
34 CachedBluetoothDevice.Callback {
John Spurlock486b78e2014-07-07 08:37:56 -040035 private static final String TAG = "BluetoothController";
36 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monk4ae97d32014-12-17 10:14:33 -050037
John Spurlockd1c86e22014-06-01 00:04:53 -040038 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
Jason Monkbe3c5db2015-02-04 13:00:55 -050039 private final LocalBluetoothManager mLocalBluetoothManager;
Jason Monk4ae97d32014-12-17 10:14:33 -050040
John Spurlockd1c86e22014-06-01 00:04:53 -040041 private boolean mEnabled;
42 private boolean mConnecting;
Jason Monkbe3c5db2015-02-04 13:00:55 -050043 private CachedBluetoothDevice mLastDevice;
John Spurlockaf8d6c42014-05-07 17:49:08 -040044
Jason Monk4ae97d32014-12-17 10:14:33 -050045 public BluetoothControllerImpl(Context context, Looper bgLooper) {
Jason Monkbe3c5db2015-02-04 13:00:55 -050046 mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null);
47 if (mLocalBluetoothManager != null) {
48 mLocalBluetoothManager.getEventManager().registerCallback(this);
49 onBluetoothStateChanged(
50 mLocalBluetoothManager.getBluetoothAdapter().getBluetoothState());
John Spurlockaf8d6c42014-05-07 17:49:08 -040051 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 }
53
John Spurlock486b78e2014-07-07 08:37:56 -040054 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
55 pw.println("BluetoothController state:");
Jason Monkbe3c5db2015-02-04 13:00:55 -050056 pw.print(" mLocalBluetoothManager="); pw.println(mLocalBluetoothManager);
Jason Monk09389a92015-05-19 16:06:52 -040057 if (mLocalBluetoothManager == null) {
58 return;
59 }
John Spurlock486b78e2014-07-07 08:37:56 -040060 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 Monkbe3c5db2015-02-04 13:00:55 -050064 pw.println(" Bluetooth Devices:");
65 for (CachedBluetoothDevice device :
66 mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) {
67 pw.println(" " + getDeviceString(device));
John Spurlock486b78e2014-07-07 08:37:56 -040068 }
69 }
70
Jason Monkbe3c5db2015-02-04 13:00:55 -050071 private String getDeviceString(CachedBluetoothDevice device) {
72 return device.getName() + " " + device.getBondState() + " " + device.isConnected();
John Spurlock486b78e2014-07-07 08:37:56 -040073 }
74
John Spurlockd1c86e22014-06-01 00:04:53 -040075 public void addStateChangedCallback(Callback cb) {
76 mCallbacks.add(cb);
John Spurlock486b78e2014-07-07 08:37:56 -040077 fireStateChange(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040078 }
79
80 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040081 public void removeStateChangedCallback(Callback cb) {
82 mCallbacks.remove(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040083 }
84
85 @Override
86 public boolean isBluetoothEnabled() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050087 return mEnabled;
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 }
89
90 @Override
91 public boolean isBluetoothConnected() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050092 return mLocalBluetoothManager != null
93 && mLocalBluetoothManager.getBluetoothAdapter().getConnectionState()
94 == BluetoothAdapter.STATE_CONNECTED;
John Spurlockaf8d6c42014-05-07 17:49:08 -040095 }
96
97 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040098 public boolean isBluetoothConnecting() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050099 return mConnecting;
John Spurlockd1c86e22014-06-01 00:04:53 -0400100 }
101
102 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 public void setBluetoothEnabled(boolean enabled) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500104 if (mLocalBluetoothManager != null) {
105 mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400106 }
107 }
108
109 @Override
110 public boolean isBluetoothSupported() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500111 return mLocalBluetoothManager != null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 }
113
John Spurlock486b78e2014-07-07 08:37:56 -0400114 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500115 public void connect(final CachedBluetoothDevice device) {
116 if (mLocalBluetoothManager == null || device == null) return;
117 device.connect(true);
John Spurlock486b78e2014-07-07 08:37:56 -0400118 }
119
120 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500121 public void disconnect(CachedBluetoothDevice device) {
122 if (mLocalBluetoothManager == null || device == null) return;
123 device.disconnect();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 }
125
126 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400127 public String getLastDeviceName() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500128 return mLastDevice != null ? mLastDevice.getName() : null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400129 }
130
Jason Monkbe3c5db2015-02-04 13:00:55 -0500131 @Override
132 public Collection<CachedBluetoothDevice> getDevices() {
133 return mLocalBluetoothManager != null
134 ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
135 : null;
Jason Monk4630c892014-12-08 16:36:16 -0500136 }
137
John Spurlock486b78e2014-07-07 08:37:56 -0400138 private void firePairedDevicesChanged() {
John Spurlockd1c86e22014-06-01 00:04:53 -0400139 for (Callback cb : mCallbacks) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500140 cb.onBluetoothDevicesChanged();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400141 }
142 }
John Spurlockccb6b9a2014-05-17 15:54:40 -0400143
John Spurlock486b78e2014-07-07 08:37:56 -0400144 private void fireStateChange() {
145 for (Callback cb : mCallbacks) {
146 fireStateChange(cb);
147 }
148 }
149
150 private void fireStateChange(Callback cb) {
John Spurlockd1c86e22014-06-01 00:04:53 -0400151 cb.onBluetoothStateChange(mEnabled, mConnecting);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400152 }
John Spurlock486b78e2014-07-07 08:37:56 -0400153
Jason Monkbe3c5db2015-02-04 13:00:55 -0500154 private void updateConnected() {
155 if (mLastDevice != null && mLastDevice.isConnected()) {
156 // Our current device is still valid.
157 return;
Jason Monk4ae97d32014-12-17 10:14:33 -0500158 }
Jason Monkbe3c5db2015-02-04 13:00:55 -0500159 for (CachedBluetoothDevice device : getDevices()) {
160 if (device.isConnected()) {
John Spurlock486b78e2014-07-07 08:37:56 -0400161 mLastDevice = device;
John Spurlock486b78e2014-07-07 08:37:56 -0400162 }
John Spurlock486b78e2014-07-07 08:37:56 -0400163 }
164 }
165
Jason Monkbe3c5db2015-02-04 13:00:55 -0500166 @Override
167 public void onBluetoothStateChanged(int bluetoothState) {
168 mEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
169 fireStateChange();
John Spurlock486b78e2014-07-07 08:37:56 -0400170 }
171
Jason Monkbe3c5db2015-02-04 13:00:55 -0500172 @Override
173 public void onScanningStateChanged(boolean started) {
174 // Don't care.
175 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500176
Jason Monkbe3c5db2015-02-04 13:00:55 -0500177 @Override
178 public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
179 cachedDevice.registerCallback(this);
180 updateConnected();
181 firePairedDevicesChanged();
182 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500183
Jason Monkbe3c5db2015-02-04 13:00:55 -0500184 @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 Spurlock486b78e2014-07-07 08:37:56 -0400208 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400209}