John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import android.bluetooth.BluetoothAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.bluetooth.BluetoothDevice; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | public class BluetoothControllerImpl extends BroadcastReceiver implements BluetoothController { |
| 31 | private static final String TAG = "StatusBar.BluetoothController"; |
| 32 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 33 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
| 34 | private final Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | private final BluetoothAdapter mAdapter; |
| 36 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 37 | private boolean mEnabled; |
| 38 | private boolean mConnecting; |
| 39 | private BluetoothDevice mLastDevice; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 40 | |
| 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 Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 58 | public void addStateChangedCallback(Callback cb) { |
| 59 | mCallbacks.add(cb); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 60 | fireCallback(cb); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 64 | public void removeStateChangedCallback(Callback cb) { |
| 65 | mCallbacks.remove(cb); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | } |
| 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 Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 80 | public boolean isBluetoothConnecting() { |
| 81 | return mAdapter != null |
| 82 | && mAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTING; |
| 83 | } |
| 84 | |
| 85 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | 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 Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 106 | public String getLastDeviceName() { |
| 107 | return mLastDevice != null ? mLastDevice.getName() |
| 108 | : mBondedDevices.size() == 1 ? mBondedDevices.iterator().next().getName() |
| 109 | : null; |
| 110 | } |
| 111 | |
| 112 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 113 | 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 Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 120 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 125 | 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 Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 150 | for (Callback cb : mCallbacks) { |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 151 | fireCallback(cb); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 154 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame^] | 155 | private void fireCallback(Callback cb) { |
| 156 | cb.onBluetoothStateChange(mEnabled, mConnecting); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 157 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 158 | } |