|
@@ -0,0 +1,401 @@
|
|
|
+package com.wdkl.app.ncs.callingdoor.fragment
|
|
|
+
|
|
|
+import android.os.Build
|
|
|
+import android.os.Handler
|
|
|
+import android.os.Looper
|
|
|
+import android.os.SystemClock
|
|
|
+import android.text.TextUtils
|
|
|
+import android.util.Log
|
|
|
+import android.view.View
|
|
|
+import com.google.gson.Gson
|
|
|
+import com.wdkl.app.ncs.callingdoor.R
|
|
|
+import com.wdkl.app.ncs.callingdoor.helper.RingPlayHelper
|
|
|
+import com.wdkl.app.ncs.callingdoor.helper.SerialPortHelper
|
|
|
+import com.wdkl.app.ncs.callingdoor.sip.WdklSipService
|
|
|
+import com.wdkl.ncs.android.lib.utils.AppTool
|
|
|
+import com.wdkl.ncs.android.lib.utils.showMessage
|
|
|
+import com.wdkl.ncs.android.middleware.common.Constant
|
|
|
+import com.wdkl.ncs.android.middleware.common.MessageEvent
|
|
|
+import com.wdkl.ncs.android.middleware.model.vo.InteractionVO
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.VoiceUtil
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.TcpType
|
|
|
+import kotlinx.android.synthetic.main.sky_voice_call_layout.*
|
|
|
+import org.greenrobot.eventbus.Subscribe
|
|
|
+import org.greenrobot.eventbus.ThreadMode
|
|
|
+import org.linphone.core.Call
|
|
|
+import org.linphone.core.Core
|
|
|
+import org.linphone.core.CoreListenerStub
|
|
|
+
|
|
|
+class SipCallFragment: BaseCallFragment() {
|
|
|
+ private val TAG = "SkyCallFragment"
|
|
|
+
|
|
|
+ //来电设备id
|
|
|
+ var fromId: Int = -1
|
|
|
+
|
|
|
+ private var interactionVO: InteractionVO? = null
|
|
|
+ private var sipCore: Core? = null
|
|
|
+
|
|
|
+ private val handler = Handler(Looper.getMainLooper())
|
|
|
+
|
|
|
+ private var audioCall: Boolean = true
|
|
|
+
|
|
|
+ private var callEnded: Boolean = false
|
|
|
+
|
|
|
+ private var outGoing: Boolean = false
|
|
|
+
|
|
|
+ // 配置通话状态监听
|
|
|
+ private val coreListener = object : CoreListenerStub() {
|
|
|
+ override fun onCallStateChanged(
|
|
|
+ core: Core,
|
|
|
+ call: Call,
|
|
|
+ state: Call.State,
|
|
|
+ message: String
|
|
|
+ ) {
|
|
|
+ if (state == Call.State.IncomingReceived) {
|
|
|
+ //来电时将自动接听
|
|
|
+ if (sipCore != null) {
|
|
|
+ val params = sipCore!!.createCallParams(call)
|
|
|
+ params.enableVideo(false)
|
|
|
+ call.acceptWithParams(params)
|
|
|
+ } else {
|
|
|
+ callEnd(true)
|
|
|
+ }
|
|
|
+ } else if (state == Call.State.End || state == Call.State.Released) {
|
|
|
+ callEnd(false)
|
|
|
+ } else if (state == Call.State.Connected) {
|
|
|
+ showCalling(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun getLayId(): Int {
|
|
|
+ if ("rk3288".equals(Build.MODEL)) {
|
|
|
+ return R.layout.sky_voice_call_layout_rk3288
|
|
|
+ } else {
|
|
|
+ return R.layout.sky_voice_call_layout
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun init() {
|
|
|
+ //初始化计时器
|
|
|
+ initCountDownTimer(sky_voice_call_timeout)
|
|
|
+ //tcp参数
|
|
|
+ if (tcpModel != null) {
|
|
|
+ fromId = tcpModel!!.fromId
|
|
|
+ interactionVO = Gson().fromJson(tcpModel!!.data.toString(), InteractionVO::class.java)
|
|
|
+ }
|
|
|
+
|
|
|
+ sipCore = WdklSipService.getCore()
|
|
|
+ if (sipCore != null) {
|
|
|
+ sipCore!!.addListener(coreListener)
|
|
|
+ }
|
|
|
+
|
|
|
+ when (callState) {
|
|
|
+ 0 -> {
|
|
|
+ //发起通话
|
|
|
+ outGoing = true
|
|
|
+ //呼叫主机
|
|
|
+ RingPlayHelper.playRingTone(baseActivity, R.raw.ring_back2, true)
|
|
|
+ if (!startOutgoing()) {
|
|
|
+ AppTool.Time.delay(3000) {
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 1 -> {
|
|
|
+ //来电
|
|
|
+ outGoing = false
|
|
|
+ showIncomingCall()
|
|
|
+ RingPlayHelper.playRingTone(baseActivity, R.raw.ring_tone, true)
|
|
|
+ }
|
|
|
+
|
|
|
+ 2 -> {
|
|
|
+ //呼叫分机
|
|
|
+ outGoing = true
|
|
|
+ RingPlayHelper.playRingTone(baseActivity, R.raw.ring_back2, true)
|
|
|
+ if (!startOutgoing()) {
|
|
|
+ AppTool.Time.delay(3000) {
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun bindEvent() {
|
|
|
+ //去电取消或通话挂断
|
|
|
+ sky_voice_call_hangup.setOnClickListener {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ if (Constant.CALL_STATE == Constant.CALL_CALLING) {
|
|
|
+ //结束sip通话
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (sky_voice_call_timer != null) {
|
|
|
+ sky_voice_call_timer.stop()
|
|
|
+ }
|
|
|
+ callTerminate()
|
|
|
+ callEnd(true)
|
|
|
+ } else {
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (callState == 0) {
|
|
|
+ VoiceUtil.cancelAudioCall(Constant.DEVICE_ID)
|
|
|
+ } else if (callState == 2) {
|
|
|
+ if (bedId != -1) {
|
|
|
+ VoiceUtil.cancelAudioCallBed(Constant.DEVICE_ID, bedId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //来电拒绝
|
|
|
+ sky_voice_call_ring_reject.setOnClickListener {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ VoiceUtil.rejectAudioCall(Constant.DEVICE_ID, fromId, interactionVO?.id)
|
|
|
+ callEnd(false)
|
|
|
+ }
|
|
|
+
|
|
|
+ //来电接听
|
|
|
+ sky_voice_call_ring_pickup_audio.setOnClickListener {
|
|
|
+ acceptCall()
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ Constant.CALL_STATE = Constant.CALL_CALLING
|
|
|
+ VoiceUtil.acceptAudioCall(Constant.DEVICE_ID, fromId, interactionVO?.id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun destroy() {
|
|
|
+ cancelTimer()
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (sky_voice_call_timer != null) {
|
|
|
+ sky_voice_call_timer.stop()
|
|
|
+ }
|
|
|
+ if (sipCore != null) {
|
|
|
+ sipCore!!.removeListener(coreListener)
|
|
|
+ }
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+
|
|
|
+ //如果当前在护理中则不操作门灯,如果不在护理中则重置门灯
|
|
|
+ if (("rk3128".equals(Build.MODEL) || "rk3368".equals(Build.MODEL) || "WDMK_I".equals(Build.MODEL))
|
|
|
+ && !Constant.inNursing) {
|
|
|
+ SerialPortHelper.setDoorLight(1, "111") //白色
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun startOutgoing(): Boolean {
|
|
|
+ if (callState == 0) {
|
|
|
+ if (Constant.CALL_TYPE == Constant.VIDEO_CALL) {
|
|
|
+ VoiceUtil.startVideoCall(Constant.DEVICE_ID)
|
|
|
+ } else {
|
|
|
+ VoiceUtil.startAudioCall(Constant.DEVICE_ID)
|
|
|
+ }
|
|
|
+ Constant.CALL_STATE = Constant.CALL_OUTGOING
|
|
|
+ sky_voice_call_timeout.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_timer.visibility = View.GONE
|
|
|
+ startTimer()
|
|
|
+ return true
|
|
|
+ } else if (callState == 2) {
|
|
|
+ if (bedId != -1) {
|
|
|
+ if (Constant.CALL_TYPE == Constant.VIDEO_CALL) {
|
|
|
+ VoiceUtil.startVideoCallBed(Constant.DEVICE_ID, bedId)
|
|
|
+ } else {
|
|
|
+ VoiceUtil.startAudioCallBed(Constant.DEVICE_ID, bedId)
|
|
|
+ }
|
|
|
+ Constant.CALL_STATE = Constant.CALL_OUTGOING
|
|
|
+ sky_voice_call_timeout.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_timer.visibility = View.GONE
|
|
|
+ startTimer()
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ showMessage("bedId为空,呼叫失败")
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ //去电界面
|
|
|
+ private fun showOutgoingCall() {
|
|
|
+ Constant.CALL_STATE = Constant.CALL_OUTGOING
|
|
|
+ sky_voice_call_calling_text.text = "呼叫成功,等待接听..."
|
|
|
+ sky_voice_call_outgoing.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_incoming.visibility = View.GONE
|
|
|
+ sky_voice_call_timeout.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_timer.visibility = View.GONE
|
|
|
+ startTimer()
|
|
|
+
|
|
|
+ if (!audioCall) {
|
|
|
+ //显示视频画面
|
|
|
+ fullscreen_video_frame.visibility = View.VISIBLE
|
|
|
+ pip_video_frame.visibility = View.VISIBLE
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //来电界面
|
|
|
+ private fun showIncomingCall() {
|
|
|
+ Constant.CALL_STATE = Constant.CALL_INCOMING
|
|
|
+ sky_voice_call_calling_text.text = "有新来电..."
|
|
|
+ sky_voice_call_outgoing.visibility = View.GONE
|
|
|
+ sky_voice_call_incoming.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_timeout.visibility = View.GONE
|
|
|
+ sky_voice_call_timer.visibility = View.GONE
|
|
|
+ cancelTimer()
|
|
|
+ }
|
|
|
+
|
|
|
+ //开始接听
|
|
|
+ private fun acceptCall() {
|
|
|
+ sky_voice_call_calling_text.text = "连接中..."
|
|
|
+ sky_voice_call_outgoing.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_incoming.visibility = View.GONE
|
|
|
+ sky_voice_call_timeout.visibility = View.GONE
|
|
|
+ sky_voice_call_timer.visibility = View.GONE
|
|
|
+ cancelTimer()
|
|
|
+ }
|
|
|
+
|
|
|
+ //呼叫取消
|
|
|
+ private fun cancelCall() {
|
|
|
+ cancelTimer()
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (sky_voice_call_timer != null) {
|
|
|
+ sky_voice_call_timer.stop()
|
|
|
+ }
|
|
|
+ callEnd(false)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun showCalling(audioOnly: Boolean) {
|
|
|
+ if (callEnded) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (audioOnly) {
|
|
|
+ ll_voice_call.visibility = View.VISIBLE
|
|
|
+ } else {
|
|
|
+ //显示视频画面
|
|
|
+ fullscreen_video_frame.visibility = View.VISIBLE
|
|
|
+ pip_video_frame.visibility = View.VISIBLE
|
|
|
+ ll_voice_call.visibility = View.GONE
|
|
|
+ }
|
|
|
+
|
|
|
+ Constant.CALL_STATE = Constant.CALL_CALLING
|
|
|
+ sky_voice_call_calling_text.text = "通话中..."
|
|
|
+ sky_voice_call_timeout.visibility = View.GONE
|
|
|
+ cancelTimer()
|
|
|
+ sky_voice_call_timer.visibility = View.VISIBLE
|
|
|
+ sky_voice_call_timer.base = SystemClock.elapsedRealtime()
|
|
|
+ sky_voice_call_timer.start()
|
|
|
+ }
|
|
|
+
|
|
|
+ //通话结束
|
|
|
+ override fun callEnd(handoff: Boolean) {
|
|
|
+ synchronized(this) {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ if (callEnded) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ callEnded = true
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (sky_voice_call_timer != null) {
|
|
|
+ sky_voice_call_timer.stop()
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.e(TAG, "call end !!!!!!!!!!!!!!!!!!")
|
|
|
+
|
|
|
+ if (handoff) {
|
|
|
+ VoiceUtil.handoffAudioCall(Constant.DEVICE_ID, fromId, Constant.interactionId)
|
|
|
+ }
|
|
|
+
|
|
|
+ backToMain()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun callTerminate() {
|
|
|
+ if (sipCore != null && sipCore!!.callsNb > 0) {
|
|
|
+ var call = sipCore!!.currentCall
|
|
|
+ if (call == null) {
|
|
|
+ call = sipCore!!.calls[0]
|
|
|
+ }
|
|
|
+ call!!.terminate()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ fun onMoonEvent(messageEvent: MessageEvent) {
|
|
|
+ when (messageEvent.type) {
|
|
|
+ Constant.EVENT_TCP_MSG -> {
|
|
|
+ if (messageEvent.message is TcpModel) {
|
|
|
+ val curTcpModel = messageEvent.message as TcpModel
|
|
|
+ if (curTcpModel.getType() == TcpType.VOICE) {
|
|
|
+ val curInteractionVO = Gson().fromJson(curTcpModel.data.toString(), InteractionVO::class.java)
|
|
|
+ if (curTcpModel.getAction() == TcpAction.VoiceAction.ACCEPT) {
|
|
|
+ //我方呼出,对方接受
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ Constant.interactionId = curInteractionVO.id
|
|
|
+ fromId = curTcpModel.fromId
|
|
|
+ acceptCall()
|
|
|
+ if (sipCore == null && TextUtils.isEmpty(curInteractionVO.toSipId)) {
|
|
|
+ //通话失败,重置并返回主界面
|
|
|
+ showMessage("Core或targetSipId为空!")
|
|
|
+ Constant.CALL_STATE = Constant.CALL_STANDBY
|
|
|
+ if (sky_voice_call_timer != null) {
|
|
|
+ sky_voice_call_timer.stop()
|
|
|
+ }
|
|
|
+ callEnd(true)
|
|
|
+ } else {
|
|
|
+ val addressToCall = sipCore!!.interpretUrl("3309")
|
|
|
+ val params = sipCore!!.createCallParams(null)
|
|
|
+ params.enableVideo(false)
|
|
|
+ if (addressToCall != null) {
|
|
|
+ sipCore!!.inviteAddressWithParams(addressToCall, params)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.REJECT) {
|
|
|
+ //我方呼出,对方拒绝
|
|
|
+ showMessage("对方已拒绝!")
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ cancelCall()
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.CALLING) {
|
|
|
+ //我方呼出,对方通话中
|
|
|
+ showMessage("对方正在忙线中,暂时无法接听!")
|
|
|
+ AppTool.Time.delay(2000) {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.SUCCESS) {
|
|
|
+ //呼叫成功
|
|
|
+ //本机呼叫的时候tcpmodel为空,只有呼叫成功的时候才能获得对应tcp相关数据
|
|
|
+ interactionVO = curInteractionVO
|
|
|
+ Constant.interactionId = curInteractionVO.id
|
|
|
+ showOutgoingCall()
|
|
|
+ //janusClient!!.connect()
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.FAILED) {
|
|
|
+ //我方呼出,对方不在线,设备离线或其它错误
|
|
|
+ showMessage("呼叫失败,找不到设备或对方不在线!")
|
|
|
+ AppTool.Time.delay(2000) {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.HANDOFF) {
|
|
|
+ //对方挂断,不论我方呼出或呼入
|
|
|
+ if (Constant.interactionId == curInteractionVO.id) {
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ } else if (curTcpModel.getAction() == TcpAction.VoiceAction.CANCEL) {
|
|
|
+ //对方呼叫时取消
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
+ cancelCall()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|