#pragma once #include "uart/ProtocolSender.h" #include #include "core/utilities.h" #include "voip/telephone.h" #include "base/strings.hpp" #include "storage/StoragePreferences.h" #include "core/sip_config.h" #include "zkaudio.h" #include "service/BusinessConfig.h" #include "net/tcp_client.h" #include "manager/ConfigManager.h" //#include "voip/wav_player.h" #include "voip/media_player.h" #include "manager/LanguageManager.h" std::string _isOutgoing; std::string _toIdStr; //voip::WAVPlayer wavPlayer; // 播放音频 std::string tid; static base::MediaPlayer mediaPlayer; static bool _isSleepTimerRegistered = false; #define CALLING_TIME 60 // 用于控制呼叫时间,定时器 const int goBack = 200; void callActivityFinish(CallFinishType type){ switch (type){ case CANCEL: LOGD("cancel"); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; case REJECT: LOGD("reject"); // wavPlayer.Stop(); mediaPlayer.Stop(); mTextViewNamePtr->setTextTr("RemoteRefuse"); mActivityPtr->unregisterUserTimer(CALLING_TIME); mActivityPtr->registerUserTimer(goBack, 5000); break; case BUSY: LOGD("BUSY"); // wavPlayer.Stop(); mediaPlayer.Stop(); mTextViewNamePtr->setTextTr("RemoteBusy"); mActivityPtr->unregisterUserTimer(CALLING_TIME); mActivityPtr->registerUserTimer(goBack, 5000); break; case HANDOFF: LOGD("handoff"); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; case FAILED: // wavPlayer.Stop(); mediaPlayer.Stop(); mTextViewNamePtr->setTextTr("CallFailed"); mActivityPtr->registerUserTimer(goBack, 5000); break; case ACCEPT: mActivityPtr->unregisterUserTimer(CALLING_TIME); break; case A1CLICK: LOGD("a1 click"); if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); //只回传iId storeModel.data = storeModel.json["id"].asString(); voip::CallInfo info = GetTelephone()->GetCallInfo(); LOGD("info state = %d",info.state); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); } else if (_isOutgoing == "false"){ //state = 5 mButtonAnswerPtr->setVisible(false); TcpModel model = CallingStatus::instance()->getTcpModel(); model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); mActivityPtr->unregisterUserTimer(CALLING_TIME); } else if (_isOutgoing == "true") { sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); } } break; case KEY6_CLICK: LOGD("KEY6 click"); if (StoragePreferences::getString(STORE_SIGNAL_TYPE,"TCP")=="TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); //只回传iId storeModel.data = storeModel.json["id"].asString(); voip::CallInfo info = GetTelephone()->GetCallInfo(); LOGD("info state = %d",info.state); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); // std::string heartStr = "DOORLED,000F"; // const char* sendMsg = heartStr.c_str(); // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); lightControl("DOORLED", "000F"); } else if (_isOutgoing == "false"){ //state = 5 mButtonAnswerPtr->setVisible(false); TcpModel model = CallingStatus::instance()->getTcpModel(); model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); mActivityPtr->unregisterUserTimer(CALLING_TIME); } } break; } } void buildSIP(std::string toSipId){ // wavPlayer.Stop(); mediaPlayer.Stop(); int port = StoragePreferences::getInt(SIP_REG_PORT, SIP_REG_PORT_DEFAULT); std::string domain = StoragePreferences::getString(SIP_REG_DOMAIN, SIP_REG_DOMAIN_DEFAULT); //std::string od_number = StoragePreferences::getString(SIP_REG_DOOR_ACCOUNT, SIP_REG_DOOR_ACCOUNT_DEFAULT); std::string od_number_uri = URI(toSipId, domain, port); LOGD("xxxxxxxxxxxxxx %s",od_number_uri.c_str()); GetTelephone()->MakeCall(od_number_uri); } void acceptSIP(){ // wavPlayer.Stop(); mediaPlayer.Stop(); GetTelephone()->Answer(); } //static ZKMediaPlayer sPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO); namespace { const int kTimerAutoAnswer = 100; std::string StateString(voip::State state) { switch (state) { case voip::STATE_CALL_CALLING: case voip::STATE_CALL_EARLY: return LANGUAGEMANAGER->getValue("Calling"); case voip::STATE_CALL_CONFIRMED: return LANGUAGEMANAGER->getValue("CallConfirmed"); case voip::STATE_CALL_CONNECTING: return LANGUAGEMANAGER->getValue("CallConnecting"); case voip::STATE_CALL_INCOMING: return LANGUAGEMANAGER->getValue("CallInComing"); default: break; } return ""; } } /* anonymous namespace */ /** * 注册定时器 * 填充数组用于注册定时器 * 注意:id不能重复 */ static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = { //{0, 6000}, //定时器id=0, 时间间隔6秒 {1, 1000}, {kTimerAutoAnswer, 2000}, }; /** * 当界面构造时触发 */ static void onUI_init(){ CallingStatus::instance()->setBusy(true); // GetTelephone()->SetInputPcmGainSize(10.0); // player = new ZKMediaPlayer(ZKMediaPlayer::E_MEDIA_TYPE_AUDIO); // player->play("/mnt/extsd/ui/hl.mp3"); //界面操作 if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){ voip::CallInfo info = GetTelephone()->GetCallInfo(); mTextViewNamePtr->setText(info.remote_uri); if (info.state == voip::STATE_CALL_CALLING) { mButtonAnswerPtr->setVisible(false); } } // mSeekBarOutputVolumePtr->setMax(zk_audio_output_volume_native_max()); bool isVolume = StoragePreferences::getBool(STORE_VOLUME, false); mSeekBarGainPtr->setVisible(isVolume); mTextViewGainPtr->setVisible(isVolume); mSeekBarInputPtr->setVisible(isVolume); mTextViewInputPtr->setVisible(isVolume); //打开时的声音大小 int serverVol = StoragePreferences::getInt(STORE_DAY_VOL,PartSetting::dayDoorVol); if (checkIsDay()){ serverVol = StoragePreferences::getInt(STORE_NIGHT_VOL,PartSetting::nightDoorVol); } LOGD("serverVol == %d", serverVol); double percent = (double) serverVol / (double)100; int vol = (int)(zk_audio_output_volume_native_max()*percent); LOGD("bed vol is %d. server is %d. max is %d",vol,serverVol, zk_audio_output_volume_native_max()); zk_audio_output_set_volume_native(vol); mSeekBarOutputVolumePtr->setProgress(vol); mTextViewOutputVolumePtr->setTouchPass(true); // mSeekBarGainPtr->setProgress(StoragePreferences::getInt(STORE_GAIN_SIZE, 2)); mTextViewGainPtr->setTouchPass(true); mSeekBarInputPtr->setProgress(StoragePreferences::getInt(STORE_VOLUME_NATIVE, 6)); mTextViewInputPtr->setTouchPass(true); // mTextViewNamePtr->setVisible(false); // mTextViewStatePtr->setVisible(false); // mTextViewDurationPtr->setVisible(false); } /** * 当切换到该界面时触发 */ static void onUI_intent(const Intent *intentPtr) { if(intentPtr != NULL) { LOGD("打开通话页面,进行通话"); BRIGHTNESSHELPER->screenOn(); _isSleepTimerRegistered = getSleepTimerRegistered(); setSleepTimerRegistered(false); // wavPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav")); // mediaPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav")); mediaPlayer.Play(CONFIGMANAGER->getResFilePath("ring.wav"), "", 1000, base::MediaPlayer::PlayMode::Loop); // 键值解析 _isOutgoing = intentPtr->getExtra(isOutgoing); std::string _audioOnly = intentPtr->getExtra(audioOnly); if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){ return; } if (_isOutgoing == "true"){ int toId = 0; _toIdStr = intentPtr->getExtra(toIdStr); if (_toIdStr != "") { toId = atoi(_toIdStr.c_str()); } // 呼叫的时候需要把tid放进去 tid = sendVoiceCall(toId); mButtonAnswerPtr->setVisible(false); std::string _callName = intentPtr->getExtra(callName); if (_callName != "") { mTextViewNamePtr->setText(LANGUAGEMANAGER->getValue("Call") + _callName); } else { mTextViewNamePtr->setTextTr("CallOutTitle"); } mActivityPtr->registerUserTimer(CALLING_TIME, 60000); // 如果对方没接听,则发送一个CANCEL回去 } else { TcpModel storeModel = CallingStatus::instance()->getTcpModel(); TcpModel backTcp = storeModel; backTcp.data = storeModel.json["id"].asString(); //back voice success sendVoiceTcp(VoiceAction::SUCCESS, backTcp, storeModel.from_id); mTextViewNamePtr->setText(storeModel.json["fromFrameName"].asString()); //自动接听处理 if (StoragePreferences::getBool(STORE_AUDO_ANSWER,false)){ mButtonAnswerPtr->setVisible(false); voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_INCOMING || info.state == voip::STATE_CALL_EARLY) { GetTelephone()->Answer(); //发出TCP sendVoiceTcp(VoiceAction::ACCEPT, storeModel, storeModel.from_id); } } } } } /* * 当界面显示时触发 */ static void onUI_show() { EASYUICONTEXT->hideStatusBar(); EASYUICONTEXT->hideNaviBar(); } /* * 当界面隐藏时触发 */ static void onUI_hide() { } /* * 当界面完全退出时触发 */ static void onUI_quit() { if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "TCP"){ EASYUICONTEXT->showNaviBar(); EASYUICONTEXT->showStatusBar(); CallingStatus::instance()->setBusy(false); CallingStatus::instance()->clearTcpModel(); voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); } } mediaPlayer.Stop(); mActivityPtr->unregisterUserTimer(goBack); } /** * 串口数据回调接口 */ static void onProtocolDataUpdate(const SProtocolData &data) { } /** * 定时器触发函数 * 不建议在此函数中写耗时操作,否则将影响UI刷新 * 参数: id * 当前所触发定时器的id,与注册时的id相同 * 返回值: true * 继续运行当前定时器 * false * 停止运行当前定时器 */ static bool onUI_Timer(int id){ switch (id) { case 1: { if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){ voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_CONFIRMED){ mButtonAnswerPtr->setVisible(false); } mTextViewNamePtr->setText(info.remote_uri); mTextViewStatePtr->setText(StateString(info.state)); mTextViewDurationPtr->setText( base::format("%02d:%02d", info.duration/1000/60, info.duration/1000%60)); } else { voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_CONFIRMED){ mButtonAnswerPtr->setVisible(false); } mTextViewStatePtr->setText(StateString(info.state)); mTextViewDurationPtr->setText( base::format("%02d:%02d", info.duration/1000/60, info.duration/1000%60)); } } break; case kTimerAutoAnswer: { voip::CallInfo info = GetTelephone()->GetCallInfo(); if (info.state == voip::STATE_CALL_INCOMING || info.state == voip::STATE_CALL_EARLY) { bool auto_answer = StoragePreferences::getBool(SIP_AUTO_ANSWER, false); LOGD("is auto answer: %d", auto_answer); if (auto_answer) { GetTelephone()->Answer(); } } } return false; break; case goBack:{ setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; } case CALLING_TIME: { sendVoiceCancel(StoragePreferences::getInt(STORE_DEVICE_ID,0), 0); // std::string heartStr = "DOORLED,000F"; // const char* sendMsg = heartStr.c_str(); // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); lightControl("DOORLED", "000F"); setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); break; } default: break; } return true; } /** * 有新的触摸事件时触发 * 参数:ev * 新的触摸事件 * 返回值:true * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上 * false * 触摸事件将继续传递到控件上 */ static bool oncallActivityTouchEvent(const MotionEvent &ev) { switch (ev.mActionStatus) { case MotionEvent::E_ACTION_DOWN://触摸按下 //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY); break; case MotionEvent::E_ACTION_MOVE://触摸滑动 break; case MotionEvent::E_ACTION_UP: //触摸抬起 break; default: break; } return false; } static void onVideoViewPlayerMessageListener_VideoView1(ZKVideoView *pVideoView, int msg) { switch (msg) { case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_ERROR: break; } } static void onProgressChanged_SeekBarOutputVolume(ZKSeekBar *pSeekBar, int progress) { zk_audio_output_set_volume_native(progress); mTextViewOutputVolumePtr->setText(base::format("%d", progress)); } static bool onButtonClick_ButtonHangup(ZKButton *pButton) { LOGD("handup click"); voip::CallInfo info = GetTelephone()->GetCallInfo(); if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "TCP"){ TcpModel storeModel = CallingStatus::instance()->getTcpModel(); if (storeModel.json != 0) { //只回传iId storeModel.data = storeModel.json["id"].asString(); if (info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CALLING || info.state == voip::STATE_CALL_CONFIRMED){ sendVoiceTcp(VoiceAction::HANDOFF, storeModel, storeModel.from_id); GetTelephone()->Hangup(info.id, voip::SIP_STATUS_CODE_DECLINE); GetTelephone()->Hangup(); } else if (_isOutgoing == "true") { string storeModelStr = getTcpModelString(storeModel); sendVoiceTcp(VoiceAction::CANCEL, storeModel, storeModel.from_id); // std::string heartStr = "DOORLED,000F"; // const char* sendMsg = heartStr.c_str(); // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); lightControl("DOORLED", "000F"); } else { storeModel.data = storeModel.json["id"].asString(); sendVoiceTcp(VoiceAction::REJECT, storeModel, storeModel.from_id); } } else { TcpModel tcpModel; tcpModel.tid = tid; sendVoiceTcp(VoiceAction::CANCEL, tcpModel, StoragePreferences::getInt(STORE_DEVICE_ID,0)); // 还原成无灯光的状态 // std::string heartStr = "DOORLED,000F"; // const char* sendMsg = heartStr.c_str(); // sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg)); lightControl("DOORLED", "000F"); } setSleepTimerRegistered(_isSleepTimerRegistered); EASYUICONTEXT->goBack(); } else { GetTelephone()->Hangup(); } return false; } static bool onButtonClick_ButtonAnswer(ZKButton *pButton) { LOGD(" ButtonClick ButtonAnswer !!!\n"); mButtonAnswerPtr->setVisible(false); if (StoragePreferences::getString(STORE_SIGNAL_TYPE, SIGNAL_TYPE) == "SIP"){ voip::CallInfo info = GetTelephone()->GetCallInfo(); GetTelephone()->Answer(); } else { TcpModel model = CallingStatus::instance()->getTcpModel(); //LOGD("storeModel :::: type=%s,action=%s,from=%d,to=%d",model.type.c_str(),model.action.c_str(),model.from_id,model.to_id); //std::string storeModelStr = getTcpModelString(model); //LOGD("storeModelStr ===== %s",storeModelStr.c_str()); //只回传iId model.data = model.json["id"].asString(); sendVoiceTcp(VoiceAction::ACCEPT, model, model.from_id); } return false; } static bool onButtonClick_ButtonVol(ZKButton *pButton) { LOGD(" ButtonClick ButtonVol !!!\n"); return false; } static void onProgressChanged_SeekBarGain(ZKSeekBar *pSeekBar, int progress) { //LOGD(" ProgressChanged SeekBarGain %d !!!\n", progress); mTextViewGainPtr->setText(base::format("%d", progress)); GetTelephone()->SetInputPcmGainSize(progress); } static void onProgressChanged_SeekBarInput(ZKSeekBar *pSeekBar, int progress) { //LOGD(" ProgressChanged SeekBarInput %d !!!\n", progress); mTextViewInputPtr->setText(base::format("%d", progress)); zk_audio_input_set_volume_native(0, 0); zk_audio_input_set_volume_native(1, progress); } static void onVideoViewPlayerMessageListener_VideoView(ZKVideoView *pVideoView, int msg) { switch (msg) { case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED: break; case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_ERROR: break; } }