Commit 153ab602 authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

Updated javascript popups events

parent ab0a5cc5
This diff is collapsed.
def PLUGIN = "flutter_inappbrowser";
def ANDROIDX_WARNING = "flutterPluginsAndroidXWarning";
gradle.buildFinished { buildResult ->
if (buildResult.failure && !rootProject.ext.has(ANDROIDX_WARNING)) {
println ' *********************************************************'
println 'WARNING: This version of ' + PLUGIN + ' will break your Android build if it or its dependencies aren\'t compatible with AndroidX.'
println ' See https://goo.gl/CP92wY for more information on the problem and how to fix it.'
println ' This warning prints for all Android build failures. The real root cause of the error may be unrelated.'
println ' *********************************************************'
rootProject.ext.set(ANDROIDX_WARNING, true);
}
}
group 'com.pichillilorenzo.flutter_inappbrowser'
version '1.0-SNAPSHOT'
......
......@@ -112,11 +112,15 @@ public class InAppWebChromeClient extends WebChromeClient {
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;
String responseMessage = (String) responseMap.get("message");
String confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
boolean handledByClient = (boolean) responseMap.get("handledByClient");
if (handledByClient) {
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) {
......@@ -127,9 +131,31 @@ public class InAppWebChromeClient extends WebChromeClient {
default:
result.cancel();
}
} else {
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;
Log.d(LOG_TAG, alertMessage);
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
......@@ -157,21 +183,6 @@ public class InAppWebChromeClient extends WebChromeClient {
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
@Override
public void error(String s, String s1, Object o) {
Log.e(LOG_TAG, s + ", " + s1);
}
@Override
public void notImplemented() {
}
});
return true;
}
@Override
public boolean onJsConfirm(final WebView view, String url, final String message,
......@@ -184,12 +195,17 @@ public class InAppWebChromeClient extends WebChromeClient {
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;
String responseMessage = (String) responseMap.get("message");
String confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
String cancelButtonTitle = (String) responseMap.get("cancelButtonTitle");
boolean handledByClient = (boolean) responseMap.get("handledByClient");
if (handledByClient) {
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) {
......@@ -200,7 +216,29 @@ public class InAppWebChromeClient extends WebChromeClient {
default:
result.cancel();
}
} else {
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;
}
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
......@@ -241,21 +279,6 @@ public class InAppWebChromeClient extends WebChromeClient {
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
@Override
public void error(String s, String s1, Object o) {
Log.e(LOG_TAG, s + ", " + s1);
}
@Override
public void notImplemented() {
}
});
return true;
}
@Override
public boolean onJsPrompt(final WebView view, String url, final String message,
......@@ -269,28 +292,54 @@ public class InAppWebChromeClient extends WebChromeClient {
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;
String responseMessage = (String) responseMap.get("message");
String responseDefaultValue = (String) responseMap.get("defaultValue");
String confirmButtonTitle = (String) responseMap.get("confirmButtonTitle");
String cancelButtonTitle = (String) responseMap.get("cancelButtonTitle");
final String value = (String) responseMap.get("value");
boolean handledByClient = (boolean) responseMap.get("handledByClient");
if (handledByClient) {
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:
if (value != null)
result.confirm(value);
else
result.confirm();
break;
case 1:
default:
result.cancel();
}
} else {
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) {
FrameLayout layout = new FrameLayout(view.getContext());
final EditText input = new EditText(view.getContext());
......@@ -305,11 +354,13 @@ public class InAppWebChromeClient extends WebChromeClient {
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(value != null ? value : text);
result.confirm(finalValue != null ? finalValue : text);
dialog.dismiss();
}
};
......@@ -346,21 +397,6 @@ public class InAppWebChromeClient extends WebChromeClient {
alertDialog.setView(layout);
alertDialog.show();
}
}
@Override
public void error(String s, String s1, Object o) {
Log.e(LOG_TAG, s + ", " + s1);
}
@Override
public void notImplemented() {
}
});
return true;
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
......
......@@ -55,13 +55,14 @@
});
});
$(document).ready(function() {
/*
alert("Alert Popup");
console.log(confirm("Press a button!"));
console.log(prompt("Please enter your name", "Harry Potter"));
console.log(prompt("Please enter your name", "Lorenzo"));
*/
console.log("jQuery ready");
/*
/*
if ("geolocation" in navigator) {
console.log("Geolocation API enabled");
navigator.geolocation.getCurrentPosition(function(position) {
......@@ -69,7 +70,8 @@
});
} else {
console.log("No geolocation API");
}*/
}
*/
});
</script>
</body>
......
......@@ -198,6 +198,54 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
return response;
},
onJsAlert: (InAppWebViewController controller, String message) async {
JsAlertResponseAction action = await createAlertDialog(context, message);
return new JsAlertResponse(handledByClient: true, action: action);
},
onJsConfirm: (InAppWebViewController controller, String message) async {
JsConfirmResponseAction action = await createConfirmDialog(context, message);
return new JsConfirmResponse(handledByClient: true, action: action);
},
onJsPrompt: (InAppWebViewController controller, String message, String defaultValue) async {
_textFieldController.text = defaultValue;
JsPromptResponseAction action = await createPromptDialog(context, message);
return new JsPromptResponse(handledByClient: true, action: action, value: _textFieldController.text);
},
),
),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Icon(Icons.arrow_back),
onPressed: () {
if (webView != null) {
webView.goBack();
}
},
),
RaisedButton(
child: Icon(Icons.arrow_forward),
onPressed: () {
if (webView != null) {
webView.goForward();
}
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () {
if (webView != null) {
webView.reload();
}
},
),
],
),
]));
}
Future<JsAlertResponseAction> createAlertDialog(BuildContext context, String message) async {
JsAlertResponseAction action;
await showDialog(
......@@ -218,9 +266,10 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
},
);
return new JsAlertResponse(handledByClient: true, action: action);
},
onJsConfirm: (InAppWebViewController controller, String message) async {
return action;
}
Future<JsConfirmResponseAction> createConfirmDialog(BuildContext context, String message) async {
JsConfirmResponseAction action;
await showDialog(
......@@ -248,11 +297,11 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
},
);
return new JsConfirmResponse(handledByClient: true, action: action);
},
onJsPrompt: (InAppWebViewController controller, String message, String defaultValue) async {
return action;
}
Future<JsPromptResponseAction> createPromptDialog(BuildContext context, String message) async {
JsPromptResponseAction action;
_textFieldController.text = defaultValue;
await showDialog(
context: context,
......@@ -282,41 +331,7 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
},
);
return new JsPromptResponse(handledByClient: true, action: action, value: _textFieldController.text);
},
),
),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Icon(Icons.arrow_back),
onPressed: () {
if (webView != null) {
webView.goBack();
}
},
),
RaisedButton(
child: Icon(Icons.arrow_forward),
onPressed: () {
if (webView != null) {
webView.goForward();
}
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () {
if (webView != null) {
webView.reload();
}
},
),
],
),
]));
return action;
}
Future<String> _findLocalPath() async {
......
......@@ -81,10 +81,27 @@ class MyInappBrowser extends InAppBrowser {
Future<GeolocationPermissionShowPromptResponse> onGeolocationPermissionsShowPrompt(String origin) async {
print("request Geolocation permission API");
}
@override
Future<JsAlertResponse> onJsAlert(String message) async {
return new JsAlertResponse(handledByClient: false, message: "coma iam");
}
@override
Future<JsConfirmResponse> onJsConfirm(String message) {
}
@override
Future<JsPromptResponse> onJsPrompt(String message, String defaultValue) {
}
}
class WebviewExampleScreen extends StatefulWidget {
final MyInappBrowser browser = new MyInappBrowser();
static BuildContext context = null;
@override
_WebviewExampleScreenState createState() => new _WebviewExampleScreenState();
}
......@@ -97,11 +114,13 @@ class _WebviewExampleScreenState extends State<WebviewExampleScreen> {
@override
Widget build(BuildContext context) {
WebviewExampleScreen.context = context;
return new Center(
child: new RaisedButton(
onPressed: () {
widget.browser.open(
url: "https://www.google.com/",
widget.browser.openFile(
"assets/index.html",
//url: "https://www.google.com/",
options: [
InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
......
......@@ -44,7 +44,6 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
do {
let jsonData = try JSONSerialization.data(withJSONObject: contentBlockers, options: [])
let blockRules = String(data: jsonData, encoding: String.Encoding.utf8)
print(blockRules)
WKContentRuleListStore.default().compileContentRuleList(
forIdentifier: "ContentBlockingRules",
encodedContentRuleList: blockRules) { (contentRuleList, error) in
......
This diff is collapsed.
......@@ -18,6 +18,7 @@ A new Flutter plugin.
s.dependency 'Flutter'
s.ios.deployment_target = '8.0'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' }
s.swift_version = '5.0'
end
......@@ -358,6 +358,17 @@ class InAppBrowser {
}
///Event that notifies the host application that web content from the specified origin is attempting to use the Geolocation API, but no permission state is currently set for that origin.
///Note that for applications targeting Android N and later SDKs (API level > `Build.VERSION_CODES.M`) this method is only called for requests originating from secure origins such as https.
///On non-secure origins geolocation requests are automatically denied.
///
///[origin] represents the origin of the web content attempting to use the Geolocation API.
///
///**NOTE**: available only for Android.
Future<GeolocationPermissionShowPromptResponse> onGeolocationPermissionsShowPrompt (String origin) {
}
///Event fires when javascript calls the `alert()` method to display an alert dialog.
///If [JsAlertResponse.handledByClient] is `true`, the webview will assume that the client will handle the dialog.
///
......@@ -383,17 +394,6 @@ class InAppBrowser {
}
///Event that notifies the host application that web content from the specified origin is attempting to use the Geolocation API, but no permission state is currently set for that origin.
///Note that for applications targeting Android N and later SDKs (API level > `Build.VERSION_CODES.M`) this method is only called for requests originating from secure origins such as https.
///On non-secure origins geolocation requests are automatically denied.
///
///[origin] represents the origin of the web content attempting to use the Geolocation API.
///
///**NOTE**: available only for Android.
Future<GeolocationPermissionShowPromptResponse> onGeolocationPermissionsShowPrompt (String origin) {
}
void throwIsAlreadyOpened({String message = ''}) {
if (this.isOpened()) {
throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is already opened.']);
......
......@@ -428,31 +428,31 @@ class InAppWebViewController {
case "onGeolocationPermissionsShowPrompt":
String origin = call.arguments["origin"];
if (_widget != null && _widget.onGeolocationPermissionsShowPrompt != null)
return (await _widget.onGeolocationPermissionsShowPrompt(this, origin)).toMap();
return (await _widget.onGeolocationPermissionsShowPrompt(this, origin))?.toMap();
else if (_inAppBrowser != null)
return (await _inAppBrowser.onGeolocationPermissionsShowPrompt(origin)).toMap();
return (await _inAppBrowser.onGeolocationPermissionsShowPrompt(origin))?.toMap();
break;
case "onJsAlert":
String message = call.arguments["message"];
if (_widget != null && _widget.onJsAlert != null)
return (await _widget.onJsAlert(this, message)).toMap();
return (await _widget.onJsAlert(this, message))?.toMap();
else if (_inAppBrowser != null)
return (await _inAppBrowser.onJsAlert(message)).toMap();
return (await _inAppBrowser.onJsAlert(message))?.toMap();
break;
case "onJsConfirm":
String message = call.arguments["message"];
if (_widget != null && _widget.onJsConfirm != null)
return (await _widget.onJsConfirm(this, message)).toMap();
return (await _widget.onJsConfirm(this, message))?.toMap();
else if (_inAppBrowser != null)
return (await _inAppBrowser.onJsConfirm(message)).toMap();
return (await _inAppBrowser.onJsConfirm(message))?.toMap();
break;
case "onJsPrompt":
String message = call.arguments["message"];
String defaultValue = call.arguments["defaultValue"];
if (_widget != null && _widget.onJsPrompt != null)
return (await _widget.onJsPrompt(this, message, defaultValue)).toMap();
return (await _widget.onJsPrompt(this, message, defaultValue))?.toMap();
else if (_inAppBrowser != null)
return (await _inAppBrowser.onJsPrompt(message, defaultValue)).toMap();
return (await _inAppBrowser.onJsPrompt(message, defaultValue))?.toMap();
break;
case "onCallJsHandler":
String handlerName = call.arguments["handlerName"];
......
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