diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 2fe30737..22dc6f32 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -15,48 +15,29 @@
-
-
-
-
-
-
+
+
-
-
-
-
+
+
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
@@ -75,8 +56,41 @@
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -86,17 +100,38 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
@@ -112,16 +147,12 @@
- onBrowserCreated
- getUrl
- onScrollChanged
initialData
openData
optionsType
InAppWebView
fromInAppBrowser
InAppWebViewController
- _handleMethod
InAppBrowser
handlerTest
onLoadStart
@@ -142,6 +173,10 @@
openFile
File
onExit
+ _handleMethod
+ javaScriptHandlersMap
+ JavaScriptHandlerCallback
+ onConsoleM
activity.getPreferences(0)
@@ -181,25 +216,25 @@
-
-
-
-
-
-
-
+
+
+
+
+
+
+
@@ -214,17 +249,6 @@
-
-
-
-
-
-
-
-
-
-
-
@@ -254,7 +278,6 @@
-
@@ -272,6 +295,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
@@ -411,18 +446,17 @@
-
-
+
+
-
-
+
@@ -430,11 +464,12 @@
-
+
-
+
+
@@ -454,11 +489,6 @@
-
-
-
-
-
@@ -616,20 +646,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -644,34 +660,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -686,31 +674,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -718,10 +681,73 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
@@ -730,15 +756,22 @@
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
diff --git a/android/build.gradle b/android/build.gradle
index f1b54ece..32b5cf75 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -53,4 +53,5 @@ dependencies {
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.squareup.okhttp3:mockwebserver:3.11.0'
+ implementation 'com.google.code.gson:gson:2.8.5'
}
diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java
index 6fd2c575..745b12f7 100644
--- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java
+++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java
@@ -76,6 +76,8 @@ public class InAppWebView extends WebView {
" }" +
"})();";
+ static final String platformReadyJS = "window.dispatchEvent(new Event('flutterInAppBrowserPlatformReady'));";
+
public InAppWebView(Context context) {
super(context);
}
diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewClient.java
index 680b546e..2fb60f01 100644
--- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewClient.java
+++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebViewClient.java
@@ -11,6 +11,7 @@ import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
+import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
@@ -195,7 +196,7 @@ public class InAppWebViewClient extends WebViewClient {
}
- public void onPageFinished(WebView view, String url) {
+ public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
((inAppBrowserActivity != null) ? inAppBrowserActivity.webView : flutterWebView.webView).isLoading = false;
@@ -213,11 +214,18 @@ public class InAppWebViewClient extends WebViewClient {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.evaluateJavascript(InAppWebView.consoleLogJS, null);
- view.evaluateJavascript(JavaScriptBridgeInterface.flutterInAppBroserJSClass, null);
+ view.evaluateJavascript(JavaScriptBridgeInterface.flutterInAppBroserJSClass, new ValueCallback() {
+ @Override
+ public void onReceiveValue(String value) {
+ view.evaluateJavascript(InAppWebView.platformReadyJS, null);
+ }
+ });
+
}
else {
view.loadUrl("javascript:"+InAppWebView.consoleLogJS);
view.loadUrl("javascript:"+JavaScriptBridgeInterface.flutterInAppBroserJSClass);
+ view.loadUrl("javascript:"+InAppWebView.platformReadyJS);
}
Map obj = new HashMap<>();
diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java
index c4d7aac7..57363bc9 100644
--- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java
+++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java
@@ -1,7 +1,12 @@
package com.pichillilorenzo.flutter_inappbrowser;
+import android.os.Build;
+import android.util.Log;
import android.webkit.JavascriptInterface;
+import com.google.gson.Gson;
+import com.pichillilorenzo.flutter_inappbrowser.InAppWebView.InAppWebView;
+
import java.util.HashMap;
import java.util.Map;
@@ -14,7 +19,11 @@ public class JavaScriptBridgeInterface {
private InAppBrowserActivity inAppBrowserActivity;
public static final String flutterInAppBroserJSClass = "window." + name + ".callHandler = function(handlerName, ...args) {" +
- "window." + name + "._callHandler(handlerName, JSON.stringify(args));" +
+ "var _callHandlerID = setTimeout(function(){});" +
+ "window." + name + "._callHandler(handlerName, _callHandlerID, JSON.stringify(args));" +
+ "return new Promise(function(resolve, reject) {" +
+ " window." + name + "[_callHandlerID] = resolve;" +
+ "});" +
"}";
public JavaScriptBridgeInterface(Object obj) {
@@ -25,13 +34,35 @@ public class JavaScriptBridgeInterface {
}
@JavascriptInterface
- public void _callHandler(String handlerName, String args) {
+ public void _callHandler(String handlerName, final String _callHandlerID, String args) {
Map obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("handlerName", handlerName);
obj.put("args", args);
- getChannel().invokeMethod("onCallJsHandler", obj);
+
+ getChannel().invokeMethod("onCallJsHandler", obj, new MethodChannel.Result() {
+ @Override
+ public void success(Object o) {
+ String json = new Gson().toJson(o);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ flutterWebView.webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + ");", null);
+ }
+ else {
+ flutterWebView.webView.loadUrl("javascript:window." + name + "[" + _callHandlerID + "](" + json + ");");
+ }
+ }
+
+ @Override
+ public void error(String s, String s1, Object o) {
+ Log.d(LOG_TAG, "ERROR: " + s + " " + s1);
+ }
+
+ @Override
+ public void notImplemented() {
+
+ }
+ });
}
private MethodChannel getChannel() {
diff --git a/example/assets/index.html b/example/assets/index.html
index c4d3df1a..4e8e6833 100644
--- a/example/assets/index.html
+++ b/example/assets/index.html
@@ -27,7 +27,14 @@