• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.btservice;
2 
3 import android.bluetooth.BluetoothAdapter;
4 import android.content.BroadcastReceiver;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.IntentFilter;
8 import android.database.ContentObserver;
9 import android.os.Handler;
10 import android.provider.Settings;
11 
12 /**
13  * This helper class monitors the state of the enabled profiles and will update and restart
14  * the adapter when necessary.
15  */
16 public class ProfileObserver extends ContentObserver {
17     private Context mContext;
18     private AdapterService mService;
19     private AdapterStateObserver mStateObserver;
20 
ProfileObserver(Context context, AdapterService service, Handler handler)21     public ProfileObserver(Context context, AdapterService service, Handler handler) {
22         super(handler);
23         mContext = context;
24         mService = service;
25         mStateObserver = new AdapterStateObserver(this);
26     }
27 
start()28     public void start() {
29         mContext.getContentResolver()
30                 .registerContentObserver(
31                         Settings.Global.getUriFor(Settings.Global.BLUETOOTH_DISABLED_PROFILES),
32                         false, this);
33     }
34 
onBluetoothOff()35     private void onBluetoothOff() {
36         mContext.unregisterReceiver(mStateObserver);
37         Config.init(mContext);
38         mService.enable();
39     }
40 
stop()41     public void stop() {
42         mContext.getContentResolver().unregisterContentObserver(this);
43     }
44 
45     @Override
onChange(boolean selfChange)46     public void onChange(boolean selfChange) {
47         if (mService.isEnabled()) {
48             mContext.registerReceiver(mStateObserver,
49                     new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
50             mService.disable();
51         }
52     }
53 
54     private static class AdapterStateObserver extends BroadcastReceiver {
55         private ProfileObserver mProfileObserver;
56 
AdapterStateObserver(ProfileObserver observer)57         AdapterStateObserver(ProfileObserver observer) {
58             mProfileObserver = observer;
59         }
60 
61         @Override
onReceive(Context context, Intent intent)62         public void onReceive(Context context, Intent intent) {
63             if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())
64                     && intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
65                     == BluetoothAdapter.STATE_OFF) {
66                 mProfileObserver.onBluetoothOff();
67             }
68         }
69     }
70 }
71