BaseWebActivity.java 14 KB

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