Skip to content

Commit

Permalink
get web content scroll info in QMUIWebContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Nov 22, 2018
1 parent 2961493 commit a9ea035
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String>() {
@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<String>() {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit a9ea035

Please sign in to comment.