Commit 7a57f80f authored by Lorenzo Pichilli's avatar Lorenzo Pichilli Committed by GitHub

Update InAppWebChromeClient.java

parent 7c5b1a1c
...@@ -9,6 +9,7 @@ import android.graphics.BitmapFactory; ...@@ -9,6 +9,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Message;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
...@@ -18,6 +19,7 @@ import android.webkit.JsResult; ...@@ -18,6 +19,7 @@ import android.webkit.JsResult;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText; import android.widget.EditText;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
...@@ -38,517 +40,540 @@ import io.flutter.plugin.common.PluginRegistry; ...@@ -38,517 +40,540 @@ import io.flutter.plugin.common.PluginRegistry;
import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
public class InAppWebChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener { public class InAppWebViewChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener {
protected static final String LOG_TAG = "IABWebChromeClient"; protected static final String LOG_TAG = "IABWebChromeClient";
private PluginRegistry.Registrar registrar; private PluginRegistry.Registrar registrar;
private FlutterWebView flutterWebView; private FlutterWebView flutterWebView;
private InAppBrowserActivity inAppBrowserActivity; private InAppBrowserActivity inAppBrowserActivity;
private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1; private final static int FILECHOOSER_RESULTCODE = 1;
private View mCustomView; private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback; private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer; private int mOriginalOrientation;
private int mOriginalOrientation; private int mOriginalSystemUiVisibility;
private int mOriginalSystemUiVisibility;
public InAppWebViewChromeClient(Object obj, PluginRegistry.Registrar registrar) {
public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) { super();
super(); this.registrar = registrar;
this.registrar = registrar; if (obj instanceof InAppBrowserActivity)
if (obj instanceof InAppBrowserActivity) this.inAppBrowserActivity = (InAppBrowserActivity) obj;
this.inAppBrowserActivity = (InAppBrowserActivity) obj; else if (obj instanceof FlutterWebView)
else if (obj instanceof FlutterWebView) this.flutterWebView = (FlutterWebView) obj;
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; public void onHideCustomView() {
} View decorView = this.registrar.activity().getWindow().getDecorView();
return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573); ((FrameLayout) decorView).removeView(this.mCustomView);
} this.mCustomView = null;
decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility);
public void onHideCustomView() { this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation);
View decorView = this.registrar.activity().getWindow().getDecorView(); this.mCustomViewCallback.onCustomViewHidden();
((FrameLayout) decorView).removeView(this.mCustomView); this.mCustomViewCallback = null;
this.mCustomView = null; }
decorView.setSystemUiVisibility(this.mOriginalSystemUiVisibility);
this.registrar.activity().setRequestedOrientation(this.mOriginalOrientation); public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
this.mCustomViewCallback.onCustomViewHidden(); if (this.mCustomView != null) {
this.mCustomViewCallback = null; onHideCustomView();
return;
} }
View decorView = this.registrar.activity().getWindow().getDecorView();
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { this.mCustomView = paramView;
if (this.mCustomView != null) { this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility();
onHideCustomView(); this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation();
return; this.mCustomViewCallback = paramCustomViewCallback;
} this.mCustomView.setBackgroundColor(Color.parseColor("#000000"));
View decorView = this.registrar.activity().getWindow().getDecorView(); ((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
this.mCustomView = paramView; decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
this.mOriginalSystemUiVisibility = decorView.getSystemUiVisibility(); }
this.mOriginalOrientation = this.registrar.activity().getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback; @Override
this.mCustomView.setBackgroundColor(Color.parseColor("#000000")); public boolean onJsAlert(final WebView view, String url, final String message,
((FrameLayout) decorView).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); final JsResult result) {
decorView.setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); Map<String, Object> obj = new HashMap<>();
} if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
@Override obj.put("message", message);
public boolean onJsAlert(final WebView view, String url, final String message,
final JsResult result) { getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() {
Map<String, Object> obj = new HashMap<>(); @Override
if (inAppBrowserActivity != null) public void success(Object response) {
obj.put("uuid", inAppBrowserActivity.uuid); String responseMessage = null;
obj.put("message", message); String confirmButtonTitle = null;
getChannel().invokeMethod("onJsAlert", obj, new MethodChannel.Result() { if (response != null) {
@Override Map<String, Object> responseMap = (Map<String, Object>) response;
public void success(Object response) { responseMessage = (String) responseMap.get("message");
String responseMessage = null; confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
String confirmButtonTitle = null; Boolean handledByClient = (Boolean) responseMap.get("handledByClient");
if (handledByClient != null && handledByClient) {
if (response != null) { Integer action = (Integer) responseMap.get("action");
Map<String, Object> responseMap = (Map<String, Object>) response; action = action != null ? action : 1;
responseMessage = (String) responseMap.get("message"); switch (action) {
confirmButtonTitle = (String) responseMap.get("confirmButtonTitle"); case 0:
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(); result.confirm();
dialog.dismiss(); break;
} case 1:
}; default:
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(); result.cancel();
dialog.dismiss();
} }
}); return;
}
}
AlertDialog alertDialog = alertDialogBuilder.create(); createAlertDialog(view, message, result, responseMessage, confirmButtonTitle);
alertDialog.show(); }
@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 alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public boolean onJsConfirm(final WebView view, String url, final String message, @Override
final JsResult result) { public void onCancel(DialogInterface dialog) {
Map<String, Object> obj = new HashMap<>(); result.cancel();
if (inAppBrowserActivity != null) dialog.dismiss();
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<String, Object> responseMap = (Map<String, Object>) 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);
result.cancel();
}
@Override
public void notImplemented() {
createConfirmDialog(view, message, result, null, null, null);
}
});
return true; AlertDialog alertDialog = alertDialogBuilder.create();
} alertDialog.show();
}
public void createConfirmDialog(WebView view, String message, final JsResult result, String responseMessage, String confirmButtonTitle, String cancelButtonTitle) { @Override
String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; public boolean onJsConfirm(final WebView view, String url, final String message,
DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { final JsResult result) {
@Override Map<String, Object> obj = new HashMap<>();
public void onClick(DialogInterface dialog, int which) { 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<String, Object> responseMap = (Map<String, Object>) 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(); result.confirm();
dialog.dismiss(); break;
} case 1:
}; default:
DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel(); 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, 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() { createConfirmDialog(view, message, result, responseMessage, confirmButtonTitle, cancelButtonTitle);
@Override }
public void onCancel(DialogInterface dialog) {
result.cancel(); @Override
dialog.dismiss(); public void error(String s, String s1, Object o) {
} Log.e(LOG_TAG, s + ", " + s1);
}); result.cancel();
}
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show(); @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()) {
@Override alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener);
public boolean onJsPrompt(final WebView view, String url, final String message, } else {
final String defaultValue, final JsPromptResult result) { alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener);
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
obj.put("message", message);
obj.put("defaultValue", defaultValue);
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;
if (response != null) {
Map<String, Object> responseMap = (Map<String, Object>) 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;
}
}
createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle);
}
@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) { alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
FrameLayout layout = new FrameLayout(view.getContext()); @Override
public void onCancel(DialogInterface dialog) {
final EditText input = new EditText(view.getContext()); result.cancel();
input.setMaxLines(1); dialog.dismiss();
input.setText((responseDefaultValue != null && !responseDefaultValue.isEmpty()) ? responseDefaultValue : defaultValue); }
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( });
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT); AlertDialog alertDialog = alertDialogBuilder.create();
input.setLayoutParams(lp); alertDialog.show();
}
layout.setPaddingRelative(45, 15, 45, 0);
layout.addView(input); @Override
public boolean onJsPrompt(final WebView view, String url, final String message,
String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message; final String defaultValue, final JsPromptResult result) {
Map<String, Object> obj = new HashMap<>();
final String finalValue = value; if (inAppBrowserActivity != null)
DialogInterface.OnClickListener confirmClickListener = new DialogInterface.OnClickListener() { obj.put("uuid", inAppBrowserActivity.uuid);
@Override obj.put("message", message);
public void onClick(DialogInterface dialog, int which) { obj.put("defaultValue", defaultValue);
String text = input.getText().toString();
result.confirm(finalValue != null ? finalValue : text); getChannel().invokeMethod("onJsPrompt", obj, new MethodChannel.Result() {
dialog.dismiss(); @Override
} public void success(Object response) {
}; String responseMessage = null;
DialogInterface.OnClickListener cancelClickListener = new DialogInterface.OnClickListener() { String responseDefaultValue = null;
@Override String confirmButtonTitle = null;
public void onClick(DialogInterface dialog, int which) { String cancelButtonTitle = null;
String value = null;
if (response != null) {
Map<String, Object> responseMap = (Map<String, Object>) 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(); 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, 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() { createPromptDialog(view, message, defaultValue, result, responseMessage, responseDefaultValue, value, cancelButtonTitle, confirmButtonTitle);
@Override }
public void onCancel(DialogInterface dialog) {
result.cancel(); @Override
dialog.dismiss(); public void error(String s, String s1, Object o) {
} Log.e(LOG_TAG, s + ", " + s1);
}); result.cancel();
}
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setView(layout); @Override
alertDialog.show(); public void notImplemented() {
} createPromptDialog(view, message, defaultValue, result, null, null, null, null, null);
}
@Override });
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult(); return true;
String data = result.getExtra(); }
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null) public void createPromptDialog(WebView view, String message, String defaultValue, final JsPromptResult result, String responseMessage, String responseDefaultValue, String value, String cancelButtonTitle, String confirmButtonTitle) {
obj.put("uuid", inAppBrowserActivity.uuid); FrameLayout layout = new FrameLayout(view.getContext());
obj.put("url", data);
getChannel().invokeMethod("onTargetBlank", obj); final EditText input = new EditText(view.getContext());
return false; 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()) {
@Override alertDialogBuilder.setNegativeButton(cancelButtonTitle, cancelClickListener);
public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { } else {
Map<String, Object> obj = new HashMap<>(); alertDialogBuilder.setNegativeButton(android.R.string.cancel, cancelClickListener);
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
public void error(String s, String s1, Object o) {
callback.invoke(origin, false, false);
}
@Override
public void notImplemented() {
callback.invoke(origin, false, false);
}
});
} }
@Override alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public boolean onConsoleMessage(ConsoleMessage consoleMessage) { @Override
Map<String, Object> obj = new HashMap<>(); public void onCancel(DialogInterface dialog) {
if (inAppBrowserActivity != null) result.cancel();
obj.put("uuid", inAppBrowserActivity.uuid); dialog.dismiss();
obj.put("sourceURL", consoleMessage.sourceId()); }
obj.put("lineNumber", consoleMessage.lineNumber()); });
obj.put("message", consoleMessage.message());
obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); AlertDialog alertDialog = alertDialogBuilder.create();
getChannel().invokeMethod("onConsoleMessage", obj); alertDialog.setView(layout);
return true; alertDialog.show();
} }
@Override @Override
public void onProgressChanged(WebView view, int progress) { public boolean onCreateWindow(WebView view, boolean isDialog, boolean userGesture, final Message resultMsg) {
if (inAppBrowserActivity != null && inAppBrowserActivity.progressBar != null) { final Map<String, Object> obj = new HashMap<>();
inAppBrowserActivity.progressBar.setVisibility(View.VISIBLE); if (inAppBrowserActivity != null)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { obj.put("uuid", inAppBrowserActivity.uuid);
inAppBrowserActivity.progressBar.setProgress(progress, true);
} else { WebView.HitTestResult result = view.getHitTestResult();
inAppBrowserActivity.progressBar.setProgress(progress); String data = result.getExtra();
}
if (progress == 100) { if (data == null) {
inAppBrowserActivity.progressBar.setVisibility(View.GONE); // to get the URL, create a temp weview
} final WebView newWebView = new WebView(view.getContext());
// disable javascript
newWebView.getSettings().setJavaScriptEnabled(false);
newWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView v, String url, Bitmap favicon) {
super.onPageStarted(v, url, favicon);
obj.put("url", url);
getChannel().invokeMethod("onTargetBlank", obj);
// stop webview loading
v.stopLoading();
// this will throw the error "Application attempted to call on a destroyed AwAutofillManager" that will kill the webview.
// that's ok.
v.destroy();
} }
});
Map<String, Object> obj = new HashMap<>(); ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
if (inAppBrowserActivity != null) resultMsg.sendToTarget();
obj.put("uuid", inAppBrowserActivity.uuid); return true;
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 obj.put("url", data);
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { getChannel().invokeMethod("onTargetBlank", obj);
mUploadMessage = uploadMsg; return false;
Intent i = new Intent(Intent.ACTION_GET_CONTENT); }
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*"); @Override
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); 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
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("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);
}
} }
//For Android 5.0+ Map<String, Object> obj = new HashMap<>();
public boolean onShowFileChooser( if (inAppBrowserActivity != null)
WebView webView, ValueCallback<Uri[]> filePathCallback, obj.put("uuid", inAppBrowserActivity.uuid);
FileChooserParams fileChooserParams) { obj.put("progress", progress);
InAppBrowserFlutterPlugin.uploadMessageArray = filePathCallback; getChannel().invokeMethod("onProgressChanged", obj);
try {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); super.onProgressChanged(view, progress);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); }
contentSelectionIntent.setType("*/*");
Intent[] intentArray; @Override
intentArray = new Intent[0]; public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); if (inAppBrowserActivity != null && inAppBrowserActivity.actionBar != null && inAppBrowserActivity.options.toolbarTopFixedTitle.isEmpty())
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); inAppBrowserActivity.actionBar.setTitle(title);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); }
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); @Override
} catch (ActivityNotFoundException e) { public void onReceivedIcon(WebView view, Bitmap icon) {
e.printStackTrace(); super.onReceivedIcon(view, icon);
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<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) {
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) { @TargetApi(Build.VERSION_CODES.LOLLIPOP)
if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED) && InAppBrowserFlutterPlugin.uploadMessageArray != null) { @Override
InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
} if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
return true; InAppBrowserFlutterPlugin.uploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
} }
return true;
}
private MethodChannel getChannel() { private MethodChannel getChannel() {
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel; return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment