黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
 
using System;
using System.Threading;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Com.ETouchSky;
using Com.Tool;
using GateWay.Droid.FengLinVideo.Interface;
using Java.Text;
using Org.Json;
using Java.Util;
 
namespace GateWay.Droid.FengLinVideo.Forms
{
    public class ReverseCallFragment : Fragment, View.IOnClickListener, View.IOnTouchListener, VideoState
    {
        private VideoPhone mPhone;
        private string param = "";
 
        private View mView;
 
        private ImageView imgScreenshot;
        private ImageView imgUnlock;
        //private ImageView imgHangup;
        //private ImageView imgCall;
 
        private RelativeLayout rlHangup;
        private RelativeLayout rlCall;
 
        private TextView tvHangup;
        private TextView tvCall;
        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 void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }
 
        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()
        {
 
            imgScreenshot = (ImageView)mView.FindViewById(Resource.Id.icon_screenshot);//
            imgUnlock = (ImageView)mView.FindViewById(Resource.Id.icon_unlock);//
 
            tvTip = (TextView)mView.FindViewById(Resource.Id.tv_tip);
            rlHangup = (RelativeLayout)mView.FindViewById(Resource.Id.rl_hangup);
            //imgHangup = (ImageView)mView.FindViewById(Resource.Id.img_hangup);
            tvHangup = (TextView)mView.FindViewById(Resource.Id.tv_hangup);
 
            rlCall = (RelativeLayout)mView.FindViewById(Resource.Id.rl_call);
            //imgCall = (ImageView)mView.FindViewById(Resource.Id.img_call);
            tvCall = (TextView)mView.FindViewById(Resource.Id.tv_call);
 
            imgScreenshot.SetOnClickListener(this);
            imgUnlock.SetOnClickListener(this);
            rlCall.SetOnClickListener(this);
            rlHangup.SetOnClickListener(this);
 
            imgUnlock.SetOnTouchListener(this);
            imgScreenshot.SetOnTouchListener(this);
 
            tvTip.SetText("来电中...", null);
        }
 
        private void ReverseCall(string param)
        {
            //
            //一般是推送过的来电信息时调用此接口打开视频窗口。然后可调用mPhone.acceptRing(param);接收来电信息
            if (mPhone != null)
            {
                mPhone.ReverseCall(param);
            }
        }
 
        //开锁
        private void Unlock()
        {
            //开锁,当收到来电信息时可进行开锁操作
            if (mPhone != null)
            {
                try
                {
                    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;
                }
            }
        }
 
        //更新开锁按钮状态
        private void UpdataUnlockState()
        {
            //开锁成功,15秒内不给再点击按钮
            imgUnlock.Enabled = false;
            imgUnlock.SetImageResource(Resource.Drawable.unlock_disable);
 
            new System.Threading.Thread(() =>
            {
                System.Threading.Thread.Sleep(15 * 1000);
                Activity.RunOnUiThread(() =>
                {
                    imgUnlock.Enabled = true;
                    imgUnlock.SetImageResource(Resource.Drawable.unlock);
                });
            }).Start();
        }
 
        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)
        {
            Activity.RunOnUiThread(() =>
            {
                Time++;
                tvTip.SetText(getTime(Time), null);
            });
        }
 
        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":
                        UpdataUnlockState();
                        break;
                    case "EVT_SnapAck":
                        break;
                    case "EVT_StartStream":
                        TimeStarts();
                        break;
                }
            }
            catch (Exception e)
            {
                string error = e.Message;
            }
        }
 
        public bool OnTouch(View v, MotionEvent e)
        {
            if (e.Action == MotionEventActions.Down)
            {
                if (v.Equals(rlCall))
                {
                    tvCall.Selected = true;
                }
                else if (v.Equals(rlHangup))
                {
                    tvHangup.Selected = true;
                }
                else if (v.Equals(imgUnlock))
                {
                    imgUnlock.Selected = true;
                }
                else if(v.Equals(imgScreenshot))
                {
                    imgScreenshot.Selected = true;
                }
            }
            else if (e.Action == MotionEventActions.Up)
            {
                if (v.Equals(rlCall))
                {
                    tvCall.Selected = false;
                }
                else if (v.Equals(rlHangup))
                {
                    tvHangup.Selected = false;
                }
                else if (v.Equals(imgUnlock))
                {
                    imgUnlock.Selected = false;
                }
                else if (v.Equals(imgScreenshot))
                {
                    imgScreenshot.Selected = false;
                }
            }
            return false;
        }
 
        public void OnClick(View v)
        {
            if (v.Equals(rlCall))
            {
                //接收来电
                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);
                    }
                }
                rlCall.Visibility = ViewStates.Gone;
            }
            else if (v.Equals(rlHangup))
            {
                if (mPhone != null)
                {
                    mPhone.HangUp();
                    TimeEnd();
                }
                Activity.Finish();
            }
            else if (v.Equals(imgScreenshot))
            {
                //有视频过来可调用此接口进行拍照
                if (mPhone != null)
                {
                    //有视频过来可调用此接口进行拍照
                    if (mPhone != null)
                    {
 
                        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                        string time = format.Format(new Date(SystemClock.CurrentThreadTimeMillis()));
 
                        string ss = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
                        string path = ss + "/" + time + ".jpg";
                        mPhone.Snap(path);
                    }
                }
            }
            else if (v.Equals(imgUnlock))
            {
                Unlock();
            }
        }
    }
}