• 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().registerContentObserver(
30                 Settings.Global.getUriFor(Settings.Global.BLUETOOTH_DISABLED_PROFILES), false,
31                 this);
32     }
33 
onBluetoothOff()34     private void onBluetoothOff() {
35         mContext.unregisterReceiver(mStateObserver);
36         Config.init(mContext);
37         mService.enable();
38     }
39 
stop()40     public void stop() {
41         mContext.getContentResolver().unregisterContentObserver(this);
42     }
43 
44     @Override
onChange(boolean selfChange)45     public void onChange(boolean selfChange) {
46         if (mService.isEnabled()) {
47             mContext.registerReceiver(mStateObserver,
48                     new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
49             mService.disable();
50         }
51     }
52 
53     private static class AdapterStateObserver extends BroadcastReceiver {
54         private ProfileObserver mProfileObserver;
55 
AdapterStateObserver(ProfileObserver observer)56         public AdapterStateObserver(ProfileObserver observer) {
57             mProfileObserver = observer;
58         }
59 
60         @Override
onReceive(Context context, Intent intent)61         public void onReceive(Context context, Intent intent) {
62             if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())
63                     && intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
64                     == BluetoothAdapter.STATE_OFF) {
65                 mProfileObserver.onBluetoothOff();
66             }
67         }
68     }
69 }
70