1 /* 2 * Copyright (C) 2010 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 import com.android.settings.WirelessSettings; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.preference.Preference; 28 import android.preference.CheckBoxPreference; 29 import android.provider.Settings; 30 import android.text.TextUtils; 31 import android.widget.Toast; 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 private final Context mContext; 40 private final CheckBoxPreference mCheckBox; 41 private final CharSequence mOriginalSummary; 42 43 private final LocalBluetoothManager mLocalManager; 44 private final IntentFilter mIntentFilter; 45 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 46 @Override 47 public void onReceive(Context context, Intent intent) { 48 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); 49 handleStateChanged(state); 50 } 51 }; 52 BluetoothEnabler(Context context, CheckBoxPreference checkBox)53 public BluetoothEnabler(Context context, CheckBoxPreference checkBox) { 54 mContext = context; 55 mCheckBox = checkBox; 56 mOriginalSummary = checkBox.getSummary(); 57 checkBox.setPersistent(false); 58 59 mLocalManager = LocalBluetoothManager.getInstance(context); 60 if (mLocalManager == null) { 61 // Bluetooth is not supported 62 checkBox.setEnabled(false); 63 } 64 mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 65 } 66 resume()67 public void resume() { 68 if (mLocalManager == null) { 69 return; 70 } 71 72 // Bluetooth state is not sticky, so set it manually 73 handleStateChanged(mLocalManager.getBluetoothState()); 74 75 mContext.registerReceiver(mReceiver, mIntentFilter); 76 mCheckBox.setOnPreferenceChangeListener(this); 77 } 78 pause()79 public void pause() { 80 if (mLocalManager == null) { 81 return; 82 } 83 84 mContext.unregisterReceiver(mReceiver); 85 mCheckBox.setOnPreferenceChangeListener(null); 86 } 87 onPreferenceChange(Preference preference, Object value)88 public boolean onPreferenceChange(Preference preference, Object value) { 89 boolean enable = (Boolean) value; 90 91 // Show toast message if Bluetooth is not allowed in airplane mode 92 if (enable && !WirelessSettings 93 .isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) { 94 Toast.makeText(mContext, R.string.wifi_in_airplane_mode, 95 Toast.LENGTH_SHORT).show(); 96 return false; 97 } 98 99 mLocalManager.setBluetoothEnabled(enable); 100 mCheckBox.setEnabled(false); 101 102 // Don't update UI to opposite state until we're sure 103 return false; 104 } 105 handleStateChanged(int state)106 private void handleStateChanged(int state) { 107 switch (state) { 108 case BluetoothAdapter.STATE_TURNING_ON: 109 mCheckBox.setSummary(R.string.wifi_starting); 110 mCheckBox.setEnabled(false); 111 break; 112 case BluetoothAdapter.STATE_ON: 113 mCheckBox.setChecked(true); 114 mCheckBox.setSummary(null); 115 mCheckBox.setEnabled(true); 116 break; 117 case BluetoothAdapter.STATE_TURNING_OFF: 118 mCheckBox.setSummary(R.string.wifi_stopping); 119 mCheckBox.setEnabled(false); 120 break; 121 case BluetoothAdapter.STATE_OFF: 122 mCheckBox.setChecked(false); 123 mCheckBox.setSummary(mOriginalSummary); 124 mCheckBox.setEnabled(true); 125 break; 126 default: 127 mCheckBox.setChecked(false); 128 mCheckBox.setSummary(R.string.wifi_error); 129 mCheckBox.setEnabled(true); 130 } 131 } 132 } 133