package com.weikou.beibeivideo.ui.video; import android.app.Instrumentation; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.PixelFormat; import android.net.Uri; import android.net.http.SslError; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import androidx.core.app.NotificationCompat; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.webkit.ConsoleMessage; import android.webkit.DownloadListener; import android.webkit.JavascriptInterface; import android.webkit.SslErrorHandler; import android.webkit.WebChromeClient; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.androidquery.AQuery; import com.lcjian.library.util.ManifestDataUtil; import com.lcjian.library.util.common.ClipboardUtil; import com.lcjian.library.util.common.StringUtils; import com.weikou.beibeivideo.R; import com.weikou.beibeivideo.ui.BaseActivity; import com.weikou.beibeivideo.ui.media.VideoDetailActivity2; import com.weikou.beibeivideo.util.ConfigUtil; import com.weikou.beibeivideo.util.downutil.DownFiles; import com.weikou.beibeivideo.util.downutil.DownFiles.IProgress; import com.weikou.beibeivideo.util.ui.TopStatusSettings; import com.yeshi.video.player.util.PlayerFullScreenUtil; import java.net.URI; import java.util.Set; public class VideoPlayerBrowserActivity extends BaseActivity implements OnClickListener { private TextView tv_top_bar_left; private TextView tv_url; private TextView tv_title; private TextView tv_right; private WebView webview; private FrameLayout fl_webview; ProgressBar progressBar; private final String TAG = "FXBrowserActivity"; private Runnable playerJSRunnable = null; private AQuery query; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.browser_fengxing_activity); query = new AQuery(this); TopStatusSettings.setStatusViewAndDeepColor(this); tv_top_bar_left = findViewById(R.id.tv_top_bar_left); tv_url = findViewById(R.id.tv_url); tv_title = findViewById(R.id.tv_title); tv_right = findViewById(R.id.tv_right); tv_right.setOnClickListener(this); query.id(R.id.tv_more).clicked(this); query.id(R.id.fl_menu).clicked(new OnClickListener() { @Override public void onClick(View v) { hiddenMenu(); } }); query.id(R.id.tv_refresh).clicked(new OnClickListener() { @Override public void onClick(View v) { hiddenMenu(); webview.reload(); } }); query.id(R.id.tv_copy).clicked(new OnClickListener() { @Override public void onClick(View v) { hiddenMenu(); ClipboardUtil.copy(getApplicationContext(), tv_url.getText().toString()); Toast.makeText(VideoPlayerBrowserActivity.this, "复制成功", Toast.LENGTH_SHORT).show(); } }); query.id(R.id.tv_open).clicked(new OnClickListener() { @Override public void onClick(View v) { hiddenMenu(); String url = tv_url.getText().toString(); try { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(VideoPlayerBrowserActivity.this, "无法找到浏览器", Toast.LENGTH_SHORT).show(); } catch (Exception e) { } } }); fl_webview = findViewById(R.id.fl_webview); tv_top_bar_left.setOnClickListener(this); webview = findViewById(R.id.webView); WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setPluginState(WebSettings.PluginState.ON); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } webview.addJavascriptInterface(new YeshiPlayerJavascriptInterface(), "yeshiPlayer"); webview.setDrawingCacheBackgroundColor(0x00000000); webview.setFocusableInTouchMode(true); webview.setFocusable(true); webview.setAnimationCacheEnabled(false); webview.setDrawingCacheEnabled(false); webview.setWillNotCacheDrawing(true); webview.setAlwaysDrawnWithCacheEnabled(false); webview.setScrollbarFadingEnabled(true); webview.setSaveEnabled(true); webview.setDownloadListener(new MyWebViewDownLoadListener()); webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i(TAG, "shouldOverrideUrlLoading:" + url); return super.shouldOverrideUrlLoading(view, url); } @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { String url = request.getUrl().toString(); Log.i(TAG, "shouldOverrideUrlLoading:" + url); if (url != null && url.startsWith("buwanprotocol://")) { String murl = url.split("buwanprotocol://")[1]; String[] params = murl.split("#"); if (params[0].equalsIgnoreCase("playvideo")) { Intent intent = new Intent(VideoPlayerBrowserActivity.this, VideoDetailActivity2.class); Bundle bundle = new Bundle(); bundle.putString("Id", params[1]); bundle.putString("ResourceId", params[2]); bundle.putString("DetailId", params[3]); bundle.putString("Share", "0"); bundle.putString("ThirdType", "0"); intent.putExtra("from", "web"); intent.putExtras(bundle); startActivity(intent); } return true; } else if (url.contains("/tbopen/") || url.startsWith("tbopen://")) { return true; } else if (!url.startsWith("http")) { Set prefixSet = ConfigUtil.getPlayerJumpOutProtocolPrefix(getApplicationContext()); prefixSet.add("tenvideo2://"); prefixSet.add("iqiyi://"); prefixSet.add("youku://"); prefixSet.add("imgotv://"); prefixSet.add("txvideo://"); for (String prefix : prefixSet) { if (url.startsWith(prefix)) { try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); } catch (Exception e) { } return true; } } return true; } else if (url.contains("/a.app.qq.com/")&&url.contains("com.qiyi.video")) { Uri uri = Uri.parse(url); if(uri!=null) { String packageName = uri.getQueryParameter("pkgname"); if(packageName==null){ return false; } runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(VideoPlayerBrowserActivity.this, "正在打开应用市场...", Toast.LENGTH_SHORT).show(); } }); try { // 优先尝试Google Play startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))); } catch (ActivityNotFoundException e) { // 如果Google Play不存在,尝试其他市场 try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("amzn://apps/android?p=" + packageName))); } catch (ActivityNotFoundException e2) { } } return true; } } return super.shouldOverrideUrlLoading(view, request); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { Log.i(TAG, "onPageStarted:" + url); super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView webView, String s) { super.onPageFinished(webView, s); Log.i(TAG, "onPageFinished:" + s); if (playerJSRunnable != null && webview != null) webview.removeCallbacks(playerJSRunnable); playerJSRunnable = new Runnable() { @Override public void run() { if (webview == null || webView == null) return; String js = null; String url = webview.getUrl(); if (webView != null && !StringUtils.isEmpty(url)) { if (url.contains(".fun.tv")) {//风行 js = PlayerFullScreenUtil.getFunShionFullScreenJS(getApplicationContext()); } else if (url.contains("m.acfun.cn")) {//Acfun js = PlayerFullScreenUtil.getAcfunFullScreenJS(getApplicationContext()); } if (!StringUtils.isEmpty(js)) webView.loadUrl("javascript:" + js); } } }; if (playerJSRunnable != null && webview != null) webview.postDelayed(playerJSRunnable, 1000); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } }); webview.setWebChromeClient(new WebChromeClient() { View myVideoView; View myNormalView; CustomViewCallback callback; @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); tv_title.setText(title); tv_url.setText(view.getUrl()); } /** * 全屏播放配置 */ @Override public void onShowCustomView(View view, CustomViewCallback customViewCallback) { LinearLayout normalView = (LinearLayout) findViewById(R.id.ll_container); ViewGroup viewGroup = (ViewGroup) normalView.getParent(); viewGroup.removeView(normalView); viewGroup.addView(view); myVideoView = view; myNormalView = normalView; callback = customViewCallback; setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs); } @Override public void onHideCustomView() { if (callback != null) { callback.onCustomViewHidden(); callback = null; } if (myVideoView != null) { ViewGroup viewGroup = (ViewGroup) myVideoView.getParent(); viewGroup.removeView(myVideoView); viewGroup.addView(myNormalView); } setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs); } @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { return super.onConsoleMessage(consoleMessage); } }); progressBar = findViewById(R.id.myProgressBar); progressBar.setMax(100); progressBar.setProgressDrawable(this.getResources() .getDrawable(R.drawable.color_progressbar)); // loadHander.sendEmptyMessage(0); String url = getIntent().getStringExtra("url"); webview.loadUrl(url); } private void hiddenMenu() { query.id(R.id.fl_menu).visibility(View.GONE); } private void showMenu() { query.id(R.id.fl_menu).visibility(View.VISIBLE); } // 文件下载监听 private class MyWebViewDownLoadListener implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { startDownLoadFile(url); /* * Uri uri = Uri.parse(url); Intent intent = new * Intent(Intent.ACTION_VIEW, uri); * startActivity(intent); */ } } private NotificationManager manager; private Notification notif; private Notification.Builder oBuilder; private NotificationCompat.Builder builder; int j = -1; private void startDownLoadFile(String url) { new DownFiles(this, new IProgress() { @Override public void getProgress(int p) { // stub if (p > 99) { handler.sendEmptyMessage(1); Log.i("DownFiles", "getProgress下载进度:" + p); } else { if (manager == null) { Toast.makeText(VideoPlayerBrowserActivity.this, "文件已经开始下载", Toast.LENGTH_SHORT).show(); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); String id = "UMENG_CHANNEL"; String description = ManifestDataUtil.getAppMetaData(VideoPlayerBrowserActivity.this, id); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(id, "123", importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.RED); mChannel.enableVibration(true); mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); manager.createNotificationChannel(mChannel); oBuilder = new Notification.Builder(VideoPlayerBrowserActivity.this, id); oBuilder.setContentTitle("影视大全") .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setContentText("热门应用下载") .setAutoCancel(true) .build(); } else { builder = new NotificationCompat.Builder(VideoPlayerBrowserActivity.this); builder.setContentTitle("影视大全") .setContentText("热门应用下载") .setSmallIcon(R.drawable.ic_launcher) .setOngoing(true);//无效 } } Message msg = handler.obtainMessage(); if (j != p) { msg.what = 0; msg.arg1 = p; handler.sendMessage(msg); } j = p; } } }).execute(url); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { oBuilder.setContentText(msg.arg1 + "%"); notif = oBuilder.build(); } else { builder.setContentText(msg.arg1 + "%"); notif = builder.build(); } manager.notify(0, notif); break; case 1: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { oBuilder.setContentText("下载完成"); notif = oBuilder.build(); } else { builder.setContentText("下载完成"); notif = builder.build(); } manager.notify(0, notif); j = 100; manager.cancelAll(); break; default: break; } } }; @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override protected void onDestroy() { if (playerJSRunnable != null) webview.removeCallbacks(playerJSRunnable); if (webview != null) { webview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null); webview.clearHistory(); ((ViewGroup) webview.getParent()).removeView(webview); webview.destroy(); webview = null; } super.onDestroy(); } @Override protected void onStop() { super.onStop(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_top_bar_left: { if (!webview.canGoBack()) { finish(); } else { webview.goBack(); } } break; case R.id.tv_top_bar_left2: { finish(); } break; case R.id.tv_right: { if (query.id(R.id.fl_menu).getView().getVisibility() == View.VISIBLE) hiddenMenu(); else showMenu(); } break; default: break; } } // 设置回退 // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); findViewById(R.id.v_status_bar).setVisibility(View.VISIBLE); if (findViewById(R.id.top) != null) findViewById(R.id.top).setVisibility(View.VISIBLE); return true; } else if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack() & getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { webview.goBack(); // goBack()表示返回WebView的上一页面 return true; } return super.onKeyDown(keyCode, event); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { findViewById(R.id.v_status_bar).setVisibility(View.GONE); if (findViewById(R.id.top) != null) findViewById(R.id.top).setVisibility(View.GONE); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { findViewById(R.id.v_status_bar).setVisibility(View.VISIBLE); if (findViewById(R.id.top) != null) findViewById(R.id.top).setVisibility(View.VISIBLE); } } class YeshiPlayerJavascriptInterface { @JavascriptInterface public boolean isFullScreen() { if ((VideoPlayerBrowserActivity.this.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) { return true; } return false; } @JavascriptInterface public void setPortrait() { if (isFullScreen()) { //模拟返回按钮 new Thread(new Runnable() { @Override public void run() { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); } }).start(); } } } }