Skip to content

Commit

Permalink
Added pause and resume methods for Android, Added pauseTimers and res…
Browse files Browse the repository at this point in the history
…umeTimers methods, Added new historyUrl optional parameter only for Android, fix #202, should fix #201
  • Loading branch information
pichillilorenzo committed Dec 2, 2019
1 parent ade4480 commit 6ba5b0c
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/BUG_REPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ about: Something is crashing or not working as intended

## Environment

**App version:** <!-- Add branch if necessary -->
**Plugin version:** <!-- Add branch if necessary -->
**Android version:** <!-- If customize ROM, write which -->
**Device information:** <!-- Manufacturer and model -->
**Flutter version:** <!-- Flutter version used -->
Expand Down
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

396 changes: 228 additions & 168 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.1.0

- Added `pause` and `resume` methods for Android.
- Added `pauseTimers` and `resumeTimers` methods.
- Added new `historyUrl` optional parameter for `loadData` and `openData` methods and `InAppWebViewInitialData` class. It is used only on Android.
- Fix "problems with onReceivedHttpAuthRequest when initialData is used" [#201](https://github.com/pichillilorenzo/flutter_inappwebview/issues/201)
- Fix "System ui (status bar and navigation bar) doesn't hide automatically" [#202](https://github.com/pichillilorenzo/flutter_inappwebview/issues/202)

## 2.0.1+1

- Fixed error "java.lang.ClassCastException: $Proxy1 cannot be cast to android.view.WindowManagerImpl" on Android when using native alert dialogs
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ Instead, on the `onLoadStop` WebView event, you can use `callHandler` directly:
* `onLoadHttpError`: Event fired when the InAppWebView main page receives an HTTP error.
* `onProgressChanged`: Event fired when the current progress of loading a page is changed.
* `onConsoleMessage`: Event fired when the InAppWebView receives a ConsoleMessage.
* `shouldOverrideUrlLoading`: Give the host application a chance to take control when a URL is about to be loaded in the current WebView.
* `onLoadResource`: Event fired when the InAppWebView loads a resource.
* `shouldOverrideUrlLoading`: Give the host application a chance to take control when a URL is about to be loaded in the current WebView (to use this event, the `useShouldOverrideUrlLoading` option must be `true`).
* `onLoadResource`: Event fired when the InAppWebView loads a resource (to use this event, the `useOnLoadResource` option must be `true`).
* `onScrollChanged`: Event fired when the InAppWebView scrolls.
* `onDownloadStart`: Event fired when InAppWebView recognizes and starts a downloadable file.
* `onDownloadStart`: Event fired when InAppWebView recognizes and starts a downloadable file (to use this event, the `useOnDownloadStart` option must be `true`).
* `onLoadResourceCustomScheme`: Event fired when the InAppWebView finds the `custom-scheme` while loading a resource. Here you can handle the url request and return a CustomSchemeResponse to load a specific resource encoded to `base64`.
* `onTargetBlank`: Event fired when the InAppWebView tries to open a link with `target="_blank"`.
* `onTargetBlank`: Event fired when the InAppWebView tries to open a link with `target="_blank"` (to use this event, the `useOnTargetBlank` option must be `true`).
* `onGeolocationPermissionsShowPrompt`: 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 (available only on Android).
* `onJsAlert`: Event fired when javascript calls the `alert()` method to display an alert dialog.
* `onJsConfirm`: Event fired when javascript calls the `confirm()` method to display a confirm dialog.
Expand All @@ -442,10 +442,10 @@ Instead, on the `onLoadStop` WebView event, you can use `callHandler` directly:
* `onReceivedServerTrustAuthRequest`: Event fired when the WebView need to perform server trust authentication (certificate validation).
* `onReceivedClientCertRequest`: Notify the host application to handle an SSL client certificate request.
* `onFindResultReceived`: Event fired as find-on-page operations progress.
* `shouldInterceptAjaxRequest`: Event fired when an `XMLHttpRequest` is sent to a server.
* `onAjaxReadyStateChange`: Event fired whenever the `readyState` attribute of an `XMLHttpRequest` changes.
* `onAjaxProgress`: Event fired as an `XMLHttpRequest` progress.
* `shouldInterceptFetchRequest`: Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API).
* `shouldInterceptAjaxRequest`: Event fired when an `XMLHttpRequest` is sent to a server (to use this event, the `useShouldInterceptAjaxRequest` option must be `true`).
* `onAjaxReadyStateChange`: Event fired whenever the `readyState` attribute of an `XMLHttpRequest` changes (to use this event, the `useShouldInterceptAjaxRequest` option must be `true`).
* `onAjaxProgress`: Event fired as an `XMLHttpRequest` progress (to use this event, the `useShouldInterceptAjaxRequest` option must be `true`).
* `shouldInterceptFetchRequest`: Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API) (to use this event, the `useShouldInterceptFetchRequest` option must be `true`).
* `onNavigationStateChange`: Event fired when the navigation state of the InAppWebView changes.
* `onPermissionRequest`: Event fired when the webview is requesting permission to access the specified resources and the permission currently isn't granted or denied (available only on Android).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public FlutterWebView(Registrar registrar, final Context context, int id, HashMa
String mimeType = initialData.get("mimeType");
String encoding = initialData.get("encoding");
String baseUrl = initialData.get("baseUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, null);
String historyUrl = initialData.get("historyUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}
else
webView.loadUrl(initialUrl, initialHeaders);
Expand Down Expand Up @@ -117,9 +118,10 @@ public void onMethodCall(MethodCall call, final Result result) {
String mimeType = (String) call.argument("mimeType");
String encoding = (String) call.argument("encoding");
String baseUrl = (String) call.argument("baseUrl");
String historyUrl = (String) call.argument("historyUrl");

if (webView != null)
webView.loadData(data, mimeType, encoding, baseUrl, result);
webView.loadData(data, mimeType, encoding, baseUrl, historyUrl, result);
else
result.success(false);
}
Expand Down Expand Up @@ -300,6 +302,38 @@ public void run() {
}
result.success(true);
break;
case "pause":
if (webView != null) {
webView.onPause();
result.success(true);
} else {
result.success(false);
}
break;
case "resume":
if (webView != null) {
webView.onResume();
result.success(true);
} else {
result.success(false);
}
break;
case "pauseTimers":
if (webView != null) {
webView.pauseTimers();
result.success(true);
} else {
result.success(false);
}
break;
case "resumeTimers":
if (webView != null) {
webView.resumeTimers();
result.success(true);
} else {
result.success(false);
}
break;
default:
result.notImplemented();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public void run() {
String mimeType = (String) call.argument("mimeType");
String encoding = (String) call.argument("encoding");
String baseUrl = (String) call.argument("baseUrl");
openData(activity, uuid, options, data, mimeType, encoding, baseUrl);
String historyUrl = (String) call.argument("historyUrl");
openData(activity, uuid, options, data, mimeType, encoding, baseUrl, historyUrl);
result.success(true);
}
});
Expand All @@ -180,7 +181,8 @@ public void run() {
String mimeType = (String) call.argument("mimeType");
String encoding = (String) call.argument("encoding");
String baseUrl = (String) call.argument("baseUrl");
loadData(uuid, data, mimeType, encoding, baseUrl, result);
String historyUrl = (String) call.argument("historyUrl");
loadData(uuid, data, mimeType, encoding, baseUrl, historyUrl, result);
}
break;
case "loadFile":
Expand Down Expand Up @@ -460,7 +462,7 @@ else if (!useChromeSafariBrowser) {
result.error(LOG_TAG, "No WebView fallback declared.", null);
}

