blob: 8d4f302edae48820ba0ae1615c3d0598e5dc2837 [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);
John Spurlock486b78e2014-07-07 08:37:56 -040057 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 Monkbe3c5db2015-02-04 13:00:55 -050061 pw.println(" Bluetooth Devices:");
62 for (CachedBluetoothDevice device :
63 mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) {
64 pw.println(" " + getDeviceString(device));
John Spurlock486b78e2014-07-07 08:37:56 -040065 }
66 }
67
Jason Monkbe3c5db2015-02-04 13:00:55 -050068 private String getDeviceString(CachedBluetoothDevice device) {
69 return device.getName() + " " + device.getBondState() + " " + device.isConnected();
John Spurlock486b78e2014-07-07 08:37:56 -040070 }
71
John Spurlockd1c86e22014-06-01 00:04:53 -040072 public void addStateChangedCallback(Callback cb) {
73 mCallbacks.add(cb);
John Spurlock486b78e2014-07-07 08:37:56 -040074 fireStateChange(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040075 }
76
77 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040078 public void removeStateChangedCallback(Callback cb) {
79 mCallbacks.remove(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040080 }
81
82 @Override
83 public boolean isBluetoothEnabled() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050084 return mEnabled;
John Spurlockaf8d6c42014-05-07 17:49:08 -040085 }
86
87 @Override
88 public boolean isBluetoothConnected() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050089 return mLocalBluetoothManager != null
90 && mLocalBluetoothManager.getBluetoothAdapter().getConnectionState()
91 == BluetoothAdapter.STATE_CONNECTED;
John Spurlockaf8d6c42014-05-07 17:49:08 -040092 }
93
94 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040095 public boolean isBluetoothConnecting() {
Jason Monkbe3c5db2015-02-04 13:00:55 -050096 return mConnecting;
John Spurlockd1c86e22014-06-01 00:04:53 -040097 }
98
99 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400100 public void setBluetoothEnabled(boolean enabled) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500101 if (mLocalBluetoothManager != null) {
102 mLocalBluetoothManager.getBluetoothAdapter().setBluetoothEnabled(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 }
104 }
105
106 @Override
107 public boolean isBluetoothSupported() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500108 return mLocalBluetoothManager != null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400109 }
110
John Spurlock486b78e2014-07-07 08:37:56 -0400111 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500112 public void connect(final CachedBluetoothDevice device) {
113 if (mLocalBluetoothManager == null || device == null) return;
114 device.connect(true);
John Spurlock486b78e2014-07-07 08:37:56 -0400115 }
116
117 @Override
Jason Monkbe3c5db2015-02-04 13:00:55 -0500118 public void disconnect(CachedBluetoothDevice device) {
119 if (mLocalBluetoothManager == null || device == null) return;
120 device.disconnect();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400121 }
122
123 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400124 public String getLastDeviceName() {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500125 return mLastDevice != null ? mLastDevice.getName() : null;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400126 }
127
Jason Monkbe3c5db2015-02-04 13:00:55 -0500128 @Override
129 public Collection<CachedBluetoothDevice> getDevices() {
130 return mLocalBluetoothManager != null
131 ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
132 : null;
Jason Monk4630c892014-12-08 16:36:16 -0500133 }
134
John Spurlock486b78e2014-07-07 08:37:56 -0400135 private void firePairedDevicesChanged() {
John Spurlockd1c86e22014-06-01 00:04:53 -0400136 for (Callback cb : mCallbacks) {
Jason Monkbe3c5db2015-02-04 13:00:55 -0500137 cb.onBluetoothDevicesChanged();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400138 }
139 }
John Spurlockccb6b9a2014-05-17 15:54:40 -0400140
John Spurlock486b78e2014-07-07 08:37:56 -0400141 private void fireStateChange() {
142 for (Callback cb : mCallbacks) {
143 fireStateChange(cb);
144 }
145 }
146
147 private void fireStateChange(Callback cb) {
John Spurlockd1c86e22014-06-01 00:04:53 -0400148 cb.onBluetoothStateChange(mEnabled, mConnecting);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400149 }
John Spurlock486b78e2014-07-07 08:37:56 -0400150
Jason Monkbe3c5db2015-02-04 13:00:55 -0500151 private void updateConnected() {
152 if (mLastDevice != null && mLastDevice.isConnected()) {
153 // Our current device is still valid.
154 return;
Jason Monk4ae97d32014-12-17 10:14:33 -0500155 }
Jason Monkbe3c5db2015-02-04 13:00:55 -0500156 for (CachedBluetoothDevice device : getDevices()) {
157 if (device.isConnected()) {
John Spurlock486b78e2014-07-07 08:37:56 -0400158 mLastDevice = device;
John Spurlock486b78e2014-07-07 08:37:56 -0400159 }
John Spurlock486b78e2014-07-07 08:37:56 -0400160 }
161 }
162
Jason Monkbe3c5db2015-02-04 13:00:55 -0500163 @Override
164 public void onBluetoothStateChanged(int bluetoothState) {
165 mEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
166 fireStateChange();
John Spurlock486b78e2014-07-07 08:37:56 -0400167 }
168
Jason Monkbe3c5db2015-02-04 13:00:55 -0500169 @Override
170 public void onScanningStateChanged(boolean started) {
171 // Don't care.
172 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500173
Jason Monkbe3c5db2015-02-04 13:00:55 -0500174 @Override
175 public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
176 cachedDevice.registerCallback(this);
177 updateConnected();
178 firePairedDevicesChanged();
179 }
Jason Monk4ae97d32014-12-17 10:14:33 -0500180
Jason Monkbe3c5db2015-02-04 13:00:55 -0500181 @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 Spurlock486b78e2014-07-07 08:37:56 -0400205 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400206}