summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/bluetooth/BluetoothEnabler.java
blob: 661700fd3c57648b19778b166f76aa5a5347e972 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.bluetooth;

import com.android.settings.R;
import com.android.settings.bluetooth.LocalBluetoothManager.ExtendedBluetoothState;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.preference.Preference;
import android.preference.CheckBoxPreference;
import android.text.TextUtils;
import android.util.Config;

/**
 * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
 * preference. It is turns on/off Bluetooth and ensures the summary of the
 * preference reflects the current state.
 */
public class BluetoothEnabler implements Preference.OnPreferenceChangeListener {
    
    private static final boolean LOCAL_LOGD = Config.LOGD || false;
    private static final String TAG = "BluetoothEnabler";
    
    private final Context mContext; 
    private final CheckBoxPreference mCheckBoxPreference;
    private final CharSequence mOriginalSummary;
    
    private final LocalBluetoothManager mLocalManager;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            handleStateChanged(mLocalManager.getBluetoothState());
        }
    };

    public BluetoothEnabler(Context context, CheckBoxPreference checkBoxPreference) {
        mContext = context;
        mCheckBoxPreference = checkBoxPreference;
        
        mOriginalSummary = checkBoxPreference.getSummary();
        checkBoxPreference.setPersistent(false);
        
        mLocalManager = LocalBluetoothManager.getInstance(context);
        if (mLocalManager == null) {
            // Bluetooth not supported
            checkBoxPreference.setEnabled(false);
        }
    }

    public void resume() {
        if (mLocalManager == null) {
            return;
        }
        
        ExtendedBluetoothState state = mLocalManager.getBluetoothState();
        // This is the widget enabled state, not the preference toggled state
        mCheckBoxPreference.setEnabled(state == ExtendedBluetoothState.ENABLED ||
                state == ExtendedBluetoothState.DISABLED);
        // BT state is not a sticky broadcast, so set it manually
        handleStateChanged(state);
        
        mContext.registerReceiver(mReceiver, 
                new IntentFilter(LocalBluetoothManager.EXTENDED_BLUETOOTH_STATE_CHANGED_ACTION));
        mCheckBoxPreference.setOnPreferenceChangeListener(this);
    }
    
    public void pause() {
        if (mLocalManager == null) {
            return;
        }
        
        mContext.unregisterReceiver(mReceiver);
        mCheckBoxPreference.setOnPreferenceChangeListener(null);
    }
    
    public boolean onPreferenceChange(Preference preference, Object value) {
        // Turn on/off BT
        setEnabled((Boolean) value);
        
        // Don't update UI to opposite state until we're sure
        return false;
    }
    
    private void setEnabled(final boolean enable) {
        // Disable preference
        mCheckBoxPreference.setEnabled(false);
        
        mLocalManager.setBluetoothEnabled(enable);
    }
    
    private void handleStateChanged(ExtendedBluetoothState state) {

        if (state == ExtendedBluetoothState.DISABLED || state == ExtendedBluetoothState.ENABLED) {
            mCheckBoxPreference.setChecked(state == ExtendedBluetoothState.ENABLED);
            mCheckBoxPreference
                    .setSummary(state == ExtendedBluetoothState.DISABLED ? mOriginalSummary : null);
            
            mCheckBoxPreference.setEnabled(isEnabledByDependency());
            
        } else if (state == ExtendedBluetoothState.ENABLING ||
                state == ExtendedBluetoothState.DISABLING) {
            mCheckBoxPreference.setSummary(state == ExtendedBluetoothState.ENABLING
                    ? R.string.wifi_starting
                    : R.string.wifi_stopping);
            
        } else if (state == ExtendedBluetoothState.UNKNOWN) {
            mCheckBoxPreference.setChecked(false);
            mCheckBoxPreference.setSummary(R.string.wifi_error);
            mCheckBoxPreference.setEnabled(true);
        }
    }

    private boolean isEnabledByDependency() {
        Preference dep = getDependencyPreference();
        if (dep == null) {
            return true;
        }
        
        return !dep.shouldDisableDependents();
    }
    
    private Preference getDependencyPreference() {
        String depKey = mCheckBoxPreference.getDependency();
        if (TextUtils.isEmpty(depKey)) {
            return null;
        }
        
        return mCheckBoxPreference.getPreferenceManager().findPreference(depKey);
    }
    
}