From df8bcaf7515569733704fe780402f9123a60d61d Mon Sep 17 00:00:00 2001 From: ekidder3 Date: Thu, 5 Feb 2015 07:20:57 -0500 Subject: [PATCH] CB-8467 Added support for hiding the web view container. This maintains the browser session without closing it. The browser window can be repeatedly hidden and shown. ** This has only been tested on android and ios ** amazon/android: An additional `hide` action was added to `InAppBrowser#execute`. It is identical to `show`, except that it calls `dialog.hide()` instead. blackberry10: no changes firefoxos: Added a `hide` method that is identical to `show`, indicating it is not supported. ios: Added a `hide` method that is identical to `show`, except that it uses `dismissViewControllerAnimated`. It checks the value of `_previousStatusBarStyle`. If it is `-1`, the method returns with no action performed. If it is not, it is set to `-1.` ubuntu: Added a `hide` method that sets `CordovaWrapper.global.inappbrowser.visible` to `false`. windows: Added a `hide` method that sets `browserWrap.style.display` to `none`. wp: Added a `hide` method that is identical to `show`, except that it sets `browser.Visibility` to `Visibility.Collapsed` and sets `AppBar.IsVisible` to `false`. --- README.md | 24 ++++++++++++++- src/amazon/InAppBrowser.java | 11 +++++++ src/android/InAppBrowser.java | 11 +++++++ src/firefoxos/InAppBrowserProxy.js | 4 +++ src/ios/CDVInAppBrowser.h | 1 + src/ios/CDVInAppBrowser.m | 48 ++++++++++++++++++++++++++++++ src/ubuntu/inappbrowser.cpp | 4 +++ src/ubuntu/inappbrowser.h | 1 + src/windows/InAppBrowserProxy.js | 5 ++++ src/wp/InAppBrowser.cs | 15 ++++++++++ www/inappbrowser.js | 5 +++- www/windows8/InAppBrowserProxy.js | 6 ++++ 12 files changed, 133 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3eecf1a..1e0645c 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i - removeEventListener - close - show +- hide - executeScript - insertCSS @@ -414,6 +415,27 @@ The function is passed an `InAppBrowserEvent` object. // some time later... ref.show(); +## InAppBrowser.hide + +> Hides the InAppBrowser window. Calling this has no effect if the InAppBrowser was already hidden. + + ref.hide(); + +- __ref__: reference to the InAppBrowser window (`InAppBrowser`) + +### Supported Platforms + +- Amazon Fire OS +- Android +- iOS +- Windows 8 and 8.1 + +### Quick Example + + var ref = cordova.InAppBrowser.open('http://apache.org', '_blank'); + // some time later... + ref.hide(); + ## InAppBrowser.executeScript > Injects JavaScript code into the `InAppBrowser` window @@ -683,4 +705,4 @@ iab.open('http://url-that-fails-whitelist.com', '_blank'); // loads in th iab.open('http://url-that-fails-whitelist.com', 'random_string'); // loads in the InAppBrowser iab.open('http://url-that-fails-whitelist.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar -``` \ No newline at end of file +``` diff --git a/src/amazon/InAppBrowser.java b/src/amazon/InAppBrowser.java index 0263ea2..5c53488 100644 --- a/src/amazon/InAppBrowser.java +++ b/src/amazon/InAppBrowser.java @@ -212,6 +212,17 @@ public class InAppBrowser extends CordovaPlugin { pluginResult.setKeepCallback(true); this.callbackContext.sendPluginResult(pluginResult); } + else if (action.equals("hide")) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.hide(); + } + }); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK); + pluginResult.setKeepCallback(true); + this.callbackContext.sendPluginResult(pluginResult); + } else { return false; } diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 3253d92..b54c224 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -253,6 +253,17 @@ public class InAppBrowser extends CordovaPlugin { pluginResult.setKeepCallback(true); this.callbackContext.sendPluginResult(pluginResult); } + else if (action.equals("hide")) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.hide(); + } + }); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK); + pluginResult.setKeepCallback(true); + this.callbackContext.sendPluginResult(pluginResult); + } else { return false; } diff --git a/src/firefoxos/InAppBrowserProxy.js b/src/firefoxos/InAppBrowserProxy.js index c09e358..33db1b9 100644 --- a/src/firefoxos/InAppBrowserProxy.js +++ b/src/firefoxos/InAppBrowserProxy.js @@ -43,6 +43,10 @@ var IABExecs = { console.error('[FirefoxOS] show not implemented'); }, + hide: function (win, lose) { + console.error('[FirefoxOS] hide not implemented'); + }, + open: function (win, lose, args) { var strUrl = args[0], target = args[1], diff --git a/src/ios/CDVInAppBrowser.h b/src/ios/CDVInAppBrowser.h index 6bb0ec1..d258eb0 100644 --- a/src/ios/CDVInAppBrowser.h +++ b/src/ios/CDVInAppBrowser.h @@ -40,6 +40,7 @@ - (void)close:(CDVInvokedUrlCommand*)command; - (void)injectScriptCode:(CDVInvokedUrlCommand*)command; - (void)show:(CDVInvokedUrlCommand*)command; +- (void)hide:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/CDVInAppBrowser.m b/src/ios/CDVInAppBrowser.m index b342ca7..a114799 100644 --- a/src/ios/CDVInAppBrowser.m +++ b/src/ios/CDVInAppBrowser.m @@ -243,6 +243,54 @@ }); } +- (void)hide:(CDVInvokedUrlCommand*)command +{ + if (self.inAppBrowserViewController == nil) { + NSLog(@"Tried to hide IAB after it was closed."); + return; + + + } + if (_previousStatusBarStyle == -1) { + NSLog(@"Tried to hide IAB while already hidden"); + return; + } + + _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle; + + // Run later to avoid the "took a long time" log message. + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.inAppBrowserViewController != nil) { + _previousStatusBarStyle = -1; + [self.viewController dismissViewControllerAnimated:YES completion:nil]; + } + }); +} + +- (void)hide:(CDVInvokedUrlCommand*)command +{ + if (self.inAppBrowserViewController == nil) { + NSLog(@"Tried to hide IAB after it was closed."); + return; + + + } + if (_previousStatusBarStyle == -1) { + NSLog(@"Tried to hide IAB while already hidden"); + return; + } + + _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle; + + // Run later to avoid the "took a long time" log message. + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.inAppBrowserViewController != nil) { + _previousStatusBarStyle = -1; + [self.viewController dismissViewControllerAnimated:YES completion:nil]; + } + }); +} + - (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options { NSURLRequest* request = [NSURLRequest requestWithURL:url]; diff --git a/src/ubuntu/inappbrowser.cpp b/src/ubuntu/inappbrowser.cpp index c5a9e64..a928eff 100644 --- a/src/ubuntu/inappbrowser.cpp +++ b/src/ubuntu/inappbrowser.cpp @@ -65,6 +65,10 @@ void Inappbrowser::show(int, int) { m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = true"); } +void Inappbrowser::hide(int, int) { + m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = false"); +} + void Inappbrowser::close(int, int) { m_cordova->execQML("CordovaWrapper.global.inappbrowser.destroy()"); this->callbackWithoutRemove(_eventCb, EXIT_EVENT); diff --git a/src/ubuntu/inappbrowser.h b/src/ubuntu/inappbrowser.h index 1da4e03..9ad61ec 100644 --- a/src/ubuntu/inappbrowser.h +++ b/src/ubuntu/inappbrowser.h @@ -46,6 +46,7 @@ public: public slots: void open(int cb, int, const QString &url, const QString &windowName, const QString &windowFeatures); void show(int, int); + void hide(int, int); void close(int, int); void injectStyleFile(int cb, int, const QString&, bool); void injectStyleCode(int cb, int, const QString&, bool); diff --git a/src/windows/InAppBrowserProxy.js b/src/windows/InAppBrowserProxy.js index 23f6e54..4776a4f 100644 --- a/src/windows/InAppBrowserProxy.js +++ b/src/windows/InAppBrowserProxy.js @@ -118,6 +118,11 @@ var IAB = { } }); }, + hide: function (win, lose) { + if (browserWrap) { + browserWrap.style.display = "none"; + } + }, open: function (win, lose, args) { // make function async so that we can add navigation events handlers before view is loaded and navigation occured setImmediate(function () { diff --git a/src/wp/InAppBrowser.cs b/src/wp/InAppBrowser.cs index ddb5122..fa6fbe4 100644 --- a/src/wp/InAppBrowser.cs +++ b/src/wp/InAppBrowser.cs @@ -131,6 +131,21 @@ namespace WPCordovaClassLib.Cordova.Commands } } + public void hide(string options) + { + string[] args = JSON.JsonHelper.Deserialize(options); + + + if (browser != null) + { + Deployment.Current.Dispatcher.BeginInvoke(() => + { + browser.Visibility = Visibility.Collapsed; + AppBar.IsVisible = false; + }); + } + } + public void injectScriptCode(string options) { string[] args = JSON.JsonHelper.Deserialize(options); diff --git a/www/inappbrowser.js b/www/inappbrowser.js index 25f6271..983dbba 100644 --- a/www/inappbrowser.js +++ b/www/inappbrowser.js @@ -50,7 +50,10 @@ exec(null, null, "InAppBrowser", "close", []); }, show: function (eventname) { - exec(null, null, "InAppBrowser", "show", []); + exec(null, null, "InAppBrowser", "show", []); + }, + hide: function (eventname) { + exec(null, null, "InAppBrowser", "hide", []); }, addEventListener: function (eventname,f) { if (eventname in this.channels) { diff --git a/www/windows8/InAppBrowserProxy.js b/www/windows8/InAppBrowserProxy.js index ed95477..d780fcd 100644 --- a/www/windows8/InAppBrowserProxy.js +++ b/www/windows8/InAppBrowserProxy.js @@ -38,6 +38,12 @@ var IAB = { }*/ }, + hide: function (win, lose) { + /* empty block, ran out of bacon? + if (browserWrap) { + + }*/ + }, open: function (win, lose, args) { var strUrl = args[0], target = args[1],