• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.tv.settings.accessories;
2 
3 import android.bluetooth.BluetoothAdapter;
4 import android.bluetooth.BluetoothDevice;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.support.v4.content.LocalBroadcastManager;
10 import android.text.TextUtils;
11 
12 import com.android.tv.settings.MainSettings;
13 
14 import java.util.HashSet;
15 import java.util.Set;
16 
17 public class BluetoothConnectionsManager extends BroadcastReceiver {
18     private static final String PREFS_NAME = "bt-connected-devs";
19     private static final String KEY_CONNECTED_SET = "conencted-set";
20     private static final String KEY_BT_STATE = "bt-state";
21 
22     public static final String ACTION_BLUETOOTH_UPDATE =
23             "BluetoothConnectionsManager.BLUETOOTH_UPDATE";
24 
onReceive(Context context, Intent intent)25     public void onReceive(Context context, Intent intent) {
26         onConnectionChanged(context, intent);
27     }
28 
onConnectionChanged(Context context, Intent intent)29     public static void onConnectionChanged(Context context, Intent intent) {
30         final String action = intent.getAction();
31 
32         final SharedPreferences prefs =
33                 context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
34         Set<String> connected = prefs.getStringSet(KEY_CONNECTED_SET, new HashSet<String>());
35         int btState = prefs.getInt(KEY_BT_STATE, BluetoothAdapter.STATE_OFF);
36 
37         boolean listChanged = false;
38         boolean btStateChanged = false;
39 
40         if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
41             final int newBtState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
42                     BluetoothAdapter.ERROR);
43 
44             if (btState != newBtState) {
45                 // if BT was just turned off, we can't be connected to any devices.
46                 // if BT was just turned on, we haven't had the time to connect to any devices yet.
47                 if (newBtState == BluetoothAdapter.STATE_ON ||
48                         newBtState == BluetoothAdapter.STATE_OFF) {
49                     listChanged = true;
50                     connected.clear();
51                 }
52 
53                 btStateChanged = true;
54                 btState = newBtState;
55             }
56         } else if (intent.getExtras() != null) {
57             BluetoothDevice device = intent.getExtras().getParcelable(BluetoothDevice.EXTRA_DEVICE);
58             if (device != null) {
59                 if (TextUtils.equals(action, BluetoothDevice.ACTION_ACL_CONNECTED)) {
60                     listChanged = connected.add(device.getAddress());
61                 } else if (TextUtils.equals(action, BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
62                     listChanged = connected.remove(device.getAddress());
63                 }
64             }
65         }
66 
67         if (btStateChanged || listChanged) {
68             SharedPreferences.Editor editor = prefs.edit();
69 
70             if (btStateChanged) {
71                 editor.putInt(KEY_BT_STATE, btState);
72             }
73             if (listChanged) {
74                 editor.putStringSet(KEY_CONNECTED_SET, connected);
75             }
76 
77             editor.apply();
78         }
79 
80         if (listChanged) {
81             LocalBroadcastManager.getInstance(context)
82                     .sendBroadcast(new Intent(ACTION_BLUETOOTH_UPDATE));
83         }
84     }
85 
getConnectedSet(Context context)86     public static Set<String> getConnectedSet(Context context) {
87         SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
88         return prefs.getStringSet(KEY_CONNECTED_SET, new HashSet<String>());
89     }
90 }
91