Program: 196578
Project: 196578
Buggy source:
@Override public void handleMessage(Message msg){
  if (DBG)   Log.d(TAG,"String_Node_Str" + msg.what);
switch (msg.what) {
case MESSAGE_GET_NAME_AND_ADDRESS:
{
      if (DBG)       Log.d(TAG,"String_Node_Str");
synchronized (mConnection) {
        if ((mBluetooth == null) && (!mBinding)) {
          if (DBG)           Log.d(TAG,"String_Node_Str");
          mConnection.setGetNameAddressOnly(true);
          Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
          mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
          Intent i=new Intent(IBluetooth.class.getName());
          if (!doBind(i,mConnection,Context.BIND_AUTO_CREATE,UserHandle.CURRENT)) {
            mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
          }
 else {
            mBinding=true;
          }
        }
 else {
          Message saveMsg=mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
          saveMsg.arg1=0;
          if (mBluetooth != null) {
            mHandler.sendMessage(saveMsg);
          }
 else {
            mHandler.sendMessageDelayed(saveMsg,TIMEOUT_SAVE_MS);
          }
        }
      }
      break;
    }
case MESSAGE_SAVE_NAME_AND_ADDRESS:
{
    boolean unbind=false;
    if (DBG)     Log.d(TAG,"String_Node_Str");
synchronized (mConnection) {
      if (!mEnable && mBluetooth != null) {
        try {
          mBluetooth.enable();
        }
 catch (        RemoteException e) {
          Log.e(TAG,"String_Node_Str",e);
        }
      }
    }
    if (mBluetooth != null)     waitForOnOff(true,false);
synchronized (mConnection) {
      if (mBluetooth != null) {
        String name=null;
        String address=null;
        try {
          name=mBluetooth.getName();
          address=mBluetooth.getAddress();
        }
 catch (        RemoteException re) {
          Log.e(TAG,"String_Node_Str",re);
        }
        if (name != null && address != null) {
          storeNameAndAddress(name,address);
          if (mConnection.isGetNameAddressOnly()) {
            unbind=true;
          }
        }
 else {
          if (msg.arg1 < MAX_SAVE_RETRIES) {
            Message retryMsg=mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
            retryMsg.arg1=1 + msg.arg1;
            if (DBG)             Log.d(TAG,"String_Node_Str" + retryMsg.arg1);
            mHandler.sendMessageDelayed(retryMsg,TIMEOUT_SAVE_MS);
          }
 else {
            Log.w(TAG,"String_Node_Str");
            if (mConnection.isGetNameAddressOnly()) {
              unbind=true;
            }
          }
        }
        if (!mEnable) {
          try {
            mBluetooth.disable();
          }
 catch (          RemoteException e) {
            Log.e(TAG,"String_Node_Str",e);
          }
        }
      }
 else {
        Message getMsg=mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
        mHandler.sendMessage(getMsg);
      }
    }
    if (!mEnable && mBluetooth != null)     waitForOnOff(false,true);
    if (unbind) {
      unbindAndFinish();
    }
    break;
  }
case MESSAGE_ENABLE:
if (DBG) {
  Log.d(TAG,"String_Node_Str" + mBluetooth);
}
mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
mEnable=true;
handleEnable(msg.arg1 == 1);
break;
case MESSAGE_DISABLE:
mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
if (mEnable && mBluetooth != null) {
waitForOnOff(true,false);
mEnable=false;
handleDisable();
waitForOnOff(false,false);
}
 else {
mEnable=false;
handleDisable();
}
break;
case MESSAGE_REGISTER_ADAPTER:
{
IBluetoothManagerCallback callback=(IBluetoothManagerCallback)msg.obj;
boolean added=mCallbacks.register(callback);
Log.d(TAG,"String_Node_Str" + (callback == null ? "String_Node_Str" : callback) + "String_Node_Str"+ added);
}
break;
case MESSAGE_UNREGISTER_ADAPTER:
{
IBluetoothManagerCallback callback=(IBluetoothManagerCallback)msg.obj;
boolean removed=mCallbacks.unregister(callback);
Log.d(TAG,"String_Node_Str" + (callback == null ? "String_Node_Str" : callback) + "String_Node_Str"+ removed);
break;
}
case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
{
IBluetoothStateChangeCallback callback=(IBluetoothStateChangeCallback)msg.obj;
if (callback != null) {
mStateChangeCallbacks.register(callback);
}
break;
}
case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
{
IBluetoothStateChangeCallback callback=(IBluetoothStateChangeCallback)msg.obj;
if (callback != null) {
mStateChangeCallbacks.unregister(callback);
}
break;
}
case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
{
if (DBG) Log.d(TAG,"String_Node_Str" + msg.arg1);
IBinder service=(IBinder)msg.obj;
synchronized (mConnection) {
if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
mBluetoothGatt=IBluetoothGatt.Stub.asInterface(service);
break;
}
mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
mBinding=false;
mBluetooth=IBluetooth.Stub.asInterface(service);
try {
boolean enableHciSnoopLog=(Settings.Secure.getInt(mContentResolver,Settings.Secure.BLUETOOTH_HCI_LOG,0) == 1);
if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
Log.e(TAG,"String_Node_Str");
}
}
 catch (RemoteException e) {
Log.e(TAG,"String_Node_Str",e);
}
if (mConnection.isGetNameAddressOnly()) {
Message getMsg=mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
mHandler.sendMessage(getMsg);
if (!mEnable) return;
}
mConnection.setGetNameAddressOnly(false);
try {
mBluetooth.registerCallback(mBluetoothCallback);
}
 catch (RemoteException re) {
Log.e(TAG,"String_Node_Str",re);
}
sendBluetoothServiceUpCallback();
try {
if (mQuietEnable == false) {
if (!mBluetooth.enable()) {
Log.e(TAG,"String_Node_Str");
}
}
 else {
if (!mBluetooth.enableNoAutoConnect()) {
Log.e(TAG,"String_Node_Str");
}
}
}
 catch (RemoteException e) {
Log.e(TAG,"String_Node_Str",e);
}
}
if (!mEnable) {
waitForOnOff(true,false);
handleDisable();
waitForOnOff(false,false);
}
break;
}
case MESSAGE_TIMEOUT_BIND:
{
Log.e(TAG,"String_Node_Str");
synchronized (mConnection) {
mBinding=false;
}
break;
}
case MESSAGE_BLUETOOTH_STATE_CHANGE:
{
int prevState=msg.arg1;
int newState=msg.arg2;
if (DBG) Log.d(TAG,"String_Node_Str" + prevState + "String_Node_Str"+ newState);
mState=newState;
bluetoothStateChangeHandler(prevState,newState);
if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && (newState == BluetoothAdapter.STATE_OFF) && (mBluetooth != null)&& mEnable) {
recoverBluetoothServiceFromError();
}
if (newState == BluetoothAdapter.STATE_ON) {
if (mErrorRecoveryRetryCounter != 0) {
Log.w(TAG,"String_Node_Str");
mErrorRecoveryRetryCounter=0;
}
}
break;
}
case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
{
Log.e(TAG,"String_Node_Str" + msg.arg1);
synchronized (mConnection) {
if (msg.arg1 == SERVICE_IBLUETOOTH) {
if (mBluetooth == null) break;
mBluetooth=null;
}
 else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
mBluetoothGatt=null;
break;
}
 else {
Log.e(TAG,"String_Node_Str" + msg.arg1);
break;
}
}
if (mEnable) {
mEnable=false;
Message restartMsg=mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
mHandler.sendMessageDelayed(restartMsg,SERVICE_RESTART_TIME_MS);
}
if (!mConnection.isGetNameAddressOnly()) {
sendBluetoothServiceDownCallback();
if ((mState == BluetoothAdapter.STATE_TURNING_ON) || (mState == BluetoothAdapter.STATE_ON)) {
bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,BluetoothAdapter.STATE_TURNING_OFF);
mState=BluetoothAdapter.STATE_TURNING_OFF;
}
if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,BluetoothAdapter.STATE_OFF);
}
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState=BluetoothAdapter.STATE_OFF;
}
break;
}
case MESSAGE_RESTART_BLUETOOTH_SERVICE:
{
Log.d(TAG,"String_Node_Str" + "String_Node_Str");
mEnable=true;
handleEnable(mQuietEnable);
break;
}
case MESSAGE_TIMEOUT_UNBIND:
{
Log.e(TAG,"String_Node_Str");
synchronized (mConnection) {
mUnbinding=false;
}
break;
}
case MESSAGE_USER_SWITCHED:
{
if (DBG) {
Log.d(TAG,"String_Node_Str");
}
mHandler.removeMessages(MESSAGE_USER_SWITCHED);
if (mEnable && mBluetooth != null) {
synchronized (mConnection) {
if (mBluetooth != null) {
try {
mBluetooth.unregisterCallback(mBluetoothCallback);
}
 catch (RemoteException re) {
Log.e(TAG,"String_Node_Str",re);
}
}
}
if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_OFF);
mState=BluetoothAdapter.STATE_OFF;
}
if (mState == BluetoothAdapter.STATE_OFF) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_TURNING_ON);
mState=BluetoothAdapter.STATE_TURNING_ON;
}
waitForOnOff(true,false);
if (mState == BluetoothAdapter.STATE_TURNING_ON) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_ON);
}
handleDisable();
bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,BluetoothAdapter.STATE_TURNING_OFF);
waitForOnOff(false,true);
bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,BluetoothAdapter.STATE_OFF);
sendBluetoothServiceDownCallback();
synchronized (mConnection) {
if (mBluetooth != null) {
mBluetooth=null;
mContext.unbindService(mConnection);
}
}
SystemClock.sleep(100);
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState=BluetoothAdapter.STATE_OFF;
handleEnable(mQuietEnable);
}
 else if (mBinding || mBluetooth != null) {
Message userMsg=mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
userMsg.arg2=1 + msg.arg2;
mHandler.sendMessageDelayed(userMsg,USER_SWITCHED_TIME_MS);
if (DBG) {
Log.d(TAG,"String_Node_Str" + userMsg.arg2);
}
}
break;
}
}
}

