Program: 24125
Project: 24125
Buggy source:
/** 
 * @see AudioManager#setRouting(int,int,int) 
 */
public void setRouting(int mode,int routes,int mask){
  int incallMask=0;
  int ringtoneMask=0;
  int normalMask=0;
  if (!checkAudioSettingsPermission("String_Node_Str")) {
    return;
  }
synchronized (mSettingsLock) {
    if (mode == AudioSystem.MODE_INVALID) {
switch (mask) {
case AudioSystem.ROUTE_SPEAKER:
        if (routes != 0 && !mSpeakerIsOn) {
          mSpeakerIsOn=true;
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
          incallMask=AudioSystem.ROUTE_ALL;
        }
 else         if (mSpeakerIsOn) {
          mSpeakerIsOn=false;
          if (mBluetoothScoIsConnected) {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_BLUETOOTH_SCO;
          }
 else           if (mHeadsetIsConnected) {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
          }
 else {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
          }
          incallMask=AudioSystem.ROUTE_ALL;
        }
      break;
case AudioSystem.ROUTE_BLUETOOTH_SCO:
    if (routes != 0 && !mBluetoothScoIsConnected) {
      mBluetoothScoIsConnected=true;
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_BLUETOOTH_SCO;
      mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_BLUETOOTH_SCO;
      mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_BLUETOOTH_SCO;
      incallMask=AudioSystem.ROUTE_ALL;
      ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
      normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    }
 else     if (mBluetoothScoIsConnected) {
      mBluetoothScoIsConnected=false;
      if (mHeadsetIsConnected) {
        mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
        mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | (AudioSystem.ROUTE_HEADSET | AudioSystem.ROUTE_SPEAKER);
        mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_HEADSET;
      }
 else {
        if (mSpeakerIsOn) {
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
        }
 else {
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
        }
        mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
        mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
      }
      incallMask=AudioSystem.ROUTE_ALL;
      ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
      normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    }
  break;
case AudioSystem.ROUTE_HEADSET:
if (routes != 0 && !mHeadsetIsConnected) {
  mHeadsetIsConnected=true;
  if (!mBluetoothScoIsConnected) {
    mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
    mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | (AudioSystem.ROUTE_HEADSET | AudioSystem.ROUTE_SPEAKER);
    mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_HEADSET;
    incallMask=AudioSystem.ROUTE_ALL;
    ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
  }
}
 else if (mHeadsetIsConnected) {
  mHeadsetIsConnected=false;
  if (!mBluetoothScoIsConnected) {
    if (mSpeakerIsOn) {
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
    }
 else {
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
    }
    mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
    mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
    incallMask=AudioSystem.ROUTE_ALL;
    ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
  }
}
break;
case AudioSystem.ROUTE_BLUETOOTH_A2DP:
if (routes != 0 && !mBluetoothA2dpIsConnected) {
mBluetoothA2dpIsConnected=true;
mRoutes[AudioSystem.MODE_RINGTONE]|=AudioSystem.ROUTE_BLUETOOTH_A2DP;
mRoutes[AudioSystem.MODE_NORMAL]|=AudioSystem.ROUTE_BLUETOOTH_A2DP;
ringtoneMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
normalMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
}
 else if (mBluetoothA2dpIsConnected) {
mBluetoothA2dpIsConnected=false;
mRoutes[AudioSystem.MODE_RINGTONE]&=~AudioSystem.ROUTE_BLUETOOTH_A2DP;
mRoutes[AudioSystem.MODE_NORMAL]&=~AudioSystem.ROUTE_BLUETOOTH_A2DP;
ringtoneMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
normalMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
}
break;
}
if (incallMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_IN_CALL,mRoutes[AudioSystem.MODE_IN_CALL],incallMask);
}
if (ringtoneMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_RINGTONE,mRoutes[AudioSystem.MODE_RINGTONE],ringtoneMask);
}
if (normalMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_NORMAL,mRoutes[AudioSystem.MODE_NORMAL],normalMask);
}
int streamType=getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
int index=mStreamStates[streamType].mIndex;
syncRingerAndNotificationStreamVolume(streamType,index,true);
setStreamVolumeInt(streamType,index,true,true);
}
}
}

Fixed version (ground truth):
/** 
 * @see AudioManager#setRouting(int,int,int) 
 */
