package com.weikou.beibeivideo.ui.mine; 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.http.SslError; 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.webkit.DownloadListener; import android.webkit.SslErrorHandler; 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.lcjian.library.util.ManifestDataUtil; import com.umeng.analytics.MobclickAgent; import com.weikou.beibeivideo.ui.BaseActivity; import com.weikou.beibeivideo.ui.media.VideoDetailActivity; import com.weikou.beibeivideo.util.downutil.DownFiles; import com.weikou.beibeivideo.util.downutil.DownFiles.IProgress; import com.weikou.beibeivideo.widget.CustomWebView; import com.yeshi.buwanshequ.R; public class FXBrowserActivity extends BaseActivity implements OnClickListener { private TextView tv_top_bar_left; private TextView tv_top_bar_left2; private TextView tv_top_bar_middle; private TextView tv_top_bar_right; private CustomWebView webview; private FrameLayout fl_webview, fl_full_play; ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.browser_fengxing_activity); /* * 计算状态栏高度并设置 */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, result); findViewById(R.id.v_status_bar).setLayoutParams(params); } else { findViewById(R.id.v_status_bar).setVisibility(View.GONE); } // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // SystemBarTintManager tintManager = new SystemBarTintManager(this); // tintManager.setStatusBarTintEnabled(true); // //此处可以重新指定状态栏颜色 // tintManager.setStatusBarTintResource(R.color.blue1); // } tv_top_bar_left = (TextView) findViewById(R.id.tv_top_bar_left); tv_top_bar_left2 = (TextView) findViewById(R.id.tv_top_bar_left2); tv_top_bar_left2.setVisibility(View.VISIBLE); tv_top_bar_middle = (TextView) findViewById(R.id.tv_top_bar_middle); tv_top_bar_right = (TextView) findViewById(R.id.tv_top_bar_right); fl_webview = (FrameLayout) findViewById(R.id.fl_webview); fl_full_play = (FrameLayout) findViewById(R.id.fl_full_play); tv_top_bar_left2.setText("关闭"); tv_top_bar_right.setText("横屏"); tv_top_bar_right.setOnClickListener(this); tv_top_bar_left.setOnClickListener(this); tv_top_bar_left2.setOnClickListener(this); tv_top_bar_right.setOnClickListener(this); webview = new CustomWebView(FXBrowserActivity.this); fl_webview.addView(webview, new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setPluginState(WebSettings.PluginState.ON); 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) { // Toast.makeText(FXBrowserActivity.this, "请求路劲----" + url, Toast.LENGTH_LONG).show(); 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(FXBrowserActivity.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.contains("/tbopen/") || url.startsWith("tbopen://")) { return true; } return super.shouldOverrideUrlLoading(view, url); // return true; } // @Override // public WebResourceResponse shouldInterceptRequest(WebView view, String url) { // if (url != null && (url.startsWith("tbopen://") || url.contains("intent"))) { // url = ""; // Toast.makeText(FXBrowserActivity.this, "请求路劲----" + url, Toast.LENGTH_LONG).show(); // } // return super.shouldInterceptRequest(view, url); // } @Override public void onPageFinished(WebView webView, String s) { super.onPageFinished(webView, s); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } }); progressBar = (ProgressBar) findViewById(R.id.myProgressBar); progressBar.setMax(100); progressBar.setProgressDrawable(this.getResources() .getDrawable(R.drawable.color_progressbar)); loadHander.sendEmptyMessage(0); } Handler loadHander = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); String url = getIntent().getStringExtra("url"); webview.loadUrl(url); } }; // 文件下载监听 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(FXBrowserActivity.this, "文件已经开始下载", Toast.LENGTH_SHORT).show(); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); String id = "UMENG_CHANNEL"; String description = ManifestDataUtil.getAppMetaData(FXBrowserActivity.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(FXBrowserActivity.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(FXBrowserActivity.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("网页"); } @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 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_top_bar_right: { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); findViewById(R.id.v_status_bar).setVisibility(View.GONE); findViewById(R.id.top).setVisibility(View.GONE); } 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); 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); findViewById(R.id.top).setVisibility(View.GONE); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { findViewById(R.id.v_status_bar).setVisibility(View.VISIBLE); findViewById(R.id.top).setVisibility(View.VISIBLE); } } }