Browse Source

<增加手柄通话>

weizhengliang 4 years ago
parent
commit
788d7ce66e

+ 22 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constants.kt

@@ -36,6 +36,28 @@ class Constants {
 
         //刷新呼叫记录
         val EVENT_REFRESH_CALL_LIST = 0x04
+
+        //手柄拿起
+        val HOOK_OFF = "com.android.PhoneWinowManager.HOOK_OFF"
+        //手柄放下
+        val HOOK_ON = "com.android.PhoneWinowManager.HOOK_ON"
+        //手柄状态: 默认放下
+        var hookOn: Boolean = true
+
+        //待机
+        val CALL_STANDBY = 0
+
+        //呼叫中
+        val CALL_OUTGOING = 1
+
+        //来电中
+        val CALL_INCOMING = 2
+
+        //通话中
+        val CALL_CALLING = 3
+
+        //通话状态
+        var CALL_STATE = CALL_STANDBY
     }
 
 

+ 42 - 1
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/activity/NurseHomeActivity.kt

@@ -177,6 +177,9 @@ class NurseHomeActivity  : BaseActivity<NurseHomeActivityPresenter, ActivityNurs
         intentFilter.addAction(Intent.ACTION_TIME_TICK)
         intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED)
         intentFilter.addAction(Intent.ACTION_TIME_CHANGED)
+        //手柄状态监听
+        intentFilter.addAction(Constants.HOOK_OFF)
+        intentFilter.addAction(Constants.HOOK_ON)
         registerReceiver(receiver, intentFilter)
     }
 
