|
@@ -0,0 +1,987 @@
|
|
|
|
+package com.wdkl.ncs.android.component.home.service
|
|
|
|
+
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
|
+import android.content.Context
|
|
|
|
+import android.content.Intent
|
|
|
|
+import android.content.IntentFilter
|
|
|
|
+import android.os.*
|
|
|
|
+import android.support.v4.media.session.MediaSessionCompat
|
|
|
|
+import android.text.TextUtils
|
|
|
|
+import android.util.Log
|
|
|
|
+import android.view.KeyEvent
|
|
|
|
+import com.alibaba.fastjson.JSON
|
|
|
|
+import com.google.common.base.Strings
|
|
|
|
+import com.google.gson.FieldNamingPolicy
|
|
|
|
+import com.google.gson.Gson
|
|
|
|
+import com.google.gson.GsonBuilder
|
|
|
|
+import com.google.gson.reflect.TypeToken
|
|
|
|
+import com.wdkl.ncs.android.component.home.R
|
|
|
|
+import com.wdkl.ncs.android.component.home.activity.AppUpdateActivity
|
|
|
|
+import com.wdkl.ncs.android.component.home.activity.NewCallListActivity
|
|
|
|
+import com.wdkl.ncs.android.component.home.activity.NewEventListActivity
|
|
|
|
+import com.wdkl.ncs.android.component.home.settingconfig.SettingConfig
|
|
|
|
+import com.wdkl.ncs.android.component.home.util.*
|
|
|
|
+import com.wdkl.ncs.android.lib.base.BaseApplication
|
|
|
|
+import com.wdkl.ncs.android.lib.utils.AppTool
|
|
|
|
+import com.wdkl.ncs.android.lib.utils.getJsonString
|
|
|
|
+import com.wdkl.ncs.android.lib.utils.showMessage
|
|
|
|
+import com.wdkl.ncs.android.lib.vo.MessageEvent
|
|
|
|
+import com.wdkl.ncs.android.middleware.api.ApiManager
|
|
|
|
+import com.wdkl.ncs.android.middleware.common.Constants
|
|
|
|
+import com.wdkl.ncs.android.middleware.model.dos.ChannelImDO
|
|
|
|
+import com.wdkl.ncs.android.middleware.model.dos.PartSettingDO
|
|
|
|
+import com.wdkl.ncs.android.middleware.model.vo.InteractionVO
|
|
|
|
+import com.wdkl.ncs.android.middleware.model.vo.WatchContactVO
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.TcpClient
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.TcpClientHandler
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.ChannelImUtil
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.DeviceChannel
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.ImUtil
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.DeviceTypeEnum
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.RoleTypeEnum
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.TcpType
|
|
|
|
+import com.wdkl.ncs.android.middleware.utils.ContactHelper
|
|
|
|
+import com.wdkl.ncs.keepbackground.work.AbsWorkService
|
|
|
|
+import com.wdkl.rtc.rtc.AudioFocusManager
|
|
|
|
+import org.greenrobot.eventbus.EventBus
|
|
|
|
+import org.greenrobot.eventbus.Subscribe
|
|
|
|
+import org.greenrobot.eventbus.ThreadMode
|
|
|
|
+import java.util.*
|
|
|
|
+import java.util.concurrent.ConcurrentLinkedQueue
|
|
|
|
+
|
|
|
|
+class WdKeepAliveService : AbsWorkService() {
|
|
|
|
+ val TAG = "WdKeepAliveService"
|
|
|
|
+
|
|
|
|
+ companion object instance {
|
|
|
|
+ var updateLastTime: Long = System.currentTimeMillis() / 1000
|
|
|
|
+ var sosQueue: ConcurrentLinkedQueue<TcpModel> = ConcurrentLinkedQueue<TcpModel>()
|
|
|
|
+
|
|
|
|
+ var mNewEventListActive = false
|
|
|
|
+ var instanceCreated = false
|
|
|
|
+ var mNewCallListActive = false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private var mediaSessionCompat: MediaSessionCompat? = null
|
|
|
|
+ private lateinit var audioFocusManager: AudioFocusManager
|
|
|
|
+
|
|
|
|
+ private var mIsRunning = false
|
|
|
|
+ private lateinit var netWorkChangeReceiver: NetWorkChangeReceiver
|
|
|
|
+ lateinit var mVibrator: Vibrator
|
|
|
|
+
|
|
|
|
+ private lateinit var receiver: PhoneStateReceiver
|
|
|
|
+
|
|
|
|
+ //当前播放留言状态: 0-无,1-个人留言,2-群留言
|
|
|
|
+ private var imPlayState = 0
|
|
|
|
+ //群留言列表
|
|
|
|
+ private var channelImList = ArrayList<ChannelImDO>()
|
|
|
|
+ //当前播放的群留言id
|
|
|
|
+ private var channelImId = 0
|
|
|
|
+ private var waitingLoop = false
|
|
|
|
+ private var waitingCall = false
|
|
|
|
+
|
|
|
|
+ private var warningTips = false
|
|
|
|
+ private val keepHandler: Handler = object : Handler(Looper.getMainLooper()) {
|
|
|
|
+ override fun handleMessage(msg: Message) {
|
|
|
|
+ warningTips = false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun startWork() {
|
|
|
|
+ EventBus.getDefault().register(this)
|
|
|
|
+
|
|
|
|
+ //全局调用一次
|
|
|
|
+ Thread {
|
|
|
|
+ Log.d(TAG, "Constants.tcpServer: " + Constants.tcpServer)
|
|
|
|
+
|
|
|
|
+ while (TextUtils.isEmpty(Constants.tcpServer)) {
|
|
|
|
+ Thread.sleep(3000)
|
|
|
|
+ }
|
|
|
|
+ TcpClient.getInstance().init(Constants.tcpServer, Constants.tcpPort, Constants.heartBeat)
|
|
|
|
+ instanceCreated = true
|
|
|
|
+ }.start()
|
|
|
|
+
|
|
|
|
+ // 动态注册广播接收器
|
|
|
|
+ // 过滤器
|
|
|
|
+ val intentFilter = IntentFilter()
|
|
|
|
+ // 系统的网络被更改的过滤器
|
|
|
|
+ intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE")
|
|
|
|
+ netWorkChangeReceiver = NetWorkChangeReceiver()
|
|
|
|
+ registerReceiver(netWorkChangeReceiver, intentFilter)
|
|
|
|
+
|
|
|
|
+ checkNetState()
|
|
|
|
+
|
|
|
|
+ //初始化TTS服务
|
|
|
|
+ SpeechUtil.getInstance().init(BaseApplication.appContext, object : ISpeechCallback {
|
|
|
|
+ override fun initFailed() {
|
|
|
|
+ Log.d(TAG, "TTS init failed")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun initSuccess() {
|
|
|
|
+ Log.d(TAG, "TTS init success")
|
|
|
|
+ SpeechUtil.getInstance().startSpeechThread()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ SoundPoolManager.getInstance().init()
|
|
|
|
+
|
|
|
|
+ //震动
|
|
|
|
+ mVibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
|
|
|
+
|
|
|
|
+ //监听语音通话
|
|
|
|
+ registerCallListener()
|
|
|
|
+
|
|
|
|
+ audioFocusManager = AudioFocusManager(BaseApplication.appContext)
|
|
|
|
+ initMediaSession()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fun registerCallListener() {
|
|
|
|
+ val intentFilter = IntentFilter()
|
|
|
|
+ intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL")
|
|
|
|
+ intentFilter.addAction("android.intent.action.PHONE_STATE")
|
|
|
|
+ receiver = PhoneStateReceiver()
|
|
|
|
+ registerReceiver(receiver, intentFilter)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fun unRegisterCallListener() {
|
|
|
|
+ if (receiver != null) {
|
|
|
|
+ unregisterReceiver(receiver)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun isWorkRunning(): Boolean {
|
|
|
|
+ return mIsRunning
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun needStartWorkService(): Boolean {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun stopWork() {
|
|
|
|
+ //注销监听
|
|
|
|
+ unRegisterCallListener()
|
|
|
|
+ releaseMediaSession()
|
|
|
|
+ keepHandler.removeCallbacksAndMessages(null)
|
|
|
|
+
|
|
|
|
+ SoundPoolManager.getInstance().release()
|
|
|
|
+ SpeechUtil.getInstance().release()
|
|
|
|
+ EventBus.getDefault().unregister(this)
|
|
|
|
+ if (netWorkChangeReceiver != null) {
|
|
|
|
+ try {
|
|
|
|
+ unregisterReceiver(netWorkChangeReceiver)
|
|
|
|
+ } catch (ex: Exception) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ instanceCreated = false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun onBindService(p0: Intent?, p1: Void?): IBinder {
|
|
|
|
+ return Messenger(Handler()).binder
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun onServiceKilled() {
|
|
|
|
+ EventBus.getDefault().unregister(this)
|
|
|
|
+ if (netWorkChangeReceiver != null) {
|
|
|
|
+ try {
|
|
|
|
+ unregisterReceiver(netWorkChangeReceiver)
|
|
|
|
+ } catch (ex: Exception) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ instanceCreated = false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
|
+ fun onMoonEvent(messageEvent: MessageEvent) {
|
|
|
|
+ when (messageEvent.tag) {
|
|
|
|
+ //拨出成功 || 有来电
|
|
|
|
+ 1 -> {
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+
|
|
|
|
+ SpeechUtil.getInstance().stopSpeak()
|
|
|
|
+ MediaPlayHelper.getInstance().releaseMusic();
|
|
|
|
+
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.type == TcpType.VOICE) {
|
|
|
|
+// var gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+
|
|
|
|
+ if (tcpModel.action == TcpAction.VoiceAction.CALL) { //有来电
|
|
|
|
+ //取消掉语音留言录音
|
|
|
|
+ RecordHelper.getInstance().stopCancelRecordByOther(true)
|
|
|
|
+ //停止媒体播放,比如正在播放留言
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
|
+ Log.i(TAG, "来电:" + JSON.toJSONString(interactionVO))
|
|
|
|
+
|
|
|
|
+ //加入呼叫列表
|
|
|
|
+ Constants.newCallList.add(tcpModel)
|
|
|
|
+ //语音播报
|
|
|
|
+ if (!DeviceChannel.calling && !RecordHelper.getInstance().isRecording) {
|
|
|
|
+ val frameName = interactionVO.fromFrameFullName.replace("-", "")
|
|
|
|
+ SpeechUtil.getInstance().speak(frameName + "呼叫," + frameName + "呼叫")
|
|
|
|
+
|
|
|
|
+ //更新界面
|
|
|
|
+ if (mNewCallListActive) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_CALL))
|
|
|
|
+ } else {
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, NewCallListActivity::class.java)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (!waitingCall) {
|
|
|
|
+ Thread {
|
|
|
|
+ while (DeviceChannel.calling || RecordHelper.getInstance().isRecording) {
|
|
|
|
+ waitingCall = true
|
|
|
|
+ Thread.sleep(500)
|
|
|
|
+ }
|
|
|
|
+ waitingCall = false
|
|
|
|
+
|
|
|
|
+ if (Constants.newCallList.size > 0) {
|
|
|
|
+ //更新界面
|
|
|
|
+ if (mNewCallListActive) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_CALL))
|
|
|
|
+ } else {
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, NewCallListActivity::class.java)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }.start()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*DeviceChannel.calling = true
|
|
|
|
+ var roomId: String
|
|
|
|
+ if (interactionVO.fromDeviceType == DeviceTypeEnum.SIMULATE_BED_DEVICE.value()) {
|
|
|
|
+ //模拟分机呼叫,通话由手机端创建
|
|
|
|
+ DeviceChannel.callId = interactionVO.fromDeviceId
|
|
|
|
+ roomId = Constants.sipId!!
|
|
|
|
+ } else {
|
|
|
|
+ if (interactionVO.fromDeviceId.equals(Constants.deviceId)) {
|
|
|
|
+ DeviceChannel.callId = interactionVO.toDeviceId
|
|
|
|
+ roomId = interactionVO.toSipId
|
|
|
|
+ } else {
|
|
|
|
+ DeviceChannel.callId = interactionVO.fromDeviceId
|
|
|
|
+ roomId = interactionVO.fromSipId
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //启动 activity
|
|
|
|
+ val intent = Intent(this, CallSingleActivity::class.java)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_ROOM_ID, roomId)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_MO, false)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_AUDIO_ONLY, true)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_TCPMODEL, tcpModel)
|
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)*/
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*11 -> {
|
|
|
|
+ //取消掉语音留言录音
|
|
|
|
+ RecordHelper.getInstance().stopCancelRecordByOther(true)
|
|
|
|
+
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ SpeechUtil.getInstance().stopSpeak()
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.type == TcpType.VOICE && tcpModel.action == TcpAction.VoiceAction.CALL) {
|
|
|
|
+
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+
|
|
|
|
+ DeviceChannel.calling = true
|
|
|
|
+ var roomId: String
|
|
|
|
+ if (interactionVO.fromDeviceId.equals(Constants.deviceId)) {
|
|
|
|
+ DeviceChannel.callId = interactionVO.toDeviceId
|
|
|
|
+ roomId = interactionVO.toSipId
|
|
|
|
+ } else {
|
|
|
|
+ DeviceChannel.callId = interactionVO.fromDeviceId
|
|
|
|
+ roomId = interactionVO.fromSipId
|
|
|
|
+ }
|
|
|
|
+ Log.i(TAG, "来电:" + JSON.toJSONString(interactionVO))
|
|
|
|
+
|
|
|
|
+ //启动 activity
|
|
|
|
+ val intent = Intent(this, CallSingleActivity::class.java)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_ROOM_ID, roomId)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_MO, false)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_AUDIO_ONLY, true)
|
|
|
|
+ intent.putExtra(CallSingleActivity.EXTRA_TCPMODEL, tcpModel);
|
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
+ startActivity(intent)
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+ Constants.EVENT_NEW_TCP -> {
|
|
|
|
+ //先点亮屏幕,然后再通知到界面
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.getAction() == TcpAction.VoiceAction.SUCCESS) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, 1))
|
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.VoiceAction.CALL) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, 1))
|
|
|
|
+
|
|
|
|
+ /*if (DeviceChannel.calling) {
|
|
|
|
+ Log.d(TAG, "通话中,来电 " + tcpModel.getFromId() + "<>" + DeviceChannel.callId)
|
|
|
|
+ //相同来源,重新建立通话
|
|
|
|
+ if (tcpModel.getFromId() == DeviceChannel.callId) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, 11))
|
|
|
|
+ }
|
|
|
|
+ } else { //得到通话
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, 1))
|
|
|
|
+ }*/
|
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.VoiceAction.ACCEPT //我方呼出,对方接受
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.REJECT //我方呼出,对方拒绝
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.CALLING //我方呼出,对方通话中
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.HANDOFF
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.FAILED //我方呼出,对方不在线,设备离线或其它错误
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.CANCEL
|
|
|
|
+ || tcpModel.getAction() == TcpAction.VoiceAction.VOICE_OFF) {
|
|
|
|
+
|
|
|
|
+ if (tcpModel.getAction() === TcpAction.VoiceAction.CANCEL || tcpModel.action == TcpAction.VoiceAction.VOICE_OFF) {
|
|
|
|
+ //对方取消呼叫或者呼叫已被其他主机处理(接听或拒绝)
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ val iterator = Constants.newCallList.iterator()
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ val it = iterator.next()
|
|
|
|
+ val interaction = Gson().fromJson(it.data.toString(), InteractionVO::class.java)
|
|
|
|
+ if (interaction.id.equals(interactionVO.id)) {
|
|
|
|
+ iterator.remove()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新界面
|
|
|
|
+ if (mNewCallListActive) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_CALL))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, 2))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //事件与语音留言通道
|
|
|
|
+ 3 -> {
|
|
|
|
+ //Util.wakeUpAndUnlock()
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.action == TcpAction.CHANNELIMAction.MSG) {
|
|
|
|
+ val channelData = Gson().fromJson(tcpModel.data.toString(), ChannelImDO::class.java)
|
|
|
|
+ if (!DeviceChannel.calling && !RecordHelper.getInstance().isRecording) {
|
|
|
|
+ //群留言
|
|
|
|
+ playChannelVoiceMsg(channelData)
|
|
|
|
+ } else {
|
|
|
|
+ //通话中或正在留言时有新的群留言进来则将其加入群留言队列中,待留言结束后自动播放
|
|
|
|
+ channelImList.add(channelData)
|
|
|
|
+ if (!waitingLoop) {
|
|
|
|
+ Thread {
|
|
|
|
+ while (DeviceChannel.calling || RecordHelper.getInstance().isRecording) {
|
|
|
|
+ waitingLoop = true
|
|
|
|
+ Thread.sleep(500)
|
|
|
|
+ }
|
|
|
|
+ waitingLoop = false
|
|
|
|
+ if (channelImList.size > 0) {
|
|
|
|
+ startPlayChannelIm(channelImList.get(0))
|
|
|
|
+ }
|
|
|
|
+ }.start()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (tcpModel.action == TcpAction.IMAction.MSG) {
|
|
|
|
+ //如果当前不在通话中则直接播放语音留言,并将该记录置为已读状态或当前正在留言中则等待留言结束再切换到留言事件界面
|
|
|
|
+ if (DeviceChannel.calling || RecordHelper.getInstance().isRecording) {
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ if (Constants.oldEvent) {
|
|
|
|
+ Constants.eventList.clear()
|
|
|
|
+ }
|
|
|
|
+ Constants.oldEvent = false
|
|
|
|
+ Constants.eventList.add(interactionVO)
|
|
|
|
+
|
|
|
|
+ if (!mNewEventListActive) {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ while (DeviceChannel.calling || RecordHelper.getInstance().isRecording) {
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ SoundPoolManager.getInstance().playSound(1)
|
|
|
|
+
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, NewEventListActivity::class.java)
|
|
|
|
+ //intent.putExtra("tcpModelStr", tcpModel.toJson())
|
|
|
|
+ //intent.putExtra("newEvent", true)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }).start()
|
|
|
|
+ } else {
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_EVENT))
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ val interaction = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ playVoiceMsg(interaction)
|
|
|
|
+ val msgTcpModel = ImUtil.imRead(Constants.deviceId, interaction.fromDeviceId, interaction.id)
|
|
|
|
+ TcpClient.getInstance().sendTcp(msgTcpModel, false, null)
|
|
|
|
+
|
|
|
|
+ /*NettyClient.instance.sendMsg(msgTcpModel.toJson()).subscribe {
|
|
|
|
+ if (it) {
|
|
|
|
+ Log.d(TAG, "TCP.发送消息完成")
|
|
|
|
+ } else {
|
|
|
|
+ Log.e(TAG, "TCP.发送消息失败")
|
|
|
|
+ HandleTcpConnect.instance.tcpReConnectWithMsgShow()
|
|
|
|
+ }
|
|
|
|
+ }*/
|
|
|
|
+ }
|
|
|
|
+ } else if (tcpModel.action == TcpAction.EventAction.KEY_CLICK) { // 收到新事件
|
|
|
|
+ //取消掉语音留言录音
|
|
|
|
+ RecordHelper.getInstance().stopCancelRecordByOther(true)
|
|
|
|
+
|
|
|
|
+ //语音播报处理
|
|
|
|
+ eventSpeechOut(tcpModel)
|
|
|
|
+ //震动提醒
|
|
|
|
+ handleVibrator()
|
|
|
|
+
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ if (Constants.oldEvent) {
|
|
|
|
+ Constants.eventList.clear()
|
|
|
|
+ }
|
|
|
|
+ Constants.oldEvent = false
|
|
|
|
+ Constants.eventList.add(interactionVO)
|
|
|
|
+
|
|
|
|
+ if (!mNewEventListActive) {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ while (DeviceChannel.calling) {
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, NewEventListActivity::class.java)
|
|
|
|
+ //intent.putExtra("tcpModelStr", tcpModel.toJson())
|
|
|
|
+ //intent.putExtra("newEvent", true)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }).start()
|
|
|
|
+ } else {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_EVENT))
|
|
|
|
+ }
|
|
|
|
+ } else if (tcpModel.action == TcpAction.EventAction.CANCEL || tcpModel.action == TcpAction.EventAction.COMPLETED) { // 收到事件取消 或事件完成
|
|
|
|
+ //语音播报处理
|
|
|
|
+ eventSpeechOut(tcpModel)
|
|
|
|
+ //震动提醒
|
|
|
|
+ handleVibrator()
|
|
|
|
+ //消除事件
|
|
|
|
+ val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ var iterator = Constants.eventList.iterator()
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ val it = iterator.next()
|
|
|
|
+ if (it.id.equals(interactionVO.id)) {
|
|
|
|
+ iterator.remove()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (mNewEventListActive) {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(tcpModel, Constants.EVENT_UPDATE_EVENT))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 4 -> { //更新通讯录,更新通话白名单列表
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ try {
|
|
|
|
+ var jsonStr = ""
|
|
|
|
+ val contactVos = ApiManager.API_WatchManageDevice.getWatchContactList(Constants.deviceId).map {
|
|
|
|
+ jsonStr = it.getJsonString()
|
|
|
|
+ val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
|
|
|
|
+ val contactList: List<WatchContactVO> = gson.fromJson(jsonStr, object : TypeToken<List<WatchContactVO>>() {}.type)
|
|
|
|
+ return@map contactList
|
|
|
|
+ }.blockingSingle()
|
|
|
|
+
|
|
|
|
+ if (contactVos != null && contactVos.size > 0) {
|
|
|
|
+ for (contactVO in contactVos) {
|
|
|
|
+ if (!Strings.isNullOrEmpty(contactVO.name) && !Strings.isNullOrEmpty(contactVO.phoneNumber)) {
|
|
|
|
+ ContactHelper.setContact(this, contactVO.name, contactVO.phoneNumber)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新白名单列表
|
|
|
|
+ val whiteList = ApiManager.API_WatchManageDevice.getPhoneWhiteList(Constants.deviceId).map {
|
|
|
|
+ jsonStr = it.getJsonString()
|
|
|
|
+ val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
|
|
|
|
+ val whiteList: List<String> = gson.fromJson(jsonStr, object : TypeToken<List<String>>() {}.type)
|
|
|
|
+ return@map whiteList
|
|
|
|
+ }.blockingSingle()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (whiteList != null && whiteList.size > 0) {
|
|
|
|
+ Constants.phoneWhiteList.clear()
|
|
|
|
+ Constants.phoneWhiteList.addAll(whiteList)
|
|
|
|
+ }
|
|
|
|
+ } catch (ex: Exception) {
|
|
|
|
+ ex.printStackTrace()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }).start()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //SOS通道
|
|
|
|
+ 999 -> {
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ val sosTcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ val sosInteractionVO = Gson().fromJson(sosTcpModel.data.toString(), InteractionVO::class.java)
|
|
|
|
+ if (sosTcpModel.action === TcpAction.SOSAction.CANCEL) {
|
|
|
|
+ //先停止铃声或其他音频播放
|
|
|
|
+ SpeechUtil.getInstance().stopSpeak()
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
|
+
|
|
|
|
+ if (mVibrator != null) {
|
|
|
|
+ mVibrator.cancel()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!Strings.isNullOrEmpty(sosInteractionVO.toRoleName)) {
|
|
|
|
+ showMessage(sosInteractionVO.toRoleName + " " + sosInteractionVO.toMemberName + " 已响应")
|
|
|
|
+ } else {
|
|
|
|
+ showMessage("已响应")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var iterator = Constants.eventList.iterator()
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ val it = iterator.next()
|
|
|
|
+ if (it.id == sosInteractionVO.id) {
|
|
|
|
+ Constants.eventList.remove(it)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(sosTcpModel, Constants.EVENT_UPDATE_EVENT))
|
|
|
|
+
|
|
|
|
+ } else if (sosTcpModel.action === TcpAction.SOSAction.CALL) {
|
|
|
|
+ //取消掉语音留言录音
|
|
|
|
+ RecordHelper.getInstance().stopCancelRecordByOther(true)
|
|
|
|
+
|
|
|
|
+ //紧急呼叫去重
|
|
|
|
+ for (e in Constants.eventList) {
|
|
|
|
+ if (e.id == sosInteractionVO.id) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (Constants.oldEvent) {
|
|
|
|
+ Constants.eventList.clear()
|
|
|
|
+ }
|
|
|
|
+ Constants.oldEvent = false
|
|
|
|
+ Constants.eventList.add(sosInteractionVO)
|
|
|
|
+ //语音播报处理
|
|
|
|
+ eventSpeechOut(sosTcpModel)
|
|
|
|
+ //震动提醒
|
|
|
|
+ handleVibrator()
|
|
|
|
+
|
|
|
|
+ if (!mNewEventListActive) {
|
|
|
|
+ Thread {
|
|
|
|
+ while (DeviceChannel.calling) {
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (sosTcpModel.action == TcpAction.SOSAction.CALL) {
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, NewEventListActivity::class.java)
|
|
|
|
+ //intent.putExtra("tcpModelStr", tcpModelIn.toJson())
|
|
|
|
+ //intent.putExtra("newEvent", true)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }
|
|
|
|
+ }.start()
|
|
|
|
+ } else {
|
|
|
|
+ EventBus.getDefault().post(MessageEvent(sosTcpModel, Constants.EVENT_UPDATE_EVENT))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //用户切换
|
|
|
|
+ Constants.EVENT_USER_CHANGE -> {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ while (DeviceChannel.calling) { //通话中不处理,一直等待至结束
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+ DeviceChannel.calling = true //置于通话状态中,不可呼叫
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ AppUtils.restartApp()
|
|
|
|
+ }).start()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //设备切换
|
|
|
|
+ Constants.EVENT_DEVICE_CHANGE -> {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ while (DeviceChannel.calling) { //通话中不处理,一直等待至结束
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ DeviceChannel.calling = true //置于通话状态中,不可呼叫
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.action == TcpAction.DeviceAction.RESTART) {
|
|
|
|
+ AppUtils.restartApp()
|
|
|
|
+ }
|
|
|
|
+ }).start()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //系统设置
|
|
|
|
+ Constants.EVENT_DEVICE_SETTING -> {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ var jsonStr = ""
|
|
|
|
+ val partSettingDO = ApiManager.API_WatchDeviceInfoAndTCPMailingAddress.getDeviceSettingData(Constants.partId).map {
|
|
|
|
+ jsonStr = it.getJsonString()
|
|
|
|
+ val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
|
|
|
|
+ val partSettingDO = gson.fromJson(jsonStr, PartSettingDO::class.java)
|
|
|
|
+ return@map partSettingDO
|
|
|
|
+ }.blockingSingle()
|
|
|
|
+
|
|
|
|
+ Log.i(TAG, "收到系统设置 " + jsonStr)
|
|
|
|
+ if (partSettingDO != null) {
|
|
|
|
+ if (partSettingDO.transferDuration != null && partSettingDO.transferDurationLeader != null) {
|
|
|
|
+ if (Constants.roleId != null) {
|
|
|
|
+ if (Constants.roleId == RoleTypeEnum.NURSE_HEAD.value()) {
|
|
|
|
+ //护士组长
|
|
|
|
+ SettingConfig.setCountdownTime(application, partSettingDO.transferDurationLeader)
|
|
|
|
+ } else if (Constants.roleId == RoleTypeEnum.NURSE.value()) {
|
|
|
|
+ //护士
|
|
|
|
+ SettingConfig.setCountdownTime(application, partSettingDO.transferDuration)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }).start()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //APP升级
|
|
|
|
+ Constants.EVENT_APP_UPDATE -> {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ while (DeviceChannel.calling) { //通话中不处理,一直等待至结束
|
|
|
|
+ Thread.sleep(400)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //DeviceChannel.calling = true //置于通话状态中,不可呼叫
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel
|
|
|
|
+ if (tcpModel.action == TcpAction.DeviceAction.APP_UPDATE) {
|
|
|
|
+ if ((System.currentTimeMillis() / 1000) - updateLastTime > 10 && !AppUpdateActivity.opened) { //大于10秒可继续升级
|
|
|
|
+ updateLastTime = System.currentTimeMillis() / 1000
|
|
|
|
+ val intent = Intent()
|
|
|
|
+ intent.setClass(this, AppUpdateActivity::class.java)
|
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
+ startActivity(intent)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }).start()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //网络断开
|
|
|
|
+ Constants.EVENT_TCP_BREAK -> {
|
|
|
|
+ Log.w(TAG, "EVENT TCP BREAK")
|
|
|
|
+ //MediaPlayHelper.getInstance().playResMusic(R.raw.tcp_disconnect, 1.0f, false)
|
|
|
|
+ //RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.tcp_disconnect, false)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //留言播放完成
|
|
|
|
+ Constants.EVENT_IM_PLAY_DONE -> {
|
|
|
|
+ //将播放完成的群留言从列表中删除
|
|
|
|
+ val iterator = channelImList.iterator()
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ val it = iterator.next()
|
|
|
|
+ if (it.id.equals(channelImId)) {
|
|
|
|
+ iterator.remove()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //群留言列表还有留言则继续播放
|
|
|
|
+ if (channelImList.size > 0) {
|
|
|
|
+ imPlayState = 2
|
|
|
|
+ channelImId = channelImList.get(0).id
|
|
|
|
+ sendChannelImRead(channelImList.get(0))
|
|
|
|
+ MediaPlayHelper.getInstance().playUrlMusic(ApiManager.urlManager.device_url + channelImList.get(0).audioPath, 1f, false)
|
|
|
|
+ } else {
|
|
|
|
+ imPlayState = 0
|
|
|
|
+ }
|
|
|
|
+ ImPlayDialogHelper.dismissIMDialog()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //电话时停止播放留言并清除群留言列表
|
|
|
|
+ Constants.EVENT_CLEAR_IM -> {
|
|
|
|
+ val stop = messageEvent.getMessage() as Boolean
|
|
|
|
+ channelImList.clear()
|
|
|
|
+ if (stop) {
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ }
|
|
|
|
+ imPlayState = 0
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun eventSpeechOut(model: TcpModel) {
|
|
|
|
+ if (model.type == TcpType.IM && model.action == TcpAction.IMAction.MSG) {
|
|
|
|
+ SoundPoolManager.getInstance().playSound(1)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //tts未初始化或者初始化不成功时重新初始化
|
|
|
|
+ if (SpeechUtil.getInstance().textToSpeech == null || Constants.ttsState != 2) {
|
|
|
|
+ initSpeech()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //先停止铃声或其他音频播放
|
|
|
|
+ SpeechUtil.getInstance().stopSpeak()
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ RingPlayHelper.stopRingTone()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //播报语音提醒
|
|
|
|
+ val interactionVO = Gson().fromJson<InteractionVO>(model.data.toString(), InteractionVO::class.java)
|
|
|
|
+ if (model.type == TcpType.EVENT && model.action == TcpAction.EventAction.KEY_CLICK) {
|
|
|
|
+ val eventStr = Util.appendSpace(interactionVO.fromFrameFullName.replace("-", ",")) + ", " + interactionVO.data
|
|
|
|
+
|
|
|
|
+ if (Constants.ttsState == 2) {
|
|
|
|
+ SpeechUtil.getInstance().speak("您有新的事件待处理, " + eventStr)
|
|
|
|
+ } else {
|
|
|
|
+ //MediaPlayHelper.getInstance().playResMusic(R.raw.new_event, 1.0f, false)
|
|
|
|
+ RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.new_event, false)
|
|
|
|
+ }
|
|
|
|
+ } else if (model.type == TcpType.SOS && model.action == TcpAction.SOSAction.CALL) {
|
|
|
|
+ AppTool.Time.delay(500) {
|
|
|
|
+ //MediaPlayHelper.getInstance().playResMusic(R.raw.sos, 1.0f, true)
|
|
|
|
+ RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.sos, true)
|
|
|
|
+ }
|
|
|
|
+ } else if (model.type == TcpType.EVENT && model.action == TcpAction.EventAction.CANCEL) { // 事件取消
|
|
|
|
+ val eventStr = Util.appendSpace(interactionVO.fromFrameFullName.replace("-", ",")) + ",已取消 " + interactionVO.data + "事件"
|
|
|
|
+
|
|
|
|
+ if (Constants.ttsState == 2) {
|
|
|
|
+ SpeechUtil.getInstance().speak(eventStr)
|
|
|
|
+ } else {
|
|
|
|
+ AppTool.Time.delay(500) {
|
|
|
|
+ //MediaPlayHelper.getInstance().playResMusic(R.raw.new_event, 1.0f, false)
|
|
|
|
+ RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.new_event, false)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (model.type == TcpType.EVENT && model.action == TcpAction.EventAction.COMPLETED) { //事件完成
|
|
|
|
+ val eventStr = Util.appendSpace(interactionVO.fromFrameFullName.replace("-", ",")) + ", " + interactionVO.data + "事件已完成"
|
|
|
|
+
|
|
|
|
+ if (Constants.ttsState == 2) {
|
|
|
|
+ SpeechUtil.getInstance().speak(eventStr)
|
|
|
|
+ } else {
|
|
|
|
+ AppTool.Time.delay(500) {
|
|
|
|
+ //MediaPlayHelper.getInstance().playResMusic(R.raw.new_event, 1.0f, false)
|
|
|
|
+ RingPlayHelper.playRingTone(BaseApplication.appContext, R.raw.new_event, false)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun handleVibrator() {
|
|
|
|
+ /*mVibrator.cancel()
|
|
|
|
+ //开启振动后 等待0.1s振动 振动2s 等待1s 振动2s 等待1s
|
|
|
|
+ val pattern = longArrayOf(100, 2000, 1000, 2000)
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
+ mVibrator.vibrate(VibrationEffect.createWaveform(pattern, -1))
|
|
|
|
+ } else {
|
|
|
|
+ mVibrator.vibrate(pattern, -1)
|
|
|
|
+ }*/
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun playVoiceMsg(interactionVO: InteractionVO) {
|
|
|
|
+ synchronized(this) {
|
|
|
|
+ channelImList.clear()
|
|
|
|
+ showMessage("播放留言...")
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ SoundPoolManager.getInstance().playSound(1)
|
|
|
|
+ AppTool.Time.delay(1000) {
|
|
|
|
+ imPlayState = 1
|
|
|
|
+ MediaPlayHelper.getInstance().playUrlMusic(ApiManager.urlManager.device_url + interactionVO.data, 1f, false)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun playChannelVoiceMsg(channelImDO: ChannelImDO) {
|
|
|
|
+ synchronized(this) {
|
|
|
|
+ //如果当前正在播放留言则将新的群留言加入待播放留言列表
|
|
|
|
+ if ((imPlayState == 2 || imPlayState == 1) /*&& MediaPlayHelper.getInstance().isMediaPlaying()*/) {
|
|
|
|
+ channelImList.add(channelImDO)
|
|
|
|
+ } else {
|
|
|
|
+ startPlayChannelIm(channelImDO)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun startPlayChannelIm(channelIm: ChannelImDO) {
|
|
|
|
+ imPlayState = 2
|
|
|
|
+ showMessage("播放群留言...")
|
|
|
|
+ MediaPlayHelper.getInstance().stopMusic(true)
|
|
|
|
+ SoundPoolManager.getInstance().playSound(4)
|
|
|
|
+ AppTool.Time.delay(1000) {
|
|
|
|
+ imPlayState = 2
|
|
|
|
+ channelImId = channelIm.id
|
|
|
|
+ sendChannelImRead(channelIm)
|
|
|
|
+ MediaPlayHelper.getInstance().playUrlMusic(ApiManager.urlManager.device_url + channelIm.audioPath, 1f, false)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressLint("CheckResult")
|
|
|
|
+ private fun sendChannelImRead(channelIm: ChannelImDO) {
|
|
|
|
+ val imTcpModel = ChannelImUtil.channelImRead(Constants.deviceId, channelIm)
|
|
|
|
+ TcpClient.getInstance().sendTcp(imTcpModel, false, null)
|
|
|
|
+
|
|
|
|
+ /*NettyClient.instance.sendMsg(imTcpModel.toJson()).subscribe {
|
|
|
|
+ if (it) {
|
|
|
|
+ Log.d(TAG, "TCP.发送消息完成")
|
|
|
|
+ } else {
|
|
|
|
+ Log.e(TAG, "TCP.发送消息失败")
|
|
|
|
+ HandleTcpConnect.instance.tcpReConnectWithMsgShow()
|
|
|
|
+ }
|
|
|
|
+ }*/
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun initSpeech() {
|
|
|
|
+ val speechCallback = object : ISpeechCallback {
|
|
|
|
+ override fun initFailed() {
|
|
|
|
+ //重新加载一次
|
|
|
|
+ SpeechUtil.getInstance().release()
|
|
|
|
+ SpeechUtil.getInstance().init(BaseApplication.appContext, object : ISpeechCallback {
|
|
|
|
+ override fun initFailed() {
|
|
|
|
+ //
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun initSuccess() {
|
|
|
|
+ SpeechUtil.getInstance().startSpeechThread()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun initSuccess() {
|
|
|
|
+ SpeechUtil.getInstance().startSpeechThread()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ SpeechUtil.getInstance().release()
|
|
|
|
+ //TTS初始化
|
|
|
|
+ SpeechUtil.getInstance().init(BaseApplication.appContext, speechCallback)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var checkNetStateTimer: Timer? = null
|
|
|
|
+ var checkNetStateTimerTask: TimerTask? = null
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 重连wdklRTC
|
|
|
|
+ */
|
|
|
|
+ private fun checkNetState() {
|
|
|
|
+ if (!mIsRunning) {
|
|
|
|
+ mIsRunning = true
|
|
|
|
+ if (checkNetStateTimer != null) checkNetStateTimer!!.purge()
|
|
|
|
+ if (checkNetStateTimerTask != null) checkNetStateTimerTask!!.cancel()
|
|
|
|
+ checkNetStateTimer = Timer()
|
|
|
|
+ checkNetStateTimerTask = object : TimerTask() {
|
|
|
|
+ override fun run() {
|
|
|
|
+ if (!EventBus.getDefault().isRegistered(this@WdKeepAliveService)) {
|
|
|
|
+ EventBus.getDefault().register(this@WdKeepAliveService)
|
|
|
|
+ }
|
|
|
|
+ Util.getCpuWakeLock(this@WdKeepAliveService)?.acquire(Constants.heartBeat * 1000L)
|
|
|
|
+ reConnect()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ checkNetStateTimer!!.schedule(checkNetStateTimerTask, 8000, Constants.heartBeat * 1000L)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fun reConnect() {
|
|
|
|
+ if (NetHelper.getInstance().getNetworkState(this@WdKeepAliveService) != NetHelper.NETWORK_NONE) {
|
|
|
|
+ if (!TcpClientHandler.getConnected()) {
|
|
|
|
+ TcpClient.getInstance().doConnect()
|
|
|
|
+ }
|
|
|
|
+ } /*else {
|
|
|
|
+ Thread(Runnable {
|
|
|
|
+ Thread.sleep(5000)
|
|
|
|
+ reConnect()
|
|
|
|
+ }).start()
|
|
|
|
+ }*/
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //耳机线控监听
|
|
|
|
+ private fun initMediaSession() {
|
|
|
|
+ if (mediaSessionCompat != null) {
|
|
|
|
+ releaseMediaSession()
|
|
|
|
+ }
|
|
|
|
+ audioFocusManager.requestFocus()
|
|
|
|
+ mediaSessionCompat = MediaSessionCompat(BaseApplication.appContext, "MyMediaSession")
|
|
|
|
+ mediaSessionCompat!!.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS)
|
|
|
|
+ mediaSessionCompat!!.setActive(true)
|
|
|
|
+ mediaSessionCompat!!.setCallback(object : MediaSessionCompat.Callback() {
|
|
|
|
+ override fun onMediaButtonEvent(intent: Intent): Boolean {
|
|
|
|
+ val action = intent.action
|
|
|
|
+ if (action != null) {
|
|
|
|
+ if (action.equals(Intent.ACTION_MEDIA_BUTTON)) {
|
|
|
|
+ val keyEvent = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
|
|
|
|
+ if (keyEvent!!.action == KeyEvent.ACTION_DOWN) {
|
|
|
|
+ val keyCode = keyEvent.keyCode
|
|
|
|
+ Log.i("wdkeepalive", "mediasession keycode $keyCode")
|
|
|
|
+ when (keyCode) {
|
|
|
|
+ KeyEvent.KEYCODE_HEADSETHOOK -> {
|
|
|
|
+ //Log.w("keepalive","headset hook...")
|
|
|
|
+ //showMessage("耳机按键按下")
|
|
|
|
+ //SoundPoolManager.getInstance().playSound(3)
|
|
|
|
+
|
|
|
|
+ //如果有呼叫或通话中按键则点亮屏幕并通知到通话界面
|
|
|
|
+ if (DeviceChannel.calling) {
|
|
|
|
+ Util.wakeUpAndUnlock()
|
|
|
|
+ EventBus.getDefault().post(MessageEvent("headset_hook", Constants.EVENT_HEADSET_HOOK))
|
|
|
|
+ }
|
|
|
|
+ //通话中或者某些特定界面不响应耳机按键留言,未绑定用户时也不响应按键
|
|
|
|
+ else if (Constants.channelId == -1) {
|
|
|
|
+ showMessage("请先建立频道")
|
|
|
|
+ } else if (!DeviceChannel.calling && Constants.allowVoiceMsg && !TextUtils.isEmpty(Constants.userName)) {
|
|
|
|
+ if (Constants.deviceType != 1) {
|
|
|
|
+ RecordHelper.getInstance().execRecordVoiceMsg()
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ //在某些特定界面不支持按键留言,如果按键的时候正在留言则停止录音并发送留言
|
|
|
|
+ if (RecordHelper.getInstance().isRecording) {
|
|
|
|
+ RecordHelper.getInstance().stopCancelRecordByOther(false)
|
|
|
|
+ IMDialogHelper.dismissIMDialog()
|
|
|
|
+ } else {
|
|
|
|
+ //当前不能按键留言
|
|
|
|
+ if (!warningTips) {
|
|
|
|
+ warningTips = true
|
|
|
|
+ keepHandler.sendEmptyMessageDelayed(0, 3000)
|
|
|
|
+ SoundPoolManager.getInstance().playSound(5)
|
|
|
|
+ }
|
|
|
|
+ showMessage("请使用屏幕按钮留言")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return super.onMediaButtonEvent(intent)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun releaseMediaSession() {
|
|
|
|
+ if (mediaSessionCompat != null) {
|
|
|
|
+ mediaSessionCompat!!.setCallback(null)
|
|
|
|
+ mediaSessionCompat!!.setActive(false)
|
|
|
|
+ mediaSessionCompat!!.release()
|
|
|
|
+ mediaSessionCompat = null
|
|
|
|
+ }
|
|
|
|
+ if (audioFocusManager != null) {
|
|
|
|
+ audioFocusManager.releaseAudioFocus()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|