1package com.android.bluetooth.btservice; 2 3import android.bluetooth.BluetoothAdapter; 4import android.content.BroadcastReceiver; 5import android.content.Context; 6import android.content.Intent; 7import android.content.IntentFilter; 8import android.database.ContentObserver; 9import android.os.Handler; 10import 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 */ 16public class ProfileObserver extends ContentObserver { 17 private Context mContext; 18 private AdapterService mService; 19 private AdapterStateObserver mStateObserver; 20 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 28 public void start() { 29 mContext.getContentResolver() 30 .registerContentObserver( 31 Settings.Global.getUriFor(Settings.Global.BLUETOOTH_DISABLED_PROFILES), 32 false, this); 33 } 34 35 private void onBluetoothOff() { 36 mContext.unregisterReceiver(mStateObserver); 37 Config.init(mContext); 38 mService.enable(); 39 } 40 41 public void stop() { 42 mContext.getContentResolver().unregisterContentObserver(this); 43 } 44 45 @Override 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 57 AdapterStateObserver(ProfileObserver observer) { 58 mProfileObserver = observer; 59 } 60 61 @Override 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