panlili2024
2025-02-27 bca03b5f891af5966a06b1ecd00f6cd573659ce6
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package com.hdl.hdllinphonesdk.activity;
 
import android.Manifest;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.AudioAttributes;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.hdl.hdllinphonesdk.HDLLinphoneKit;
import com.hdl.hdllinphonesdk.R;
import com.hdl.hdllinphonesdk.callback.OnLPOpenDoorCallBack;
import com.hdl.hdllinphonesdk.dialog.LinphoneCommonDialog;
import com.hdl.hdllinphonesdk.receiver.CallBroadcastReceiver;
import com.hdl.hdllinphonesdk.utils.HDLImageUtils;
import com.hdl.hdllinphonesdk.utils.HDLLog;
 
import org.linphone.core.Call;
import org.linphone.core.CallParams;
import org.linphone.core.Core;
import org.linphone.core.CoreListenerStub;
 
/**
 * 反呼页面
 */
public class HDLLinphoneReverseCallActivity extends LPCheckPermissionsActivity implements View.OnClickListener {
    private static final String TAG = "HDLLinphoneReverseCallActivity";
 
    private RelativeLayout ll_lp_btn_back;
    //控件
    private TextView tv_lp_title;
    private ImageView iv_lp_screenshot;
    private ImageView iv_lp_unlock;
 
    private ImageView iv_lp_hangUp;
    private TextView tv_lp_hangUp;
 
    private ImageView iv_lp_answer;
    private TextView tv_lp_answer;
    private RelativeLayout rl_lp_answerView;
 
    private ImageView iv_lp_rejectall;
    private TextView tv_lp_rejectall;
    private RelativeLayout rl_lp_rejectallView;
 
    private LinearLayout ll_video_lp_rendering;
    private TextureView video_lp_rendering;
    private TextureView video_lp_rendering2;
    //    private TextView tv_lp_calltime;
    private Chronometer timer_lp_calltime;
    private TextView tv_lp_calltime;
 
    //参数
    private boolean isAnswer = false;//是否已经接听了
    private int callTime;//通话时间
    //倒计时
    private CountDownTimer mCountDownTimer;
    /**
     * 手机振动器
     */
    private Vibrator mVibrator;
 
    //Linphone
    private CoreListenerStub mCoreListener;
 
    private String deviceSipAccount;//设备SIP账号
    private boolean enableVideo = false;
 
    private CallBroadcastReceiver callBroadcastReceiver;
    private IntentFilter callFilter;
 
