Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
injectedOnStartLoadingJavaScript on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Dec 16, 2016
1 parent 85c4266 commit 3814679
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ repositories {
}

dependencies {
compile 'com.facebook.react:react-native:0.19.+'
compile 'com.facebook.react:react-native:0.39.+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,44 @@
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.views.webview.ReactWebViewManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import android.text.TextUtils;
import android.graphics.Bitmap;

import java.util.ArrayList;
import java.util.Map;

import javax.annotation.Nullable;
import android.webkit.WebChromeClient;
import android.view.ViewGroup.LayoutParams;
import com.facebook.react.common.build.ReactBuildConfig;
import android.os.Build;
import android.webkit.GeolocationPermissions;
import com.facebook.react.views.webview.WebViewConfig;
import android.webkit.ValueCallback;

public class WebViewBridgeManager extends ReactWebViewManager {
private static final String REACT_CLASS = "RCTWebViewBridge";
private static final String HTML_ENCODING = "UTF-8";
private static final String HTML_MIME_TYPE = "text/html; charset=utf-8";

private static final String HTTP_METHOD_POST = "POST";

public static final int COMMAND_SEND_TO_BRIDGE = 101;

private static final String BLANK_URL = "about:blank";

private WebViewConfig mWebViewConfig;

public WebViewBridgeManager() {
mWebViewConfig = new WebViewConfig() {
public void configWebView(WebView webView) {
}
};
}

public WebViewBridgeManager(WebViewConfig webViewConfig) {
mWebViewConfig = webViewConfig;
}

@Override
public String getName() {
return REACT_CLASS;
Expand All @@ -33,11 +60,52 @@ Map<String, Integer> getCommandsMap() {
return commandsMap;
}

protected static class ReactWebChromeClient extends WebChromeClient {

private boolean was = false;
private int last = 100;

public void onProgressChanged(WebView view, int newProgress) {
if (newProgress < 70) {
if(was == false || last < newProgress){
was = true;
last = newProgress;
((ReactWebView) view).callInjectedOnStartLoadingJavaScript();
}
} else {
was = false;
last = 100;
}
}

}

@Override
protected WebView createViewInstance(ThemedReactContext reactContext) {
WebView root = super.createViewInstance(reactContext);
root.addJavascriptInterface(new JavascriptBridge(root), "WebViewBridge");
return root;
ReactWebView webView = new ReactWebView(reactContext);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
reactContext.addLifecycleEventListener(webView);
mWebViewConfig.configWebView(webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

// Fixes broken full-screen modals/galleries due to body height being 0.
webView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

if (ReactBuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}

webView.setWebChromeClient(new ReactWebChromeClient());

return webView;
}

@Override
Expand Down Expand Up @@ -75,4 +143,50 @@ public void setAllowFileAccessFromFileURLs(WebView root, boolean allows) {
public void setAllowUniversalAccessFromFileURLs(WebView root, boolean allows) {
root.getSettings().setAllowUniversalAccessFromFileURLs(allows);
}
}

@ReactProp(name = "injectedOnStartLoadingJavaScript")
public void setInjectedOnStartLoadingJavaScript(WebView view, @Nullable String injectedJavaScript) {
((ReactWebView) view).setInjectedOnStartLoadingJavaScript(injectedJavaScript);
}

@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
// Do not register default touch emitter and let WebView implementation handle touches
view.setWebViewClient(new ReactWebViewClient());
}

protected static class ReactWebView extends ReactWebViewManager.ReactWebView {
private @Nullable String injectedOnStartLoadingJS;

public void setInjectedOnStartLoadingJavaScript(@Nullable String js) {
injectedOnStartLoadingJS = js;
}

public ReactWebView(ThemedReactContext reactContext) {
super(reactContext);
}

public void callInjectedOnStartLoadingJavaScript() {
if (injectedOnStartLoadingJS != null &&
!TextUtils.isEmpty(injectedOnStartLoadingJS)) {
evaluateJavascript(injectedOnStartLoadingJS, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {

}
}
);
}
}
}

protected static class ReactWebViewClient extends ReactWebViewManager.ReactWebViewClient {
@Override
public void onPageStarted(WebView webView, String url, Bitmap favicon) {
ReactWebView reactWebView = (ReactWebView) webView;
reactWebView.callInjectedOnStartLoadingJavaScript();
super.onPageStarted(webView, url, favicon);
}
}

}
4 changes: 3 additions & 1 deletion webview-bridge/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var WebViewBridge = React.createClass({
* Will be called once the message is being sent from webview
*/
onBridgeMessage: PropTypes.func,

injectedOnStartLoadingJavaScript: PropTypes.string,
},

getInitialState: function() {
Expand Down Expand Up @@ -230,4 +232,4 @@ var styles = StyleSheet.create({
},
});

module.exports = WebViewBridge;
module.exports = WebViewBridge;

0 comments on commit 3814679

Please sign in to comment.