Skip to content

Commit

Permalink
fix 修复h5页面无故竖屏或横屏的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Sep 24, 2021
1 parent ad84afa commit d300f1c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.PermissionRequest;
Expand Down Expand Up @@ -42,6 +41,10 @@ public class ByWebChromeClient extends WebChromeClient {
private CustomViewCallback mCustomViewCallback;
private ByFullscreenHolder videoFullView;
private OnTitleProgressCallback onByWebChromeCallback;
// 修复可能部分h5无故横屏问题
private boolean isFixScreenLandscape = false;
// 修复可能部分h5无故竖屏问题
private boolean isFixScreenPortrait = false;

ByWebChromeClient(Activity activity, ByWebView byWebView) {
mActivityWeakReference = new WeakReference<Activity>(activity);
Expand All @@ -52,6 +55,14 @@ void setOnByWebChromeCallback(OnTitleProgressCallback onByWebChromeCallback) {
this.onByWebChromeCallback = onByWebChromeCallback;
}

public void setFixScreenLandscape(boolean fixScreenLandscape) {
isFixScreenLandscape = fixScreenLandscape;
}

public void setFixScreenPortrait(boolean fixScreenPortrait) {
isFixScreenPortrait = fixScreenPortrait;
}

/**
* 播放网络视频时全屏会被调用的方法
*/
Expand All @@ -60,7 +71,9 @@ void setOnByWebChromeCallback(OnTitleProgressCallback onByWebChromeCallback) {
public void onShowCustomView(View view, CustomViewCallback callback) {
Activity mActivity = this.mActivityWeakReference.get();
if (mActivity != null && !mActivity.isFinishing()) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (!isFixScreenLandscape) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
mByWebView.getWebView().setVisibility(View.INVISIBLE);

// 如果一个视图已经存在,那么立刻终止并新建一个
Expand Down Expand Up @@ -93,7 +106,9 @@ public void onHideCustomView() {
return;
}
// 还原到之前的屏幕状态
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (!isFixScreenPortrait) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

mCustomView.setVisibility(View.GONE);
if (videoFullView != null) {
Expand Down
17 changes: 17 additions & 0 deletions ByWebView/src/main/java/me/jingbin/web/ByWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,23 @@ public static Builder with(@NonNull Activity activity) {
return new Builder(activity);
}

/**
* 修复可能部分h5无故竖屏问题,如果h5里有视频全屏播放请禁用
*/
public void setFixScreenPortrait(boolean fixScreenPortrait) {
if (mWebChromeClient != null) {
mWebChromeClient.setFixScreenPortrait(fixScreenPortrait);
}
}

/**
* 修复可能部分h5无故横屏问题,如果h5里有视频全屏播放请禁用
*/
public void setFixScreenLandscape(boolean fixScreenLandscape) {
if (mWebChromeClient != null) {
mWebChromeClient.setFixScreenLandscape(fixScreenLandscape);
}
}

public static class Builder {
private Activity mActivity;
Expand Down

0 comments on commit d300f1c

Please sign in to comment.