am fcd4e7fe: am 04bdae75: Merge "b/2332395 Fixed a bug where BT is not disabled if the phone was inserted and removed quickly." into eclair

Merge commit 'fcd4e7fec3c80d0a16630246c4942595d34c655c'

* commit 'fcd4e7fec3c80d0a16630246c4942595d34c655c':
  b/2332395 Fixed a bug where BT is not disabled if the phone was inserted and removed quickly.
This commit is contained in:
Michael Chan
2010-01-22 18:49:07 -08:00
committed by Android Git Automerger

View File

@@ -84,6 +84,9 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
private static final String SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED = private static final String SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED =
"disable_bt_when_undock"; "disable_bt_when_undock";
private static final String SHARED_PREFERENCES_KEY_DISABLE_BT =
"disable_bt";
private static final int INVALID_STARTID = -100; private static final int INVALID_STARTID = -100;
// Created in OnCreate() // Created in OnCreate()
@@ -107,6 +110,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
private BluetoothDevice mPendingDevice; private BluetoothDevice mPendingDevice;
private int mPendingStartId; private int mPendingStartId;
private int mPendingTurnOnStartId = INVALID_STARTID; private int mPendingTurnOnStartId = INVALID_STARTID;
private int mPendingTurnOffStartId = INVALID_STARTID;
@Override @Override
public void onCreate() { public void onCreate() {
@@ -219,6 +223,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT); mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT);
mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT); mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT);
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
if (!device.equals(mDevice)) { if (!device.equals(mDevice)) {
if (mDevice != null) { if (mDevice != null) {
@@ -240,8 +245,13 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
case MSG_TYPE_UNDOCKED_PERMANENT: case MSG_TYPE_UNDOCKED_PERMANENT:
// Grace period passed. Disconnect. // Grace period passed. Disconnect.
handleUndocked(mContext, mBtManager, device); handleUndocked(mContext, mBtManager, device);
if(DEBUG) Log.d(TAG, "getDisableBtSetting = " + getDisableBtSetting());
if (getDisableBtSetting()) { if (DEBUG) {
Log.d(TAG, "DISABLE_BT_WHEN_UNDOCKED = "
+ getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
}
if (getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED)) {
// BT was disabled when we first docked // BT was disabled when we first docked
if (!hasOtherConnectedDevices(device)) { if (!hasOtherConnectedDevices(device)) {
if(DEBUG) Log.d(TAG, "QUEUED BT DISABLE"); if(DEBUG) Log.d(TAG, "QUEUED BT DISABLE");
@@ -252,7 +262,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
deferFinishCall = true; deferFinishCall = true;
} else { } else {
// Don't disable BT if something is connected // Don't disable BT if something is connected
removeDisableBtSetting(); removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
} }
} }
break; break;
@@ -266,8 +276,15 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
case MSG_TYPE_DISABLE_BT: case MSG_TYPE_DISABLE_BT:
if(DEBUG) Log.d(TAG, "BT DISABLE"); if(DEBUG) Log.d(TAG, "BT DISABLE");
mBtManager.getBluetoothAdapter().disable(); if (mBtManager.getBluetoothAdapter().disable()) {
removeDisableBtSetting(); removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
} else {
// disable() returned an error. Persist a flag to disable BT later
setSetting(SHARED_PREFERENCES_KEY_DISABLE_BT, true);
mPendingTurnOffStartId = startId;
deferFinishCall = true;
if(DEBUG) Log.d(TAG, "disable failed. try again later " + startId);
}
break; break;
} }
@@ -490,6 +507,10 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
mPendingDevice = null; mPendingDevice = null;
DockEventReceiver.finishStartingService(mContext, mPendingStartId); DockEventReceiver.finishStartingService(mContext, mPendingStartId);
} else { } else {
if (DEBUG) {
Log.d(TAG, "A DISABLE_BT_WHEN_UNDOCKED = "
+ getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
}
// Reconnect if docked and bluetooth was enabled by user. // Reconnect if docked and bluetooth was enabled by user.
Intent i = registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT)); Intent i = registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT));
if (i != null) { if (i != null) {
@@ -501,6 +522,11 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
if (device != null) { if (device != null) {
connectIfEnabled(device); connectIfEnabled(device);
} }
} else if (getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT)
&& mBtManager.getBluetoothAdapter().disable()) {
mPendingTurnOffStartId = startId;
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
return;
} }
} }
} }
@@ -516,12 +542,19 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
// The rational is that: // The rational is that:
// a) if BT is off at undock time, no work needs to be done // a) if BT is off at undock time, no work needs to be done
// b) if BT is on at undock time, the user wants it on. // b) if BT is on at undock time, the user wants it on.
removeDisableBtSetting(); removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
DockEventReceiver.finishStartingService(this, startId); DockEventReceiver.finishStartingService(this, startId);
} else if (btState == BluetoothAdapter.STATE_OFF) { } else if (btState == BluetoothAdapter.STATE_OFF) {
// Bluetooth was turning off as we were trying to turn it on. // Bluetooth was turning off as we were trying to turn it on.
// Let's try again // Let's try again
if(DEBUG) Log.d(TAG, "Bluetooth = OFF mPendingDevice = " + mPendingDevice); if(DEBUG) Log.d(TAG, "Bluetooth = OFF mPendingDevice = " + mPendingDevice);
if (mPendingTurnOffStartId != INVALID_STARTID) {
DockEventReceiver.finishStartingService(this, mPendingTurnOffStartId);
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
mPendingTurnOffStartId = INVALID_STARTID;
}
if (mPendingDevice != null) { if (mPendingDevice != null) {
mBtManager.getBluetoothAdapter().enable(); mBtManager.getBluetoothAdapter().enable();
mPendingTurnOnStartId = startId; mPendingTurnOnStartId = startId;
@@ -579,7 +612,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
mPendingDevice = device; mPendingDevice = device;
mPendingStartId = startId; mPendingStartId = startId;
if (btState != BluetoothAdapter.STATE_TURNING_ON) { if (btState != BluetoothAdapter.STATE_TURNING_ON) {
setDisableBtSetting(true); setSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED, true);
} }
return; return;
} }
@@ -643,24 +676,24 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
return cachedBluetoothDevice; return cachedBluetoothDevice;
} }
private boolean getDisableBtSetting() { private boolean getSetting(String key) {
SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME, SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME,
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
return sharedPref.getBoolean(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED, false); return sharedPref.getBoolean(key, false);
} }
private void setDisableBtSetting(boolean disableBt) { private void setSetting(String key, boolean disableBt) {
SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFERENCES_NAME, SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFERENCES_NAME,
Context.MODE_PRIVATE).edit(); Context.MODE_PRIVATE).edit();
editor.putBoolean(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED, disableBt); editor.putBoolean(key, disableBt);
editor.commit(); editor.commit();
} }
private void removeDisableBtSetting() { private void removeSetting(String key) {
SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME, SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME,
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit(); SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED); editor.remove(key);
editor.commit(); editor.commit();
return; return;
} }