BaseWebActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package com.haochuan.hciptvbasic;
  2. /*
  3. * 这是主页面的基类,负责webview的初始化工作
  4. *
  5. * */
  6. import android.annotation.SuppressLint;
  7. import android.app.ActivityManager;
  8. import android.app.AlertDialog;
  9. import android.content.Context;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.util.Log;
  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.haochuan.core.BaseMediaPlayer;
  23. import com.haochuan.core.IVideoPlayer;
  24. import com.haochuan.core.Logger;
  25. import com.haochuan.core.util.HandlerUtil;
  26. import com.haochuan.gsyvideo.HCGsyVideoPlayer;
  27. import com.haochuan.hciptvbasic.webview.PayToJS;
  28. import com.haochuan.hciptvbasic.webview.PlayerToJS;
  29. import com.haochuan.hciptvbasic.webview.HCWebChromeClient;
  30. import com.haochuan.hciptvbasic.webview.UtilToJS;
  31. import com.haochuan.systemvideo.SystemVideoPlayer;
  32. import com.haochuan.weilai_video.CNTVLogin;
  33. import com.haochuan.weilai_video.WeiLaiVideoPlayer;
  34. import com.haochuan.weilai_video.util.ReportCNTVLog;
  35. import java.util.List;
  36. public abstract class BaseWebActivity extends AppCompatActivity {
  37. private WebView webView; //整个应用唯一的webview
  38. private PlayerToJS playerToJS; //PlayerToJS类实例
  39. private PayToJS payToJS; // PayToJS类实例
  40. private UtilToJS utilToJS; //ToolToJS实例
  41. String playerToJSName = PlayerToJS.class.getSimpleName(); //playerToJS类名
  42. String payToJSName = PayToJS.class.getSimpleName(); //payToJS类名
  43. String toolToJSName = UtilToJS.class.getSimpleName(); //toolToJS类名
  44. //播放器
  45. private BaseMediaPlayer mHCPlayer = null;
  46. /**-----------------------虚函数-----------------------*/
  47. //获取启动页web地址
  48. protected abstract String getIndexURL();
  49. /*--------------------生命周期---------------------*/
  50. @Override
  51. protected void onCreate(@Nullable Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. //初始化日志
  54. Logger.init(this,getWebView());
  55. //如果是未来版本,需要先初始化其sdk
  56. if(BuildConfig.player_type == 2){
  57. CNTVInit();
  58. }
  59. //初始化播放器
  60. initPlayer();
  61. webView = new WebView(this);
  62. webView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
  63. setContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  64. initWebSetting(webView);
  65. //未来版本需要先初始化SDK成功再加载页面
  66. if(BuildConfig.player_type != 2){
  67. runH5();
  68. }
  69. }
  70. private void runH5(){
  71. webView.loadUrl(getIndexURL());
  72. }
  73. @Override
  74. protected void onStart() {
  75. super.onStart();
  76. //关闭软键盘
  77. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  78. }
  79. @Override
  80. protected void onResume() {
  81. super.onResume();
  82. if (webView != null) {
  83. webView.onResume();
  84. }
  85. if(mHCPlayer !=null){
  86. mHCPlayer.resume();
  87. }
  88. }
  89. @Override
  90. protected void onPause() {
  91. super.onPause();
  92. if (webView != null) {
  93. webView.onPause();
  94. }
  95. if(mHCPlayer !=null){
  96. mHCPlayer.pause();
  97. }
  98. }
  99. @Override
  100. protected void onStop(){
  101. super.onStop();
  102. boolean isForeground = isRunningForeground(this);
  103. if (!isForeground) {
  104. Handler handler = new Handler(getMainLooper());
  105. handler.postDelayed(() -> AppExit(), 500);
  106. }
  107. }
  108. @Override
  109. protected void onDestroy() {
  110. if (webView != null) {
  111. webView.removeJavascriptInterface(playerToJSName);
  112. webView.clearCache(true);
  113. webView.clearFormData();
  114. webView.clearMatches();
  115. webView.clearSslPreferences();
  116. webView.clearDisappearingChildren();
  117. webView.clearHistory();
  118. webView.clearAnimation();
  119. webView.loadUrl("about:blank");
  120. webView.removeAllViews();
  121. webView = null;
  122. }
  123. if(mHCPlayer !=null){
  124. mHCPlayer.release();
  125. }
  126. super.onDestroy();
  127. }
  128. /**
  129. * webView对象获取"返回"按键事件
  130. */
  131. @Override
  132. public void onBackPressed() {
  133. if(CNTVLogin.getInstance().isOpenAdshow()){
  134. Log.d("djbl","onBackPressed close ad");
  135. ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
  136. CNTVLogin.getInstance().removeADImage(this,viewGroup);
  137. }else{
  138. utilToJS.onBackPressed();
  139. }
  140. }
  141. /*--------------------------初始化函数---------------------------*/
  142. /*
  143. * 初始化播放器
  144. * */
  145. private void initPlayer(){
  146. switch (BuildConfig.player_type){
  147. case 1:
  148. mHCPlayer = new SystemVideoPlayer(this);
  149. break;
  150. case 2:
  151. mHCPlayer = new WeiLaiVideoPlayer(this);
  152. break;
  153. case 3:
  154. mHCPlayer = new HCGsyVideoPlayer(this);
  155. break;
  156. default:
  157. break;
  158. }
  159. mHCPlayer.setVideoPlayerListener(new IVideoPlayer() {
  160. @Override
  161. public void onPreparing() {
  162. playerToJS.onPlayerPreparing();
  163. }
  164. @Override
  165. public void onPlaying() {
  166. playerToJS.onPlayerPlaying();
  167. }
  168. @Override
  169. public void onResume() {
  170. playerToJS.onPlayerResume();
  171. }
  172. @Override
  173. public void onPause() {
  174. playerToJS.onPlayerPause();
  175. }
  176. @Override
  177. public void onDestroy() {
  178. }
  179. @Override
  180. public void onPlayingBuffering() {
  181. playerToJS.onPlayingBuffer();
  182. }
  183. @Override
  184. public void onCompletion() {
  185. playerToJS.onPlayerComplete();
  186. }
  187. @Override
  188. public void onError(int what, int extra) {
  189. playerToJS.onPlayerError(what,extra);
  190. if(BuildConfig.player_type == 2){
  191. CNTVPlayerErrorAlert();
  192. }
  193. }
  194. });
  195. }
  196. /*---------------------------------------未来电视方法-------------------------------------*/
  197. //这部分代码只在接入未来电视播放时有,如果不是,请注释掉
  198. /*
  199. *初始化未来SDK
  200. * 加载未来电视广告图片
  201. * */
  202. private void CNTVInit(){
  203. CNTVLogin.getInstance().init(this, new CNTVLogin.OnCNTVListener() {
  204. @Override
  205. public void onOttLoginSuccess() {
  206. runH5();
  207. }
  208. @Override
  209. public void onOttLoginFail(String code, String msg) {
  210. OttLoginFailAlert(code,msg);
  211. }
  212. @Override
  213. public void onOttLoginError(Throwable throwable) {
  214. OttLoginFailAlert("-1",throwable == null ? "发生未知异常" : throwable.getMessage());
  215. }
  216. });
  217. ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
  218. CNTVLogin.getInstance().showAdImage(this,viewGroup);
  219. }
  220. /*
  221. *ott登陆失败提示
  222. * */
  223. private void OttLoginFailAlert(String code,String message){
  224. HandlerUtil.runOnUiThread(()->{
  225. String msg = String.format("认证失败 code:%s,失败信息:%s",code,message);
  226. new AlertDialog.Builder(this)
  227. .setTitle("ott认证失败")
  228. .setMessage(msg)
  229. .setPositiveButton("确定", (dialog, which) -> {
  230. dialog.dismiss();
  231. AppExit();
  232. })
  233. .show();
  234. });
  235. }
  236. /*
  237. * 未来电视功能:视频播放错误,提示并且退出播放
  238. * */
  239. private void CNTVPlayerErrorAlert(){
  240. HandlerUtil.runOnUiThread(()->{
  241. new AlertDialog.Builder(BaseWebActivity.this)
  242. .setTitle("提示")
  243. .setMessage("该节目已下线!")
  244. .setCancelable(false)
  245. .setPositiveButton("确定", (dialog, which) -> {
  246. playerToJS.stop();
  247. }).show();
  248. });
  249. }
  250. /*---------------------------------------未来电视方法 end-------------------------------------*/
  251. @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface", "AddJavascriptInterface"})
  252. private void initWebSetting(WebView webView) {
  253. WebSettings webSettings = webView.getSettings();
  254. // 由H5端适配屏幕,具体参考文档:https://developer.chrome.com/multidevice/webview/pixelperfect
  255. webSettings.setUseWideViewPort(true);
  256. webSettings.setLoadWithOverviewMode(true);
  257. webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  258. // 设置JS交互
  259. webSettings.setJavaScriptEnabled(true);
  260. HCWebChromeClient hcWebChromeClient = new HCWebChromeClient();
  261. setPlayerToJS();
  262. setPayToJS();
  263. setToolToJS();
  264. webView.addJavascriptInterface(playerToJS,playerToJSName);
  265. webView.addJavascriptInterface(payToJS,payToJSName);
  266. webView.addJavascriptInterface(utilToJS,toolToJSName);
  267. // 设置WebClient
  268. webView.setWebViewClient(new WebViewClient());
  269. webView.setWebChromeClient(hcWebChromeClient);
  270. // 设置是否开启web内容调试,具体调试方式查看:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_source=dcc&utm_medium=redirect&utm_campaign=2016q3
  271. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  272. WebView.setWebContentsDebuggingEnabled(BuildConfig.isDebug);
  273. }
  274. }
  275. private void setPlayerToJS(){
  276. playerToJS = new PlayerToJS(this,webView,mHCPlayer);
  277. }
  278. private void setPayToJS(){ payToJS = new PayToJS(this,webView); }
  279. private void setToolToJS(){ utilToJS = new UtilToJS(this,webView); }
  280. /*-----------------------------------功能函数 start----------------------------------*/
  281. /**
  282. * 判断应用是否处于前台
  283. *
  284. * @return <code>true</code>为前台,反之为后台
  285. */
  286. public boolean isRunningForeground(Context context) {
  287. ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  288. if (activityManager == null) return false;
  289. List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
  290. // 枚举进程
  291. for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
  292. if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  293. if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
  294. Logger.d("应用处于前台状态");
  295. return true;
  296. }
  297. }
  298. }
  299. Logger.d("应用退到后台");
  300. return false;
  301. }
  302. /*
  303. * 退出应用
  304. * */
  305. public void AppExit() {
  306. if(BuildConfig.player_type == 2){
  307. new ReportCNTVLog().reportExitLog();
  308. }
  309. android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
  310. System.exit(0);
  311. }
  312. /*------------------------子类获取实例接口------------------------------*/
  313. /**
  314. * 获取当前WebView对象
  315. */
  316. protected WebView getWebView(){return this.webView;}
  317. /*
  318. * 获取播放器实例
  319. * */
  320. protected BaseMediaPlayer getMediaPlayer(){return this.mHCPlayer;}
  321. /*
  322. * 获取PlayerToJs实例
  323. * */
  324. protected PlayerToJS getPlayerToJS(){return playerToJS;}
  325. /*
  326. * 获取PayToJS实例
  327. * */
  328. protected PayToJS getPayToJS(){return payToJS;}
  329. /*
  330. * 获取ToolToJS实例
  331. * */
  332. protected UtilToJS getUtilToJS(){return utilToJS;}
  333. }