diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewClient.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewClient.java index d7000ceeb..0679ff84a 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewClient.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewClient.java @@ -1,8 +1,8 @@ package com.qmuiteam.qmui.widget.webview; +import android.graphics.Bitmap; import android.os.Build; import android.os.SystemClock; -import android.util.Log; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.webkit.ValueCallback; @@ -14,49 +14,70 @@ public class QMUIWebViewClient extends WebViewClient { public static final int JS_FAKE_KEY_CODE_EVENT = 112; // F1 private boolean mNeedDispatchSafeAreaInset; + private boolean mIsPageFinished = false; public QMUIWebViewClient(boolean needDispatchSafeAreaInset) { mNeedDispatchSafeAreaInset = needDispatchSafeAreaInset; } + public void setNeedDispatchSafeAreaInset(QMUIWebView webView, boolean needDispatchSafeAreaInset) { + if (mNeedDispatchSafeAreaInset != needDispatchSafeAreaInset) { + mNeedDispatchSafeAreaInset = needDispatchSafeAreaInset; + if (mNeedDispatchSafeAreaInset && mIsPageFinished) { + dispatchFullscreenRequestAction(webView); + } + } + } + + @Override + public void onPageStarted(WebView view, String url, Bitmap favicon) { + mIsPageFinished = false; + super.onPageStarted(view, url, favicon); + } + @Override public void onPageFinished(final WebView view, String url) { super.onPageFinished(view, url); - boolean sureNotSupportModifyCssEnv = (view instanceof QMUIWebView) && - ((QMUIWebView) view).isNotSupportChangeCssEnv(); - if (mNeedDispatchSafeAreaInset && !sureNotSupportModifyCssEnv) { - String jsCode = "(function(){\n" + - " document.body.addEventListener('keydown', function(e){\n" + - " if(e.keyCode == " + JS_FAKE_KEY_CODE_EVENT + "){\n" + - " var html = document.documentElement;\n" + - " var requestFullscreen = html.requestFullscreen || html.webkitRequestFullscreen;\n" + - " requestFullscreen.call(html);\n" + - " }\n" + - " })\n" + - "})()"; - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){ - view.evaluateJavascript(jsCode, new ValueCallback() { - @Override - public void onReceiveValue(String value) { - dispatchFullscreenRequestEvent(view); - } - }); - }else{ - // Usually, there is no chance to come here. - view.loadUrl("javascript:" + jsCode); - view.postDelayed(new Runnable() { - @Override - public void run() { - dispatchFullscreenRequestEvent(view); - } - }, 250); - } - + mIsPageFinished = true; + if (mNeedDispatchSafeAreaInset && view instanceof QMUIWebView) { + dispatchFullscreenRequestAction((QMUIWebView) view); + } + } + private void dispatchFullscreenRequestAction(final QMUIWebView webView) { + boolean sureNotSupportModifyCssEnv = webView.isNotSupportChangeCssEnv(); + if (sureNotSupportModifyCssEnv) { + return; + } + String jsCode = "(function(){\n" + + " document.body.addEventListener('keydown', function(e){\n" + + " if(e.keyCode == " + JS_FAKE_KEY_CODE_EVENT + "){\n" + + " var html = document.documentElement;\n" + + " var requestFullscreen = html.requestFullscreen || html.webkitRequestFullscreen;\n" + + " requestFullscreen.call(html);\n" + + " }\n" + + " })\n" + + "})()"; + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { + webView.evaluateJavascript(jsCode, new ValueCallback() { + @Override + public void onReceiveValue(String value) { + dispatchFullscreenRequestEvent(webView); + } + }); + } else { + // Usually, there is no chance to come here. + webView.loadUrl("javascript:" + jsCode); + webView.postDelayed(new Runnable() { + @Override + public void run() { + dispatchFullscreenRequestEvent(webView); + } + }, 250); } } - private void dispatchFullscreenRequestEvent(WebView webView){ + private void dispatchFullscreenRequestEvent(WebView webView) { KeyEvent keyEvent = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_F1, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0); diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewContainer.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewContainer.java index 7954e7b76..1fb51b1e8 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewContainer.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/webview/QMUIWebViewContainer.java @@ -170,6 +170,24 @@ public boolean applySystemWindowInsets21(Object insets) { return super.applySystemWindowInsets21(insets); } + public int getWebContentScrollY(){ + if(mCustomView instanceof ViewGroup && ((ViewGroup)mCustomView).getChildCount() > 0){ + ((ViewGroup)mCustomView).getChildAt(0).getScrollY(); + }else if(mWebView != null){ + return mWebView.getScrollY(); + } + return 0; + } + + public int getWebContentScrollX(){ + if(mCustomView instanceof ViewGroup && ((ViewGroup)mCustomView).getChildCount() > 0){ + ((ViewGroup)mCustomView).getChildAt(0).getScrollX(); + }else if(mWebView != null){ + return mWebView.getScrollX(); + } + return 0; + } + public interface Callback { void onShowCustomView();