parseCallEvent method

  1. @override
CallEvent parseCallEvent(
  1. String state
)
override

Sends call events

Implementation

@override
CallEvent parseCallEvent(String state) {
  if (state.startsWith("DEVICETOKEN|")) {
    var token = state.split('|')[1];
    if (deviceTokenChanged != null) {
      deviceTokenChanged!(token);
    }
    return CallEvent.log;
  } else if (state.startsWith("LOG|PERMISSION|")) {
    List<String> tokens = state.split('|');
    if (kDebugMode) {
      if (tokens.length == 4) {
        printDebug("Name: ${tokens[2]}, granted state: ${tokens[3]}");
      }
    }
    return CallEvent.permission;
  } else if (state.startsWith("LOG|")) {
    List<String> tokens = state.split('|');
    if (kDebugMode) {
      printDebug(tokens[1]);
    }

    // source: https://www.twilio.com/docs/api/errors/31603
    // The callee does not wish to participate in the call.
    //
    // https://www.twilio.com/docs/api/errors/31486
    // The callee is busy.
    if (tokens[1].contains("31603") || tokens[1].contains("31486")) {
      call.activeCall = null;
      return CallEvent.declined;
    } else if (tokens.toString().toLowerCase().contains("call rejected")) {
      // Android call reject from string: "LOG|Call Rejected"
      call.activeCall = null;
      return CallEvent.declined;
    } else if (tokens.toString().toLowerCase().contains("rejecting call")) {
      // iOS call reject from string: "LOG|provider:performEndCallAction: rejecting call"
      call.activeCall = null;
      return CallEvent.declined;
    } else if (tokens[1].contains("Call Rejected")) {
      // macOS / web call reject from string: "Call Rejected"
      call.activeCall = null;
      return CallEvent.declined;
    }
    return CallEvent.log;
  } else if (state.startsWith("Connected|")) {
    call.activeCall = createCallFromState(state, initiated: true);
    if (kDebugMode) {
      printDebug(
          'Connected - From: ${call.activeCall!.from}, To: ${call.activeCall!.to}, StartOn: ${call.activeCall!.initiated}, Direction: ${call.activeCall!.callDirection}');
    }
    return CallEvent.connected;
  } else if (state.startsWith("Incoming|")) {
    // Added as temporary override for incoming calls, not breaking current (expected) Ringing behaviour
    call.activeCall = createCallFromState(state, callDirection: CallDirection.incoming);

    if (kDebugMode) {
      printDebug('Incoming - From: ${call.activeCall!.from}, To: ${call.activeCall!.to}, Direction: ${call.activeCall!.callDirection}');
    }

    return CallEvent.incoming;
  } else if (state.startsWith("Ringing|")) {
    call.activeCall = createCallFromState(state);

    if (kDebugMode) {
      printDebug('Ringing - From: ${call.activeCall!.from}, To: ${call.activeCall!.to}, Direction: ${call.activeCall!.callDirection}');
    }

    return CallEvent.ringing;
  } else if (state.startsWith("Answer")) {
    call.activeCall = createCallFromState(state, callDirection: CallDirection.incoming);
    if (kDebugMode) {
      printDebug('Answer - From: ${call.activeCall!.from}, To: ${call.activeCall!.to}, Direction: ${call.activeCall!.callDirection}');
    }

    return CallEvent.answer;
  } else if (state.startsWith("ReturningCall")) {
    call.activeCall = createCallFromState(state, callDirection: CallDirection.outgoing);

    if (kDebugMode) {
      printDebug('Returning Call - From: ${call.activeCall!.from}, To: ${call.activeCall!.to}, Direction: ${call.activeCall!.callDirection}');
    }

    return CallEvent.returningCall;
  } else if (state.startsWith("Reconnecting")) {
    return CallEvent.reconnecting;
  }
  switch (state) {
    case 'Ringing':
      return CallEvent.ringing;
    case 'Connected':
      return CallEvent.connected;
    case 'Call Ended':
      call.activeCall = null;
      return CallEvent.callEnded;
    case 'Missed Call':
      return CallEvent.missedCall;
    case 'Unhold':
      return CallEvent.unhold;
    case 'Hold':
      return CallEvent.hold;
    case 'Unmute':
      return CallEvent.unmute;
    case 'Mute':
      return CallEvent.mute;
    case 'Speaker On':
      return CallEvent.speakerOn;
    case 'Speaker Off':
      return CallEvent.speakerOff;
    case 'Bluetooth On':
      return CallEvent.bluetoothOn;
    case 'Bluetooth Off':
      return CallEvent.bluetoothOff;
    case 'Reconnected':
      return CallEvent.reconnected;
    default:
      if (kDebugMode) {
        printDebug('$state is not a valid CallState.');
      }
      throw ArgumentError('$state is not a valid CallState.');
  }
}