infil00p closed pull request #99: inAppBrowser custom application scheme handling on android URL: https://github.com/apache/cordova-plugin-inappbrowser/pull/99
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index c10617e5..2e517771 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -22,6 +22,8 @@ Licensed to the Apache Software Foundation (ASF) under one import android.content.Context; import android.content.Intent; import android.provider.Browser; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; @@ -68,6 +70,7 @@ Licensed to the Apache Software Foundation (ASF) under one import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.List; import java.util.StringTokenizer; @SuppressLint("SetJavaScriptEnabled") @@ -88,6 +91,7 @@ Licensed to the Apache Software Foundation (ASF) under one private static final String CLEAR_SESSION_CACHE = "clearsessioncache"; private static final String HARDWARE_BACK_BUTTON = "hardwareback"; private static final String MEDIA_PLAYBACK_REQUIRES_USER_ACTION = "mediaPlaybackRequiresUserAction"; + private static final String DISMISSABLE_WITH_BACK_BUTTON = "dismissablewithbackbutton"; private static final String SHOULD_PAUSE = "shouldPauseOnSuspend"; private static final Boolean DEFAULT_HARDWARE_BACK = true; private static final String USER_WIDE_VIEW_PORT = "useWideViewPort"; @@ -103,6 +107,7 @@ Licensed to the Apache Software Foundation (ASF) under one private boolean clearSessionCache = false; private boolean hadwareBackButton = true; private boolean mediaPlaybackRequiresUserGesture = false; + private boolean dismissableWithBackButton = true; private boolean shouldPauseInAppBrowser = false; private boolean useWideViewPort = true; private ValueCallback<Uri> mUploadCallback; @@ -477,6 +482,14 @@ public boolean hardwareBack() { return hadwareBackButton; } + /** + * Has the user set that back button can dismiss the dialog + * @return + */ + public boolean isDismissableWithBackButton() { + return dismissableWithBackButton; + } + /** * Checks to see if it is possible to go forward one page in history, then does so. */ @@ -549,6 +562,10 @@ public String showWebPage(final String url, HashMap<String, Boolean> features) { } else { hadwareBackButton = DEFAULT_HARDWARE_BACK; } + Boolean dismissable = features.get(DISMISSABLE_WITH_BACK_BUTTON); + if (dismissable != null) { + dismissableWithBackButton = dismissable.booleanValue(); + } Boolean mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION); if (mediaPlayback != null) { mediaPlaybackRequiresUserGesture = mediaPlayback.booleanValue(); @@ -943,7 +960,15 @@ public InAppBrowserClient(CordovaWebView webView, EditText mEditText) { */ @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { - if (url.startsWith(WebView.SCHEME_TEL)) { + // handle back to application redirect without processing url by webView + final Intent customSchemeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + final PackageManager packageManager = cordova.getActivity().getApplicationContext().getPackageManager(); + final List<ResolveInfo> resolvedActivities = packageManager.queryIntentActivities(customSchemeIntent, 0); + + String newloc = ""; + if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("file:")) { + newloc = url; + } else if (url.startsWith(WebView.SCHEME_TEL)) { try { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse(url)); @@ -992,6 +1017,10 @@ else if (url.startsWith("sms:")) { } catch (android.content.ActivityNotFoundException e) { LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString()); } + } else if(resolvedActivities.size() > 0) { + cordova.getActivity().startActivity(customSchemeIntent); + closeDialog(); + return true; } return false; } diff --git a/src/android/InAppBrowserDialog.java b/src/android/InAppBrowserDialog.java index e7b212f2..0914d199 100644 --- a/src/android/InAppBrowserDialog.java +++ b/src/android/InAppBrowserDialog.java @@ -49,7 +49,7 @@ public void onBackPressed () { // because it does a clean up if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) { this.inAppBrowser.goBack(); - } else { + } else if (this.inAppBrowser.isDismissableWithBackButton()) { this.inAppBrowser.closeDialog(); } } diff --git a/src/ios/CDVInAppBrowser.m b/src/ios/CDVInAppBrowser.m index c9ebad24..980ead2b 100644 --- a/src/ios/CDVInAppBrowser.m +++ b/src/ios/CDVInAppBrowser.m @@ -37,6 +37,7 @@ Licensed to the Apache Software Foundation (ASF) under one @interface CDVInAppBrowser () { NSInteger _previousStatusBarStyle; + BOOL _retryFailingRequest; } @end @@ -46,6 +47,7 @@ - (void)pluginInitialize { _previousStatusBarStyle = -1; _callbackIdPattern = nil; + _retryFailingRequest = YES; } - (id)settingForKey:(NSString*)key @@ -471,13 +473,21 @@ - (void)webViewDidFinishLoad:(UIWebView*)theWebView - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error { - if (self.callbackId != nil) { - NSString* url = [self.inAppBrowserViewController.currentURL absoluteString]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR - messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}]; - [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; - - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; + if (_retryFailingRequest) { + _retryFailingRequest = NO; + NSURL *url =[NSURL URLWithString:error.userInfo[@"NSErrorFailingURLStringKey"]]; + [self openInSystem:url]; + return; + } else { + _retryFailingRequest = YES; + if (self.callbackId != nil) { + NSString* url = [self.inAppBrowserViewController.currentURL absoluteString]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR + messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}]; + [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; + } } } @@ -783,6 +793,16 @@ - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition - (void)viewDidLoad { + CGRect frame = [UIApplication sharedApplication].statusBarFrame; + UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:frame]; + bgToolbar.barStyle = UIBarStyleDefault; + bgToolbar.tintColor = [UIColor whiteColor]; + bgToolbar.barTintColor = [UIColor whiteColor]; +// [[UIBarItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor : [UIColor whiteColor]}]; + + + [self.view addSubview:bgToolbar]; + [super viewDidLoad]; } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org