blob: 1c7119facf3aca3219a535655159658ff50c7649 [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;
20import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
21import android.bluetooth.BluetoothDevice;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26
27import java.util.ArrayList;
28import java.util.HashSet;
29import java.util.Set;
30
31public class BluetoothControllerImpl extends BroadcastReceiver implements BluetoothController {
32 private static final String TAG = "StatusBar.BluetoothController";
33
34 private final BluetoothAdapter mAdapter;
35
36 private boolean mEnabled = false;
37
38 private Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>();
39
40 private ArrayList<BluetoothStateChangeCallback> mChangeCallbacks =
41 new ArrayList<BluetoothStateChangeCallback>();
42
43 public BluetoothControllerImpl(Context context) {
44 mAdapter = BluetoothAdapter.getDefaultAdapter();
45
46 IntentFilter filter = new IntentFilter();
47 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
48 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
49 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
50 context.registerReceiver(this, filter);
51
52 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
53 if (adapter != null) {
54 handleAdapterStateChange(adapter.getState());
55 }
56 fireCallbacks();
57 updateBondedBluetoothDevices();
58 }
59
60 public void addStateChangedCallback(BluetoothStateChangeCallback cb) {
61 mChangeCallbacks.add(cb);
62 }
63
64 @Override
65 public void removeStateChangedCallback(BluetoothStateChangeCallback cb) {
66 mChangeCallbacks.remove(cb);
67 }
68
69 @Override
70 public boolean isBluetoothEnabled() {
71 return mAdapter != null && mAdapter.isEnabled();
72 }
73
74 @Override
75 public boolean isBluetoothConnected() {
76 return mAdapter != null
77 && mAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
78 }
79
80 @Override
81 public void setBluetoothEnabled(boolean enabled) {
82 if (mAdapter != null) {
83 if (enabled) {
84 mAdapter.enable();
85 } else {
86 mAdapter.disable();
87 }
88 }
89 }
90
91 @Override
92 public boolean isBluetoothSupported() {
93 return mAdapter != null;
94 }
95
96 public Set<BluetoothDevice> getBondedBluetoothDevices() {
97 return mBondedDevices;
98 }
99
100 @Override
101 public void onReceive(Context context, Intent intent) {
102 final String action = intent.getAction();
103
104 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
105 handleAdapterStateChange(
106 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
107 }
108 fireCallbacks();
109 updateBondedBluetoothDevices();
110 }
111
112 private void updateBondedBluetoothDevices() {
113 mBondedDevices.clear();
114
115 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
116 if (adapter != null) {
117 Set<BluetoothDevice> devices = adapter.getBondedDevices();
118 if (devices != null) {
119 for (BluetoothDevice device : devices) {
120 if (device.getBondState() != BluetoothDevice.BOND_NONE) {
121 mBondedDevices.add(device);
122 }
123 }
124 }
125 }
126 }
127
128 private void handleAdapterStateChange(int adapterState) {
129 mEnabled = (adapterState == BluetoothAdapter.STATE_ON);
130 }
131
132 private void fireCallbacks() {
133 for (BluetoothStateChangeCallback cb : mChangeCallbacks) {
134 cb.onBluetoothStateChange(mEnabled);
135 }
136 }
137}