Program: 180943
Project: 180943
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);
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" + getDisableBtSetting());
if (getDisableBtSetting()) {
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 {
removeDisableBtSetting();
}
}
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");
mBtManager.getBluetoothAdapter().disable();
removeDisableBtSetting();
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;
  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" + getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
}
if (getSetting(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 {
setSetting(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);
}
}