Code drop from //branches/cupcake/...@124589
This commit is contained in:
@@ -16,25 +16,13 @@
|
||||
|
||||
package com.android.settings;
|
||||
|
||||
import com.android.settings.bluetooth.BluetoothEnabler;
|
||||
import com.android.settings.wifi.WifiEnabler;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothIntent;
|
||||
import android.bluetooth.IBluetoothDeviceCallback;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.provider.Settings;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class WirelessSettings extends PreferenceActivity {
|
||||
|
||||
@@ -44,13 +32,7 @@ public class WirelessSettings extends PreferenceActivity {
|
||||
|
||||
private WifiEnabler mWifiEnabler;
|
||||
private AirplaneModeEnabler mAirplaneModeEnabler;
|
||||
|
||||
private CheckBoxPreference mToggleBluetooth;
|
||||
|
||||
private IntentFilter mIntentFilter;
|
||||
|
||||
private static final int EVENT_FAILED_BT_ENABLE = 1;
|
||||
private static final int EVENT_PASSED_BT_ENABLE = 2;
|
||||
private BluetoothEnabler mBtEnabler;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -58,30 +40,25 @@ public class WirelessSettings extends PreferenceActivity {
|
||||
|
||||
addPreferencesFromResource(R.xml.wireless_settings);
|
||||
|
||||
mIntentFilter = new IntentFilter();
|
||||
mIntentFilter.addAction(BluetoothIntent.ENABLED_ACTION);
|
||||
mIntentFilter.addAction(BluetoothIntent.DISABLED_ACTION);
|
||||
|
||||
initToggles();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
refreshToggles();
|
||||
registerReceiver(mReceiver, mIntentFilter);
|
||||
|
||||
mWifiEnabler.resume();
|
||||
mAirplaneModeEnabler.resume();
|
||||
mBtEnabler.resume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(mReceiver);
|
||||
|
||||
|
||||
mWifiEnabler.pause();
|
||||
mAirplaneModeEnabler.pause();
|
||||
mBtEnabler.pause();
|
||||
}
|
||||
|
||||
private void initToggles() {
|
||||
@@ -95,116 +72,9 @@ public class WirelessSettings extends PreferenceActivity {
|
||||
this,
|
||||
(CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE));
|
||||
|
||||
mToggleBluetooth = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
|
||||
mToggleBluetooth.setPersistent(false);
|
||||
mBtEnabler = new BluetoothEnabler(
|
||||
this,
|
||||
(CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH));
|
||||
}
|
||||
|
||||
private void refreshToggles() {
|
||||
mToggleBluetooth.setChecked(isBluetoothEnabled());
|
||||
mToggleBluetooth.setEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
|
||||
if (preference == mToggleBluetooth) {
|
||||
setBluetoothEnabled(mToggleBluetooth.isChecked());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isBluetoothEnabled() {
|
||||
BluetoothDevice device = (BluetoothDevice)getSystemService(BLUETOOTH_SERVICE);
|
||||
if (device != null) {
|
||||
return device.isEnabled();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void setBluetoothEnabled(boolean enabled) {
|
||||
try {
|
||||
BluetoothDevice device = (BluetoothDevice)getSystemService(BLUETOOTH_SERVICE);
|
||||
if (enabled) {
|
||||
// Turn it off until intent or callback is delivered
|
||||
mToggleBluetooth.setChecked(false);
|
||||
if (device.enable(mBtCallback)) {
|
||||
mToggleBluetooth.setSummary(R.string.bluetooth_enabling);
|
||||
mToggleBluetooth.setEnabled(false);
|
||||
}
|
||||
} else {
|
||||
if (device.disable()) {
|
||||
Settings.System.putInt(getContentResolver(),
|
||||
Settings.System.BLUETOOTH_ON, 0);
|
||||
} else {
|
||||
// Unusual situation, that you can't turn off bluetooth
|
||||
mToggleBluetooth.setChecked(true);
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
// TODO: 1071858
|
||||
mToggleBluetooth.setChecked(false);
|
||||
mToggleBluetooth.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
private IBluetoothDeviceCallback mBtCallback = new IBluetoothDeviceCallback.Stub() {
|
||||
|
||||
public void onEnableResult(int res) {
|
||||
switch (res) {
|
||||
case BluetoothDevice.RESULT_FAILURE:
|
||||
mHandler.sendMessage(mHandler.obtainMessage(EVENT_FAILED_BT_ENABLE, 0));
|
||||
break;
|
||||
case BluetoothDevice.RESULT_SUCCESS:
|
||||
mHandler.sendMessage(mHandler.obtainMessage(EVENT_PASSED_BT_ENABLE, 0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void onCreateBondingResult(String device, int res) {
|
||||
// Don't care
|
||||
}
|
||||
public void onGetRemoteServiceChannelResult(String address, int channel) { }
|
||||
};
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(BluetoothIntent.ENABLED_ACTION)) {
|
||||
updateBtStatus(true);
|
||||
} else if (action.equals(BluetoothIntent.DISABLED_ACTION)) {
|
||||
mToggleBluetooth.setChecked(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void updateBtStatus(boolean enabled) {
|
||||
mToggleBluetooth.setChecked(enabled);
|
||||
mToggleBluetooth.setEnabled(true);
|
||||
mToggleBluetooth.setSummary(R.string.bluetooth_quick_toggle_summary);
|
||||
if (enabled) {
|
||||
Settings.System.putInt(getContentResolver(),
|
||||
Settings.System.BLUETOOTH_ON, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case EVENT_PASSED_BT_ENABLE:
|
||||
updateBtStatus(true);
|
||||
break;
|
||||
case EVENT_FAILED_BT_ENABLE:
|
||||
updateBtStatus(false);
|
||||
Toast.makeText(WirelessSettings.this,
|
||||
getResources().getString(R.string.bluetooth_failed_to_enable),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user