From ffb36fa966cec7c5d1f5cd2ba9feca9e1a959e2b Mon Sep 17 00:00:00 2001 From: crazecoder <21527312@qq.com> Date: Wed, 13 Nov 2019 16:02:19 +0800 Subject: [PATCH 1/3] Fix bugs when selecting a file crash when a webview widget is reopened --- .../InAppBrowserFlutterPlugin.java | 5 + .../InAppWebView/InAppWebChromeClient.java | 923 +++++++++--------- 2 files changed, 466 insertions(+), 462 deletions(-) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java index a4247e05..f1faf117 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java @@ -1,6 +1,8 @@ package com.pichillilorenzo.flutter_inappbrowser; +import android.net.Uri; import android.os.Build; +import android.webkit.ValueCallback; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.PluginRegistry; @@ -15,6 +17,8 @@ public class InAppBrowserFlutterPlugin implements FlutterPlugin { public static InAppBrowser inAppBrowser; public static MyCookieManager myCookieManager; public static CredentialDatabaseHandler credentialDatabaseHandler; + public static ValueCallback uploadMessageArray; + public InAppBrowserFlutterPlugin() {} @@ -61,5 +65,6 @@ public class InAppBrowserFlutterPlugin implements FlutterPlugin { credentialDatabaseHandler.dispose(); credentialDatabaseHandler = null; } + uploadMessageArray = null; } } diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java index 04ccc4e3..2fa37a50 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java @@ -40,516 +40,515 @@ import static android.app.Activity.RESULT_OK; public class InAppWebChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener { - protected static final String LOG_TAG = "IABWebChromeClient"; - private PluginRegistry.Registrar registrar; - private FlutterWebView flutterWebView; - private InAppBrowserActivity inAppBrowserActivity; - private ValueCallback mUploadMessageArray; - private ValueCallback mUploadMessage; - private final static int FILECHOOSER_RESULTCODE = 1; + protected static final String LOG_TAG = "IABWebChromeClient"; + private PluginRegistry.Registrar registrar; + private FlutterWebView flutterWebView; + private InAppBrowserActivity inAppBrowserActivity; + private ValueCallback mUploadMessage; + private final static int FILECHOOSER_RESULTCODE = 1; - private View mCustomView; - private WebChromeClient.CustomViewCallback mCustomViewCallback; - protected FrameLayout mFullscreenContainer; - private int mOriginalOrientation; - private int mOriginalSystemUiVisibility; + private View mCustomView; + private WebChromeClient.CustomViewCallback mCustomViewCallback; + protected FrameLayout mFullscreenContainer; + private int mOriginalOrientation; + private int mOriginalSystemUiVisibility; - public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { - super(); - this.registrar = registrar; - if (obj instanceof InAppBrowserActivity) - this.inAppBrowserActivity = (InAppBrowserActivity) obj; - else if (obj instanceof FlutterWebView) - this.flutterWebView = (FlutterWebView) obj; + public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { + super(); + this.registrar = registrar; + if (obj instanceof InAppBrowserActivity) + this.inAppBrowserActivity = (InAppBrowserActivity) obj; + else if (obj instanceof FlutterWebView) + this.flutterWebView = (FlutterWebView) obj; - registrar.addActivityResultListener(this); - } - - public Bitmap getDefaultVideoPoster() { - if (mCustomView == null) { - return null; + registrar.addActivityResultListener(this); } - return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573); - } - public void onHideCustomView() { - View decorView = this.registrar.activity().getWindow().getDecorView(); - ((FrameLayout) decorView).removeView(this.mCustomView); - this.mCustomView = null; - decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility); - this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation); - this.mCustomViewCallback.onCustomViewHidden(); - this.mCustomViewCallback = null; - } - - public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { - if (this.mCustomView != null) { - onHideCustomView(); - return; - } - View decorView = this.registrar.activity().getWindow().getDecorView(); - this.mCustomView = paramView; - this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility(); - this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation(); - this.mCustomViewCallback = paramCustomViewCallback; - this.mCustomView.setBackgroundColor(Color.parseColor("#000000")); - ((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); - decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); - } - - @Override - public boolean onJsAlert(final WebView view, String url, final String message, - final JsResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); - - getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String confirmButtonTitle = null; - - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: - result.confirm(); - break; - case 1: - default: - result.cancel(); - } - return; - } + public Bitmap getDefaultVideoPoster() { + if (mCustomView == null) { + return null; } - - createAlertDialog(view, message, result, responseMessage, confirmButtonTitle); - } - - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); - result.cancel(); - } - - @Override - public void notImplemented() { - createAlertDialog(view, message, result, null, null); - } - }); - - return true; - } - - public void createAlertDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle) { - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - - DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.confirm(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, clickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, clickListener); + return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573); } - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); + public void onHideCustomView() { + View decorView = this.registrar.activity().getWindow().getDecorView(); + ((FrameLayout) decorView).removeView(this.mCustomView); + this.mCustomView = null; + decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility); + this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation); + this.mCustomViewCallback.onCustomViewHidden(); + this.mCustomViewCallback = null; + } - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); - } + public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { + if (this.mCustomView != null) { + onHideCustomView(); + return; + } + View decorView = this.registrar.activity().getWindow().getDecorView(); + this.mCustomView = paramView; + this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility(); + this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation(); + this.mCustomViewCallback = paramCustomViewCallback; + this.mCustomView.setBackgroundColor(Color.parseColor("#000000")); + ((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); + decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); + } - @Override - public boolean onJsConfirm(final WebView view, String url, final String message, + @Override + public boolean onJsAlert(final WebView view, String url, final String message, final JsResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); - getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String confirmButtonTitle = null; - String cancelButtonTitle = null; + getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String confirmButtonTitle = null; - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: + result.confirm(); + break; + case 1: + default: + result.cancel(); + } + return; + } + } + + createAlertDialog(view, message, result, responseMessage, confirmButtonTitle); + } + + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); + result.cancel(); + } + + @Override + public void notImplemented() { + createAlertDialog(view, message, result, null, null); + } + }); + + return true; + } + + public void createAlertDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle) { + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; + + DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { result.confirm(); - break; - case 1: - default: - result.cancel(); + dialog.dismiss(); } - return; - } + }; + + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, clickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, clickListener); } - createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle); - } + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); - result.cancel(); - } - - @Override - public void notImplemented() { - createConfirmDialog(view, message, result, null, null, null); - } - }); - - return true; - } - - public void createConfirmDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle, String cancelButtonTitle) { - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.confirm(); - dialog.dismiss(); - } - }; - DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.cancel(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); - } - if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { - alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); - } else { - alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.show(); } - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); + @Override + public boolean onJsConfirm(final WebView view, String url, final String message, + final JsResult result) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); - } + getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String confirmButtonTitle = null; + String cancelButtonTitle = null; - @Override - public boolean onJsPrompt(final WebView view, String url, final String message, - final String defaultValue, final JsPromptResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); - obj.put("defaultValue", defaultValue); + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: + result.confirm(); + break; + case 1: + default: + result.cancel(); + } + return; + } + } - getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String responseDefaultValue = null; - String confirmButtonTitle = null; - String cancelButtonTitle = null; - String value = null; + createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle); + } - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - responseDefaultValue = (String) responseMap.get("defaultValue"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); - value = (String) responseMap.get("value"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: - result.confirm(value); - break; - case 1: - default: + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); result.cancel(); } - return; - } + + @Override + public void notImplemented() { + createConfirmDialog(view, message, result, null, null, null); + } + }); + + return true; + } + + public void createConfirmDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle, String cancelButtonTitle) { + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; + DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.confirm(); + dialog.dismiss(); + } + }; + DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.cancel(); + dialog.dismiss(); + } + }; + + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); + } + if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { + alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); + } else { + alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); } - createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle); - } + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); - result.cancel(); - } - - @Override - public void notImplemented() { - createPromptDialog(view, message, defaultValue, result, null, null, null, null, null); - } - }); - - return true; - } - - public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) { - FrameLayout layout = new FrameLayout(view.getContext()); - - final EditText input = new EditText(view.getContext()); - input.setMaxLines(1); - input.setText((responseDefaultValue != null && !responseDefaultValue.isEmpty()) ? responseDefaultValue : defaultValue); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT); - input.setLayoutParams(lp); - - layout.setPaddingRelative(45, 15, 45, 0); - layout.addView(input); - - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - - final String finalValue = value; - DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String text = input.getText().toString(); - result.confirm(finalValue != null ? finalValue : text); - dialog.dismiss(); - } - }; - DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.cancel(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); - } - if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { - alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); - } else { - alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.show(); } - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); + @Override + public boolean onJsPrompt(final WebView view, String url, final String message, + final String defaultValue, final JsPromptResult result) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); + obj.put("defaultValue", defaultValue); - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.setView(layout); - alertDialog.show(); - } + getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String responseDefaultValue = null; + String confirmButtonTitle = null; + String cancelButtonTitle = null; + String value = null; - @Override - public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { - WebView.HitTestResult result = view.getHitTestResult(); - String data = result.getExtra(); - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("url", data); - getChannel().invokeMethod("onTargetBlank", obj); - return false; - } + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + responseDefaultValue = (String) responseMap.get("defaultValue"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); + value = (String) responseMap.get("value"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: + result.confirm(value); + break; + case 1: + default: + result.cancel(); + } + return; + } + } - @Override - public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("origin", origin); - getChannel().invokeMethod("onGeolocationPermissionsShowPrompt", obj, new MethodChannel.Result() { - @Override - public void success(Object o) { - Map response = (Map) o; - if (response != null) - callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain")); - else - callback.invoke(origin, false, false); - } + createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle); + } - @Override - public void error(String s, String s1, Object o) { - callback.invoke(origin, false, false); - } + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); + result.cancel(); + } - @Override - public void notImplemented() { - callback.invoke(origin, false, false); - } - }); - } + @Override + public void notImplemented() { + createPromptDialog(view, message, defaultValue, result, null, null, null, null, null); + } + }); - @Override - public boolean onConsoleMessage(ConsoleMessage consoleMessage) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("sourceURL", consoleMessage.sourceId()); - obj.put("lineNumber", consoleMessage.lineNumber()); - obj.put("message", consoleMessage.message()); - obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); - getChannel().invokeMethod("onConsoleMessage", obj); - return true; - } - - @Override - public void onProgressChanged(WebView view, int progress) { - if (inAppBrowserActivity != null && inAppBrowserActivity.progressBar != null) { - inAppBrowserActivity.progressBar.setVisibility(View.VISIBLE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - inAppBrowserActivity.progressBar.setProgress(progress, true); - } else { - inAppBrowserActivity.progressBar.setProgress(progress); - } - if (progress == 100) { - inAppBrowserActivity.progressBar.setVisibility(View.GONE); - } + return true; } - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("progress", progress); - getChannel().invokeMethod("onProgressChanged", obj); + public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) { + FrameLayout layout = new FrameLayout(view.getContext()); - super.onProgressChanged(view, progress); - } + final EditText input = new EditText(view.getContext()); + input.setMaxLines(1); + input.setText((responseDefaultValue != null && !responseDefaultValue.isEmpty()) ? responseDefaultValue : defaultValue); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + input.setLayoutParams(lp); - @Override - public void onReceivedTitle(WebView view, String title) { - super.onReceivedTitle(view, title); - if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty()) - inAppBrowserActivity.actionBar.setTitle(title); - } + layout.setPaddingRelative(45, 15, 45, 0); + layout.addView(input); - @Override - public void onReceivedIcon(WebView view, Bitmap icon) { - super.onReceivedIcon(view, icon); - } + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - //The undocumented magic method override - //Eclipse will swear at you if you try to put @Override here - // For Android 3.0+ - public void openFileChooser(ValueCallback uploadMsg) { + final String finalValue = value; + DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String text = input.getText().toString(); + result.confirm(finalValue != null ? finalValue : text); + dialog.dismiss(); + } + }; + DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.cancel(); + dialog.dismiss(); + } + }; - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); + } + if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { + alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); + } else { + alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); + } - } + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); - // For Android 3.0+ - public void openFileChooser(ValueCallback uploadMsg, String acceptType) { - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("*/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult( - Intent.createChooser(i, "File Browser"), - FILECHOOSER_RESULTCODE); - } - - //For Android 4.1 - public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); - - } - - //For Android 5.0+ - public boolean onShowFileChooser( - WebView webView, ValueCallback filePathCallback, - FileChooserParams fileChooserParams) { - mUploadMessageArray = filePathCallback; - try { - Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); - contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); - contentSelectionIntent.setType("*/*"); - Intent[] intentArray; - intentArray = new Intent[0]; - - Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); - chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); - chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); - } catch (ActivityNotFoundException e) { - e.printStackTrace(); - return false; + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.setView(layout); + alertDialog.show(); } - return true; - } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - @Override - public boolean onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) { - mUploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); + @Override + public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { + WebView.HitTestResult result = view.getHitTestResult(); + String data = result.getExtra(); + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("url", data); + getChannel().invokeMethod("onTargetBlank", obj); + return false; } - return false; - } - private MethodChannel getChannel() { - return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; - } + @Override + public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("origin", origin); + getChannel().invokeMethod("onGeolocationPermissionsShowPrompt", obj, new MethodChannel.Result() { + @Override + public void success(Object o) { + Map response = (Map) o; + if (response != null) + callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain")); + else + callback.invoke(origin, false, false); + } + + @Override + public void error(String s, String s1, Object o) { + callback.invoke(origin, false, false); + } + + @Override + public void notImplemented() { + callback.invoke(origin, false, false); + } + }); + } + + @Override + public boolean onConsoleMessage(ConsoleMessage consoleMessage) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("sourceURL", consoleMessage.sourceId()); + obj.put("lineNumber", consoleMessage.lineNumber()); + obj.put("message", consoleMessage.message()); + obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); + getChannel().invokeMethod("onConsoleMessage", obj); + return true; + } + + @Override + public void onProgressChanged(WebView view, int progress) { + if (inAppBrowserActivity != null && inAppBrowserActivity.progressBar != null) { + inAppBrowserActivity.progressBar.setVisibility(View.VISIBLE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + inAppBrowserActivity.progressBar.setProgress(progress, true); + } else { + inAppBrowserActivity.progressBar.setProgress(progress); + } + if (progress == 100) { + inAppBrowserActivity.progressBar.setVisibility(View.GONE); + } + } + + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("progress", progress); + getChannel().invokeMethod("onProgressChanged", obj); + + super.onProgressChanged(view, progress); + } + + @Override + public void onReceivedTitle(WebView view, String title) { + super.onReceivedTitle(view, title); + if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty()) + inAppBrowserActivity.actionBar.setTitle(title); + } + + @Override + public void onReceivedIcon(WebView view, Bitmap icon) { + super.onReceivedIcon(view, icon); + } + + //The undocumented magic method override + //Eclipse will swear at you if you try to put @Override here + // For Android 3.0+ + public void openFileChooser(ValueCallback uploadMsg) { + + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("image/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); + + } + + // For Android 3.0+ + public void openFileChooser(ValueCallback uploadMsg, String acceptType) { + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("*/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult( + Intent.createChooser(i, "File Browser"), + FILECHOOSER_RESULTCODE); + } + + //For Android 4.1 + public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("image/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); + + } + + //For Android 5.0+ + public boolean onShowFileChooser( + WebView webView, ValueCallback filePathCallback, + FileChooserParams fileChooserParams) { + InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback; + try { + Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); + contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); + contentSelectionIntent.setType("*/*"); + Intent[] intentArray; + intentArray = new Intent[0]; + + Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); + chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); + chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); + } catch (ActivityNotFoundException e) { + e.printStackTrace(); + return false; + } + return true; + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + @Override + public boolean onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED) && InAppBrowserFlutterPlugin.uploadMessageArray != null) { + InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); + } + return true; + } + + private MethodChannel getChannel() { + return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; + } } From b389760d46c0ec62b8bf784b2d451147a7ccdd0a Mon Sep 17 00:00:00 2001 From: crazecoder <21527312@qq.com> Date: Wed, 13 Nov 2019 16:11:02 +0800 Subject: [PATCH 2/3] add proguard --- android/build.gradle | 11 +++++++++++ android/proguard-rules.pro | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 android/proguard-rules.pro diff --git a/android/build.gradle b/android/build.gradle index ee6a0047..3fe16b1c 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -29,10 +29,21 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true + consumerProguardFiles 'proguard-rules.pro' } lintOptions { disable 'InvalidPackage' } + buildTypes { + debug { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } dependencies { implementation 'androidx.webkit:webkit:1.0.0' implementation 'androidx.browser:browser:1.0.0' diff --git a/android/proguard-rules.pro b/android/proguard-rules.pro new file mode 100644 index 00000000..4930aa46 --- /dev/null +++ b/android/proguard-rules.pro @@ -0,0 +1,17 @@ +# WebView +-keepattributes *JavascriptInterface* +-keepclassmembers class * { + @android.webkit.JavascriptInterface ; +} +-keepclassmembers class * extends android.webkit.WebViewClient { + public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); + public boolean *(android.webkit.WebView, java.lang.String); + public void *(android.webkit.webView, jav.lang.String); +} +-keepclassmembers class com.pichillilorenzo.flutter_inappbrowser$JavaScriptBridgeInterface { + ; + ; + public *; + private *; +} +-keep class com.pichillilorenzo.flutter_inappbrowser.** { *; } \ No newline at end of file From 7a57f80f295a94cdc0a1092f0dd456e6f4bc7631 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Mon, 25 Nov 2019 12:19:47 +0100 Subject: [PATCH 3/3] Update InAppWebChromeClient.java --- .../InAppWebView/InAppWebChromeClient.java | 953 +++++++++--------- 1 file changed, 489 insertions(+), 464 deletions(-) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java index 2fa37a50..d181497c 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java @@ -9,6 +9,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.Uri; import android.os.Build; +import android.os.Message; import android.util.Log; import android.view.View; import android.webkit.ConsoleMessage; @@ -18,6 +19,7 @@ import android.webkit.JsResult; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; +import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; @@ -38,517 +40,540 @@ import io.flutter.plugin.common.PluginRegistry; import static android.app.Activity.RESULT_CANCELED; 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"; - private PluginRegistry.Registrar registrar; - private FlutterWebView flutterWebView; - private InAppBrowserActivity inAppBrowserActivity; - private ValueCallback mUploadMessage; - private final static int FILECHOOSER_RESULTCODE = 1; + protected static final String LOG_TAG = "IABWebChromeClient"; + private PluginRegistry.Registrar registrar; + private FlutterWebView flutterWebView; + private InAppBrowserActivity inAppBrowserActivity; + private ValueCallback mUploadMessage; + private final static int FILECHOOSER_RESULTCODE = 1; - private View mCustomView; - private WebChromeClient.CustomViewCallback mCustomViewCallback; - protected FrameLayout mFullscreenContainer; - private int mOriginalOrientation; - private int mOriginalSystemUiVisibility; + private View mCustomView; + private WebChromeClient.CustomViewCallback mCustomViewCallback; + private int mOriginalOrientation; + private int mOriginalSystemUiVisibility; - public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { - super(); - this.registrar = registrar; - if (obj instanceof InAppBrowserActivity) - this.inAppBrowserActivity = (InAppBrowserActivity) obj; - else if (obj instanceof FlutterWebView) - this.flutterWebView = (FlutterWebView) obj; + public InAppWebViewChromeClient(Object obj, PluginRegistry.Registrar registrar) { + super(); + this.registrar = registrar; + if (obj instanceof InAppBrowserActivity) + this.inAppBrowserActivity = (InAppBrowserActivity) obj; + else if (obj instanceof FlutterWebView) + this.flutterWebView = (FlutterWebView) obj; - registrar.addActivityResultListener(this); + registrar.addActivityResultListener(this); + } + + public Bitmap getDefaultVideoPoster() { + if (mCustomView == null) { + return null; } + return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573); + } - public Bitmap getDefaultVideoPoster() { - if (mCustomView == null) { - return null; - } - return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573); + public void onHideCustomView() { + View decorView = this.registrar.activity().getWindow().getDecorView(); + ((FrameLayout) decorView).removeView(this.mCustomView); + this.mCustomView = null; + decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility); + this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation); + this.mCustomViewCallback.onCustomViewHidden(); + this.mCustomViewCallback = null; + } + + public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { + if (this.mCustomView != null) { + onHideCustomView(); + return; } + View decorView = this.registrar.activity().getWindow().getDecorView(); + this.mCustomView = paramView; + this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility(); + this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation(); + this.mCustomViewCallback = paramCustomViewCallback; + this.mCustomView.setBackgroundColor(Color.parseColor("#000000")); + ((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); + decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); + } - public void onHideCustomView() { - View decorView = this.registrar.activity().getWindow().getDecorView(); - ((FrameLayout) decorView).removeView(this.mCustomView); - this.mCustomView = null; - decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility); - this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation); - this.mCustomViewCallback.onCustomViewHidden(); - this.mCustomViewCallback = null; - } + @Override + public boolean onJsAlert(final WebView view, String url, final String message, + final JsResult result) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); - public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { - if (this.mCustomView != null) { - onHideCustomView(); + getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String confirmButtonTitle = null; + + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: + result.confirm(); + break; + case 1: + default: + result.cancel(); + } return; + } } - View decorView = this.registrar.activity().getWindow().getDecorView(); - this.mCustomView = paramView; - this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility(); - this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation(); - this.mCustomViewCallback = paramCustomViewCallback; - this.mCustomView.setBackgroundColor(Color.parseColor("#000000")); - ((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); - decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); + + createAlertDialog(view, message, result, responseMessage, confirmButtonTitle); + } + + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); + result.cancel(); + } + + @Override + public void notImplemented() { + createAlertDialog(view, message, result, null, null); + } + }); + + return true; + } + + public void createAlertDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle) { + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; + + DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.confirm(); + dialog.dismiss(); + } + }; + + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, clickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, clickListener); } - @Override - public boolean onJsAlert(final WebView view, String url, final String message, + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); + + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.show(); + } + + @Override + public boolean onJsConfirm(final WebView view, String url, final String message, final JsResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); - getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String confirmButtonTitle = null; + getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String confirmButtonTitle = null; + String cancelButtonTitle = null; - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: - result.confirm(); - break; - case 1: - default: - result.cancel(); - } - return; - } - } - - createAlertDialog(view, message, result, responseMessage, confirmButtonTitle); - } - - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); - result.cancel(); - } - - @Override - public void notImplemented() { - createAlertDialog(view, message, result, null, null); - } - }); - - return true; - } - - public void createAlertDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle) { - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - - DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: result.confirm(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, clickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, clickListener); - } - - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); - - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); - } - - @Override - public boolean onJsConfirm(final WebView view, String url, final String message, - final JsResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); - - getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String confirmButtonTitle = null; - String cancelButtonTitle = null; - - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: - result.confirm(); - break; - case 1: - default: - result.cancel(); - } - return; - } - } - - createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle); - } - - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); + break; + case 1: + default: result.cancel(); } - - @Override - public void notImplemented() { - createConfirmDialog(view, message, result, null, null, null); - } - }); - - return true; - } - - public void createConfirmDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle, String cancelButtonTitle) { - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.confirm(); - dialog.dismiss(); - } - }; - DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.cancel(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); - } - if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { - alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); - } else { - alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); + return; + } } - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); + createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle); + } - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); + result.cancel(); + } + + @Override + public void notImplemented() { + createConfirmDialog(view, message, result, null, null, null); + } + }); + + return true; + } + + public void createConfirmDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle, String cancelButtonTitle) { + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; + DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.confirm(); + dialog.dismiss(); + } + }; + DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.cancel(); + dialog.dismiss(); + } + }; + + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); + } + if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { + alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); + } else { + alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); } - @Override - public boolean onJsPrompt(final WebView view, String url, final String message, - final String defaultValue, final JsPromptResult result) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("message", message); - obj.put("defaultValue", defaultValue); + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); - getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() { - @Override - public void success(Object response) { - String responseMessage = null; - String responseDefaultValue = null; - String confirmButtonTitle = null; - String cancelButtonTitle = null; - String value = null; + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.show(); + } - if (response != null) { - Map responseMap = (Map) response; - responseMessage = (String) responseMap.get("message"); - responseDefaultValue = (String) responseMap.get("defaultValue"); - confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); - cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); - value = (String) responseMap.get("value"); - Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); - if (handledByClient != null && handledByClient) { - Integer action = (Integer) responseMap.get("action"); - action = action != null ? action : 1; - switch (action) { - case 0: - result.confirm(value); - break; - case 1: - default: - result.cancel(); - } - return; - } - } + @Override + public boolean onJsPrompt(final WebView view, String url, final String message, + final String defaultValue, final JsPromptResult result) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", message); + obj.put("defaultValue", defaultValue); - createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle); - } + getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() { + @Override + public void success(Object response) { + String responseMessage = null; + String responseDefaultValue = null; + String confirmButtonTitle = null; + String cancelButtonTitle = null; + String value = null; - @Override - public void error(String s, String s1, Object o) { - Log.e(LOG_TAG, s + ", " + s1); + if (response != null) { + Map responseMap = (Map) response; + responseMessage = (String) responseMap.get("message"); + responseDefaultValue = (String) responseMap.get("defaultValue"); + confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); + cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); + value = (String) responseMap.get("value"); + Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); + if (handledByClient != null && handledByClient) { + Integer action = (Integer) responseMap.get("action"); + action = action != null ? action : 1; + switch (action) { + case 0: + result.confirm(value); + break; + case 1: + default: result.cancel(); } - - @Override - public void notImplemented() { - createPromptDialog(view, message, defaultValue, result, null, null, null, null, null); - } - }); - - return true; - } - - public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) { - FrameLayout layout = new FrameLayout(view.getContext()); - - final EditText input = new EditText(view.getContext()); - input.setMaxLines(1); - input.setText((responseDefaultValue != null && !responseDefaultValue.isEmpty()) ? responseDefaultValue : defaultValue); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT); - input.setLayoutParams(lp); - - layout.setPaddingRelative(45, 15, 45, 0); - layout.addView(input); - - String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; - - final String finalValue = value; - DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String text = input.getText().toString(); - result.confirm(finalValue != null ? finalValue : text); - dialog.dismiss(); - } - }; - DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - result.cancel(); - dialog.dismiss(); - } - }; - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); - alertDialogBuilder.setMessage(alertMessage); - if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { - alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); - } else { - alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); - } - if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { - alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); - } else { - alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); + return; + } } - alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - result.cancel(); - dialog.dismiss(); - } - }); + createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle); + } - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.setView(layout); - alertDialog.show(); + @Override + public void error(String s, String s1, Object o) { + Log.e(LOG_TAG, s + ", " + s1); + result.cancel(); + } + + @Override + public void notImplemented() { + createPromptDialog(view, message, defaultValue, result, null, null, null, null, null); + } + }); + + return true; + } + + public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) { + FrameLayout layout = new FrameLayout(view.getContext()); + + final EditText input = new EditText(view.getContext()); + input.setMaxLines(1); + input.setText((responseDefaultValue != null && !responseDefaultValue.isEmpty()) ? responseDefaultValue : defaultValue); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + input.setLayoutParams(lp); + + layout.setPaddingRelative(45, 15, 45, 0); + layout.addView(input); + + String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; + + final String finalValue = value; + DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String text = input.getText().toString(); + result.confirm(finalValue != null ? finalValue : text); + dialog.dismiss(); + } + }; + DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + result.cancel(); + dialog.dismiss(); + } + }; + + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert); + alertDialogBuilder.setMessage(alertMessage); + if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) { + alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener); + } else { + alertDialogBuilder.setPositiveButton(android.R.string.ok, confirmClickListener); + } + if (cancelButtonTitle != null && !cancelButtonTitle.isEmpty()) { + alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener); + } else { + alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener); } - @Override - public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) { - WebView.HitTestResult result = view.getHitTestResult(); - String data = result.getExtra(); - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("url", data); - getChannel().invokeMethod("onTargetBlank", obj); - return false; - } + alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + result.cancel(); + dialog.dismiss(); + } + }); - @Override - public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("origin", origin); - getChannel().invokeMethod("onGeolocationPermissionsShowPrompt", obj, new MethodChannel.Result() { - @Override - public void success(Object o) { - Map response = (Map) o; - if (response != null) - callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain")); - else - callback.invoke(origin, false, false); - } + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.setView(layout); + alertDialog.show(); + } - @Override - public void error(String s, String s1, Object o) { - callback.invoke(origin, false, false); - } + @Override + public boolean onCreateWindow(WebView view, boolean isDialog, boolean userGesture, final Message resultMsg) { + final Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); - @Override - public void notImplemented() { - callback.invoke(origin, false, false); - } - }); - } + WebView.HitTestResult result = view.getHitTestResult(); + String data = result.getExtra(); - @Override - public boolean onConsoleMessage(ConsoleMessage consoleMessage) { - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("sourceURL", consoleMessage.sourceId()); - obj.put("lineNumber", consoleMessage.lineNumber()); - obj.put("message", consoleMessage.message()); - obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); - getChannel().invokeMethod("onConsoleMessage", obj); - return true; - } + 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); - @Override - public void onProgressChanged(WebView view, int progress) { - if (inAppBrowserActivity != null && inAppBrowserActivity.progressBar != null) { - inAppBrowserActivity.progressBar.setVisibility(View.VISIBLE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - inAppBrowserActivity.progressBar.setProgress(progress, true); - } else { - inAppBrowserActivity.progressBar.setProgress(progress); - } - if (progress == 100) { - inAppBrowserActivity.progressBar.setVisibility(View.GONE); - } + 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(); } - - Map obj = new HashMap<>(); - if (inAppBrowserActivity != null) - obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("progress", progress); - getChannel().invokeMethod("onProgressChanged", obj); - - super.onProgressChanged(view, progress); + }); + ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView); + resultMsg.sendToTarget(); + return true; } - @Override - public void onReceivedTitle(WebView view, String title) { - super.onReceivedTitle(view, title); - if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty()) - inAppBrowserActivity.actionBar.setTitle(title); + obj.put("url", data); + getChannel().invokeMethod("onTargetBlank", obj); + return false; + } + + @Override + public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("origin", origin); + getChannel().invokeMethod("onGeolocationPermissionsShowPrompt", obj, new MethodChannel.Result() { + @Override + public void success(Object o) { + Map response = (Map) o; + if (response != null) + callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain")); + else + callback.invoke(origin, false, false); + } + + @Override + public void error(String s, String s1, Object o) { + callback.invoke(origin, false, false); + } + + @Override + public void notImplemented() { + callback.invoke(origin, false, false); + } + }); + } + + @Override + public boolean onConsoleMessage(ConsoleMessage consoleMessage) { + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("message", consoleMessage.message()); + obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); + getChannel().invokeMethod("onConsoleMessage", obj); + return true; + } + + @Override + public void onProgressChanged(WebView view, int progress) { + if (inAppBrowserActivity != null && inAppBrowserActivity.progressBar != null) { + inAppBrowserActivity.progressBar.setVisibility(View.VISIBLE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + inAppBrowserActivity.progressBar.setProgress(progress, true); + } else { + inAppBrowserActivity.progressBar.setProgress(progress); + } + if (progress == 100) { + inAppBrowserActivity.progressBar.setVisibility(View.GONE); + } } - @Override - public void onReceivedIcon(WebView view, Bitmap icon) { - super.onReceivedIcon(view, icon); + Map obj = new HashMap<>(); + if (inAppBrowserActivity != null) + obj.put("uuid", inAppBrowserActivity.uuid); + obj.put("progress", progress); + getChannel().invokeMethod("onProgressChanged", obj); + + super.onProgressChanged(view, progress); + } + + @Override + public void onReceivedTitle(WebView view, String title) { + super.onReceivedTitle(view, title); + if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty()) + inAppBrowserActivity.actionBar.setTitle(title); + } + + @Override + public void onReceivedIcon(WebView view, Bitmap icon) { + super.onReceivedIcon(view, icon); + } + + //The undocumented magic method override + //Eclipse will swear at you if you try to put @Override here + // For Android 3.0+ + public void openFileChooser(ValueCallback uploadMsg) { + + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("image/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); + + } + + // For Android 3.0+ + public void openFileChooser(ValueCallback uploadMsg, String acceptType) { + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("*/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult( + Intent.createChooser(i, "File Browser"), + FILECHOOSER_RESULTCODE); + } + + //For Android 4.1 + public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { + mUploadMessage = uploadMsg; + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("image/*"); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); + + } + + //For Android 5.0+ + public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) { + InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback; + try { + Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); + contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); + contentSelectionIntent.setType("*/*"); + Intent[] intentArray; + intentArray = new Intent[0]; + + Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); + chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); + chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); + ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); + } catch (ActivityNotFoundException e) { + e.printStackTrace(); + return false; } + return true; + } - //The undocumented magic method override - //Eclipse will swear at you if you try to put @Override here - // For Android 3.0+ - public void openFileChooser(ValueCallback uploadMsg) { - - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); - + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + @Override + public boolean onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) { + InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); } + return true; + } - // For Android 3.0+ - public void openFileChooser(ValueCallback uploadMsg, String acceptType) { - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("*/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult( - Intent.createChooser(i, "File Browser"), - FILECHOOSER_RESULTCODE); - } - - //For Android 4.1 - public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); - - } - - //For Android 5.0+ - public boolean onShowFileChooser( - WebView webView, ValueCallback filePathCallback, - FileChooserParams fileChooserParams) { - InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback; - try { - Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); - contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); - contentSelectionIntent.setType("*/*"); - Intent[] intentArray; - intentArray = new Intent[0]; - - Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); - chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); - chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); - ((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); - } catch (ActivityNotFoundException e) { - e.printStackTrace(); - return false; - } - return true; - } - - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - @Override - public boolean onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED) && InAppBrowserFlutterPlugin.uploadMessageArray != null) { - InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); - } - return true; - } - - private MethodChannel getChannel() { - return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; - } + private MethodChannel getChannel() { + return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; + } }