diff --git a/ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java b/ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java index 64a4b57..ccff876 100644 --- a/ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java +++ b/ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java @@ -30,12 +30,12 @@ */ public class ByWebChromeClient extends WebChromeClient { - private WeakReference mActivityWeakReference = null; + private WeakReference mActivityWeakReference; private ByWebView mByWebView; private ValueCallback mUploadMessage; private ValueCallback mUploadMessageForAndroid5; - private static int RESULT_CODE_FILE_CHOOSER = 1; - private static int RESULT_CODE_FILE_CHOOSER_FOR_ANDROID_5 = 2; + private static final int RESULT_CODE_FILE_CHOOSER = 1; + private static final int RESULT_CODE_FILE_CHOOSER_FOR_ANDROID_5 = 2; private View mProgressVideo; private View mCustomView; @@ -73,7 +73,10 @@ public void onShowCustomView(View view, CustomViewCallback callback) { Activity mActivity = this.mActivityWeakReference.get(); if (mActivity != null && !mActivity.isFinishing()) { if (!isFixScreenLandscape) { - mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (onByWebChromeCallback == null || !onByWebChromeCallback.onHandleScreenOrientation(true)) { + // 为空或返回为ture时,自己处理横竖屏。否则全屏时默认设置为横屏 + mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } } mByWebView.getWebView().setVisibility(View.INVISIBLE); @@ -108,7 +111,10 @@ public void onHideCustomView() { } // 还原到之前的屏幕状态 if (!isFixScreenPortrait) { - mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + if (onByWebChromeCallback == null || !onByWebChromeCallback.onHandleScreenOrientation(false)) { + // 为空或返回为ture时,自己处理横竖屏。否则默认设置为竖屏 + mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } mCustomView.setVisibility(View.GONE); diff --git a/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java b/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java index fee21e7..3fd08fd 100644 --- a/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java +++ b/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java @@ -137,6 +137,7 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc @Override public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) { if (onByWebClientCallback == null || !onByWebClientCallback.onReceivedSslError(view, handler, error)) { + // 默认http请求会有弹框提示,如果要自己处理需要使用onReceivedSslError()且返回true AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); builder.setMessage("SSL认证失败,是否继续访问?"); builder.setPositiveButton("继续", new DialogInterface.OnClickListener() { @@ -153,8 +154,6 @@ public void onClick(DialogInterface dialog, int which) { }); AlertDialog dialog = builder.create(); dialog.show(); - } else { - onByWebClientCallback.onReceivedSslError(view, handler, error); } } diff --git a/ByWebView/src/main/java/me/jingbin/web/OnTitleProgressCallback.java b/ByWebView/src/main/java/me/jingbin/web/OnTitleProgressCallback.java index 184509c..789c8e0 100644 --- a/ByWebView/src/main/java/me/jingbin/web/OnTitleProgressCallback.java +++ b/ByWebView/src/main/java/me/jingbin/web/OnTitleProgressCallback.java @@ -5,12 +5,28 @@ */ public abstract class OnTitleProgressCallback { + /** + * @param title 返回的标题 + */ public void onReceivedTitle(String title) { } + /** + * @param newProgress 返回的进度 + */ public void onProgressChanged(int newProgress) { } + /** + * 全屏显示时处理横竖屏。 + * 默认返回false,全屏时为横屏,全屏还原后为竖屏 + * 如果要手动处理,需要返回true! + * + * @param isShow 是否显示了全屏视频 ture点击了全屏显示,false全屏视频还原 + */ + public boolean onHandleScreenOrientation(boolean isShow) { + return false; + } } diff --git a/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java index 94b17da..c00f828 100644 --- a/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java +++ b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java @@ -6,11 +6,6 @@ import android.net.Uri; import android.net.http.SslError; import android.os.Bundle; - -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; -import androidx.core.content.ContextCompat; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; @@ -21,7 +16,11 @@ import android.widget.TextView; import android.widget.Toast; -import com.example.jingbin.webviewstudy.App; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; + import com.example.jingbin.webviewstudy.MainActivity; import com.example.jingbin.webviewstudy.R; import com.example.jingbin.webviewstudy.config.MyJavascriptInterface; @@ -116,6 +115,17 @@ public void onReceivedTitle(String title) { Log.e("---title", title); tvGunTitle.setText(title); } + +// // 视频全屏时,横竖屏自己处理 +// @Override +// public boolean onHandleScreenOrientation(boolean isShow) { +// if (isShow) { +// ByWebViewActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); +// } else { +// ByWebViewActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); +// } +// return true; +// } }; private OnByWebClientCallback onByWebClientCallback = new OnByWebClientCallback() { @@ -235,7 +245,7 @@ private void loadWebsiteSourceCodeJs() { */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { - super.onActivityResult(requestCode,resultCode,intent); + super.onActivityResult(requestCode, resultCode, intent); byWebView.handleFileChooser(requestCode, resultCode, intent); }