BaseWebActivity.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package com.haochuan.hciptvbasic;
  2. /*
  3. * 这是主页面的基类,负责webview的初始化工作
  4. *
  5. * */
  6. import android.annotation.SuppressLint;
  7. import android.app.ActivityManager;
  8. import android.content.Context;
  9. import android.content.res.Configuration;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.view.ViewGroup;
  13. import android.view.WindowManager;
  14. import android.webkit.WebSettings;
  15. import android.webkit.WebView;
  16. import android.webkit.WebViewClient;
  17. import androidx.annotation.Nullable;
  18. import androidx.appcompat.app.AppCompatActivity;
  19. import androidx.core.content.ContextCompat;
  20. import com.asha.vrlib.MDVRLibrary;
  21. import com.haochuan.core.BaseMediaPlayer;
  22. import com.haochuan.core.IVideoPlayer;
  23. import com.haochuan.core.Logger;
  24. import com.haochuan.core.util.ELS;
  25. import com.haochuan.core.util.HandlerUtil;
  26. import com.haochuan.hciptvbasic.util.ELSUtil;
  27. import com.haochuan.hciptvbasic.util.VRUtil;
  28. import com.haochuan.hciptvbasic.webview.PayToJS;
  29. import com.haochuan.hciptvbasic.webview.PlayerToJS;
  30. import com.haochuan.hciptvbasic.webview.HCWebChromeClient;
  31. import com.haochuan.hciptvbasic.webview.UtilToJS;
  32. import com.haochuan.ijk.IjkVideoPlayer;
  33. import com.haochuan.systemvideo.SystemVideoPlayer;
  34. import java.util.List;
  35. public abstract class BaseWebActivity extends AppCompatActivity {
  36. private WebView webView; //整个应用唯一的webview
  37. private PlayerToJS playerToJS; //PlayerToJS类实例
  38. private PayToJS payToJS; // PayToJS类实例
  39. private UtilToJS utilToJS; //ToolToJS实例
  40. String playerToJSName = PlayerToJS.class.getSimpleName(); //playerToJS类名
  41. String payToJSName = PayToJS.class.getSimpleName(); //payToJS类名
  42. String toolToJSName = UtilToJS.class.getSimpleName(); //toolToJS类名
  43. //播放器
  44. private BaseMediaPlayer mHCPlayer = null;
  45. protected ELS els;
  46. VRUtil vrUtil;
  47. //vr模块
  48. MDVRLibrary mVRLibrary;
  49. /**-----------------------虚函数-----------------------*/
  50. //获取启动页web地址
  51. protected abstract String getIndexURL();
  52. /*--------------------生命周期---------------------*/
  53. @Override
  54. protected void onCreate(@Nullable Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. webView = new WebView(this);
  57. webView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
  58. setContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  59. initWebSetting(webView);
  60. //初始化播放器和vr
  61. initPlayerAndVr();
  62. //初始化els日志记录系统
  63. els = ELS.getInstance(this);
  64. String uid = "12398658";
  65. if(utilToJS != null){
  66. uid = utilToJS.getUserName();
  67. }
  68. Logger.d("uid:" + uid);
  69. ELSUtil.Init(this,els,uid);
  70. //初始化日志
  71. Logger.init(this,getWebView());
  72. runH5();
  73. }
  74. private void runH5(){
  75. webView.loadUrl(getIndexURL());
  76. }
  77. @Override
  78. protected void onStart() {
  79. super.onStart();
  80. //关闭软键盘
  81. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  82. }
  83. @Override
  84. protected void onResume() {
  85. super.onResume();
  86. if (webView != null) {
  87. webView.onResume();
  88. }
  89. if(mHCPlayer !=null){
  90. mHCPlayer.resume();
  91. }
  92. if(mVRLibrary != null){
  93. mVRLibrary.onResume(this);
  94. }
  95. }
  96. @Override
  97. protected void onPause() {
  98. super.onPause();
  99. if (webView != null) {
  100. webView.onPause();
  101. }
  102. if(mHCPlayer !=null){
  103. mHCPlayer.pause();
  104. }
  105. if(mVRLibrary != null){
  106. mVRLibrary.onPause(this);
  107. }
  108. }
  109. @Override
  110. protected void onStop(){
  111. super.onStop();
  112. }
  113. @Override
  114. protected void onDestroy() {
  115. if (webView != null) {
  116. webView.removeJavascriptInterface(playerToJSName);
  117. webView.clearCache(true);
  118. webView.clearFormData();
  119. webView.clearMatches();
  120. webView.clearSslPreferences();
  121. webView.clearDisappearingChildren();
  122. webView.clearHistory();
  123. webView.clearAnimation();
  124. webView.loadUrl("about:blank");
  125. webView.removeAllViews();
  126. webView = null;
  127. }
  128. if(mHCPlayer !=null){
  129. mHCPlayer.release();
  130. }
  131. if(mVRLibrary != null){
  132. mVRLibrary.onDestroy();
  133. }
  134. super.onDestroy();
  135. }
  136. @Override
  137. public void onConfigurationChanged(Configuration newConfig) {
  138. super.onConfigurationChanged(newConfig);
  139. mVRLibrary.onOrientationChanged(this);
  140. }
  141. /**
  142. * webView对象获取"返回"按键事件
  143. */
  144. @Override
  145. public void onBackPressed() {
  146. //如果是除未来其他版本,请用这段代码
  147. utilToJS.onBackPressed();
  148. }
  149. /*--------------------------初始化函数---------------------------*/
  150. /*
  151. * 初始化播放器
  152. * */
  153. private void initPlayer(){
  154. Logger.d("BaseWebActivity,initPlayer()");
  155. mHCPlayer = new SystemVideoPlayer(this);
  156. mHCPlayer.setVideoPlayerListener(new IVideoPlayer() {
  157. @Override
  158. public void onPreparing() {
  159. playerToJS.onPlayerPreparing();
  160. }
  161. @Override
  162. public void onPlaying() {
  163. playerToJS.onPlayerPlaying();
  164. }
  165. @Override
  166. public void onResume() {
  167. playerToJS.onPlayerResume();
  168. }
  169. @Override
  170. public void onPause() {
  171. playerToJS.onPlayerPause();
  172. }
  173. @Override
  174. public void onDestroy() {
  175. }
  176. @Override
  177. public void onPlayingBuffering() {
  178. playerToJS.onPlayingBuffer();
  179. }
  180. @Override
  181. public void onCompletion() {
  182. playerToJS.onPlayerComplete();
  183. }
  184. @Override
  185. public void onError(int what, int extra) {
  186. playerToJS.onPlayerError(what,extra);
  187. //取消提示框显示,改由前端页面实现
  188. /* if(BuildConfig.player_type == 2){
  189. CNTVPlayerErrorAlert();
  190. }*/
  191. }
  192. });
  193. }
  194. /*
  195. * 初始化vrLib库
  196. * */
  197. private void initVrLib(){
  198. if(vrUtil == null){
  199. vrUtil = new VRUtil();
  200. }
  201. mVRLibrary = vrUtil.createVRLibrary(this,mHCPlayer);
  202. }
  203. /**
  204. *
  205. * 初始化播放器和vr
  206. */
  207. public void initPlayerAndVr(){
  208. HandlerUtil.runOnUiThread(()->{
  209. Logger.d("刷新播放器");
  210. //先将播放器和vr库消除
  211. mHCPlayer = null;
  212. mVRLibrary = null;
  213. if(vrUtil !=null){
  214. vrUtil.release(this);
  215. }
  216. //再初始化播放器和vr库
  217. initPlayer();
  218. initVrLib();
  219. getPlayerToJS().setBaseMediaPlayer(mHCPlayer);
  220. });
  221. }
  222. /**
  223. * 初始化webview
  224. * @param webView
  225. */
  226. @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface", "AddJavascriptInterface"})
  227. private void initWebSetting(WebView webView) {
  228. Logger.d("BaseWebActivity,initWebSetting()");
  229. try{
  230. WebSettings webSettings = webView.getSettings();
  231. // 由H5端适配屏幕,具体参考文档:https://developer.chrome.com/multidevice/webview/pixelperfect
  232. webSettings.setUseWideViewPort(true);
  233. webSettings.setLoadWithOverviewMode(true);
  234. webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  235. // 设置JS交互
  236. webSettings.setJavaScriptEnabled(true);
  237. HCWebChromeClient hcWebChromeClient = new HCWebChromeClient();
  238. setPlayerToJS();
  239. setPayToJS();
  240. setToolToJS();
  241. webView.addJavascriptInterface(playerToJS,playerToJSName);
  242. webView.addJavascriptInterface(payToJS,payToJSName);
  243. webView.addJavascriptInterface(utilToJS,toolToJSName);
  244. // 设置WebClient
  245. webView.setWebViewClient(new WebViewClient());
  246. webView.setWebChromeClient(hcWebChromeClient);
  247. // 设置是否开启web内容调试,具体调试方式查看:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_source=dcc&utm_medium=redirect&utm_campaign=2016q3
  248. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  249. WebView.setWebContentsDebuggingEnabled(BuildConfig.isDebug);
  250. }
  251. webView.requestFocus();
  252. }catch (Exception e){
  253. e.printStackTrace();
  254. }
  255. }
  256. private void setPlayerToJS(){
  257. playerToJS = new PlayerToJS(this,webView,mHCPlayer);
  258. //添加VrRotate接口
  259. playerToJS.setVrRotate((int x, int y)->{
  260. mVRLibrary.rotateScreen(Float.parseFloat(String.valueOf(x)),Float.parseFloat(String.valueOf(y)));
  261. });
  262. }
  263. private void setPayToJS(){ payToJS = new PayToJS(this,webView); }
  264. private void setToolToJS(){ utilToJS = new UtilToJS(this,webView); }
  265. /*-----------------------------------功能函数 start----------------------------------*/
  266. /**
  267. * 判断应用是否处于前台
  268. *
  269. * @return <code>true</code>为前台,反之为后台
  270. */
  271. public boolean isRunningForeground(Context context) {
  272. Logger.d("BaseWebActivity,isRunningForeground()");
  273. ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  274. if (activityManager == null) return false;
  275. List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
  276. // 枚举进程
  277. for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
  278. if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  279. if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
  280. Logger.d("应用处于前台状态");
  281. return true;
  282. }
  283. }
  284. }
  285. Logger.d("应用退到后台");
  286. return false;
  287. }
  288. /*
  289. * 退出应用
  290. * */
  291. public void AppExit() {
  292. Logger.d("BaseWebActivity,AppExit()");
  293. android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
  294. System.exit(0);
  295. }
  296. /*------------------------子类获取实例接口------------------------------*/
  297. /**
  298. * 获取当前WebView对象
  299. */
  300. protected WebView getWebView(){return this.webView;}
  301. /*
  302. * 获取播放器实例
  303. * */
  304. protected BaseMediaPlayer getMediaPlayer(){return this.mHCPlayer;}
  305. /*
  306. * 获取PlayerToJs实例
  307. * */
  308. protected PlayerToJS getPlayerToJS(){return playerToJS;}
  309. /*
  310. * 获取PayToJS实例
  311. * */
  312. protected PayToJS getPayToJS(){return payToJS;}
  313. /*
  314. * 获取ToolToJS实例
  315. * */
  316. protected UtilToJS getUtilToJS(){return utilToJS;}
  317. public interface VrRotate{
  318. void rotate(int x, int y);
  319. }
  320. }