public void openData(Activity activity, String uuid, HashMap<String, Object> options, String data, String mimeType, String encoding, String baseUrl) {
public void openData(Activity activity, String uuid, HashMap<String, Object> options, String data, String mimeType, String encoding, String baseUrl, String historyUrl) {
Intent intent = new Intent(activity, InAppBrowserActivity.class);
Bundle extras = new Bundle();

Expand All @@ -471,6 +473,7 @@ public void openData(Activity activity, String uuid, HashMap<String, Object> opt
extras.putString("mimeType", mimeType);
extras.putString("encoding", encoding);
extras.putString("baseUrl", baseUrl);
extras.putString("historyUrl", historyUrl);

intent.putExtras(extras);
activity.startActivity(intent);
Expand Down Expand Up @@ -513,10 +516,10 @@ public void postUrl(String uuid, String url, byte[] postData, Result result) {
inAppBrowserActivity.postUrl(url, postData, result);
}

public void loadData(String uuid, String data, String mimeType, String encoding, String baseUrl, Result result) {
public void loadData(String uuid, String data, String mimeType, String encoding, String baseUrl, String historyUrl, Result result) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
inAppBrowserActivity.loadData(data, mimeType, encoding, baseUrl, result);
inAppBrowserActivity.loadData(data, mimeType, encoding, baseUrl, historyUrl, result);
}

public void loadFile(String uuid, String url, Map<String, String> headers, Result result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ protected void onCreate(Bundle savedInstanceState) {
String mimeType = b.getString("mimeType");
String encoding = b.getString("encoding");
String baseUrl = b.getString("baseUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, null);
String historyUrl = b.getString("historyUrl");
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}

Map<String, Object> obj = new HashMap<>();
Expand Down Expand Up @@ -228,9 +229,9 @@ public void postUrl(String url, byte[] postData, MethodChannel.Result result) {
}
}

public void loadData(String data, String mimeType, String encoding, String baseUrl, MethodChannel.Result result) {
public void loadData(String data, String mimeType, String encoding, String baseUrl, String historyUrl, MethodChannel.Result result) {
if (webView != null) {
webView.loadData(data, mimeType, encoding, baseUrl, result);
webView.loadData(data, mimeType, encoding, baseUrl, historyUrl, result);
} else {
result.error(LOG_TAG, "webView is null", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ public void postUrl(String url, byte[] postData, MethodChannel.Result result) {
result.success(true);
}

public void loadData(String data, String mimeType, String encoding, String baseUrl, MethodChannel.Result result) {
loadDataWithBaseURL(baseUrl, data, mimeType, encoding, null);
public void loadData(String data, String mimeType, String encoding, String baseUrl, String historyUrl, MethodChannel.Result result) {
loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
result.success(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback
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);
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions example/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Flutter InAppBrowser</title>
<title>Flutter InAppWebView</title>
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
Expand All @@ -14,7 +14,7 @@
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
<header class="masthead mb-auto">
<div class="inner">
<h3 class="masthead-brand">Flutter InAppBrowser</h3>
<h3 class="masthead-brand">Flutter InAppWebView</h3>
<nav class="nav nav-masthead justify-content-center">
<a class="nav-link active" href="index.html">Home</a>
<a class="nav-link" href="page-1.html">Page 1</a>
Expand All @@ -37,5 +37,6 @@ <h1 class="cover-heading">Inline WebView</h1>
</div>
</footer>
</div>
<script src="js/main.js"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions example/assets/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function add(a, b) {
return a + b;
}
console.log(add(10, 20));
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ flutter:
- assets/index.html
- assets/page-1.html
- assets/page-2.html
- assets/js/
- assets/css/
- assets/images/
- assets/favicon.ico
Expand Down
12 changes: 12 additions & 0 deletions ios/Classes/FlutterWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
}
result(true)
break
case "pauseTimers":
if webView != nil {
webView!.pauseTimers()
}
result(true)
break
case "resumeTimers":
if webView != nil {
webView!.resumeTimers()
}
result(true)
break
case "removeFromSuperview":
webView!.removeFromSuperview()
result(true)
Expand Down
34 changes: 29 additions & 5 deletions ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
static var credentialsProposed: [URLCredential] = []
var lastScrollX: CGFloat = 0
var lastScrollY: CGFloat = 0
var isPausedTimers = false
var isPausedTimersCompletionHandler: (() -> Void)?

init(frame: CGRect, configuration: WKWebViewConfiguration, IABController: InAppBrowserWebViewController?, IAWController: FlutterWebViewController?) {

Expand All @@ -705,6 +707,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
}

public func prepare() {

addObserver(self,
forKeyPath: #keyPath(WKWebView.estimatedProgress),
options: .new,
Expand Down Expand Up @@ -1632,6 +1635,11 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
public func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {

if (isPausedTimers) {
isPausedTimersCompletionHandler = completionHandler
return
}

onJsAlert(message: message, result: {(result) -> Void in
if result is FlutterError {
print((result as! FlutterError).message)
Expand Down Expand Up @@ -2134,29 +2142,44 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
return (IABController != nil) ? SwiftFlutterPlugin.instance!.channel! : ((IAWController != nil) ? IAWController!.channel! : nil);
}

func findAllAsync(find: String?, completionHandler: ((Any?, Error?) -> Void)?) {
public func findAllAsync(find: String?, completionHandler: ((Any?, Error?) -> Void)?) {
let startSearch = "wkwebview_FindAllAsync('\(find ?? "")');"
evaluateJavaScript(startSearch, completionHandler: completionHandler)
}

func findNext(forward: Bool, completionHandler: ((Any?, Error?) -> Void)?) {
public func findNext(forward: Bool, completionHandler: ((Any?, Error?) -> Void)?) {
evaluateJavaScript("wkwebview_FindNext(\(forward ? "true" : "false"));", completionHandler: completionHandler)
}

func clearMatches(completionHandler: ((Any?, Error?) -> Void)?) {
public func clearMatches(completionHandler: ((Any?, Error?) -> Void)?) {
evaluateJavaScript("wkwebview_ClearMatches();", completionHandler: completionHandler)
}

func scrollTo(x: Int, y: Int) {
public func scrollTo(x: Int, y: Int) {
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: false)
}

func scrollBy(x: Int, y: Int) {
public func scrollBy(x: Int, y: Int) {
let newX = CGFloat(x) + scrollView.contentOffset.x
let newY = CGFloat(y) + scrollView.contentOffset.y
scrollView.setContentOffset(CGPoint(x: newX, y: newY), animated: false)
}


public func pauseTimers() {
isPausedTimers = true
let script = "alert();";
self.evaluateJavaScript(script, completionHandler: nil)
}

public func resumeTimers() {
if let completionHandler = isPausedTimersCompletionHandler {
completionHandler()
isPausedTimersCompletionHandler = nil
}
isPausedTimers = false
}

public override func removeFromSuperview() {
configuration.userContentController.removeScriptMessageHandler(forName: "consoleLog")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleDebug")
Expand All @@ -2175,5 +2198,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
IAWController?.channel?.setMethodCallHandler(nil)
IABController?.webView = nil
IAWController?.webView = nil
isPausedTimersCompletionHandler = nil
}
}
7 changes: 5 additions & 2 deletions lib/src/in_app_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,19 @@ class InAppBrowser {

///Opens a new [InAppBrowser] instance with [data] as a content, using [baseUrl] as the base URL for it.
///
///The [mimeType] parameter specifies the format of the data.
///The [mimeType] parameter specifies the format of the data. The default value is `"text/html"`.
///
///The [encoding] parameter specifies the encoding of the data.
///The [encoding] parameter specifies the encoding of the data. The default value is `"utf8"`.
///
///The [historyUrl] parameter is the URL to use as the history entry. The default value is `about:blank`. If non-null, this must be a valid URL. This parameter is used only on Android.
///
///The [options] parameter specifies the options for the [InAppBrowser].
Future<void> openData(
{@required String data,
String mimeType = "text/html",
String encoding = "utf8",
String baseUrl = "about:blank",
String historyUrl = "about:blank",
InAppBrowserClassOptions options}) async {
assert(data != null);

Expand Down
Loading

0 comments on commit 6ba5b0c

Please sign in to comment.