observer property

NECallEngineDelegate observer
final

Implementation

final NECallEngineDelegate observer = NECallEngineDelegate(
  onReceiveInvited: (NEInviteInfo info) async {
    CallKitUILog.i(_tag,
        'NECallObserver onReceiveInvited(callerAccId:${info.callerAccId}, callType:${info.callType})');
    CallingBellFeature.startRing();
    // 处理来电逻辑
    await CallState.instance.handleCallReceivedData(
        info.callerAccId, [], info.channelId ?? '', info.callType);
    await NECallKitPlatform.instance.updateCallStateToNative();
    // await CallManager.instance.enableWakeLock(true);
    if (Platform.isIOS) {
      if (CallState.instance.enableIncomingBanner) {
        CallState.instance.isInNativeIncomingBanner = true;
        await NECallKitPlatform.instance.showIncomingBanner();
      } else {
        CallState.instance.isInNativeIncomingBanner = false;
        CallManager.instance.launchCallingPage();
      }
    } else if (Platform.isAndroid) {
      if (await CallManager.instance.isScreenLocked()) {
        CallManager.instance.openLockScreenApp();
        return;
      }
      if (CallState.instance.enableIncomingBanner &&
          await NECallKitPlatform.instance.hasFloatPermission() &&
          !(await CallManager.instance.isSamsungDevice())) {
        CallState.instance.isInNativeIncomingBanner = true;
        CallManager.instance.showIncomingBanner();
      } else {
        if (await NECallKitPlatform.instance.isAppInForeground()) {
          CallState.instance.isInNativeIncomingBanner = false;
          CallManager.instance.launchCallingPage();
        } else {
          CallManager.instance.pullBackgroundApp();
        }
      }
    }
  },
  onCallEnd: (NECallEndInfo info) async {
    CallKitUILog.i(_tag,
        'NECallObserver onCallEnd(reasonCode:${info.reasonCode}, message:${info.message})');
    if (info.reasonCode == NECallTerminalCode.busy) {
      CallManager.instance.showToast(NECallKitUI.localizations.userBusy);
    } else if (info.reasonCode == NECallTerminalCode.calleeReject ||
        info.reasonCode == NECallTerminalCode.callerRejected) {
      if (CallState.instance.selfUser.callRole == NECallRole.caller) {
        CallManager.instance
            .showToast(NECallKitUI.localizations.remoteUserReject);
      }
    } else if (info.reasonCode == NECallTerminalCode.otherAccepted) {
      CallManager.instance
          .showToast(NECallKitUI.localizations.answerOnOtherDevice);
    } else if (info.reasonCode == NECallTerminalCode.otherRejected) {
      CallManager.instance
          .showToast(NECallKitUI.localizations.rejectOnOtherDevice);
    } else if (info.reasonCode == NECallTerminalCode.timeOut) {
      if (CallState.instance.selfUser.callRole == NECallRole.caller) {
        CallManager.instance
            .showToast(NECallKitUI.localizations.remoteTimeout);
      }
    } else if (info.reasonCode == NECallTerminalCode.calleeCancel) {
      CallManager.instance.showToast(NECallKitUI.localizations.remoteCancel);
    } else {
      CallKitUILog.i(_tag, 'NECallObserver onCallEnd: ${info.reasonCode}');
    }
    CallingBellFeature.stopRing();
    if (CallState.instance.callType == NECallType.video &&
        CallState.instance.isCameraOpen) {
      CallManager.instance.closeCamera();
    }

    // 如果启用了应用外悬浮窗,清理画中画资源
    if (Platform.isIOS &&
        CallState.instance.enableFloatWindowOutOfApp &&
        CallState.instance.enableFloatWindow) {
      await NECallKitPlatform.instance.disposePIP();
    }

    CallState.instance.cleanState();
    NEEventNotify().notify(setStateEventOnCallEnd);
    CallManager.instance.enableWakeLock(false);
    CallState.instance.stopTimer();
    NECallKitPlatform.instance.updateCallStateToNative();
  },
  onCallConnected: (NECallInfo info) async {
    CallKitUILog.i(_tag,
        'NECallObserver onCallConnected(callId:${info.callId}, callType:${info.callType})');
    CallState.instance.startTime =
        DateTime.now().millisecondsSinceEpoch ~/ 1000;
    CallingBellFeature.stopRing();
    CallState.instance.callType = info.callType;
    CallState.instance.selfUser.callStatus = NECallStatus.accept;
    if (CallState.instance.isMicrophoneMute) {
      CallManager.instance.closeMicrophone();
    } else {
      CallManager.instance.openMicrophone();
    }
    CallManager.instance
        .setSpeakerphoneOn(CallState.instance.isEnableSpeaker);
    CallState.instance.startTimer();
    CallState.instance.isLocalViewBig = false;
    CallState.instance.isInNativeIncomingBanner = false;

    CallKitUILog.i(_tag,
        'NECallObserver onCallConnected: enableFloatWindowOutOfApp = ${CallState.instance.enableFloatWindowOutOfApp}, enableFloatWindow = ${CallState.instance.enableFloatWindow}');

    // 如果启用了应用外悬浮窗,设置画中画
    if (Platform.isIOS &&
        CallState.instance.enableFloatWindowOutOfApp &&
        CallState.instance.enableFloatWindow &&
        CallState.instance.callType == NECallType.video) {
      final success = await NECallKitPlatform.instance.setupPIP();
      CallKitUILog.i(
          _tag, 'NECallObserver onCallConnected: setupPIP result = $success');
    }

    NEEventNotify().notify(setStateEvent);
    NEEventNotify().notify(setStateEventOnCallBegin);
    NECallKitPlatform.instance.updateCallStateToNative();
  },
  onCallTypeChange: (NECallTypeChangeInfo info) {
    CallKitUILog.i(_tag,
        'NECallObserver onCallTypeChange(callType:${info.callType}, state:${info.state})');
    CallState.instance.callType = info.callType;
    NEEventNotify().notify(setStateEvent);
    NECallKitPlatform.instance.updateCallStateToNative();
  },
  onVideoAvailable: (bool available, String userID) {
    CallKitUILog.i(_tag,
        'NECallObserver onVideoAvailable(userId:$userID, isVideoAvailable:$available)');
    for (var remoteUser in CallState.instance.remoteUserList) {
      if (remoteUser.id == userID) {
        remoteUser.videoAvailable = available;
        NEEventNotify().notify(setStateEvent);
        NECallKitPlatform.instance.updateCallStateToNative();
        return;
      }
    }
  },
  onVideoMuted: (bool muted, String userID) {
    CallKitUILog.i(
        _tag, 'NECallObserver onVideoMuted(userId:$userID, muted:$muted)');
    // 处理视频静音逻辑
    for (var remoteUser in CallState.instance.remoteUserList) {
      if (remoteUser.id == userID) {
        remoteUser.videoAvailable = !muted;
        NEEventNotify().notify(setStateEvent);
        NECallKitPlatform.instance.updateCallStateToNative();
        return;
      }
    }
  },
  onAudioMuted: (bool muted, String userID) {
    CallKitUILog.i(
        _tag, 'NECallObserver onAudioMuted(userId:$userID, muted:$muted)');
    for (var remoteUser in CallState.instance.remoteUserList) {
      if (remoteUser.id == userID) {
        remoteUser.audioAvailable = !muted;
        NEEventNotify().notify(setStateEvent);
        return;
      }
    }
    NECallKitPlatform.instance.updateCallStateToNative();
  },
  onLocalAudioMuted: (bool muted) {
    CallKitUILog.i(_tag, 'NECallObserver onLocalAudioMuted(muted:$muted)');
    CallState.instance.isMicrophoneMute = muted;
    NECallKitPlatform.instance.updateCallStateToNative();
  },
  onRtcInitEnd: () {
    CallKitUILog.i(_tag, 'NECallObserver onRtcInitEnd()');
    NECallKitPlatform.instance.updateCallStateToNative();
    // RTC 初始化完成
  },
  onNERtcEngineVirtualBackgroundSourceEnabled: (bool enabled, int reason) {
    CallKitUILog.i(_tag,
        'NECallObserver onNERtcEngineVirtualBackgroundSourceEnabled(enabled:$enabled, reason:$reason)');
    // 处理虚拟背景
    NECallKitPlatform.instance.updateCallStateToNative();
  },
);