Home | History | Annotate | Download | only in policy
      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;
     20 import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
     21 import android.bluetooth.BluetoothDevice;
     22 import android.content.BroadcastReceiver;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 
     27 import java.util.ArrayList;
     28 import java.util.HashSet;
     29 import java.util.Set;
     30 
     31 public class BluetoothController extends BroadcastReceiver {
     32     private static final String TAG = "StatusBar.BluetoothController";
     33 
     34     private boolean mEnabled = false;
     35 
     36     private Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>();
     37 
     38     private ArrayList<BluetoothStateChangeCallback> mChangeCallbacks =
     39             new ArrayList<BluetoothStateChangeCallback>();
     40 
     41     public BluetoothController(Context context) {
     42 
     43         IntentFilter filter = new IntentFilter();
     44         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
     45         filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
     46         filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
     47         context.registerReceiver(this, filter);
     48 
     49         final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
     50         if (adapter != null) {
     51             handleAdapterStateChange(adapter.getState());
     52         }
     53         fireCallbacks();
     54         updateBondedBluetoothDevices();
     55     }
     56 
     57     public void addStateChangedCallback(BluetoothStateChangeCallback cb) {
     58         mChangeCallbacks.add(cb);
     59     }
     60 
     61     public Set<BluetoothDevice> getBondedBluetoothDevices() {
     62         return mBondedDevices;
     63     }
     64 
     65     @Override
     66     public void onReceive(Context context, Intent intent) {
     67         final String action = intent.getAction();
     68 
     69         if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
     70             handleAdapterStateChange(
     71                     intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
     72         }
     73         fireCallbacks();
     74         updateBondedBluetoothDevices();
     75     }
     76 
     77     private void updateBondedBluetoothDevices() {
     78         mBondedDevices.clear();
     79 
     80         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
     81         if (adapter != null) {
     82             Set<BluetoothDevice> devices = adapter.getBondedDevices();
     83             if (devices != null) {
     84                 for (BluetoothDevice device : devices) {
     85                     if (device.getBondState() != BluetoothDevice.BOND_NONE) {
     86                         mBondedDevices.add(device);
     87                     }
     88                 }
     89             }
     90         }
     91     }
     92 
     93     private void handleAdapterStateChange(int adapterState) {
     94         mEnabled = (adapterState == BluetoothAdapter.STATE_ON);
     95     }
     96 
     97     private void fireCallbacks() {
     98         for (BluetoothStateChangeCallback cb : mChangeCallbacks) {
     99             cb.onBluetoothStateChange(mEnabled);
    100         }
    101     }
    102 }
    103