package com.haochuan.hciptvbasic;
/*
* 这是主页面的基类,负责webview的初始化工作
*
* */
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.asha.vrlib.MDVRLibrary;
import com.haochuan.core.BaseMediaPlayer;
import com.haochuan.core.IVideoPlayer;
import com.haochuan.core.Logger;
import com.haochuan.core.util.ELS;
import com.haochuan.core.util.HandlerUtil;
import com.haochuan.hciptvbasic.util.ELSUtil;
import com.haochuan.hciptvbasic.util.VRUtil;
import com.haochuan.hciptvbasic.webview.PayToJS;
import com.haochuan.hciptvbasic.webview.PlayerToJS;
import com.haochuan.hciptvbasic.webview.HCWebChromeClient;
import com.haochuan.hciptvbasic.webview.UtilToJS;
import com.haochuan.ijk.IjkVideoPlayer;
import com.haochuan.systemvideo.SystemVideoPlayer;
import java.util.List;
public abstract class BaseWebActivity extends AppCompatActivity {
private WebView webView; //整个应用唯一的webview
private PlayerToJS playerToJS; //PlayerToJS类实例
private PayToJS payToJS; // PayToJS类实例
private UtilToJS utilToJS; //ToolToJS实例
String playerToJSName = PlayerToJS.class.getSimpleName(); //playerToJS类名
String payToJSName = PayToJS.class.getSimpleName(); //payToJS类名
String toolToJSName = UtilToJS.class.getSimpleName(); //toolToJS类名
//播放器
private BaseMediaPlayer mHCPlayer = null;
protected ELS els;
VRUtil vrUtil;
//vr模块
MDVRLibrary mVRLibrary;
/**-----------------------虚函数-----------------------*/
//获取启动页web地址
protected abstract String getIndexURL();
/*--------------------生命周期---------------------*/
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
setContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
initWebSetting(webView);
//初始化播放器和vr
initPlayerAndVr();
//初始化els日志记录系统
els = ELS.getInstance(this);
String uid = "12398658";
if(utilToJS != null){
uid = utilToJS.getUserName();
}
Logger.d("uid:" + uid);
ELSUtil.Init(this,els,uid);
//初始化日志
Logger.init(this,getWebView());
runH5();
}
private void runH5(){
webView.loadUrl(getIndexURL());
}
@Override
protected void onStart() {
super.onStart();
//关闭软键盘
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
protected void onResume() {
super.onResume();
if (webView != null) {
webView.onResume();
}
if(mHCPlayer !=null){
mHCPlayer.resume();
}
if(mVRLibrary != null){
mVRLibrary.onResume(this);
}
}
@Override
protected void onPause() {
super.onPause();
if (webView != null) {
webView.onPause();
}
if(mHCPlayer !=null){
mHCPlayer.pause();
}
if(mVRLibrary != null){
mVRLibrary.onPause(this);
}
}
@Override
protected void onStop(){
super.onStop();
}
@Override
protected void onDestroy() {
if (webView != null) {
webView.removeJavascriptInterface(playerToJSName);
webView.clearCache(true);
webView.clearFormData();
webView.clearMatches();
webView.clearSslPreferences();
webView.clearDisappearingChildren();
webView.clearHistory();
webView.clearAnimation();
webView.loadUrl("about:blank");
webView.removeAllViews();
webView = null;
}
if(mHCPlayer !=null){
mHCPlayer.release();
}
if(mVRLibrary != null){
mVRLibrary.onDestroy();
}
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mVRLibrary.onOrientationChanged(this);
}
/**
* webView对象获取"返回"按键事件
*/
@Override
public void onBackPressed() {
//如果是除未来其他版本,请用这段代码
utilToJS.onBackPressed();
}
/*--------------------------初始化函数---------------------------*/
/*
* 初始化播放器
* */
private void initPlayer(){
Logger.d("BaseWebActivity,initPlayer()");
mHCPlayer = new SystemVideoPlayer(this);
mHCPlayer.setVideoPlayerListener(new IVideoPlayer() {
@Override
public void onPreparing() {
playerToJS.onPlayerPreparing();
}
@Override
public void onPlaying() {
playerToJS.onPlayerPlaying();
}
@Override
public void onResume() {
playerToJS.onPlayerResume();
}
@Override
public void onPause() {
playerToJS.onPlayerPause();
}
@Override
public void onDestroy() {
}
@Override
public void onPlayingBuffering() {
playerToJS.onPlayingBuffer();
}
@Override
public void onCompletion() {
playerToJS.onPlayerComplete();
}
@Override
public void onError(int what, int extra) {
playerToJS.onPlayerError(what,extra);
//取消提示框显示,改由前端页面实现
/* if(BuildConfig.player_type == 2){
CNTVPlayerErrorAlert();
}*/
}
});
}
/*
* 初始化vrLib库
* */
private void initVrLib(){
if(vrUtil == null){
vrUtil = new VRUtil();
}
mVRLibrary = vrUtil.createVRLibrary(this,mHCPlayer);
}
/**
*
* 初始化播放器和vr
*/
public void initPlayerAndVr(){
HandlerUtil.runOnUiThread(()->{
Logger.d("刷新播放器");
//先将播放器和vr库消除
mHCPlayer = null;
mVRLibrary = null;
if(vrUtil !=null){
vrUtil.release(this);
}
//再初始化播放器和vr库
initPlayer();
initVrLib();
getPlayerToJS().setBaseMediaPlayer(mHCPlayer);
});
}
/**
* 初始化webview
* @param webView
*/
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface", "AddJavascriptInterface"})
private void initWebSetting(WebView webView) {
Logger.d("BaseWebActivity,initWebSetting()");
try{
WebSettings webSettings = webView.getSettings();
// 由H5端适配屏幕,具体参考文档:https://developer.chrome.com/multidevice/webview/pixelperfect
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// 设置JS交互
webSettings.setJavaScriptEnabled(true);
HCWebChromeClient hcWebChromeClient = new HCWebChromeClient();
setPlayerToJS();
setPayToJS();
setToolToJS();
webView.addJavascriptInterface(playerToJS,playerToJSName);
webView.addJavascriptInterface(payToJS,payToJSName);
webView.addJavascriptInterface(utilToJS,toolToJSName);
// 设置WebClient
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(hcWebChromeClient);
// 设置是否开启web内容调试,具体调试方式查看:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_source=dcc&utm_medium=redirect&utm_campaign=2016q3
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(BuildConfig.isDebug);
}
webView.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
private void setPlayerToJS(){
playerToJS = new PlayerToJS(this,webView,mHCPlayer);
//添加VrRotate接口
playerToJS.setVrRotate((int x, int y)->{
mVRLibrary.rotateScreen(Float.parseFloat(String.valueOf(x)),Float.parseFloat(String.valueOf(y)));
});
}
private void setPayToJS(){ payToJS = new PayToJS(this,webView); }
private void setToolToJS(){ utilToJS = new UtilToJS(this,webView); }
/*-----------------------------------功能函数 start----------------------------------*/
/**
* 判断应用是否处于前台
*
* @return true
为前台,反之为后台
*/
public boolean isRunningForeground(Context context) {
Logger.d("BaseWebActivity,isRunningForeground()");
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) return false;
List appProcessInfos = activityManager.getRunningAppProcesses();
// 枚举进程
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
Logger.d("应用处于前台状态");
return true;
}
}
}
Logger.d("应用退到后台");
return false;
}
/*
* 退出应用
* */
public void AppExit() {
Logger.d("BaseWebActivity,AppExit()");
android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
System.exit(0);
}
/*------------------------子类获取实例接口------------------------------*/
/**
* 获取当前WebView对象
*/
protected WebView getWebView(){return this.webView;}
/*
* 获取播放器实例
* */
protected BaseMediaPlayer getMediaPlayer(){return this.mHCPlayer;}
/*
* 获取PlayerToJs实例
* */
protected PlayerToJS getPlayerToJS(){return playerToJS;}
/*
* 获取PayToJS实例
* */
protected PayToJS getPayToJS(){return payToJS;}
/*
* 获取ToolToJS实例
* */
protected UtilToJS getUtilToJS(){return utilToJS;}
public interface VrRotate{
void rotate(int x, int y);
}
}