fix choose file callback in android

This commit is contained in:
crazecoder 2019-11-04 16:36:05 +08:00
parent 2811fce463
commit 17c005561b
1 changed files with 465 additions and 456 deletions

View File

@ -1,5 +1,6 @@
package com.pichillilorenzo.flutter_inappbrowser.InAppWebView; package com.pichillilorenzo.flutter_inappbrowser.InAppWebView;
import android.annotation.TargetApi;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -33,511 +34,519 @@ import java.util.Map;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
public class InAppWebChromeClient extends WebChromeClient { import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
protected static final String LOG_TAG = "IABWebChromeClient"; public class InAppWebChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener {
private PluginRegistry.Registrar registrar;
private FlutterWebView flutterWebView;
private InAppBrowserActivity inAppBrowserActivity;
private ValueCallback<Uri[]> mUploadMessageArray;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
private View mCustomView; protected static final String LOG_TAG = "IABWebChromeClient";
private WebChromeClient.CustomViewCallback mCustomViewCallback; private PluginRegistry.Registrar registrar;
protected FrameLayout mFullscreenContainer; private FlutterWebView flutterWebView;
private int mOriginalOrientation; private InAppBrowserActivity inAppBrowserActivity;
private int mOriginalSystemUiVisibility; private ValueCallback<Uri[]> mUploadMessageArray;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { private View mCustomView;
super(); private WebChromeClient.CustomViewCallback mCustomViewCallback;
this.registrar = registrar; protected FrameLayout mFullscreenContainer;
if (obj instanceof InAppBrowserActivity) private int mOriginalOrientation;
this.inAppBrowserActivity = (InAppBrowserActivity) obj; private int mOriginalSystemUiVisibility;
else if (obj instanceof FlutterWebView)
this.flutterWebView = (FlutterWebView) obj;
}
public Bitmap getDefaultVideoPoster() public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) {
{ super();
if (mCustomView == null) { this.registrar = registrar;
return null; if (obj instanceof InAppBrowserActivity)
this.inAppBrowserActivity = (InAppBrowserActivity) obj;
else if (obj instanceof FlutterWebView)
this.flutterWebView = (FlutterWebView) obj;
registrar.addActivityResultListener(this);
} }
return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573);
}
public void onHideCustomView() public Bitmap getDefaultVideoPoster() {
{ if (mCustomView == null) {
View decorView = this.registrar.activity().getWindow().getDecorView(); return null;
((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<String, Object> 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<String, Object> responseMap = (Map<String, Object>) 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;
}
} }
return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573);
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);
} }
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onHideCustomView() {
@Override View decorView = this.registrar.activity().getWindow().getDecorView();
public void onCancel(DialogInterface dialog) { ((FrameLayout) decorView).removeView(this.mCustomView);
result.cancel(); this.mCustomView = null;
dialog.dismiss(); decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility);
} this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation);
}); this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
AlertDialog alertDialog = alertDialogBuilder.create(); public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
alertDialog.show(); 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 @Override
public boolean onJsConfirm(final WebView view, String url, final String message, public boolean onJsAlert(final WebView view, String url, final String message,
final JsResult result) { final JsResult result) {
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("message", message); obj.put("message", message);
getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() { getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() {
@Override @Override
public void success(Object response) { public void success(Object response) {
String responseMessage = null; String responseMessage = null;
String confirmButtonTitle = null; String confirmButtonTitle = null;
String cancelButtonTitle = null;
if (response != null) { if (response != null) {
Map<String, Object> responseMap = (Map<String, Object>) response; Map<String, Object> responseMap = (Map<String, Object>) response;
responseMessage = (String) responseMap.get("message"); responseMessage = (String) responseMap.get("message");
confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
cancelButtonTitle = (String) responseMap.get("cancelButtonTitle"); Boolean handledByClient = (Boolean) responseMap.get("handledByClient");
Boolean handledByClient = (Boolean) responseMap.get("handledByClient"); if (handledByClient != null && handledByClient) {
if (handledByClient != null && handledByClient) { Integer action = (Integer) responseMap.get("action");
Integer action = (Integer) responseMap.get("action"); action = action != null ? action : 1;
action = action != null ? action : 1; switch (action) {
switch (action) { case 0:
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(); result.confirm();
break; dialog.dismiss();
case 1:
default:
result.cancel();
} }
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 AlertDialog alertDialog = alertDialogBuilder.create();
public void error(String s, String s1, Object o) { alertDialog.show();
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);
} }
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override
@Override public boolean onJsConfirm(final WebView view, String url, final String message,
public void onCancel(DialogInterface dialog) { final JsResult result) {
result.cancel(); Map<String, Object> obj = new HashMap<>();
dialog.dismiss(); if (inAppBrowserActivity != null)
} obj.put("uuid", inAppBrowserActivity.uuid);
}); obj.put("message", message);
AlertDialog alertDialog = alertDialogBuilder.create(); getChannel().invokeMethod("onJsConfirm", obj, new MethodChannel.Result() {
alertDialog.show(); @Override
} public void success(Object response) {
String responseMessage = null;
String confirmButtonTitle = null;
String cancelButtonTitle = null;
@Override if (response != null) {
public boolean onJsPrompt(final WebView view, String url, final String message, Map<String, Object> responseMap = (Map<String, Object>) response;
final String defaultValue, final JsPromptResult result) { responseMessage = (String) responseMap.get("message");
Map<String, Object> obj = new HashMap<>(); confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
if (inAppBrowserActivity != null) cancelButtonTitle = (String) responseMap.get("cancelButtonTitle");
obj.put("uuid", inAppBrowserActivity.uuid); Boolean handledByClient = (Boolean) responseMap.get("handledByClient");
obj.put("message", message); if (handledByClient != null && handledByClient) {
obj.put("defaultValue", defaultValue); 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() { createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle);
@Override }
public void success(Object response) {
String responseMessage = null;
String responseDefaultValue = null;
String confirmButtonTitle = null;
String cancelButtonTitle = null;
String value = null;
if (response != null) { @Override
Map<String, Object> responseMap = (Map<String, Object>) response; public void error(String s, String s1, Object o) {
responseMessage = (String) responseMap.get("message"); Log.e(LOG_TAG, s + ", " + s1);
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(); 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 AlertDialog alertDialog = alertDialogBuilder.create();
public void error(String s, String s1, Object o) { alertDialog.show();
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);
} }
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override
@Override public boolean onJsPrompt(final WebView view, String url, final String message,
public void onCancel(DialogInterface dialog) { final String defaultValue, final JsPromptResult result) {
result.cancel(); Map<String, Object> obj = new HashMap<>();
dialog.dismiss(); if (inAppBrowserActivity != null)
} obj.put("uuid", inAppBrowserActivity.uuid);
}); obj.put("message", message);
obj.put("defaultValue", defaultValue);
AlertDialog alertDialog = alertDialogBuilder.create(); getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() {
alertDialog.setView(layout); @Override
alertDialog.show(); public void success(Object response) {
} String responseMessage = null;
String responseDefaultValue = null;
String confirmButtonTitle = null;
String cancelButtonTitle = null;
String value = null;
@Override if (response != null) {
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) Map<String, Object> responseMap = (Map<String, Object>) response;
{ responseMessage = (String) responseMap.get("message");
WebView.HitTestResult result = view.getHitTestResult(); responseDefaultValue = (String) responseMap.get("defaultValue");
String data = result.getExtra(); confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
Map<String, Object> obj = new HashMap<>(); cancelButtonTitle = (String) responseMap.get("cancelButtonTitle");
if (inAppBrowserActivity != null) value = (String) responseMap.get("value");
obj.put("uuid", inAppBrowserActivity.uuid); Boolean handledByClient = (Boolean) responseMap.get("handledByClient");
obj.put("url", data); if (handledByClient != null && handledByClient) {
getChannel().invokeMethod("onTargetBlank", obj); Integer action = (Integer) responseMap.get("action");
return false; action = action != null ? action : 1;
} switch (action) {
case 0:
result.confirm(value);
break;
case 1:
default:
result.cancel();
}
return;
}
}
@Override createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle);
public void onGeolocationPermissionsShowPrompt (final String origin, final GeolocationPermissions.Callback callback) { }
Map<String, Object> 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<String, Object> response = (Map<String, Object>) 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 @Override
public void error(String s, String s1, Object o) { public void error(String s, String s1, Object o) {
callback.invoke(origin,false,false); Log.e(LOG_TAG, s + ", " + s1);
} result.cancel();
}
@Override @Override
public void notImplemented() { public void notImplemented() {
callback.invoke(origin,false,false); createPromptDialog(view, message, defaultValue, result, null, null, null, null, null);
} }
}); });
}
@Override return true;
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Map<String, Object> 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<String, Object> obj = new HashMap<>(); public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) {
if (inAppBrowserActivity != null) FrameLayout layout = new FrameLayout(view.getContext());
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("progress", progress);
getChannel().invokeMethod("onProgressChanged", obj);
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 layout.setPaddingRelative(45, 15, 45, 0);
public void onReceivedTitle(WebView view, String title) { layout.addView(input);
super.onReceivedTitle(view, title);
if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty())
inAppBrowserActivity.actionBar.setTitle(title);
}
@Override String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message;
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
//The undocumented magic method override final String finalValue = value;
//Eclipse will swear at you if you try to put @Override here DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() {
// For Android 3.0+ @Override
public void openFileChooser(ValueCallback<Uri> uploadMsg) { 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; AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext(), R.style.Theme_AppCompat_Dialog_Alert);
Intent i = new Intent(Intent.ACTION_GET_CONTENT); alertDialogBuilder.setMessage(alertMessage);
i.addCategory(Intent.CATEGORY_OPENABLE); if (confirmButtonTitle != null && !confirmButtonTitle.isEmpty()) {
i.setType("image/*"); alertDialogBuilder.setPositiveButton(confirmButtonTitle, confirmClickListener);
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } 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+ AlertDialog alertDialog = alertDialogBuilder.create();
public void openFileChooser(ValueCallback uploadMsg, String acceptType) { alertDialog.setView(layout);
mUploadMessage = uploadMsg; alertDialog.show();
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<Uri> 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<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (mUploadMessageArray != null) {
mUploadMessageArray.onReceiveValue(null);
} }
mUploadMessageArray = filePathCallback;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); @Override
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
contentSelectionIntent.setType("*/*"); WebView.HitTestResult result = view.getHitTestResult();
Intent[] intentArray; String data = result.getExtra();
intentArray = new Intent[0]; Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("url", data);
getChannel().invokeMethod("onTargetBlank", obj);
return false;
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); @Override
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) {
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); Map<String, Object> obj = new HashMap<>();
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); if (inAppBrowserActivity != null)
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); obj.put("uuid", inAppBrowserActivity.uuid);
return true; obj.put("origin", origin);
} getChannel().invokeMethod("onGeolocationPermissionsShowPrompt", obj, new MethodChannel.Result() {
@Override
public void success(Object o) {
Map<String, Object> response = (Map<String, Object>) o;
if (response != null)
callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain"));
else
callback.invoke(origin, false, false);
}
private MethodChannel getChannel() { @Override
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; 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<String, Object> 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<String, Object> 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<Uri> 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<Uri> 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<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (mUploadMessageArray == null) {
mUploadMessageArray = filePathCallback;
}
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);
return true;
}
private MethodChannel getChannel() {
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel;
}
@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));
}
return false;
}
} }