    private boolean callAllRejection = false;//自研门口机是否支持全部拒接
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_hdllinphone_intercom);
        initView();
        initData();
        initOnClick();
        initCallBack();
        initCountDownTimer();
        initLinphone();
    }
 
    /**
     * onDestroy
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //1.挂断
        HDLLinphoneKit.getInstance().hangUp();
        //2.暂停计时
        if (null != timer_lp_calltime) {
            timer_lp_calltime.stop();
        }
        //3.注销callBack
        HDLLinphoneKit.getInstance().setOpenOpenDoorCallBack(null);
 
        //4.
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
        onDestroyLinphone();
 
        if (callBroadcastReceiver != null) {
            unregisterReceiver(callBroadcastReceiver);
            callBroadcastReceiver = null;
        }
 
        HDLLog.i(TAG, "onDestroy");
    }
 
    /**
     * initView
     */
    private void initView() {
        ll_lp_btn_back = findViewById(R.id.ll_lp_btn_back);
        tv_lp_title = findViewById(R.id.tv_lp_title);
        iv_lp_screenshot = findViewById(R.id.iv_lp_screenshot);
        iv_lp_unlock = findViewById(R.id.iv_lp_unlock);
        iv_lp_hangUp = findViewById(R.id.iv_lp_hangUp);
        tv_lp_hangUp = findViewById(R.id.tv_lp_hangUp);
        iv_lp_answer = findViewById(R.id.iv_lp_answer);
        tv_lp_answer = findViewById(R.id.tv_lp_answer);
        rl_lp_answerView = findViewById(R.id.rl_lp_answerView);
        iv_lp_rejectall = findViewById(R.id.iv_lp_hangUpall);
        tv_lp_rejectall = findViewById(R.id.tv_lp_hangUpall);
        rl_lp_rejectallView = findViewById(R.id.rl_lp_hangUpallView);
        ll_video_lp_rendering = findViewById(R.id.ll_video_lp_rendering);
        video_lp_rendering = findViewById(R.id.video_lp_rendering);
        video_lp_rendering2 = findViewById(R.id.video_lp_rendering2);
//        tv_lp_calltime = findViewById(R.id.tv_lp_calltime);
        timer_lp_calltime = (Chronometer) findViewById(R.id.timer_lp_calltime);
        tv_lp_calltime = findViewById(R.id.tv_lp_calltime);
        setWidthHeightWithRatio(ll_video_lp_rendering, 16, 9);
 
        iv_lp_screenshot.setEnabled(false);
        iv_lp_unlock.setEnabled(false);
    }
 
    /**
     * initData
     */
    private void initData() {
        Bundle extras = getIntent().getExtras();
        String interphoneType = "";
        if (extras != null) {
            String titleName = extras.getString(HDLLinphoneKit.KEY_TITLE_NAME);
            if (!TextUtils.isEmpty(titleName)) {
                tv_lp_title.setText(titleName);
            }
            deviceSipAccount = extras.getString(HDLLinphoneKit.KEY_SIP_ACCOUNT);
            interphoneType = extras.getString("interphoneType");
            callAllRejection = extras.getBoolean(HDLLinphoneKit.KEY_CALL_ALL_REJECTION);
        }
 
        rl_lp_rejectallView.setVisibility(callAllRejection ? View.VISIBLE : View.GONE);
 
        //监听呼叫广播
        callFilter = new IntentFilter();
        callFilter.addAction("com.hdl.homepro.call.action");
        callBroadcastReceiver = new CallBroadcastReceiver();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            registerReceiver(callBroadcastReceiver, callFilter, RECEIVER_EXPORTED);
        } else {
            registerReceiver(callBroadcastReceiver, callFilter);
        }
 
        if (callBroadcastReceiver != null) {
            String finalInterphoneType = interphoneType;
            callBroadcastReceiver.setOnReceivedCallBroadcastListener(new CallBroadcastReceiver.CallBroadcastListener() {
                @Override
                public void onReceived(String callFrom) {
                    //如果收到推送通知把监视页面finish
                    if (!TextUtils.isEmpty(callFrom) && !finalInterphoneType.equals(callFrom)) {
                        Log.d("panlili", "HDLLinphoneIntercomActivity.java:onReceived-----> " + callFrom);
                        finish();//结束当前页面
                    }
                }
            });
        }
    }
 
    /**
     * 指定屏幕宽度 根据宽高比设置控件宽高
     */
    public void setWidthHeightWithRatio(View view, int widthRatio, int heightRatio) {
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
 
        if (width <= 0) width = view.getWidth();
        int height = width * heightRatio / widthRatio;
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.height = height;
            layoutParams.width = width;
            view.setLayoutParams(layoutParams);
        }
    }
 
    /**
     * init OnClickListener
     */
    private void initOnClick() {
        ll_lp_btn_back.setOnClickListener(this);
        iv_lp_screenshot.setOnClickListener(this);
        iv_lp_unlock.setOnClickListener(this);
        iv_lp_hangUp.setOnClickListener(this);
        tv_lp_hangUp.setOnClickListener(this);
        iv_lp_answer.setOnClickListener(this);
        tv_lp_answer.setOnClickListener(this);
        iv_lp_rejectall.setOnClickListener(this);
        tv_lp_rejectall.setOnClickListener(this);
    }
 
    /**
     * 初始化开门回调事件
     */
    void initCallBack() {
        HDLLinphoneKit.getInstance().setOpenOpenDoorCallBack(new OnLPOpenDoorCallBack() {
            @Override
            public void onOpenSuccess() {
                setOpenDoorSuccess();
            }
 
            @Override
            public void onOpenError(String errorMes) {
                showToast(getString(R.string.lp_unlockfailureStr) + errorMes);
            }
        });
    }
 
    /**
     * 初始化开锁屏蔽倒计时
     */
    void initCountDownTimer() {
 
        mCountDownTimer = new CountDownTimer(10 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (!HDLLinphoneReverseCallActivity.this.isFinishing()) {
                }
            }
 
            /**
             *倒计时结束后调用的
             */
            @Override
            public void onFinish() {
//                showToast("开锁onFinish");
                iv_lp_unlock.setEnabled(true);
            }
 
        };
//
    }
 
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_lp_screenshot) {
            //动态权限申请
            if (ContextCompat.checkSelfPermission(HDLLinphoneReverseCallActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(HDLLinphoneReverseCallActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(HDLLinphoneReverseCallActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            } else {
                //截图
                onClickScreenshot();
            }
        } else if (id == R.id.iv_lp_unlock) {
            //开锁
            onClickUnlock();
        } else if (id == R.id.iv_lp_hangUp || id == R.id.tv_lp_hangUp) {
            //挂断
            onClickHangUp();
        } else if (id == R.id.iv_lp_answer || id == R.id.tv_lp_answer) {
            //接听
            onClickAnswer();
        } else if (id == R.id.iv_lp_hangUpall || id == R.id.tv_lp_hangUpall) {
            //拒接全部
            onClickRejectAll();
        } else if (id == R.id.ll_lp_btn_back) {
            //返回按钮 挂断
            if (isAnswer) {
                HDLLinphoneKit.getInstance().hangUp();
            }
            finish();
        }
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //截图
                    onClickScreenshot();
                } else {
                    LinphoneCommonDialog.getInstance().showSettingPermissionDialog(HDLLinphoneReverseCallActivity.this, getString(R.string.open_save_permission));
                }
                break;
            default:
        }
    }
 
    /**
     * 截图点击事件
     */
    private void onClickScreenshot() {
//        Bitmap img = HDLImageUtils.createViewBitmap(video_lp_rendering);
        Bitmap img = video_lp_rendering.getBitmap();
        boolean isSuccess = HDLImageUtils.saveImageToGallery(this, img);
 
        if (isSuccess) {
            showToast(getString(R.string.lp_saveToTheAlbumsStr));
        } else {
            showToast(getString(R.string.lp_operationFailedStr));
        }
    }
 
 
    /**
     * 开锁点击事件
     */
    private void onClickUnlock() {
        LinphoneCommonDialog.getInstance().showDialog(HDLLinphoneReverseCallActivity.this, getString(R.string.tip_confirm_unlock), getString(R.string.lp_confirm), new LinphoneCommonDialog.OnOKClickListener() {
            @Override
            public void onOKClick() {
                if (HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener() != null) {
                    HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener().onUnlockAction();
                }
            }
        });
    }
 
    /**
     * 挂断点击事件
     */
    private void onClickHangUp() {
        //2.回调事件
        if (HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener() != null) {
            if (isAnswer) {
                //如果之前已经接听了,回调是挂断
                callTime = getTimerCallTime();
                HDLLog.i("callTime", "callTime" + callTime);
                HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener().onHangUpAction(callTime);
            } else {
                //如果之前没接听了,回调是拒接
                HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener().onRejectCallAction();
            }
        }
        //2.页面关闭
        finish();
    }
 
 
    /**
     * 拒接全部点击事件
     */
    private void onClickRejectAll() {
        //1.回调事件
        if (HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener() != null) {
            HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener().onRejectCallAllAction();
        }
        //2.页面关闭
        finish();
    }
 
    /**
     * 接听按钮点击事件
     */
    private void onClickAnswer() {
        //1.接听 反呼
        if (TextUtils.isEmpty(deviceSipAccount)) {
            getString(R.string.lp_sipAccount_null);
            return;
        }
        HDLLinphoneKit.getInstance().callTo(deviceSipAccount, true);
 
        //2.修改UI
        isAnswer = true;
        tv_lp_hangUp.setText(getString(R.string.lp_hangupStr));
        setViewGone(rl_lp_answerView);
        setViewGone(rl_lp_rejectallView);
        setViewGone(tv_lp_calltime);
        setViewVisible(timer_lp_calltime);
        startCallTimeCountdown();
        //接听通知
        if (HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener() != null) {
            HDLLinphoneKit.getInstance().getOnHDLLinphoneCallListener().onAnswerAction();
        }
 
        iv_lp_screenshot.setEnabled(true);
        iv_lp_unlock.setEnabled(true);
    }
 
    /****************开锁成功********/
 
    /**
     * 开锁成功后,10s内不能重复开锁
     * 开锁按钮禁用,倒计时结束后重新允许点击
     */
    void setOpenDoorSuccess() {
        showToast(getString(R.string.lp_unlockSuccessfullyStr));
        iv_lp_unlock.setEnabled(false);
        startOpenDoorCountdown();
    }
 
    /**
     * 开启倒计时
     */
    void startOpenDoorCountdown() {
        mCountDownTimer.start();
    }
 
    /**
     * 通话记录开启倒计时
     */
    void startCallTimeCountdown() {
        timer_lp_calltime.setBase(SystemClock.elapsedRealtime());//计时器清零
        timer_lp_calltime.start();
    }
 
    int getTimerCallTime() {
        int temp0 = Integer.parseInt(timer_lp_calltime.getText().toString().split(":")[0]);
        int temp1 = Integer.parseInt(timer_lp_calltime.getText().toString().split(":")[1]);
        return temp0 * 60 + temp1;
    }
 
    /****************封装常用方法********/
    /**
     * showToast
     */
    private void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
 
    /**
     * setViewVisible
     */
    private static void setViewVisible(View view) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
    }
 
    /**
     * setViewGone
     */
    private static void setViewGone(View view) {
        if (view.getVisibility() != View.GONE) {
            view.setVisibility(View.GONE);
        }
    }
 
    /**********LinPhone**********/
    /**
     * initLinphone
     */
    void initLinphone() {
        mCoreListener = new CoreListenerStub() {
            @Override
            public void onCallStateChanged(Core core, Call call, Call.State state, String message) {
                if (state == Call.State.End || state == Call.State.Released) {
                    // Once call is finished (end state), terminate the activity
                    // We also check for released state (called a few seconds later) just in case
                    // we missed the first one
                    enableVideo = false;
                    showToast(getString(R.string.lp_endOfCallStr));
                    finish();
                }
            }
        };
 
        Core core = HDLLinphoneKit.getInstance().getCore();
        // We need to tell the core in which to display what
        if (core != null) {
            core.setNativeVideoWindowId(video_lp_rendering);
            core.setNativePreviewWindowId(video_lp_rendering2);
            // Listen for call state changes
            core.addListener(mCoreListener);
            core.enableMic(true);//开启麦克风
        }
 
    }
 
 
    void enableVideo(Core core, Call call) {
//        Call currentCall = HDLLinphoneService.getCore().getCurrentCall();
 
        CallParams params = call.getCurrentParams();
        if (params == null) {
            params = core.createCallParams(call);
        }
        params.enableVideo(true);
        params.enableLowBandwidth(false);
        params.setAudioBandwidthLimit(0); // disable limitation
 
        call.update(params);
    }
 
    /**
     * onDestroyLinphone
     */
    void onDestroyLinphone() {
        Core core = HDLLinphoneKit.getInstance().getCore();
        if (core != null) {
            core.removeListener(mCoreListener);
            core.setNativeVideoWindowId(null);
            core.setNativePreviewWindowId(null);
        }
        video_lp_rendering = null;
        video_lp_rendering2 = null;
    }
 
}