319 lines
9.5 KiB
Java
319 lines
9.5 KiB
Java
|
|
package com.tianrun.sipcall;
|
|||
|
|
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.os.Bundle;
|
|||
|
|
|
|||
|
|
import com.tianrun.sipcall.call.CallMediaUtils;
|
|||
|
|
import com.tianrun.sipcall.call.InCallActivity;
|
|||
|
|
import com.tianrun.sipcall.call.InCallMeetingActivity;
|
|||
|
|
import com.tianrun.sipcall.call.utils.InCallUtils;
|
|||
|
|
import com.tianrun.sipcall.net.Net;
|
|||
|
|
import com.tianrun.sipcall.ui.ActivityMgr;
|
|||
|
|
import com.tianrun.sipcall.ui.UIUtl;
|
|||
|
|
import com.tianrun.sipcall.utils.CONS;
|
|||
|
|
import com.tianrun.sipcall.utils.HttpUtl;
|
|||
|
|
import com.tianrun.sipcall.utils.logmy;
|
|||
|
|
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
import blue.all.BluetelEngine;
|
|||
|
|
import blue.bluetelobserver.BluetelInterface;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class SipEngine implements BluetelInterface {
|
|||
|
|
|
|||
|
|
public static final String TAG = "TestEngine";
|
|||
|
|
|
|||
|
|
private static SipEngine Instance;
|
|||
|
|
public boolean onLine = false;
|
|||
|
|
public boolean isRelogin = false;
|
|||
|
|
private String name = "0";
|
|||
|
|
private String pw = "0";
|
|||
|
|
public String ip = "0";
|
|||
|
|
private int port = 0;
|
|||
|
|
public static List<InCallUtils> callPagesConfig = new ArrayList<InCallUtils>();//所有通话页面的集合
|
|||
|
|
|
|||
|
|
private BluetelEngine myBluetelEngine;
|
|||
|
|
|
|||
|
|
public static SipEngine getInstance() {
|
|||
|
|
if (Instance == null)
|
|||
|
|
Instance = new SipEngine();
|
|||
|
|
return Instance;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//删除页面配置
|
|||
|
|
private void removeConfig(int callid) {
|
|||
|
|
//移除页面配置
|
|||
|
|
InCallUtils cursorPagesConfig = null;
|
|||
|
|
for (InCallUtils icu : callPagesConfig) {
|
|||
|
|
if (icu.getCallId() == callid) {
|
|||
|
|
cursorPagesConfig = icu;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (cursorPagesConfig != null) {
|
|||
|
|
callPagesConfig.remove(cursorPagesConfig);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据号码查通话的id
|
|||
|
|
*
|
|||
|
|
* @param callnumber
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public int getCallId(String callnumber) {
|
|||
|
|
int res = 0;
|
|||
|
|
for (InCallUtils icu : callPagesConfig) {
|
|||
|
|
if (icu.getCallNumber().equals(callnumber)) {
|
|||
|
|
res = icu.getCallId();
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return res;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 退出
|
|||
|
|
*/
|
|||
|
|
public void stop() {
|
|||
|
|
myBluetelEngine.deinit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getip() {
|
|||
|
|
return ip;
|
|||
|
|
}
|
|||
|
|
public String getName() {return name;};
|
|||
|
|
|
|||
|
|
public boolean isonLine() {
|
|||
|
|
return onLine;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void init() {
|
|||
|
|
if (myBluetelEngine == null) {
|
|||
|
|
myBluetelEngine = new BluetelEngine(App.getContext(), this);
|
|||
|
|
} else {
|
|||
|
|
myBluetelEngine.Stop();
|
|||
|
|
myBluetelEngine = new BluetelEngine(App.getContext(), this);
|
|||
|
|
}
|
|||
|
|
logmy.e("服务启动");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 注册
|
|||
|
|
*
|
|||
|
|
* @param name
|
|||
|
|
* @param pw
|
|||
|
|
* @param ip
|
|||
|
|
* @param port
|
|||
|
|
*/
|
|||
|
|
public void Register(String name, String pw, String ip, int port) {
|
|||
|
|
this.name = name;
|
|||
|
|
this.pw = pw;
|
|||
|
|
this.ip = ip;
|
|||
|
|
this.port = port;
|
|||
|
|
myBluetelEngine.Register(name, pw, ip, port);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 注销
|
|||
|
|
*/
|
|||
|
|
public void Unregister() {
|
|||
|
|
onLine = false;
|
|||
|
|
myBluetelEngine.Stop();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**************************** 接口 *************************************************/
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void AccountState(String uri, boolean state) {
|
|||
|
|
logmy.e(uri + "-->" + state);
|
|||
|
|
if (state) {
|
|||
|
|
if (!onLine) {
|
|||
|
|
onLine = true;
|
|||
|
|
Net.login(this.name, this.pw, new HttpUtl.CallBack() {
|
|||
|
|
@Override
|
|||
|
|
public void onRequestComplete(int cmd, String result, Object orgs) {
|
|||
|
|
ActivityMgr.sendMsg(CONS.LOGIN, null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onRequestError(int cmd, String result, Object orgs) {
|
|||
|
|
UIUtl.toastI("取得token失败");
|
|||
|
|
ActivityMgr.sendMsg(CONS.LOGINFAILED, null);
|
|||
|
|
}
|
|||
|
|
}, null);
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
if(!onLine && !isRelogin) {
|
|||
|
|
UIUtl.toastI("Sip登陆失败");
|
|||
|
|
ActivityMgr.sendMsg(CONS.LOGINFAILED, null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
isRelogin = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public boolean isMeetingCall(String phoneNo) {
|
|||
|
|
if (phoneNo.length() >= 5) {
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void FirstIncoming(String incomingNumber, boolean isVideo, int rPort, int lPort, boolean isHolder, int callid) {
|
|||
|
|
logmy.e("FirstIncoming:" + incomingNumber + "<>" + isVideo + "<>" + rPort + "<>" + lPort + "<>" + isHolder + "<>" + callid);
|
|||
|
|
int calltype = isVideo == false ? 0 : 1;
|
|||
|
|
String state = isVideo == true ? "视频来电" : "音频来电";
|
|||
|
|
callPagesConfig.add(new InCallUtils(incomingNumber, isHolder, isVideo, rPort, lPort, incomingNumber, state, false, 0, callid));
|
|||
|
|
GoToInCall(App.getContext(), incomingNumber, "来电", calltype, callid);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void OtherIncoming(String incomingNumber, boolean isVideo, int rPort, int lPort, boolean isHolder, int callid) {
|
|||
|
|
logmy.e("OtherIncoming:" + incomingNumber + "<>" + isVideo + "<>" + rPort + "<>" + lPort + "<>" + isHolder + "<>" + callid);
|
|||
|
|
String state = isVideo == true ? "视频来电" : "音频来电";
|
|||
|
|
callPagesConfig.add(new InCallUtils(incomingNumber, isHolder, isVideo, rPort, lPort, incomingNumber, state, false, 0, callid));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void StateCallDown(int callid, String number) {
|
|||
|
|
logmy.e(callid + "<>" + number);
|
|||
|
|
removeConfig(callid);
|
|||
|
|
ActivityMgr.sendMsg(CONS.CALLDOWN, callid);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void CallState(int callid, int callstate, String number) {
|
|||
|
|
String stateValues = "未知状态";
|
|||
|
|
switch (callstate) {
|
|||
|
|
case 1:
|
|||
|
|
stateValues = "呼叫中";//也可以在这里启动主动呼叫界面(此demo主动启动页面做在了button上)
|
|||
|
|
break;
|
|||
|
|
case 2:
|
|||
|
|
stateValues = "未知";
|
|||
|
|
break;
|
|||
|
|
case 3:
|
|||
|
|
stateValues = "振铃中";
|
|||
|
|
break;
|
|||
|
|
case 4:
|
|||
|
|
stateValues = "连接中";
|
|||
|
|
break;
|
|||
|
|
case 5:
|
|||
|
|
stateValues = "通话中";
|
|||
|
|
//一般在这里进行增益调节
|
|||
|
|
break;
|
|||
|
|
case 6:
|
|||
|
|
stateValues = "挂断";
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
logmy.e(callid + "<>" + stateValues + "<>" + number);
|
|||
|
|
|
|||
|
|
ActivityMgr.sendMsg(CONS.CALLSTATE, stateValues);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void NotifyCallMediaState(int callid, int r, int l, int payload) {
|
|||
|
|
logmy.e("NotifyCallMediaState" + callid + "<>" + r + "<>" + l + "<>" + payload);
|
|||
|
|
ActivityMgr.sendMsg(CONS.MEDIASTATE, new CallMediaUtils(callid, payload, r, l));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void ErrorMessage(String s) {
|
|||
|
|
logmy.e(s);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/************************ 方法 ***************************************************/
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 去通话页面 要在NotifyCallMediaState接口之前页面开好
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @param callnumber
|
|||
|
|
* @param callstate
|
|||
|
|
* @param calltype
|
|||
|
|
* @param callid
|
|||
|
|
*/
|
|||
|
|
public void GoToInCall(Context context, String callnumber, String callstate, int calltype, int callid) {
|
|||
|
|
Intent intent = null;
|
|||
|
|
if (isMeetingCall(callnumber)) {
|
|||
|
|
intent = new Intent(context, InCallMeetingActivity.class);
|
|||
|
|
} else {
|
|||
|
|
intent = new Intent(context, InCallActivity.class);
|
|||
|
|
}
|
|||
|
|
Bundle bundle = new Bundle();
|
|||
|
|
bundle.putInt("callid", callid);
|
|||
|
|
bundle.putString("callnumber", callnumber);
|
|||
|
|
bundle.putString("callstate", callstate);
|
|||
|
|
bundle.putInt("calltype", calltype);
|
|||
|
|
intent.putExtras(bundle);
|
|||
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|||
|
|
context.startActivity(intent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 打电话
|
|||
|
|
*
|
|||
|
|
* @param number 呼叫的号码
|
|||
|
|
* @param isVideo 是否是视频呼叫
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public int CallNumber(String number, boolean isVideo) {
|
|||
|
|
int callid = myBluetelEngine.CallNumber(number, ip, port, isVideo);
|
|||
|
|
int calltype = isVideo ? 0 : 1;
|
|||
|
|
GoToInCall(App.getContext(), number, "呼叫中", calltype, callid);
|
|||
|
|
callPagesConfig.add(new InCallUtils(number, false, isVideo, 0, 0, number, "呼叫中", false, 0, callid));
|
|||
|
|
return callid;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 挂断电话
|
|||
|
|
*
|
|||
|
|
* @param callid
|
|||
|
|
*/
|
|||
|
|
public void hangup(int callid) {
|
|||
|
|
myBluetelEngine.hangup(callid);
|
|||
|
|
removeConfig(callid);//移除页面配置
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 接听电话
|
|||
|
|
*
|
|||
|
|
* @param callid
|
|||
|
|
*/
|
|||
|
|
public void answer(int callid) {
|
|||
|
|
myBluetelEngine.answer(callid);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 通话增益调节
|
|||
|
|
*
|
|||
|
|
* @param callId
|
|||
|
|
* @param tx
|
|||
|
|
* @param rx
|
|||
|
|
*/
|
|||
|
|
public void setAdjustAudio(int callId, int tx, int rx) {
|
|||
|
|
myBluetelEngine.setAdjustAudio2(callId, tx, rx);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 通话保持
|
|||
|
|
*
|
|||
|
|
* @param isHolder
|
|||
|
|
* @param callid
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public boolean Holder(boolean isHolder, int callid) {
|
|||
|
|
return myBluetelEngine.Holder(isHolder, callid);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|