summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/btservice/ProfileObserver.java
blob: cccd96ad4e1ed03beda1b33866d0e6a604fff6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.android.bluetooth.btservice;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;

/**
 * This helper class monitors the state of the enabled profiles and will update and restart
 * the adapter when necessary.
 */
public class ProfileObserver extends ContentObserver {
    private Context mContext;
    private AdapterService mService;
    private AdapterStateObserver mStateObserver;

    public ProfileObserver(Context context, AdapterService service, Handler handler) {
        super(handler);
        mContext = context;
        mService = service;
        mStateObserver = new AdapterStateObserver(this);
    }

    public void start() {
        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.BLUETOOTH_DISABLED_PROFILES), false,
                this);
    }

    private void onBluetoothOff() {
        mContext.unregisterReceiver(mStateObserver);
        Config.init(mContext);
        mService.enable();
    }

    public void stop() {
        mContext.getContentResolver().unregisterContentObserver(this);
    }

    @Override
    public void onChange(boolean selfChange) {
        if (mService.isEnabled()) {
            mContext.registerReceiver(mStateObserver,
                    new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
            mService.disable();
        }
    }

    private static class AdapterStateObserver extends BroadcastReceiver {
        private ProfileObserver mProfileObserver;

        public AdapterStateObserver(ProfileObserver observer) {
            mProfileObserver = observer;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())
                    && intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                    == BluetoothAdapter.STATE_OFF) {
                mProfileObserver.onBluetoothOff();
            }
        }
    }
}