blob: 117bf61ccf57b6ef7a13bf9f764e2d6daa63a09a [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.bluetooth.BluetoothDevice;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25
26import java.util.ArrayList;
27import java.util.HashSet;
28import java.util.Set;
29
30public class BluetoothControllerImpl extends BroadcastReceiver implements BluetoothController {
31 private static final String TAG = "StatusBar.BluetoothController";
32
John Spurlockd1c86e22014-06-01 00:04:53 -040033 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
34 private final Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>();
John Spurlockaf8d6c42014-05-07 17:49:08 -040035 private final BluetoothAdapter mAdapter;
36
John Spurlockd1c86e22014-06-01 00:04:53 -040037 private boolean mEnabled;
38 private boolean mConnecting;
39 private BluetoothDevice mLastDevice;
John Spurlockaf8d6c42014-05-07 17:49:08 -040040
41 public BluetoothControllerImpl(Context context) {
42 mAdapter = BluetoothAdapter.getDefaultAdapter();
43
44 IntentFilter filter = new IntentFilter();
45 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
46 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
47 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
48 context.registerReceiver(this, filter);
49
50 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
51 if (adapter != null) {
52 handleAdapterStateChange(adapter.getState());
53 }
54 fireCallbacks();
55 updateBondedBluetoothDevices();
56 }
57
John Spurlockd1c86e22014-06-01 00:04:53 -040058 public void addStateChangedCallback(Callback cb) {
59 mCallbacks.add(cb);
John Spurlockccb6b9a2014-05-17 15:54:40 -040060 fireCallback(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040061 }
62
63 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040064 public void removeStateChangedCallback(Callback cb) {
65 mCallbacks.remove(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -040066 }
67
68 @Override
69 public boolean isBluetoothEnabled() {
70 return mAdapter != null && mAdapter.isEnabled();
71 }
72
73 @Override
74 public boolean isBluetoothConnected() {
75 return mAdapter != null
76 && mAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
77 }
78
79 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -040080 public boolean isBluetoothConnecting() {
81 return mAdapter != null
82 && mAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTING;
83 }
84
85 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 public void setBluetoothEnabled(boolean enabled) {
87 if (mAdapter != null) {
88 if (enabled) {
89 mAdapter.enable();
90 } else {
91 mAdapter.disable();
92 }
93 }
94 }
95
96 @Override
97 public boolean isBluetoothSupported() {
98 return mAdapter != null;
99 }
100
101 public Set<BluetoothDevice> getBondedBluetoothDevices() {
102 return mBondedDevices;
103 }
104
105 @Override
John Spurlockd1c86e22014-06-01 00:04:53 -0400106 public String getLastDeviceName() {
107 return mLastDevice != null ? mLastDevice.getName()
108 : mBondedDevices.size() == 1 ? mBondedDevices.iterator().next().getName()
109 : null;
110 }
111
112 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 public void onReceive(Context context, Intent intent) {
114 final String action = intent.getAction();
115
116 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
117 handleAdapterStateChange(
118 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
119 }
John Spurlockd1c86e22014-06-01 00:04:53 -0400120 if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
121 mConnecting = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, -1)
122 == BluetoothAdapter.STATE_CONNECTING;
123 mLastDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
124 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400125 fireCallbacks();
126 updateBondedBluetoothDevices();
127 }
128
129 private void updateBondedBluetoothDevices() {
130 mBondedDevices.clear();
131
132 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
133 if (adapter != null) {
134 Set<BluetoothDevice> devices = adapter.getBondedDevices();
135 if (devices != null) {
136 for (BluetoothDevice device : devices) {
137 if (device.getBondState() != BluetoothDevice.BOND_NONE) {
138 mBondedDevices.add(device);
139 }
140 }
141 }
142 }
143 }
144
145 private void handleAdapterStateChange(int adapterState) {
146 mEnabled = (adapterState == BluetoothAdapter.STATE_ON);
147 }
148
149 private void fireCallbacks() {
John Spurlockd1c86e22014-06-01 00:04:53 -0400150 for (Callback cb : mCallbacks) {
John Spurlockccb6b9a2014-05-17 15:54:40 -0400151 fireCallback(cb);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400152 }
153 }
John Spurlockccb6b9a2014-05-17 15:54:40 -0400154
John Spurlockd1c86e22014-06-01 00:04:53 -0400155 private void fireCallback(Callback cb) {
156 cb.onBluetoothStateChange(mEnabled, mConnecting);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400157 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400158}