public void setRouting(int mode,int routes,int mask){
  int incallMask=0;
  int ringtoneMask=0;
  int normalMask=0;
  if (!checkAudioSettingsPermission("String_Node_Str")) {
    return;
  }
synchronized (mSettingsLock) {
    if (mode == AudioSystem.MODE_INVALID) {
switch (mask) {
case AudioSystem.ROUTE_SPEAKER:
        if (routes != 0 && !mSpeakerIsOn) {
          mSpeakerIsOn=true;
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
          incallMask=AudioSystem.ROUTE_ALL;
        }
 else         if (routes == 0 && mSpeakerIsOn) {
          mSpeakerIsOn=false;
          if (mBluetoothScoIsConnected) {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_BLUETOOTH_SCO;
          }
 else           if (mHeadsetIsConnected) {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
          }
 else {
            mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
          }
          incallMask=AudioSystem.ROUTE_ALL;
        }
      break;
case AudioSystem.ROUTE_BLUETOOTH_SCO:
    if (routes != 0 && !mBluetoothScoIsConnected) {
      mBluetoothScoIsConnected=true;
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_BLUETOOTH_SCO;
      mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_BLUETOOTH_SCO;
      mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_BLUETOOTH_SCO;
      incallMask=AudioSystem.ROUTE_ALL;
      ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
      normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    }
 else     if (routes == 0 && mBluetoothScoIsConnected) {
      mBluetoothScoIsConnected=false;
      if (mHeadsetIsConnected) {
        mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
        mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | (AudioSystem.ROUTE_HEADSET | AudioSystem.ROUTE_SPEAKER);
        mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_HEADSET;
      }
 else {
        if (mSpeakerIsOn) {
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
        }
 else {
          mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
        }
        mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
        mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
      }
      incallMask=AudioSystem.ROUTE_ALL;
      ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
      normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    }
  break;
case AudioSystem.ROUTE_HEADSET:
if (routes != 0 && !mHeadsetIsConnected) {
  mHeadsetIsConnected=true;
  if (!mBluetoothScoIsConnected) {
    mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_HEADSET;
    mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | (AudioSystem.ROUTE_HEADSET | AudioSystem.ROUTE_SPEAKER);
    mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_HEADSET;
    incallMask=AudioSystem.ROUTE_ALL;
    ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
  }
}
 else if (routes == 0 && mHeadsetIsConnected) {
  mHeadsetIsConnected=false;
  if (!mBluetoothScoIsConnected) {
    if (mSpeakerIsOn) {
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_SPEAKER;
    }
 else {
      mRoutes[AudioSystem.MODE_IN_CALL]=AudioSystem.ROUTE_EARPIECE;
    }
    mRoutes[AudioSystem.MODE_RINGTONE]=(mRoutes[AudioSystem.MODE_RINGTONE] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
    mRoutes[AudioSystem.MODE_NORMAL]=(mRoutes[AudioSystem.MODE_NORMAL] & AudioSystem.ROUTE_BLUETOOTH_A2DP) | AudioSystem.ROUTE_SPEAKER;
    incallMask=AudioSystem.ROUTE_ALL;
    ringtoneMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
    normalMask=AudioSystem.ROUTE_ALL & ~AudioSystem.ROUTE_BLUETOOTH_A2DP;
  }
}
break;
case AudioSystem.ROUTE_BLUETOOTH_A2DP:
if (routes != 0 && !mBluetoothA2dpIsConnected) {
mBluetoothA2dpIsConnected=true;
mRoutes[AudioSystem.MODE_RINGTONE]|=AudioSystem.ROUTE_BLUETOOTH_A2DP;
mRoutes[AudioSystem.MODE_NORMAL]|=AudioSystem.ROUTE_BLUETOOTH_A2DP;
ringtoneMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
normalMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
}
 else if (routes == 0 && mBluetoothA2dpIsConnected) {
mBluetoothA2dpIsConnected=false;
mRoutes[AudioSystem.MODE_RINGTONE]&=~AudioSystem.ROUTE_BLUETOOTH_A2DP;
mRoutes[AudioSystem.MODE_NORMAL]&=~AudioSystem.ROUTE_BLUETOOTH_A2DP;
ringtoneMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
normalMask=AudioSystem.ROUTE_BLUETOOTH_A2DP;
}
break;
}
if (incallMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_IN_CALL,mRoutes[AudioSystem.MODE_IN_CALL],incallMask);
}
if (ringtoneMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_RINGTONE,mRoutes[AudioSystem.MODE_RINGTONE],ringtoneMask);
}
if (normalMask != 0) {
AudioSystem.setRouting(AudioSystem.MODE_NORMAL,mRoutes[AudioSystem.MODE_NORMAL],normalMask);
}
int streamType=getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
int index=mStreamStates[streamType].mIndex;
syncRingerAndNotificationStreamVolume(streamType,index,true);
setStreamVolumeInt(streamType,index,true,true);
}
}
}