Update InAppWebChromeClient.java

This commit is contained in:
Lorenzo Pichilli 2019-11-25 12:19:47 +01:00 committed by GitHub
parent 7c5b1a1cec
commit 7a57f80f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 489 additions and 464 deletions

View File

@ -9,6 +9,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Message;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
@ -18,6 +19,7 @@ import android.webkit.JsResult;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText; import android.widget.EditText;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -38,7 +40,7 @@ import io.flutter.plugin.common.PluginRegistry;
import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
public class InAppWebChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener { public class InAppWebViewChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener {
protected static final String LOG_TAG = "IABWebChromeClient"; protected static final String LOG_TAG = "IABWebChromeClient";
private PluginRegistry.Registrar registrar; private PluginRegistry.Registrar registrar;
@ -49,11 +51,10 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis
private View mCustomView; private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback; private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation; private int mOriginalOrientation;
private int mOriginalSystemUiVisibility; private int mOriginalSystemUiVisibility;
public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { public InAppWebViewChromeClient(Object obj, PluginRegistry.Registrar registrar) {
super(); super();
this.registrar = registrar; this.registrar = registrar;
if (obj instanceof InAppBrowserActivity) if (obj instanceof InAppBrowserActivity)
@ -394,12 +395,40 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis
} }
@Override @Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { public boolean onCreateWindow(WebView view, boolean isDialog, boolean userGesture, final Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult(); final Map<String, Object> obj = new HashMap<>();
String data = result.getExtra();
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null) if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid); obj.put("uuid", inAppBrowserActivity.uuid);
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
if (data == null) {
// to get the URL, create a temp weview
final WebView newWebView = new WebView(view.getContext());
// disable javascript
newWebView.getSettings().setJavaScriptEnabled(false);
newWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView v, String url, Bitmap favicon) {
super.onPageStarted(v, url, favicon);
obj.put("url", url);
getChannel().invokeMethod("onTargetBlank", obj);
// stop webview loading
v.stopLoading();
// this will throw the error "Application attempted to call on a destroyed AwAutofillManager" that will kill the webview.
// that's ok.
v.destroy();
}
});
((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
obj.put("url", data); obj.put("url", data);
getChannel().invokeMethod("onTargetBlank", obj); getChannel().invokeMethod("onTargetBlank", obj);
return false; return false;
@ -438,8 +467,6 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis
Map<String, Object> obj = new HashMap<>(); Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null) if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid); obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("sourceURL", consoleMessage.sourceId());
obj.put("lineNumber", consoleMessage.lineNumber());
obj.put("message", consoleMessage.message()); obj.put("message", consoleMessage.message());
obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); obj.put("messageLevel", consoleMessage.messageLevel().ordinal());
getChannel().invokeMethod("onConsoleMessage", obj); getChannel().invokeMethod("onConsoleMessage", obj);
@ -516,9 +543,7 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis
} }
//For Android 5.0+ //For Android 5.0+
public boolean onShowFileChooser( public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback; InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback;
try { try {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
@ -542,7 +567,7 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override @Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) { public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED) && InAppBrowserFlutterPlugin.uploadMessageArray != null) { if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
} }
return true; return true;