Commit 5978aba7 authored by pichillilorenzo's avatar pichillilorenzo

fix hidden attribute, added isHidden() method

parent 57e1a86b
This diff is collapsed.
...@@ -29,25 +29,21 @@ class MyInAppBrowser extends InAppBrowser { ...@@ -29,25 +29,21 @@ class MyInAppBrowser extends InAppBrowser {
@override @override
void onLoadStart(String url) { void onLoadStart(String url) {
super.onLoadStart(url);
print("\n\nStarted $url\n\n"); print("\n\nStarted $url\n\n");
} }
@override @override
void onLoadStop(String url) { void onLoadStop(String url) {
super.onLoadStop(url);
print("\n\nStopped $url\n\n"); print("\n\nStopped $url\n\n");
} }
@override @override
void onLoadError(String url, int code, String message) { void onLoadError(String url, int code, String message) {
super.onLoadError(url, code, message);
print("\n\nCan't load $url.. Error: $message\n\n"); print("\n\nCan't load $url.. Error: $message\n\n");
} }
@override @override
void onExit() { void onExit() {
super.onExit();
print("\n\nBrowser closed!\n\n"); print("\n\nBrowser closed!\n\n");
} }
......
...@@ -195,13 +195,16 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -195,13 +195,16 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
case "canGoForward": case "canGoForward":
result.success(canGoForward()); result.success(canGoForward());
break; break;
case "isLoading":
result.success(isLoading());
break;
case "stopLoading": case "stopLoading":
stopLoading(); stopLoading();
result.success(true); result.success(true);
break; break;
case "isLoading":
result.success(isLoading());
break;
case "isHidden":
result.success(isHidden());
break;
default: default:
result.notImplemented(); result.notImplemented();
} }
...@@ -343,7 +346,7 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -343,7 +346,7 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
} }
// If the current app package isn't a target for this URL, then use // If the current app package isn't a target for this URL, then use
// the normal launch behavior // the normal launch behavior
if (hasCurrentPackage == false || targetIntents.size() == 0) { if (!hasCurrentPackage || targetIntents.size() == 0) {
activity.startActivity(intent); activity.startActivity(intent);
} }
// If there's only one possible intent, launch it directly // If there's only one possible intent, launch it directly
...@@ -400,6 +403,12 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -400,6 +403,12 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
return false; return false;
} }
public boolean isHidden() {
if (webViewActivity != null)
return webViewActivity.isHidden;
return false;
}
public void stopLoading() { public void stopLoading() {
if (webViewActivity != null) if (webViewActivity != null)
webViewActivity.stopLoading(); webViewActivity.stopLoading();
......
...@@ -28,6 +28,8 @@ public class InAppBrowserWebViewClient extends WebViewClient { ...@@ -28,6 +28,8 @@ public class InAppBrowserWebViewClient extends WebViewClient {
@Override @Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) { public boolean shouldOverrideUrlLoading(WebView webView, String url) {
//return true;
if (url.startsWith(WebView.SCHEME_TEL)) { if (url.startsWith(WebView.SCHEME_TEL)) {
try { try {
Intent intent = new Intent(Intent.ACTION_DIAL); Intent intent = new Intent(Intent.ACTION_DIAL);
......
...@@ -7,6 +7,7 @@ import android.os.Build; ...@@ -7,6 +7,7 @@ import android.os.Build;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
...@@ -34,10 +35,12 @@ public class WebViewActivity extends AppCompatActivity { ...@@ -34,10 +35,12 @@ public class WebViewActivity extends AppCompatActivity {
InAppBrowserOptions options; InAppBrowserOptions options;
ProgressBar progressBar; ProgressBar progressBar;
public boolean isLoading = false; public boolean isLoading = false;
public boolean isHidden = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view); setContentView(R.layout.activity_web_view);
webView = findViewById(R.id.webView); webView = findViewById(R.id.webView);
...@@ -273,12 +276,21 @@ public class WebViewActivity extends AppCompatActivity { ...@@ -273,12 +276,21 @@ public class WebViewActivity extends AppCompatActivity {
} }
public void hide() { public void hide() {
if (webView != null) isHidden = true;
webView.setVisibility(View.INVISIBLE); Intent openMainActivity= new Intent(this, InAppBrowserFlutterPlugin.registrar.activity().getClass());
openMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openMainActivity, 0);
} }
public void show() { public void show() {
isHidden = false;
Intent openMainActivity= new Intent(InAppBrowserFlutterPlugin.registrar.activity(), WebViewActivity.class);
openMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openMainActivity, 0);
}
public void stopLoading(){
if (webView != null) if (webView != null)
webView.setVisibility(View.VISIBLE); webView.stopLoading();
} }
public boolean isLoading() { public boolean isLoading() {
...@@ -287,11 +299,6 @@ public class WebViewActivity extends AppCompatActivity { ...@@ -287,11 +299,6 @@ public class WebViewActivity extends AppCompatActivity {
return false; return false;
} }
public void stopLoading(){
if (webView != null)
webView.stopLoading();
}
private void clearCookies() { private void clearCookies() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() { CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
......
...@@ -5,30 +5,30 @@ import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; ...@@ -5,30 +5,30 @@ import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
class MyInAppBrowser extends InAppBrowser { class MyInAppBrowser extends InAppBrowser {
@override @override
void onLoadStart(String url) { Future onLoadStart(String url) async {
super.onLoadStart(url);
print("\n\nStarted $url\n\n"); print("\n\nStarted $url\n\n");
//print("\n\n ${await this.isHidden()} \n\n");
} }
@override @override
void onLoadStop(String url) async { Future onLoadStop(String url) async {
super.onLoadStop(url);
print("\n\nStopped $url\n\n"); print("\n\nStopped $url\n\n");
print(await this.injectScriptCode("document.body.innerHTML")); // print(await this.injectScriptCode("document.body.innerHTML"));
print(await this.injectScriptCode("3")); // print(await this.injectScriptCode("3"));
print(await this.injectScriptCode(""" // print(await this.injectScriptCode("""
function asd (a,b) { // function asd (a,b) {
return a+b; // return a+b;
}; // };
asd(3,5); // asd(3,5);
""")); // """));
print(await this.injectScriptCode(""" // print(await this.injectScriptCode("""
["3",56,"sdf"]; // ["3",56,"sdf"];
""")); // """));
print(await this.injectScriptCode(""" // print(await this.injectScriptCode("""
var x = {"as":4, "dfdfg": 6}; // var x = {"as":4, "dfdfg": 6};
x; // x;
""")); // """));
//print("\n\n ${await this.isHidden()} \n\n");
/*this.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js"); /*this.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
this.injectScriptCode(""" this.injectScriptCode("""
\$( "body" ).html( "Next Step..." ) \$( "body" ).html( "Next Step..." )
...@@ -44,13 +44,11 @@ class MyInAppBrowser extends InAppBrowser { ...@@ -44,13 +44,11 @@ class MyInAppBrowser extends InAppBrowser {
@override @override
void onLoadError(String url, int code, String message) { void onLoadError(String url, int code, String message) {
super.onLoadError(url, code, message);
print("\n\nCan't load $url.. Error: $message\n\n"); print("\n\nCan't load $url.. Error: $message\n\n");
} }
@override @override
void onExit() { void onExit() {
super.onExit();
print("\n\nBrowser closed!\n\n"); print("\n\nBrowser closed!\n\n");
} }
...@@ -82,11 +80,13 @@ class _MyAppState extends State<MyApp> { ...@@ -82,11 +80,13 @@ class _MyAppState extends State<MyApp> {
body: new Center( body: new Center(
child: new RaisedButton(onPressed: () { child: new RaisedButton(onPressed: () {
inAppBrowser.open("https://flutter.io/", options: { inAppBrowser.open("https://flutter.io/", options: {
//"hidden": true
//"toolbarTopFixedTitle": "Fixed title", //"toolbarTopFixedTitle": "Fixed title",
//"hideUrlBar": true, //"hideUrlBar": true,
//"toolbarTop": false, //"toolbarTop": false,
//"toolbarBottom": false //"toolbarBottom": false
}); });
}, },
child: Text("Open InAppBrowser") child: Text("Open InAppBrowser")
), ),
......
...@@ -79,6 +79,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio ...@@ -79,6 +79,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
var tmpWindow: UIWindow? var tmpWindow: UIWindow?
var browserOptions: InAppBrowserOptions? var browserOptions: InAppBrowserOptions?
var initHeaders: [String: String]? var initHeaders: [String: String]?
var isHidden = false
required init(coder aDecoder: NSCoder) { required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)! super.init(coder: aDecoder)!
......
...@@ -87,6 +87,9 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -87,6 +87,9 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
self.webViewController?.webView.stopLoading() self.webViewController?.webView.stopLoading()
result(true) result(true)
break break
case "isHidden":
result((self.webViewController?.isHidden ?? false) == true)
break
case "injectScriptCode": case "injectScriptCode":
self.injectScriptCode(arguments: arguments!, result: result) self.injectScriptCode(arguments: arguments!, result: result)
break break
...@@ -176,7 +179,12 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -176,7 +179,12 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
let browserOptions = InAppBrowserOptions() let browserOptions = InAppBrowserOptions()
browserOptions.parse(options: options) browserOptions.parse(options: options)
if webViewController == nil { if webViewController != nil {
close()
}
else if self.previousStatusBarStyle == -1 {
self.previousStatusBarStyle = UIApplication.shared.statusBarStyle.rawValue
}
if !(self.tmpWindow != nil) { if !(self.tmpWindow != nil) {
let frame: CGRect = UIScreen.main.bounds let frame: CGRect = UIScreen.main.bounds
...@@ -187,43 +195,58 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -187,43 +195,58 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
let vc = storyboard.instantiateViewController(withIdentifier: WEBVIEW_STORYBOARD_CONTROLLER_ID) let vc = storyboard.instantiateViewController(withIdentifier: WEBVIEW_STORYBOARD_CONTROLLER_ID)
webViewController = vc as? InAppBrowserWebViewController webViewController = vc as? InAppBrowserWebViewController
webViewController?.browserOptions = browserOptions webViewController?.browserOptions = browserOptions
webViewController?.isHidden = browserOptions.hidden
webViewController?.tmpWindow = tmpWindow webViewController?.tmpWindow = tmpWindow
webViewController?.currentURL = url webViewController?.currentURL = url
webViewController?.initHeaders = headers webViewController?.initHeaders = headers
webViewController?.navigationDelegate = self webViewController?.navigationDelegate = self
}
if !browserOptions.hidden { let tmpController = UIViewController()
show() let baseWindowLevel = UIApplication.shared.keyWindow?.windowLevel
self.tmpWindow?.rootViewController = tmpController
self.tmpWindow?.windowLevel = UIWindowLevel(baseWindowLevel! + 1)
self.tmpWindow?.makeKeyAndVisible()
if browserOptions.hidden {
webViewController!.view.isHidden = true
tmpController.present(self.webViewController!, animated: false, completion: {() -> Void in
if self.previousStatusBarStyle != -1 {
UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: self.previousStatusBarStyle)!
}
})
if self.previousStatusBarStyle != -1 {
UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: self.previousStatusBarStyle)!
}
webViewController?.presentingViewController?.dismiss(animated: false, completion: {() -> Void in
self.tmpWindow?.windowLevel = 0.0
UIApplication.shared.delegate?.window??.makeKeyAndVisible()
if self.previousStatusBarStyle != -1 {
UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: self.previousStatusBarStyle)!
}
})
}
else {
tmpController.present(webViewController!, animated: true, completion: nil)
} }
} }
public func show() { public func show() {
if webViewController == nil { if webViewController == nil {
print("Tried to show IAB after it was closed.") print("Tried to hide IAB after it was closed.")
return
}
if previousStatusBarStyle != -1 {
print("Tried to show IAB while already shown")
return return
} }
weak var weakSelf: SwiftFlutterPlugin? = self self.webViewController?.isHidden = false
self.webViewController!.view.isHidden = false
// Run later to avoid the "took a long time" log message. // Run later to avoid the "took a long time" log message.
DispatchQueue.main.async(execute: {() -> Void in DispatchQueue.main.async(execute: {() -> Void in
if weakSelf?.webViewController != nil { if self.webViewController != nil {
if !(self.tmpWindow != nil) {
let frame: CGRect = UIScreen.main.bounds
self.tmpWindow = UIWindow(frame: frame)
}
let tmpController = UIViewController()
let baseWindowLevel = UIApplication.shared.keyWindow?.windowLevel let baseWindowLevel = UIApplication.shared.keyWindow?.windowLevel
self.tmpWindow?.rootViewController = tmpController
self.tmpWindow?.windowLevel = UIWindowLevel(baseWindowLevel! + 1) self.tmpWindow?.windowLevel = UIWindowLevel(baseWindowLevel! + 1)
self.tmpWindow?.makeKeyAndVisible() self.tmpWindow?.makeKeyAndVisible()
UIApplication.shared.delegate?.window??.makeKeyAndVisible()
tmpController.present(self.webViewController!, animated: true, completion: nil) self.tmpWindow?.rootViewController?.present(self.webViewController!, animated: true, completion: nil)
} }
}) })
} }
...@@ -233,19 +256,20 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -233,19 +256,20 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
print("Tried to hide IAB after it was closed.") print("Tried to hide IAB after it was closed.")
return return
} }
if previousStatusBarStyle == -1 {
print("Tried to hide IAB while already hidden") if self.webViewController != nil {
return self.webViewController?.isHidden = true
} }
previousStatusBarStyle = UIApplication.shared.statusBarStyle.rawValue
// Run later to avoid the "took a long time" log message. // Run later to avoid the "took a long time" log message.
DispatchQueue.main.async(execute: {() -> Void in DispatchQueue.main.async(execute: {() -> Void in
if self.webViewController != nil { if self.webViewController != nil {
self.previousStatusBarStyle = -1
self.webViewController?.presentingViewController?.dismiss(animated: true, completion: {() -> Void in self.webViewController?.presentingViewController?.dismiss(animated: true, completion: {() -> Void in
self.tmpWindow?.windowLevel = 0.0 self.tmpWindow?.windowLevel = 0.0
UIApplication.shared.delegate?.window??.makeKeyAndVisible() UIApplication.shared.delegate?.window??.makeKeyAndVisible()
if self.previousStatusBarStyle != -1 {
UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: self.previousStatusBarStyle)!
}
}) })
} }
}) })
...@@ -345,8 +369,6 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -345,8 +369,6 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: previousStatusBarStyle)! UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: previousStatusBarStyle)!
} }
previousStatusBarStyle = -1
// this value was reset before reapplying it. caused statusbar to stay black on ios7
} }
} }
...@@ -173,8 +173,13 @@ class InAppBrowser { ...@@ -173,8 +173,13 @@ class InAppBrowser {
return await _channel.invokeMethod('stopLoading'); return await _channel.invokeMethod('stopLoading');
} }
///Check if the Web View of the [InAppBrowser] instance is hidden.
Future<bool> isHidden() async {
return await _channel.invokeMethod('isHidden');
}
///Injects JavaScript code into the [InAppBrowser] window. (Only available when the target is set to `_blank` or to `_self`) ///Injects JavaScript code into the [InAppBrowser] window. (Only available when the target is set to `_blank` or to `_self`)
Future<dynamic> injectScriptCode(String source) async { Future<String> injectScriptCode(String source) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('source', () => source); args.putIfAbsent('source', () => source);
return await _channel.invokeMethod('injectScriptCode', args); return await _channel.invokeMethod('injectScriptCode', args);
......
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