public void onAudioPortListUpdate(AudioPort[] portList) {
          synchronized (mLockForPatch) {
            log("onAudioPortListUpdate");
            mOutPortSpeaker = null;
            mInPortA2dpSink = null;

            for (int i = 0; i < portList.length; i++) {
              AudioPort port = portList[i];
              if (port instanceof AudioDevicePort) {
                AudioDevicePort devicePort = (AudioDevicePort) port;
                if (devicePort.type() == AudioSystem.DEVICE_OUT_SPEAKER) {
                  log("Updating Speaker Port");
                  mOutPortSpeaker = devicePort;
                } else if (devicePort.type() == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) {
                  log("Updating In Port A2DP Sink");
                  mInPortA2dpSink = devicePort;
                  /* Check if we still have focus */
                  if ((mAudioFocusAcquired == AUDIO_FOCUS_GAIN)
                      && (mInPortA2dpSink != null)
                      && (mA2dpSinkAudioPatch == null)) {
                    /* This is the case of Port available
                     * later than focus acquired. Try patching ports now
                     */
                    log(" Sink Port updated, but patch not made");
                    patchPorts();
                  }
                }
              }
            }
          }
        }
  private void patchPorts() {
    /* If we are using AudioTrack, we need to broadcast and inform bluedroid from here */
    if (USE_AUDIOTRACK) {
      log(" patchPorts: AudioTrack, inform focus gain");
      broadcastAudioState(
          mPlayingDevice, BluetoothA2dpSink.STATE_PLAYING, BluetoothA2dpSink.STATE_NOT_PLAYING);
      informAudioFocusStateNative(STATE_FOCUS_GRANTED);
      return;
    }
    synchronized (mLockForPatch) {
      log(
          "patchPorts : mA2dpSinkAudioPatch: "
              + mA2dpSinkAudioPatch
              + " mPlayingDevice "
              + mPlayingDevice
              + "mOutPortSpeaker"
              + mOutPortSpeaker);
      if ((mA2dpSinkAudioPatch == null)
          && (mPlayingDevice != null)
          && (mOutPortSpeaker != null)
          && (mInPortA2dpSink != null)) {
        if ((mAudioConfigs == null) || (!mAudioConfigs.containsKey(mPlayingDevice))) {
          log(" AudioConfigs not yet received, returning");
          return;
        }
        /*Fix for below klockworks issue */
        /*Null pointer dereference of 'getAudioConfig(...)' where null is returned from a map or a collection */
        /*We are checking mAudioConfigs in above if condition and Fix is not allowd will update in False positive doc */
        int sampleRate = getAudioConfig(mPlayingDevice).getSampleRate();
        int channelMask = getAudioConfig(mPlayingDevice).getChannelConfig();
        int format = getAudioConfig(mPlayingDevice).getAudioFormat();

        AudioPortConfig sourcePortArray[] = {
          mInPortA2dpSink.buildConfig(sampleRate, channelMask, format, null)
        };
        AudioPortConfig sinkPortArray[] = {
          mOutPortSpeaker.buildConfig(sampleRate, channelMask, format, null)
        };
        AudioPatch patchPortArray[] = {null};
        /*  broadCast Audio State */
        broadcastAudioState(
            mPlayingDevice, BluetoothA2dpSink.STATE_PLAYING, BluetoothA2dpSink.STATE_NOT_PLAYING);

        int ret = mAudioManager.createAudioPatch(patchPortArray, sourcePortArray, sinkPortArray);
        if (ret == 0) {
          mA2dpSinkAudioPatch = patchPortArray[0];
          log("PatchCreated success: " + ret + " mA2dpSinkAudioPatch: " + mA2dpSinkAudioPatch);
        } else {
          log("PatchCreated failed returned: " + ret);
        }
      }
    }
  }