Fixed version (ground truth):
@Override public void handleMessage(Message msg){
  if (DBG)   Log.d(TAG,"String_Node_Str" + msg.what);
switch (msg.what) {
case MESSAGE_GET_NAME_AND_ADDRESS:
{
      if (DBG)       Log.d(TAG,"String_Node_Str");
synchronized (mConnection) {
        if ((mBluetooth == null) && (!mBinding)) {
          if (DBG)           Log.d(TAG,"String_Node_Str");
          mConnection.setGetNameAddressOnly(true);
          Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
          mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
          Intent i=new Intent(IBluetooth.class.getName());
          if (!doBind(i,mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,UserHandle.CURRENT)) {
            mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
          }
 else {
            mBinding=true;
          }
        }
 else {
          Message saveMsg=mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
          saveMsg.arg1=0;
          if (mBluetooth != null) {
            mHandler.sendMessage(saveMsg);
          }
 else {
            mHandler.sendMessageDelayed(saveMsg,TIMEOUT_SAVE_MS);
          }
        }
      }
      break;
    }
case MESSAGE_SAVE_NAME_AND_ADDRESS:
{
    boolean unbind=false;
    if (DBG)     Log.d(TAG,"String_Node_Str");
synchronized (mConnection) {
      if (!mEnable && mBluetooth != null) {
        try {
          mBluetooth.enable();
        }
 catch (        RemoteException e) {
          Log.e(TAG,"String_Node_Str",e);
        }
      }
    }
    if (mBluetooth != null)     waitForOnOff(true,false);
synchronized (mConnection) {
      if (mBluetooth != null) {
        String name=null;
        String address=null;
        try {
          name=mBluetooth.getName();
          address=mBluetooth.getAddress();
        }
 catch (        RemoteException re) {
          Log.e(TAG,"String_Node_Str",re);
        }
        if (name != null && address != null) {
          storeNameAndAddress(name,address);
          if (mConnection.isGetNameAddressOnly()) {
            unbind=true;
          }
        }
 else {
          if (msg.arg1 < MAX_SAVE_RETRIES) {
            Message retryMsg=mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS);
            retryMsg.arg1=1 + msg.arg1;
            if (DBG)             Log.d(TAG,"String_Node_Str" + retryMsg.arg1);
            mHandler.sendMessageDelayed(retryMsg,TIMEOUT_SAVE_MS);
          }
 else {
            Log.w(TAG,"String_Node_Str");
            if (mConnection.isGetNameAddressOnly()) {
              unbind=true;
            }
          }
        }
        if (!mEnable) {
          try {
            mBluetooth.disable();
          }
 catch (          RemoteException e) {
            Log.e(TAG,"String_Node_Str",e);
          }
        }
      }
 else {
        Message getMsg=mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
        mHandler.sendMessage(getMsg);
      }
    }
    if (!mEnable && mBluetooth != null)     waitForOnOff(false,true);
    if (unbind) {
      unbindAndFinish();
    }
    break;
  }
case MESSAGE_ENABLE:
if (DBG) {
  Log.d(TAG,"String_Node_Str" + mBluetooth);
}
mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
mEnable=true;
handleEnable(msg.arg1 == 1);
break;
case MESSAGE_DISABLE:
mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
if (mEnable && mBluetooth != null) {
waitForOnOff(true,false);
mEnable=false;
handleDisable();
waitForOnOff(false,false);
}
 else {
mEnable=false;
handleDisable();
}
break;
case MESSAGE_REGISTER_ADAPTER:
{
IBluetoothManagerCallback callback=(IBluetoothManagerCallback)msg.obj;
boolean added=mCallbacks.register(callback);
Log.d(TAG,"String_Node_Str" + (callback == null ? "String_Node_Str" : callback) + "String_Node_Str"+ added);
}
break;
case MESSAGE_UNREGISTER_ADAPTER:
{
IBluetoothManagerCallback callback=(IBluetoothManagerCallback)msg.obj;
boolean removed=mCallbacks.unregister(callback);
Log.d(TAG,"String_Node_Str" + (callback == null ? "String_Node_Str" : callback) + "String_Node_Str"+ removed);
break;
}
case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
{
IBluetoothStateChangeCallback callback=(IBluetoothStateChangeCallback)msg.obj;
if (callback != null) {
mStateChangeCallbacks.register(callback);
}
break;
}
case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
{
IBluetoothStateChangeCallback callback=(IBluetoothStateChangeCallback)msg.obj;
if (callback != null) {
mStateChangeCallbacks.unregister(callback);
}
break;
}
case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
{
if (DBG) Log.d(TAG,"String_Node_Str" + msg.arg1);
IBinder service=(IBinder)msg.obj;
synchronized (mConnection) {
if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
mBluetoothGatt=IBluetoothGatt.Stub.asInterface(service);
break;
}
mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
mBinding=false;
mBluetooth=IBluetooth.Stub.asInterface(service);
try {
boolean enableHciSnoopLog=(Settings.Secure.getInt(mContentResolver,Settings.Secure.BLUETOOTH_HCI_LOG,0) == 1);
if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
Log.e(TAG,"String_Node_Str");
}
}
 catch (RemoteException e) {
Log.e(TAG,"String_Node_Str",e);
}
if (mConnection.isGetNameAddressOnly()) {
Message getMsg=mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);
mHandler.sendMessage(getMsg);
if (!mEnable) return;
}
mConnection.setGetNameAddressOnly(false);
try {
mBluetooth.registerCallback(mBluetoothCallback);
}
 catch (RemoteException re) {
Log.e(TAG,"String_Node_Str",re);
}
sendBluetoothServiceUpCallback();
try {
if (mQuietEnable == false) {
if (!mBluetooth.enable()) {
Log.e(TAG,"String_Node_Str");
}
}
 else {
if (!mBluetooth.enableNoAutoConnect()) {
Log.e(TAG,"String_Node_Str");
}
}
}
 catch (RemoteException e) {
Log.e(TAG,"String_Node_Str",e);
}
}
if (!mEnable) {
waitForOnOff(true,false);
handleDisable();
waitForOnOff(false,false);
}
break;
}
case MESSAGE_TIMEOUT_BIND:
{
Log.e(TAG,"String_Node_Str");
synchronized (mConnection) {
mBinding=false;
}
break;
}
case MESSAGE_BLUETOOTH_STATE_CHANGE:
{
int prevState=msg.arg1;
int newState=msg.arg2;
if (DBG) Log.d(TAG,"String_Node_Str" + prevState + "String_Node_Str"+ newState);
mState=newState;
bluetoothStateChangeHandler(prevState,newState);
if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && (newState == BluetoothAdapter.STATE_OFF) && (mBluetooth != null)&& mEnable) {
recoverBluetoothServiceFromError();
}
if (newState == BluetoothAdapter.STATE_ON) {
if (mErrorRecoveryRetryCounter != 0) {
Log.w(TAG,"String_Node_Str");
mErrorRecoveryRetryCounter=0;
}
}
break;
}
case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
{
Log.e(TAG,"String_Node_Str" + msg.arg1);
synchronized (mConnection) {
if (msg.arg1 == SERVICE_IBLUETOOTH) {
if (mBluetooth == null) break;
mBluetooth=null;
}
 else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
mBluetoothGatt=null;
break;
}
 else {
Log.e(TAG,"String_Node_Str" + msg.arg1);
break;
}
}
if (mEnable) {
mEnable=false;
Message restartMsg=mHandler.obtainMessage(MESSAGE_RESTART_BLUETOOTH_SERVICE);
mHandler.sendMessageDelayed(restartMsg,SERVICE_RESTART_TIME_MS);
}
if (!mConnection.isGetNameAddressOnly()) {
sendBluetoothServiceDownCallback();
if ((mState == BluetoothAdapter.STATE_TURNING_ON) || (mState == BluetoothAdapter.STATE_ON)) {
bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,BluetoothAdapter.STATE_TURNING_OFF);
mState=BluetoothAdapter.STATE_TURNING_OFF;
}
if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,BluetoothAdapter.STATE_OFF);
}
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState=BluetoothAdapter.STATE_OFF;
}
break;
}
case MESSAGE_RESTART_BLUETOOTH_SERVICE:
{
Log.d(TAG,"String_Node_Str" + "String_Node_Str");
mEnable=true;
handleEnable(mQuietEnable);
break;
}
case MESSAGE_TIMEOUT_UNBIND:
{
Log.e(TAG,"String_Node_Str");
synchronized (mConnection) {
mUnbinding=false;
}
break;
}
case MESSAGE_USER_SWITCHED:
{
if (DBG) {
Log.d(TAG,"String_Node_Str");
}
mHandler.removeMessages(MESSAGE_USER_SWITCHED);
if (mEnable && mBluetooth != null) {
synchronized (mConnection) {
if (mBluetooth != null) {
try {
mBluetooth.unregisterCallback(mBluetoothCallback);
}
 catch (RemoteException re) {
Log.e(TAG,"String_Node_Str",re);
}
}
}
if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_OFF);
mState=BluetoothAdapter.STATE_OFF;
}
if (mState == BluetoothAdapter.STATE_OFF) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_TURNING_ON);
mState=BluetoothAdapter.STATE_TURNING_ON;
}
waitForOnOff(true,false);
if (mState == BluetoothAdapter.STATE_TURNING_ON) {
bluetoothStateChangeHandler(mState,BluetoothAdapter.STATE_ON);
}
handleDisable();
bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,BluetoothAdapter.STATE_TURNING_OFF);
waitForOnOff(false,true);
bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,BluetoothAdapter.STATE_OFF);
sendBluetoothServiceDownCallback();
synchronized (mConnection) {
if (mBluetooth != null) {
mBluetooth=null;
mContext.unbindService(mConnection);
}
}
SystemClock.sleep(100);
mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
mState=BluetoothAdapter.STATE_OFF;
handleEnable(mQuietEnable);
}
 else if (mBinding || mBluetooth != null) {
Message userMsg=mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
userMsg.arg2=1 + msg.arg2;
mHandler.sendMessageDelayed(userMsg,USER_SWITCHED_TIME_MS);
if (DBG) {
Log.d(TAG,"String_Node_Str" + userMsg.arg2);
}
}
break;
}
}
}