The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 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; |
| 18 | |
| 19 | import com.android.settings.wifi.WifiEnabler; |
| 20 | |
| 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothIntent; |
| 23 | import android.bluetooth.IBluetoothDeviceCallback; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.net.wifi.WifiManager; |
| 29 | import android.os.Bundle; |
| 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
| 32 | import android.preference.Preference; |
| 33 | import android.preference.PreferenceActivity; |
| 34 | import android.preference.PreferenceScreen; |
| 35 | import android.preference.CheckBoxPreference; |
| 36 | import android.provider.Settings; |
| 37 | import android.widget.Toast; |
| 38 | |
| 39 | public class WirelessSettings extends PreferenceActivity { |
| 40 | |
| 41 | private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane"; |
| 42 | private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth"; |
| 43 | private static final String KEY_TOGGLE_WIFI = "toggle_wifi"; |
| 44 | |
| 45 | private WifiEnabler mWifiEnabler; |
| 46 | private AirplaneModeEnabler mAirplaneModeEnabler; |
| 47 | |
| 48 | private CheckBoxPreference mToggleBluetooth; |
| 49 | |
| 50 | private IntentFilter mIntentFilter; |
| 51 | |
| 52 | private static final int EVENT_FAILED_BT_ENABLE = 1; |
| 53 | private static final int EVENT_PASSED_BT_ENABLE = 2; |
| 54 | |
| 55 | @Override |
| 56 | protected void onCreate(Bundle savedInstanceState) { |
| 57 | super.onCreate(savedInstanceState); |
| 58 | |
| 59 | addPreferencesFromResource(R.xml.wireless_settings); |
| 60 | |
| 61 | mIntentFilter = new IntentFilter(); |
| 62 | mIntentFilter.addAction(BluetoothIntent.ENABLED_ACTION); |
| 63 | mIntentFilter.addAction(BluetoothIntent.DISABLED_ACTION); |
| 64 | |
| 65 | initToggles(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onResume() { |
| 70 | super.onResume(); |
| 71 | refreshToggles(); |
| 72 | registerReceiver(mReceiver, mIntentFilter); |
| 73 | |
| 74 | mWifiEnabler.resume(); |
| 75 | mAirplaneModeEnabler.resume(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onPause() { |
| 80 | super.onPause(); |
| 81 | unregisterReceiver(mReceiver); |
| 82 | |
| 83 | mWifiEnabler.pause(); |
| 84 | mAirplaneModeEnabler.pause(); |
| 85 | } |
| 86 | |
| 87 | private void initToggles() { |
| 88 | |
| 89 | mWifiEnabler = new WifiEnabler( |
| 90 | this, |
| 91 | (WifiManager) getSystemService(WIFI_SERVICE), |
| 92 | (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI)); |
| 93 | |
| 94 | mAirplaneModeEnabler = new AirplaneModeEnabler( |
| 95 | this, |
| 96 | (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE)); |
| 97 | |
| 98 | mToggleBluetooth = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH); |
| 99 | mToggleBluetooth.setPersistent(false); |
| 100 | } |
| 101 | |
| 102 | private void refreshToggles() { |
| 103 | mToggleBluetooth.setChecked(isBluetoothEnabled()); |
| 104 | mToggleBluetooth.setEnabled(true); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 109 | if (preference == mToggleBluetooth) { |
| 110 | setBluetoothEnabled(mToggleBluetooth.isChecked()); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | private boolean isBluetoothEnabled() { |
| 118 | BluetoothDevice device = (BluetoothDevice)getSystemService(BLUETOOTH_SERVICE); |
| 119 | if (device != null) { |
| 120 | return device.isEnabled(); |
| 121 | } else { |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private void setBluetoothEnabled(boolean enabled) { |
| 127 | try { |
| 128 | BluetoothDevice device = (BluetoothDevice)getSystemService(BLUETOOTH_SERVICE); |
| 129 | if (enabled) { |
| 130 | // Turn it off until intent or callback is delivered |
| 131 | mToggleBluetooth.setChecked(false); |
| 132 | if (device.enable(mBtCallback)) { |
| 133 | mToggleBluetooth.setSummary(R.string.bluetooth_enabling); |
| 134 | mToggleBluetooth.setEnabled(false); |
| 135 | } |
| 136 | } else { |
| 137 | if (device.disable()) { |
| 138 | Settings.System.putInt(getContentResolver(), |
| 139 | Settings.System.BLUETOOTH_ON, 0); |
| 140 | } else { |
| 141 | // Unusual situation, that you can't turn off bluetooth |
| 142 | mToggleBluetooth.setChecked(true); |
| 143 | } |
| 144 | } |
| 145 | } catch (NullPointerException e) { |
| 146 | // TODO: 1071858 |
| 147 | mToggleBluetooth.setChecked(false); |
| 148 | mToggleBluetooth.setEnabled(false); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private IBluetoothDeviceCallback mBtCallback = new IBluetoothDeviceCallback.Stub() { |
| 153 | |
| 154 | public void onEnableResult(int res) { |
| 155 | switch (res) { |
| 156 | case BluetoothDevice.RESULT_FAILURE: |
| 157 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_FAILED_BT_ENABLE, 0)); |
| 158 | break; |
| 159 | case BluetoothDevice.RESULT_SUCCESS: |
| 160 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_PASSED_BT_ENABLE, 0)); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | public void onCreateBondingResult(String device, int res) { |
| 166 | // Don't care |
| 167 | } |
| 168 | public void onGetRemoteServiceChannelResult(String address, int channel) { } |
| 169 | }; |
| 170 | |
| 171 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 172 | @Override |
| 173 | public void onReceive(Context context, Intent intent) { |
| 174 | String action = intent.getAction(); |
| 175 | if (action.equals(BluetoothIntent.ENABLED_ACTION)) { |
| 176 | updateBtStatus(true); |
| 177 | } else if (action.equals(BluetoothIntent.DISABLED_ACTION)) { |
| 178 | mToggleBluetooth.setChecked(false); |
| 179 | } |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | private void updateBtStatus(boolean enabled) { |
| 184 | mToggleBluetooth.setChecked(enabled); |
| 185 | mToggleBluetooth.setEnabled(true); |
| 186 | mToggleBluetooth.setSummary(R.string.bluetooth_quick_toggle_summary); |
| 187 | if (enabled) { |
| 188 | Settings.System.putInt(getContentResolver(), |
| 189 | Settings.System.BLUETOOTH_ON, 1); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private Handler mHandler = new Handler() { |
| 194 | @Override |
| 195 | public void handleMessage(Message msg) { |
| 196 | switch (msg.what) { |
| 197 | case EVENT_PASSED_BT_ENABLE: |
| 198 | updateBtStatus(true); |
| 199 | break; |
| 200 | case EVENT_FAILED_BT_ENABLE: |
| 201 | updateBtStatus(false); |
| 202 | Toast.makeText(WirelessSettings.this, |
| 203 | getResources().getString(R.string.bluetooth_failed_to_enable), |
| 204 | Toast.LENGTH_SHORT).show(); |
| 205 | |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | } |