package com.hdl.hdllinphonesdk.activity;
|
|
import android.Manifest;
|
import android.annotation.SuppressLint;
|
import android.content.IntentFilter;
|
import android.content.pm.PackageManager;
|
import android.graphics.Bitmap;
|
import android.os.Bundle;
|
import android.os.CountDownTimer;
|
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.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 HDLLinphoneMonitorActivity extends LPCheckPermissionsActivity implements View.OnClickListener {
|
private static final String TAG = "HDLLinphoneMonitorActivity";
|
|
private RelativeLayout ll_lp_btn_back;
|
//控件
|
private TextView tv_lp_title;
|
|
private ImageView iv_lp_screenshot;
|
private ImageView iv_lp_unlock;
|
|
private LinearLayout ll_video_lp_rendering;
|
private TextureView video_lp_rendering;
|
private TextureView video_lp_rendering2;
|
|
private CountDownTimer mCountDownTimer;
|
|
//Linphone
|
private CoreListenerStub mCoreListener;
|
private boolean enableVideo = false;
|
|
private CallBroadcastReceiver callBroadcastReceiver;
|
private IntentFilter callFilter;
|
|
@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_monitor);
|
|
initView();
|
initData();
|
initOnClick();
|
initCallBack();
|
initCountDownTimer();
|
initLinphone();
|
}
|
|
/**
|
* onDestroy
|
*/
|
@SuppressLint("LongLogTag")
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
//1.挂断
|
HDLLinphoneKit.getInstance().hangUp();
|
|
HDLLinphoneKit.getInstance().setOpenOpenDoorCallBack(null);
|
|
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);
|
|
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);
|
|
setWidthHeightWithRatio(ll_video_lp_rendering, 16, 9);
|
}
|
|
/**
|
* 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);
|
}
|
interphoneType = extras.getString("interphoneType");
|
}
|
|
//监听呼叫广播
|
callFilter = new IntentFilter();
|
callFilter.addAction("com.hdl.homepro.call.action");
|
callBroadcastReceiver = new CallBroadcastReceiver();
|
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", "HDLLinphoneMonitorActivity finish-----> ");
|
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);
|
}
|
|
/**
|
* 初始化开门回调事件
|
*/
|
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 (!HDLLinphoneMonitorActivity.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(HDLLinphoneMonitorActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
&& ActivityCompat.checkSelfPermission(HDLLinphoneMonitorActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
ActivityCompat.requestPermissions(HDLLinphoneMonitorActivity.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.ll_lp_btn_back) {
|
//返回按钮
|
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(HDLLinphoneMonitorActivity.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(HDLLinphoneMonitorActivity.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();
|
}
|
}
|
});
|
|
}
|
|
/****************开锁成功********/
|
/**
|
* 开锁成功后,10s内不能重复开锁
|
* 开锁按钮禁用,倒计时结束后重新允许点击
|
*/
|
void setOpenDoorSuccess() {
|
showToast(getString(R.string.lp_unlockSuccessfullyStr));
|
iv_lp_unlock.setEnabled(false);
|
startOpenDoorCountdown();
|
}
|
|
/**
|
* 开启倒计时
|
*/
|
void startOpenDoorCountdown() {
|
mCountDownTimer.start();
|
}
|
|
/****************封装常用方法********/
|
/**
|
* 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_endMonitoringStr));
|
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(false);//关闭麦克风
|
}
|
}
|
|
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;
|
}
|
}
|