Program: 180820
Project: 180820
Buggy source:
private synchronized void processMessage(Message msg){
  int msgType=msg.what;
  int state=msg.arg1;
  int startId=msg.arg2;
  boolean deferFinishCall=false;
  BluetoothDevice device=null;
  if (msg.obj != null) {
    device=(BluetoothDevice)msg.obj;
  }
  if (DEBUG)   Log.d(TAG,"String_Node_Str" + msgType + "String_Node_Str"+ state+ "String_Node_Str"+ (device == null ? "String_Node_Str" : device.toString()));
switch (msgType) {
case MSG_TYPE_SHOW_UI:
    if (mDialog != null) {
      mDialog.dismiss();
      mDialog=null;
    }
  mDevice=device;
createDialog(mContext,mDevice,state,startId);
break;
case MSG_TYPE_DOCKED:
if (DEBUG) {
Log.d(TAG,"String_Node_Str" + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT,mDevice));
Log.d(TAG,"String_Node_Str" + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT,device));
}
mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT);
mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT);
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
if (!device.equals(mDevice)) {
if (mDevice != null) {
handleUndocked(mContext,mBtManager,mDevice);
}
mDevice=device;
if (mBtManager.getDockAutoConnectSetting(device.getAddress())) {
initBtSettings(mContext,device,state,false);
applyBtSettings(mDevice,startId);
}
 else {
createDialog(mContext,mDevice,state,startId);
}
}
break;
case MSG_TYPE_UNDOCKED_PERMANENT:
handleUndocked(mContext,mBtManager,device);
if (DEBUG) {
Log.d(TAG,"String_Node_Str" + getSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
}
if (getSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED)) {
if (!hasOtherConnectedDevices(device)) {
if (DEBUG) Log.d(TAG,"String_Node_Str");
Message newMsg=mServiceHandler.obtainMessage(MSG_TYPE_DISABLE_BT,0,startId,null);
mServiceHandler.sendMessageDelayed(newMsg,DISABLE_BT_GRACE_PERIOD);
deferFinishCall=true;
}
 else {
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
}
}
break;
case MSG_TYPE_UNDOCKED_TEMPORARY:
Message newMsg=mServiceHandler.obtainMessage(MSG_TYPE_UNDOCKED_PERMANENT,state,startId,device);
mServiceHandler.sendMessageDelayed(newMsg,UNDOCKED_GRACE_PERIOD);
break;
case MSG_TYPE_DISABLE_BT:
if (DEBUG) Log.d(TAG,"String_Node_Str");
if (mBtManager.getBluetoothAdapter().disable()) {
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
}
 else {
setSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT,true);
mPendingTurnOffStartId=startId;
deferFinishCall=true;
if (DEBUG) Log.d(TAG,"String_Node_Str" + startId);
}
break;
}
if (mDialog == null && mPendingDevice == null && msgType != MSG_TYPE_UNDOCKED_TEMPORARY && !deferFinishCall) {
DockEventReceiver.finishStartingService(DockService.this,startId);
}
}

Fixed version (ground truth):
private synchronized void processMessage(Message msg){
  int msgType=msg.what;
  final int state=msg.arg1;
  final int startId=msg.arg2;
  boolean deferFinishCall=false;
  BluetoothDevice device=null;
  if (msg.obj != null) {
    device=(BluetoothDevice)msg.obj;
  }
  if (DEBUG)   Log.d(TAG,"String_Node_Str" + msgType + "String_Node_Str"+ state+ "String_Node_Str"+ (device == null ? "String_Node_Str" : device.toString()));
switch (msgType) {
case MSG_TYPE_SHOW_UI:
    if (mDialog != null) {
      mDialog.dismiss();
      mDialog=null;
    }
  mDevice=device;
createDialog(mContext,mDevice,state,startId);
break;
case MSG_TYPE_DOCKED:
if (DEBUG) {
Log.d(TAG,"String_Node_Str" + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT,mDevice));
Log.d(TAG,"String_Node_Str" + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT,device));
}
mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT);
mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT);
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
if (!device.equals(mDevice)) {
if (mDevice != null) {
handleUndocked(mContext,mBtManager,mDevice);
}
mDevice=device;
LocalBluetoothProfileManager.addServiceListener(this);
if (LocalBluetoothProfileManager.isManagerReady()) {
handleDocked(device,state,startId);
LocalBluetoothProfileManager.removeServiceListener(this);
}
 else {
final BluetoothDevice d=device;
mRunnable=new Runnable(){
public void run(){
handleDocked(d,state,startId);
}
}
;
deferFinishCall=true;
}
}
break;
case MSG_TYPE_UNDOCKED_PERMANENT:
handleUndocked(mContext,mBtManager,device);
if (DEBUG) {
Log.d(TAG,"String_Node_Str" + getSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
}
if (getSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED)) {
if (!hasOtherConnectedDevices(device)) {
if (DEBUG) Log.d(TAG,"String_Node_Str");
Message newMsg=mServiceHandler.obtainMessage(MSG_TYPE_DISABLE_BT,0,startId,null);
mServiceHandler.sendMessageDelayed(newMsg,DISABLE_BT_GRACE_PERIOD);
deferFinishCall=true;
}
 else {
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
}
}
break;
case MSG_TYPE_UNDOCKED_TEMPORARY:
Message newMsg=mServiceHandler.obtainMessage(MSG_TYPE_UNDOCKED_PERMANENT,state,startId,device);
mServiceHandler.sendMessageDelayed(newMsg,UNDOCKED_GRACE_PERIOD);
break;
case MSG_TYPE_DISABLE_BT:
if (DEBUG) Log.d(TAG,"String_Node_Str");
if (mBtManager.getBluetoothAdapter().disable()) {
removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
}
 else {
setSettingBool(SHARED_PREFERENCES_KEY_DISABLE_BT,true);
mPendingTurnOffStartId=startId;
deferFinishCall=true;
if (DEBUG) Log.d(TAG,"String_Node_Str" + startId);
}
break;
}
if (mDialog == null && mPendingDevice == null && msgType != MSG_TYPE_UNDOCKED_TEMPORARY && !deferFinishCall) {
DockEventReceiver.finishStartingService(DockService.this,startId);
}
}