• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.bluetooth;
18 
19 import com.android.settings.R;
20 
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothError;
23 import android.bluetooth.BluetoothIntent;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.preference.Preference;
29 import android.preference.CheckBoxPreference;
30 import android.text.TextUtils;
31 import android.util.Config;
32 
33 /**
34  * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
35  * preference. It is turns on/off Bluetooth and ensures the summary of the
36  * preference reflects the current state.
37  */
38 public class BluetoothEnabler implements Preference.OnPreferenceChangeListener {
39 
40     private static final boolean LOCAL_LOGD = Config.LOGD || false;
41     private static final String TAG = "BluetoothEnabler";
42 
43     private final Context mContext;
44     private final CheckBoxPreference mCheckBoxPreference;
45     private final CharSequence mOriginalSummary;
46 
47     private final LocalBluetoothManager mLocalManager;
48 
49     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
50         @Override
51         public void onReceive(Context context, Intent intent) {
52             int state = intent.getIntExtra(BluetoothIntent.BLUETOOTH_STATE,
53                     BluetoothError.ERROR);
54             handleStateChanged(state);
55         }
56     };
57 
BluetoothEnabler(Context context, CheckBoxPreference checkBoxPreference)58     public BluetoothEnabler(Context context, CheckBoxPreference checkBoxPreference) {
59         mContext = context;
60         mCheckBoxPreference = checkBoxPreference;
61 
62         mOriginalSummary = checkBoxPreference.getSummary();
63         checkBoxPreference.setPersistent(false);
64 
65         mLocalManager = LocalBluetoothManager.getInstance(context);
66         if (mLocalManager == null) {
67             // Bluetooth not supported
68             checkBoxPreference.setEnabled(false);
69         }
70     }
71 
resume()72     public void resume() {
73         if (mLocalManager == null) {
74             return;
75         }
76 
77         int state = mLocalManager.getBluetoothState();
78         // This is the widget enabled state, not the preference toggled state
79         mCheckBoxPreference.setEnabled(state == BluetoothDevice.BLUETOOTH_STATE_ON ||
80                 state == BluetoothDevice.BLUETOOTH_STATE_OFF);
81         // BT state is not a sticky broadcast, so set it manually
82         handleStateChanged(state);
83 
84         mContext.registerReceiver(mReceiver,
85                 new IntentFilter(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION));
86         mCheckBoxPreference.setOnPreferenceChangeListener(this);
87     }
88 
pause()89     public void pause() {
90         if (mLocalManager == null) {
91             return;
92         }
93 
94         mContext.unregisterReceiver(mReceiver);
95         mCheckBoxPreference.setOnPreferenceChangeListener(null);
96     }
97 
onPreferenceChange(Preference preference, Object value)98     public boolean onPreferenceChange(Preference preference, Object value) {
99         // Turn on/off BT
100         setEnabled((Boolean) value);
101 
102         // Don't update UI to opposite state until we're sure
103         return false;
104     }
105 
setEnabled(final boolean enable)106     private void setEnabled(final boolean enable) {
107         // Disable preference
108         mCheckBoxPreference.setEnabled(false);
109 
110         mLocalManager.setBluetoothEnabled(enable);
111     }
112 
handleStateChanged(int state)113     private void handleStateChanged(int state) {
114 
115         if (state == BluetoothDevice.BLUETOOTH_STATE_OFF ||
116                 state == BluetoothDevice.BLUETOOTH_STATE_ON) {
117             mCheckBoxPreference.setChecked(state == BluetoothDevice.BLUETOOTH_STATE_ON);
118             mCheckBoxPreference.setSummary(state == BluetoothDevice.BLUETOOTH_STATE_OFF ?
119                                            mOriginalSummary :
120                                            null);
121 
122             mCheckBoxPreference.setEnabled(isEnabledByDependency());
123 
124         } else if (state == BluetoothDevice.BLUETOOTH_STATE_TURNING_ON ||
125                 state == BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF) {
126             mCheckBoxPreference.setSummary(state == BluetoothDevice.BLUETOOTH_STATE_TURNING_ON
127                     ? R.string.wifi_starting
128                     : R.string.wifi_stopping);
129 
130         } else {
131             mCheckBoxPreference.setChecked(false);
132             mCheckBoxPreference.setSummary(R.string.wifi_error);
133             mCheckBoxPreference.setEnabled(true);
134         }
135     }
136 
isEnabledByDependency()137     private boolean isEnabledByDependency() {
138         Preference dep = getDependencyPreference();
139         if (dep == null) {
140             return true;
141         }
142 
143         return !dep.shouldDisableDependents();
144     }
145 
getDependencyPreference()146     private Preference getDependencyPreference() {
147         String depKey = mCheckBoxPreference.getDependency();
148         if (TextUtils.isEmpty(depKey)) {
149             return null;
150         }
151 
152         return mCheckBoxPreference.getPreferenceManager().findPreference(depKey);
153     }
154 
155 }
156