package com.weikou.beibeivideo.ui.video;
|
|
import android.app.Notification;
|
import android.app.NotificationChannel;
|
import android.app.NotificationManager;
|
import android.content.Intent;
|
import android.content.pm.ActivityInfo;
|
import android.content.res.Configuration;
|
import android.graphics.BitmapFactory;
|
import android.graphics.Color;
|
import android.graphics.PixelFormat;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.support.v4.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.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.LinearLayout.LayoutParams;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.lcjian.library.util.ManifestDataUtil;
|
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
|
import com.tencent.smtt.export.external.interfaces.SslError;
|
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
|
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
|
import com.tencent.smtt.sdk.DownloadListener;
|
import com.tencent.smtt.sdk.WebChromeClient;
|
import com.tencent.smtt.sdk.WebSettings;
|
import com.tencent.smtt.sdk.WebView;
|
import com.tencent.smtt.sdk.WebViewClient;
|
import com.umeng.analytics.MobclickAgent;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.ui.BaseActivity;
|
import com.weikou.beibeivideo.ui.media.VideoDetailActivity;
|
import com.weikou.beibeivideo.ui.media.VideoDetailActivity2;
|
import com.weikou.beibeivideo.util.browser.BWJavaInterface;
|
import com.weikou.beibeivideo.util.downutil.DownFiles;
|
import com.weikou.beibeivideo.util.downutil.DownFiles.IProgress;
|
import com.weikou.beibeivideo.util.x5.X5PlayerWebView;
|
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback;
|
|
/**
|
* 视频播放网页
|
*/
|
public class VideoPlayerActivity extends BaseActivity implements OnClickListener {
|
|
private TextView tv_top_bar_left;
|
private TextView tv_title;
|
private TextView tv_url;
|
private ImageView iv_refresh;
|
private X5PlayerWebView webview;
|
ProgressBar progressBar;
|
|
|
private void initX5WebView() {
|
FrameLayout webViewContainer = findViewById(R.id.webview_container);
|
webview = new X5PlayerWebView(this, null);
|
webViewContainer.addView(webview, new FrameLayout.LayoutParams(
|
FrameLayout.LayoutParams.MATCH_PARENT,
|
FrameLayout.LayoutParams.MATCH_PARENT));
|
webview.setDownloadListener(new MyWebViewDownLoadListener());
|
|
webview.setWebViewClient(new WebViewClient() {
|
@Override
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
if (url != null && url.startsWith("weixin://")) {
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
startActivity(intent);
|
return true;
|
} else 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(VideoPlayerActivity.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.putExtras(bundle);
|
startActivity(intent);
|
}
|
return true;
|
} else if (url != null && (!url.startsWith("http"))) {
|
return true;
|
}
|
return super.shouldOverrideUrlLoading(view, url);
|
}
|
|
@Override
|
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
|
return super.shouldOverrideUrlLoading(webView, webResourceRequest);
|
}
|
|
@Override
|
public void onPageFinished(WebView webView, String s) {
|
super.onPageFinished(webView, s);
|
//acfun全屏
|
if (webView.getUrl() != null && webView.getUrl().contains("m.acfun.cn"))
|
webView.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
String js = "setInterval(function(){ $('.acmplayer-fullscreen-btn').click(function(){";
|
js += "var el= document.getElementById('mPlayer');";
|
js += "if (el.requestFullscreen) {\n" +
|
"el.requestFullscreen();\n" +
|
"} else if (el.msRequestFullscreen) {\n" +
|
"el.msRequestFullscreen();\n" +
|
"} else if (el.mozRequestFullScreen) {\n" +
|
"el.mozRequestFullScreen();\n" +
|
"} else if (el.webkitRequestFullscreen) {\n" +
|
"el.webkitRequestFullscreen();\n" +
|
"}";
|
js += "});},2000);";
|
if (webView != null)
|
webView.loadUrl("javascript:" + js);
|
}
|
}, 1000);
|
}
|
|
@Override
|
public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
|
}
|
});
|
|
webview.setWebChromeClient(new WebChromeClient() {
|
|
View myVideoView;
|
View myNormalView;
|
IX5WebChromeClient.CustomViewCallback callback;
|
|
@Override
|
public void onReceivedTitle(WebView view, String title) {
|
tv_title.setText(title);
|
}
|
|
@Override
|
public void onProgressChanged(WebView webView, int i) {
|
if (i == 100) {
|
progressBar.setVisibility(View.GONE);
|
} else {
|
if (View.INVISIBLE == progressBar.getVisibility()) {
|
progressBar.setVisibility(View.VISIBLE);
|
}
|
progressBar.setProgress(i);
|
}
|
super.onProgressChanged(webView, i);
|
}
|
|
/**
|
* 全屏播放配置
|
*/
|
@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);
|
}
|
|
});
|
WebSettings webSetting = webview.getSettings();
|
webSetting.setJavaScriptEnabled(true);
|
webSetting.setTextZoom(100);
|
webview.addJavascriptInterface(new BWJavaInterface(this, webview), "yestv");
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
getWindow().setFormat(PixelFormat.TRANSLUCENT);
|
setContentView(R.layout.activity_video_player);
|
initStatusBar();
|
initX5WebView();
|
|
tv_title = findViewById(R.id.tv_title);
|
tv_url = findViewById(R.id.tv_url);
|
iv_refresh = findViewById(R.id.iv_refresh);
|
tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
|
|
iv_refresh.setOnClickListener(this);
|
tv_top_bar_left.setOnClickListener(this);
|
|
progressBar = (ProgressBar) findViewById(R.id.myProgressBar);
|
progressBar.setMax(100);
|
progressBar.setProgressDrawable(this.getResources()
|
.getDrawable(R.drawable.color_progressbar));
|
|
String url = getIntent().getStringExtra("url");
|
webview.loadUrl(url);
|
tv_url.setText(webview.getUrl());
|
}
|
// 文件下载监听
|
|
private class MyWebViewDownLoadListener implements DownloadListener {
|
|
@Override
|
public void onDownloadStart(String url, String userAgent,
|
String contentDisposition, String mimetype, long contentLength) {
|
startDownLoadFile(url);
|
}
|
}
|
|
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(VideoPlayerActivity.this, "文件已经开始下载",
|
Toast.LENGTH_SHORT).show();
|
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
String id = "UMENG_CHANNEL";
|
String description = ManifestDataUtil.getAppMetaData(VideoPlayerActivity.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(VideoPlayerActivity.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(VideoPlayerActivity.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 (webview != null) {
|
webview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
|
webview.clearHistory();
|
|
((ViewGroup) webview.getParent()).removeView(webview);
|
webview.destroy();
|
webview = null;
|
}
|
super.onDestroy();
|
}
|
|
@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.iv_refresh:
|
webview.reload();
|
break;
|
default:
|
break;
|
}
|
}
|
|
// 设置回退
|
// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
|
webview.goBack(); // goBack()表示返回WebView的上一页面
|
return true;
|
}
|
return super.onKeyDown(keyCode, event);
|
}
|
|
@Override
|
public void onConfigurationChanged(Configuration newConfig) {
|
super.onConfigurationChanged(newConfig);
|
}
|
}
|