using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Icu.Text;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.ETouchSky;
using Com.Tool;
using HDL_ON.Common;
using HDL_ON.DAL.Server;
using HDL_ON_Android.FengLinVideo.Interface;
using Java.Util;
using Org.Json;
namespace HDL_ON_Android.FengLinVideo.Form
{
public class ReverseCallFragment : Fragment, View.IOnClickListener, VideoState
{
private VideoPhone mPhone;
private string param = "";
private bool isCalling = false;
private View mView;
private ImageView screenshotImg;// 截图
private ImageView unlockImg;// 开锁
private LinearLayout answerLayout; // 接听
private ImageView hangupImg; // 接听
private ImageView answerImg;// 挂断
private TextView hangupText;
private TextView tvTip;
private System.Threading.Timer timer = null;
private int Time = 0;
public ReverseCallFragment(VideoPhone _phone, string _param)
{
this.mPhone = _phone;
this.param = _param;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mView = inflater.Inflate(Resource.Layout.fragment_call, container, false);
IniView();
ReverseCall(param);
return mView;
}
private void IniView()
{
screenshotImg = (ImageView)mView.FindViewById(Resource.Id.callScreenshotImg);//
unlockImg = (ImageView)mView.FindViewById(Resource.Id.callUnlockImg);//
tvTip = (TextView)mView.FindViewById(Resource.Id.callTipText);
answerLayout = (LinearLayout)mView.FindViewById(Resource.Id.callAnswerLayout);
hangupImg = (ImageView)mView.FindViewById(Resource.Id.callHangupImg);
answerImg = (ImageView)mView.FindViewById(Resource.Id.callAnswerImg);
hangupText = (TextView)mView.FindViewById(Resource.Id.callHangupText);
screenshotImg.SetOnClickListener(this);
unlockImg.SetOnClickListener(this);
hangupImg.SetOnClickListener(this);
answerImg.SetOnClickListener(this);
hangupText.SetText(GetString(Resource.String.video_not_answer), null);
tvTip.SetText(GetString(Resource.String.calling), null);
}
///
/// 一般是推送过的来电信息时调用此接口打开视频窗口。然后可调用mPhone.acceptRing(param);接收来电信息
///
///
private void ReverseCall(string param)
{
if (mPhone != null)
{
mPhone.ReverseCall(param);
}
}
///
/// 开锁
///
private void Unlock()
{
//开锁,当收到来电信息时可进行开锁操作
if (mPhone != null)
{
try
{
unlockImg.Selected = true;
JSONObject ht = new JSONObject();
ht.Put("command", "open");//固定参数
ht.Put("room_id", 123); //动态参数 ,传递开门的房间号。这个开门口记录就能记录谁开的门
ht.Put("devType", 7); //固定参数
mPhone.SendCustomData(ht.ToString());
}
catch (Exception e)
{
string erro = e.Message;
}
}
}
public static Thread unlockThread = null;//公开出来,关闭activity的时候要结束这个线程,不然会引起崩溃
///
/// 更新开锁按钮状态,开锁成功,15秒内不给再点击按钮
///
private void UpdataUnlockState()
{
try
{
if (unlockImg == null) return;
unlockImg.Enabled = false;
unlockThread = new Thread(() =>
{
try
{
Thread.Sleep(15 * 1000);
Activity.RunOnUiThread(() =>
{
try {
if (unlockImg != null)
unlockImg.Enabled = true;
}
catch { }
});
}
catch { }
});//.Start();
unlockThread.Start();
PostUnlock();
}
catch { }
}
private string GetTime(int time)
{
int m = time / 60;
int s = time % 60;
return UnitFormat(m) + ":" + UnitFormat(s);
}
private static string UnitFormat(int i)
{
string retStr = null;
if (i >= 0 && i < 10)
retStr = "0" + i;
else
retStr = "" + i;
return retStr;
}
///
/// 通话记录计时器,从拉流成功开始计算时间
///
private void TimeStarts()
{
try
{
if (timer != null)
timer.Dispose();
TimerCallback timerCallback = new TimerCallback(Tick);
timer = new System.Threading.Timer(timerCallback, null, 0, 1000);
}
catch (Exception) { }
}
private void TimeEnd()
{
Time = 0;
if (timer != null)
{
timer.Dispose();
timer = null;
}
}
public void Tick(Object state)
{
try
{
Activity.RunOnUiThread(() =>
{
try
{
Time++;
if (tvTip != null)
tvTip.SetText(GetTime(Time), null);
}
catch { }
});
}
catch { }
}
///
/// 通话消息回调方法
///
///
public void OnPhoneEvent(string msg)
{
try
{
//
TextProtocol tp = new TextProtocol();
tp.Parse(msg);
string event1 = tp.GetString("event");
switch (event1)
{
case "EVT_RECV_CUSTOM_DATA":// 开锁成功
string data = tp.GetString("data");
UpdataUnlockState();
break;
case "EVT_StartStream":// 拉流成功,开始记录通话时间
isCalling = true;
TimeStarts();
hangupText.SetText(GetString(Resource.String.video_hang_up), null);
break;
case "EVT_StopStream":
if (isCalling == false)
PostReject();// 拒接
else
PostHangup();// 正常挂断
break;
case "EVT_SnapAck":
int error = tp.GetInt("error");
string filePath = tp.GetString("filePath");
if (error == 0)
{
screenshotImg.Selected = true;
PostScreenshot(filePath);
}
else
{
screenshotImg.Selected = false;
}
break;
}
}
catch (Exception e)
{
string error = e.Message;
}
}
public void OnClick(View v)
{
if (v.Equals(answerImg))
{
//接收来电
if (mPhone != null)
{
if (mPhone.IsRinging)
{
string UserData = "user text";
//注意:RequestAudio 请求对方音频,RequestVideo请求对方视频 SendAudio发送本地音频 SendVideo 发送本地视频 一般门口不接收到视频,所以最好设置0,减少流量消耗
string param = string.Format("RequestAudio=1\r\n" + "RequestVideo=1\r\n" + "SendAudio=1\r\n" + "SendVideo=1r\n" + "UserData=%s\r\n", UserData);
mPhone.AcceptRing(param);
answerLayout.Visibility = ViewStates.Gone;
PostAnswer();
}
}
}
else if (v.Equals(hangupImg))
{
if (mPhone != null)
{
mPhone.HangUp();
TimeEnd();
}
Activity.Finish();
}
else if (v.Equals(screenshotImg))
{
//有视频过来可调用此接口进行拍照
if (mPhone != null)
{
// 内部储存/DCIM/Camera/.....jpg
screenshotImg.Selected = true;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
string time = format.Format(new Date(SystemClock.CurrentThreadTimeMillis()));
string ss = Android.OS.Environment.ExternalStorageDirectory.Path + "/DCIM/Camera";
string path = ss + "/" + time + ".jpg";
mPhone.Snap(path);
//2021-09-01 增加发送广播,通知刷新图库的显示
HDLONUtils.SendBroadcastScanFile(this.Context, path);
}
}
else if (v.Equals(unlockImg))
{
Unlock();
}
}
///
/// post 截图
///
/// 截图保留的路径
private void PostScreenshot(string path)
{
new Thread(() =>
{
try
{
string[] str = path.Split("/");
string img_name = str.GetValue(str.Length - 1).ToString().Replace(".jpg", "");
byte[] images = FileUtlis.Files.ReadFileForPath(path);
Dictionary d = new Dictionary();
d.Add("callId", VideoActivity.CallId);
d.Add("images", images);
d.Add("imagesName", "_Android.jpg");
string jsonString = HttpUtil.GetSignRequestJson(d);
string url = "/home-wisdom/app/fl/vi/screenshot";
ResponsePackNew response = HttpUtil.RequestHttpsPost(url, jsonString);
Log.Info("FengLinVideo", "Post Screenshot Response code=" + response.Code);
}
catch { }
}).Start();
}
///
/// post 接听电话记录
///
private void PostAnswer()
{
new Thread(() =>
{
try
{
Dictionary d = new Dictionary();
d.Add("uuid", VideoActivity.UUId);//丰林请求的唯一id string
d.Add("cmtID", VideoActivity.CmtID);//丰林社区id string
d.Add("roomno", VideoActivity.Roomno);//丰林房间号 string
d.Add("unitno", VideoActivity.Unitno);//丰林楼栋号 string
d.Add("HomeID", VideoActivity.HomeID);//丰林住宅id string
d.Add("callId", VideoActivity.CallId);//呼叫记录id int
string jsonString = HttpUtil.GetSignRequestJson(d);
string url = "/home-wisdom/app/fl/vi/answer";
ResponsePackNew response = HttpUtil.RequestHttpsPost(url, jsonString);
Log.Info("FengLinVideo", "Post Answer Response code=" + response.Code);
}
catch { }
}).Start();
}
///
/// post 挂断电话记录
///
private void PostHangup()
{
new Thread(() =>
{
try
{
Dictionary d = new Dictionary();
d.Add("callId", VideoActivity.CallId);//呼叫记录id int
d.Add("callDuration", Time);//通话时长(秒) int
string jsonString = HttpUtil.GetSignRequestJson(d);
string url = "/home-wisdom/app/fl/vi/hang-up";
ResponsePackNew response = HttpUtil.RequestHttpsPost(url, jsonString);
Log.Info("FengLinVideo", "Post Hangup Response code=" + response.Code);
}
catch { }
}).Start();
}
///
/// post 拒接记录
///
private void PostReject()
{
new Thread(() =>
{
try
{
Dictionary d = new Dictionary();
d.Add("callId", VideoActivity.CallId);//呼叫记录id int
string jsonString = HttpUtil.GetSignRequestJson(d);
string url = "/home-wisdom/app/fl/vi/reject";
ResponsePackNew response = HttpUtil.RequestHttpsPost(url, jsonString);
Log.Info("FengLinVideo", "Post Reject Response code=" + response.Code);
}
catch { }
}).Start();
}
///
/// post 开锁记录
///
private void PostUnlock()
{
new Thread(() =>
{
try
{
Dictionary d = new Dictionary();
d.Add("callId", VideoActivity.CallId);//呼叫记录id int
string jsonString = HttpUtil.GetSignRequestJson(d);
string url = "/home-wisdom/app/fl/vi/unlock";
ResponsePackNew response = HttpUtil.RequestHttpsPost(url, jsonString);
Log.Info("FengLinVideo", "Post Reject Response code=" + response.Code);
}
catch { }
}).Start();
}
}
}