package com.hanju.video.app.ui.mine;
|
|
import android.Manifest;
|
import android.app.Notification;
|
import android.app.NotificationChannel;
|
import android.app.NotificationManager;
|
import android.content.Intent;
|
import android.content.pm.PackageManager;
|
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 androidx.annotation.Nullable;
|
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.NotificationCompat;
|
import androidx.core.content.ContextCompat;
|
|
import android.text.TextUtils;
|
import android.util.Log;
|
import android.view.KeyEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.hanju.video.app.util.common.FileUtils;
|
import com.hanju.video.app.util.downutils.StringUtils;
|
import com.hanju.lib.library.util.ManifestDataUtil;
|
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.ValueCallback;
|
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.hanju.video.app.R;
|
import com.hanju.video.app.ui.BaseActivity;
|
import com.hanju.video.app.ui.media.VideoDetailActivity;
|
import com.hanju.video.app.util.browser.HanJuJavaInterface;
|
import com.hanju.video.app.util.downutils.DownFiles;
|
import com.hanju.video.app.util.downutils.DownFiles.IProgress;
|
import com.hanju.video.app.util.ui.StatusBarUtil;
|
import com.hanju.video.app.util.x5web.X5WebView;
|
|
import java.io.File;
|
|
public class BrowserActivity extends BaseActivity implements OnClickListener {
|
private final static int FILECHOOSER_RESULTCODE = 10002;
|
|
private static final String TAG = BrowserActivity.class.getSimpleName();
|
private TextView tv_top_bar_left;
|
private TextView tv_top_bar_left2;
|
private TextView tv_top_bar_middle;
|
private X5WebView webview;
|
ProgressBar progressBar;
|
private ValueCallback mUploadMessage;
|
|
|
private void initX5WebView() {
|
webview = findViewById(R.id.webview);
|
webview.setDownloadListener(new MyWebViewDownLoadListener());
|
webview.setWebViewClient(new WebViewClient() {
|
@Override
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
Log.i(TAG, "url:" + 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(BrowserActivity.this, VideoDetailActivity.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) {
|
String url = webResourceRequest.getUrl().toString();
|
Log.i(TAG, "url:" + url);
|
return super.shouldOverrideUrlLoading(webView, webResourceRequest);
|
}
|
|
@Override
|
public void onPageFinished(WebView webView, String s) {
|
super.onPageFinished(webView, s);
|
}
|
|
@Override
|
public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
|
}
|
});
|
|
webview.setWebChromeClient(new WebChromeClient() {
|
|
@Override
|
public void onReceivedTitle(WebView view, String title) {
|
if (!StringUtils.isNullOrEmpty(wholeTitle))
|
return;
|
tv_top_bar_middle.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 openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
|
if (mUploadMessage != null) {
|
mUploadMessage.onReceiveValue(null);
|
}
|
if (lacksPermissions(Manifest.permission.READ_EXTERNAL_STORAGE)) { //存储权限未开启
|
valueCallback.onReceiveValue(null);
|
ActivityCompat.requestPermissions(BrowserActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 10023);
|
return;
|
}
|
mUploadMessage = valueCallback;
|
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
String type = TextUtils.isEmpty(acceptType) ? "*/*" : acceptType;
|
i.setType(type);
|
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
|
super.openFileChooser(valueCallback, acceptType, capture);
|
}
|
|
// 判断权限集合 是否授权 false授权 true未授权
|
public boolean lacksPermissions(String... permissions) {
|
for (String permission : permissions) {
|
if (lacksPermission(permission)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
// 判断是否缺少权限
|
private boolean lacksPermission(String permission) {
|
//权限未授权
|
return ContextCompat.checkSelfPermission(getApplicationContext(), permission) == PackageManager.PERMISSION_DENIED;
|
}
|
|
@Override
|
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) {
|
if (mUploadMessage != null) {
|
mUploadMessage.onReceiveValue(null);
|
}
|
if (lacksPermissions(Manifest.permission.READ_EXTERNAL_STORAGE)) { //存储权限未开启
|
valueCallback.onReceiveValue(null);
|
ActivityCompat.requestPermissions(BrowserActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 10023);
|
return true;
|
}
|
|
|
mUploadMessage = valueCallback;
|
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
if (fileChooserParams != null && fileChooserParams.getAcceptTypes() != null
|
&& fileChooserParams.getAcceptTypes().length > 0) {
|
i.setType(fileChooserParams.getAcceptTypes()[0]);
|
} else {
|
i.setType("*/*");
|
}
|
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
|
return true;
|
}
|
});
|
WebSettings webSetting = webview.getSettings();
|
webSetting.setJavaScriptEnabled(true);
|
webSetting.setTextZoom(100);
|
webSetting.setDomStorageEnabled(true);
|
webview.addJavascriptInterface(new HanJuJavaInterface(this, webview), "yestv");
|
}
|
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
if (requestCode == FILECHOOSER_RESULTCODE) {
|
if (null == mUploadMessage) return;
|
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
|
if (result == null) {
|
mUploadMessage.onReceiveValue(null);
|
mUploadMessage = null;
|
return;
|
}
|
String path = FileUtils.getFilePathByUri(this, result);
|
if (TextUtils.isEmpty(path)) {
|
mUploadMessage.onReceiveValue(null);
|
mUploadMessage = null;
|
return;
|
}
|
Uri uri = Uri.fromFile(new File(path));
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
mUploadMessage.onReceiveValue(new Uri[]{uri});
|
} else {
|
mUploadMessage.onReceiveValue(uri);
|
}
|
mUploadMessage = null;
|
}
|
}
|
|
private String wholeTitle;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
getWindow().setFormat(PixelFormat.TRANSLUCENT);
|
setContentView(R.layout.browser_activity);
|
StatusBarUtil.init(this);
|
tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
|
tv_top_bar_left2 = findViewById(R.id.tv_top_bar_left2);
|
tv_top_bar_left2.setVisibility(View.VISIBLE);
|
tv_top_bar_middle = findViewById(R.id.tv_top_bar_middle);
|
|
tv_top_bar_left2.setText("关闭");
|
tv_top_bar_left.setOnClickListener(this);
|
tv_top_bar_left2.setOnClickListener(this);
|
|
progressBar = findViewById(R.id.myProgressBar);
|
progressBar.setMax(100);
|
progressBar.setProgressDrawable(this.getResources()
|
.getDrawable(R.drawable.color_progressbar));
|
initX5WebView();
|
webview.loadUrl(getIntent().getStringExtra("url"));
|
wholeTitle = getIntent().getStringExtra("title");
|
if (!StringUtils.isNullOrEmpty(wholeTitle)) {
|
tv_top_bar_middle.setText(wholeTitle);
|
}
|
}
|
// 文件下载监听
|
|
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(BrowserActivity.this, "文件已经开始下载",
|
Toast.LENGTH_SHORT).show();
|
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
String id = "UMENG_CHANNEL";
|
String description = ManifestDataUtil.getAppMetaData(BrowserActivity.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(BrowserActivity.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(BrowserActivity.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();
|
MobclickAgent.onPageStart("网页");
|
// webview.reload();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
MobclickAgent.onPageEnd("网页");
|
}
|
|
@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.tv_top_bar_left2: {
|
finish();
|
}
|
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);
|
}
|
}
|