@@ -267,7 +270,10 @@ class NurseHomeActivity  : BaseActivity<NurseHomeActivityPresenter, ActivityNurs
      *页面销毁回调
      */
     override fun destory() {
+        Constants.CALL_STATE = Constants.CALL_STANDBY
         countDownTimer.cancel()
+        unregisterReceiver(receiver)
+        SocketManager.getInstance().unConnect()
     }
     /**
      *处理错误信息
@@ -576,6 +582,33 @@ fun call(tyte:Int){
                 } else {
                     view_title_layout_tv_point.setBackgroundResource(R.color.red_color)
                 }
+            } else if (intent.action == Constants.HOOK_ON) {
+                Log.e(TAG,"手柄放下 ")
+                Constants.hookOn = true
+                if (Constants.CALL_STATE == Constants.CALL_OUTGOING) {
+                    //呼出取消
+                    Constants.CALL_STATE = Constants.CALL_STANDBY
+                    DeviceChannel.calling = false
+                    VoiceUtil.cancelAudioCall(Integer.parseInt(Constants.ids), callTargetId)
+                    CallDialogHelper.dismissCallDialog()
+                    countDownTimer.cancel()
+                } else if (Constants.CALL_STATE == Constants.CALL_INCOMING) {
+                    //来电拒接
+                    Constants.CALL_STATE = Constants.CALL_STANDBY
+                    DeviceChannel.calling = false
+                    VoiceUtil.rejectAudioCall(Integer.parseInt(Constants.ids), Constants.fromId, Constants.interactionId)
+                    CallDialogHelper.dismissCallDialog()
+                }
+            } else if (intent.action == Constants.HOOK_OFF) {
+                Log.e(TAG,"手柄拿起 ")
+                Constants.hookOn = false
+                if (Constants.CALL_STATE == Constants.CALL_INCOMING) {
+                    //来电接听
+                    Constants.CALL_STATE = Constants.CALL_CALLING
+                    DeviceChannel.calling = true
+                    VoiceUtil.acceptAudioCall(Integer.parseInt(Constants.ids), Constants.fromId, Constants.interactionId)
+                    CallDialogHelper.dismissCallDialog()
+                }
             }
         }
     }
@@ -657,6 +690,7 @@ fun call(tyte:Int){
                     if (tcpModel.getAction() == TcpAction.VoiceAction.CALL) {//收到新的呼叫
                         Constants.fromId = tcpModel.fromId
                         Constants.interactionId = interactionVO.id
+                        Constants.CALL_STATE = Constants.CALL_INCOMING
 
                         CallDialogHelper.dismissCallDialog()
                         CallDialogHelper.showCallDialog(this@NurseHomeActivity, 1, interactionVO.fromFrameFullName, View.OnClickListener {
@@ -682,42 +716,49 @@ fun call(tyte:Int){
                         countDownTimer.cancel()
 
                         DeviceChannel.calling = true
+                        Constants.CALL_STATE = Constants.CALL_CALLING
                         if (Constants.call_type == 1) {
                             CallSingleActivity.openActivity(activity, interactionVO.toSipId, true, interactionVO.toSipId, false, false)
                         } else {
                             CallSingleActivity.openActivity(activity, interactionVO.toSipId, true, interactionVO.toSipId, true, false)
                         }
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.REJECT) {//对方拒绝
+                        Constants.CALL_STATE = Constants.CALL_STANDBY
                         DeviceChannel.calling = false
                         CallDialogHelper.dismissCallDialog()
                         countDownTimer.cancel()
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.CANCEL) {//对方取消
+                        Constants.CALL_STATE = Constants.CALL_STANDBY
                         DeviceChannel.calling = false
                         CallDialogHelper.dismissCallDialog()
                         countDownTimer.cancel()
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.CALLING) {//对方通话中
                         showMessage("对方忙线中")
+                        Constants.CALL_STATE = Constants.CALL_STANDBY
                         DeviceChannel.calling = false
                         CallDialogHelper.dismissCallDialog()
                         countDownTimer.cancel()
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.FAILED) {//对方不在线 呼叫失败
                         showMessage("对方离线或不存在,呼叫失败")
                         DeviceChannel.calling = false
+                        Constants.CALL_STATE = Constants.CALL_STANDBY
                         CallDialogHelper.dismissCallDialog()
                         countDownTimer.cancel()
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.HANDOFF) {//对方挂断
+                        Constants.CALL_STATE = Constants.CALL_STANDBY
                         DeviceChannel.calling = false
                         CallDialogHelper.dismissCallDialog()
                         countDownTimer.cancel()
                     } else if (tcpModel.getAction() == TcpAction.VoiceAction.SUCCESS) {//服务器返回的呼叫成功tcp
                         callTargetId = interactionVO.toDeviceId
                         countDownTimer.start()
+                        Constants.CALL_STATE = Constants.CALL_OUTGOING
 
                         CallDialogHelper.dismissCallDialog()
                         CallDialogHelper.showCallDialog(this@NurseHomeActivity, 0, "", View.OnClickListener {
                             //呼出取消
                             DeviceChannel.calling = false
-                            VoiceUtil.cancelAudioCall(Integer.parseInt(Constants.ids), interactionVO.toDeviceId)
+                            VoiceUtil.cancelAudioCall(Integer.parseInt(Constants.ids), callTargetId)
                             CallDialogHelper.dismissCallDialog()
                             countDownTimer.cancel()
                         }, View.OnClickListener {

+ 4 - 3
rtc-chat/src/main/java/com/wdkl/skywebrtc/engine/webrtc/WebRTCEngine.java

@@ -125,8 +125,8 @@ public class WebRTCEngine implements IEngine, Peer.IPeerEvent {
             mCallback.joinRoomSucc();
         }
 
-        toggleSpeaker(true);
-        audioManager.setMode(AudioManager.MODE_IN_CALL);
+        //toggleSpeaker(true);
+        //audioManager.setMode(AudioManager.MODE_IN_CALL);
     }
 
     @Override
@@ -359,8 +359,9 @@ public class WebRTCEngine implements IEngine, Peer.IPeerEvent {
     public boolean toggleSpeaker(boolean enable) {
         if (audioManager != null) {
             isSpeakerOn = enable;
-            audioManager.setMode(AudioManager.MODE_IN_CALL);
             if (enable) {
+                //免提
+                audioManager.setMode(AudioManager.MODE_NORMAL);
                 audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                         audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
                         AudioManager.FX_KEY_CLICK);

+ 51 - 1
webrtc/src/main/java/com/wdkl/core/voip/CallSingleActivity.java

@@ -3,9 +3,12 @@ package com.wdkl.core.voip;
 import android.Manifest;
 import android.annotation.TargetApi;
 import android.app.Activity;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.graphics.Color;
+import android.media.AudioManager;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
@@ -59,6 +62,8 @@ public class CallSingleActivity extends BaseActivity implements CallSession.Call
     private SingleCallFragment currentFragment;
     private String room;
 
+    private BroadcastReceiver receiver;
+
     public static Intent getCallIntent(Context context, String targetId, boolean isOutgoing, String inviteUserName,
                                        boolean isAudioOnly, boolean isClearTop) {
         Intent voip = new Intent(context, CallSingleActivity.class);
@@ -105,6 +110,43 @@ public class CallSingleActivity extends BaseActivity implements CallSession.Call
 
         init(targetId, isOutgoing, isAudioOnly);
 
+        //注册手柄监听广播
+        receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                if (intent != null) {
+                    if (Constants.Companion.getHOOK_ON().equals(intent.getAction())) {
+                        //手柄放下
+                        Constants.Companion.setHookOn(true);
+                        CallSession session = gEngineKit.getCurrentSession();
+                        if (session != null) {
+                            SkyEngineKit.Instance().endCall();
+                        }
+                        switchAudioMode(true);
+                        finish();
+                    } else if (Constants.Companion.getHOOK_OFF().equals(intent.getAction())) {
+                        //手柄拿起
+                        Constants.Companion.setHookOn(false);
+                        switchAudioMode(false);
+                    }
+                }
+            }
+        };
+
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Constants.Companion.getHOOK_OFF());
+        filter.addAction(Constants.Companion.getHOOK_ON());
+        registerReceiver(receiver, filter);
+    }
+
+    private void switchAudioMode(boolean speakerOn) {
+        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setSpeakerphoneOn(speakerOn);
+        if (speakerOn) {
+            audioManager.setMode(AudioManager.MODE_NORMAL);
+        } else {
+            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+        }
     }
 
     @Override
@@ -147,7 +189,7 @@ public class CallSingleActivity extends BaseActivity implements CallSession.Call
                 finish();
             } else {
                 session.setSessionCallback(this);
-                session.toggleSpeaker(true);
+                //session.toggleSpeaker(true);
             }
         } else {
             CallSession session = gEngineKit.getCurrentSession();
@@ -158,6 +200,12 @@ public class CallSingleActivity extends BaseActivity implements CallSession.Call
             }
         }
 
+        if (Constants.Companion.getHookOn()) {
+            switchAudioMode(true);
+        } else {
+            switchAudioMode(false);
+        }
+
     }
 
 
@@ -294,6 +342,8 @@ public class CallSingleActivity extends BaseActivity implements CallSession.Call
     @Override
     protected void onDestroy() {
         super.onDestroy();
+        unregisterReceiver(receiver);
+        Constants.Companion.setCALL_STATE(Constants.Companion.getCALL_STANDBY());
         DeviceChannel.calling = false;
         VoiceUtil.handoffAudioCall(Integer.parseInt(Constants.Companion.getIds()), Constants.Companion.getFromId(), Constants.Companion.getInteractionId());
         EventBus.getDefault().post(new MessageEvent(0, Constants.Companion.getEVENT_REFRESH_CALL_LIST()));

+ 1 - 1
webrtc/src/main/java/com/wdkl/core/voip/FragmentAudio.java

@@ -76,7 +76,7 @@ public class FragmentAudio extends SingleCallFragment implements View.OnClickLis
 
                 if (currentSession.getState() == CallState.Incoming) {
                     currentSession.joinHome(currentSession.getRoomId());
-                    currentSession.toggleSpeaker(true);
+                    //currentSession.toggleSpeaker(true);
                 }
             }
         }

+ 1 - 1
webrtc/src/main/java/com/wdkl/core/voip/FragmentVideo.java

@@ -143,7 +143,7 @@ public class FragmentVideo extends SingleCallFragment implements View.OnClickLis
                     }
 
                     session.joinHome(session.getRoomId());
-                    session.toggleSpeaker(true);
+                    //session.toggleSpeaker(true);
                 }
             }
         }