├── AUTHORS ├── CONTRIBUTING.md ├── CONTRIBUTORS ├── LICENSE ├── README.md ├── RELEASENOTES.md ├── demo ├── .gitignore ├── config.xml └── www │ ├── css │ └── index.css │ ├── index.html │ └── js │ └── index.js └── plugin ├── README.md ├── package.json ├── plugin.xml ├── src ├── android │ ├── BrowserTab.gradle │ ├── BrowserTab.java │ └── res │ │ └── browsertab.xml └── ios │ ├── CBTBrowserTab.h │ └── CBTBrowserTab.m └── www └── browsertab.js /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the official list of authors for copyright purposes. 2 | # This file is distinct from the CONTRIBUTORS files. 3 | # See the latter for an explanation. 4 | # Names should be added to this file as: 5 | # Name or Organization 6 | # The email address is not required for organizations. 7 | 8 | Google Inc. -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at 2 | the end). 3 | 4 | ### Before you contribute 5 | Before we can use your code, you must sign the 6 | [Google Individual Contributor License Agreement] 7 | (https://cla.developers.google.com/about/google-individual) 8 | (CLA), which you can do online. The CLA is necessary mainly because you own the 9 | copyright to your changes, even after your contribution becomes part of our 10 | codebase, so we need your permission to use and distribute your code. We also 11 | need to be sure of various other things—for instance that you'll tell us if you 12 | know that your code infringes on other people's patents. You don't have to sign 13 | the CLA until after you've submitted your code for review and a member has 14 | approved it, but you must do it before we can put your code into our codebase. 15 | Before you start working on a larger contribution, you should get in touch with 16 | us first through the issue tracker with your idea so that we can help out and 17 | possibly guide you. Coordinating up front makes it much easier to avoid 18 | frustration later on. 19 | 20 | ### Code reviews 21 | All submissions, including submissions by project members, require review. We 22 | use Github pull requests for this purpose. 23 | 24 | ### The small print 25 | Contributions made by corporations are covered by a different agreement than 26 | the one above, the 27 | [Software Grant and Corporate Contributor License Agreement] 28 | (https://cla.developers.google.com/about/google-corporate). 29 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # People who have agreed to one of the CLAs and can contribute patches. 2 | # The AUTHORS file lists the copyright holders; this file 3 | # lists people. For example, Google employees are listed here 4 | # but not in AUTHORS, because Google holds the copyright. 5 | # 6 | # Names should be added to this file as: 7 | # Name 8 | 9 | Iain McGinniss 10 | William Denniss 11 | Rahul Ravikumar 12 | Zachary Cardoza 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | plugin/README.md -------------------------------------------------------------------------------- /RELEASENOTES.md: -------------------------------------------------------------------------------- 1 | ### 0.2.1 2 | * Add support for `CUSTOM_TAB_COLOR_RGB` parameter to customize browser tab background (only Android supported at the moment) -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | platforms/ 2 | plugins/ 3 | -------------------------------------------------------------------------------- /demo/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | BrowserTabDemo 8 | 9 | A sample Cordova application that demonstrates the use of 10 | cordova-plugin-browsertab. 11 | 12 | Google, Inc. 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /demo/www/css/index.css: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | * { 20 | -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */ 21 | } 22 | 23 | html { 24 | background-color:#E4E4E4; 25 | } 26 | 27 | body { 28 | -webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */ 29 | -webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */ 30 | -webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */ 31 | font-family: sans-serif; 32 | font-size:12px; 33 | padding: 24px; 34 | } 35 | 36 | #app { 37 | display: -webkit-box; 38 | display: -webkit-flex; 39 | display: -ms-flexbox; 40 | display: flex; 41 | flex-direction: column; 42 | justify-content: flex-start; 43 | align-items: center; 44 | } 45 | 46 | #app h1 { 47 | font-size:24px; 48 | font-weight:normal; 49 | margin:16px; 50 | overflow:visible; 51 | padding:0px; 52 | text-align:center; 53 | } 54 | 55 | 56 | button { 57 | margin: 8px; 58 | background-color: #3F51B5; 59 | color: rgba(255, 255, 255, 0.87); 60 | padding: 8px 16px; 61 | border-radius: 2px; 62 | border: 1px solid #C5CAE9; 63 | width: 300px; 64 | } 65 | 66 | button:active { 67 | background-color: #303F9F; 68 | } 69 | -------------------------------------------------------------------------------- /demo/www/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 11 | Browser tab demo 12 | 13 | 14 |
15 |

Browser tab demo

16 |
Open www.google.com with:
17 | 18 | 19 | 20 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /demo/www/js/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 | * in compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 | * express or implied. See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | 15 | var app = { 16 | initialize: function() { 17 | this.bindEvents(); 18 | }, 19 | 20 | bindEvents: function() { 21 | document.addEventListener('deviceready', this.onDeviceReady, false); 22 | }, 23 | 24 | onDeviceReady: function() { 25 | var button = document.querySelector('#go'); 26 | var nobrowser = document.querySelector('#nobrowser'); 27 | var error = document.querySelector('#error'); 28 | 29 | var testURL = 'https://www.google.com'; 30 | 31 | document.querySelector('#browsertab').addEventListener('click', function(ev) { 32 | cordova.plugins.browsertab.isAvailable(function(result) { 33 | if (!result) { 34 | nobrowser.style.display = ''; 35 | } else { 36 | cordova.plugins.browsertab.openUrl( 37 | testURL, 38 | function(successResp) {}, 39 | function(failureResp) { 40 | error.textContent = 'failed to launch browser tab'; 41 | error.style.display = ''; 42 | }); 43 | // Uncomment to test the SVC close functionality. Normally this would 44 | // be done as the result of an incoming link. 45 | //setTimeout(function() { cordova.plugins.browsertab.close() }, 3000); 46 | } 47 | }, 48 | function(isAvailableError) { 49 | error.textContent = 'failed to query availability of in-app browser tab'; 50 | error.style.display = ''; 51 | }); 52 | }); 53 | 54 | document.querySelector('#browser').addEventListener('click', function(ev) { 55 | cordova.InAppBrowser.open(testURL, '_system'); 56 | }); 57 | 58 | document.querySelector('#tabwithfallback').addEventListener('click', function(ev) { 59 | cordova.plugins.browsertab.isAvailable(function(result) { 60 | if (!result) { 61 | cordova.InAppBrowser.open(testURL, '_system'); 62 | } else { 63 | cordova.plugins.browsertab.openUrl( 64 | testURL, 65 | function(successResp) {}, 66 | function(failureResp) { 67 | error.textContent = 'failed to launch browser tab'; 68 | error.style.display = ''; 69 | }); 70 | } 71 | }, 72 | function(isAvailableError) { 73 | error.textContent = 'failed to query availability of in-app browser tab'; 74 | error.style.display = ''; 75 | }); 76 | }); 77 | 78 | } 79 | }; 80 | 81 | app.initialize(); 82 | -------------------------------------------------------------------------------- /plugin/README.md: -------------------------------------------------------------------------------- 1 | # cordova-plugin-browsertab 2 | 3 | Note: This is not an official Google product. 4 | 5 | ## About 6 | 7 | This plugin provides an interface to in-app browser tabs that exist on some 8 | mobile platforms, specifically 9 | [Custom Tabs](http://developer.android.com/tools/support-library/features.html#custom-tabs) 10 | on Android (including the 11 | [Chrome Custom Tabs](https://developer.chrome.com/multidevice/android/customtabs) 12 | implementation), and 13 | [SFSafariViewController](https://developer.apple.com/library/ios/documentation/SafariServices/Reference/SFSafariViewController_Ref/) 14 | on iOS. 15 | 16 | ## Usage 17 | 18 | To open a URL in an in-app browser tab on a compatible platform: 19 | 20 | cordova.plugins.browsertab.openUrl('https://www.google.com'); 21 | 22 | This plugin is designed to complement cordova-plugin-inappbrowser. No fallback 23 | is triggered automatically, you need to test whether it will succeed, and then 24 | perform your own fallback logic like opening the link in the system browser 25 | instead using cordova-plugin-inappbrowser. 26 | 27 | cordova.InAppBrowser.open('https://www.google.com/', '_system'); 28 | 29 | Complete example with fallback handling: 30 | 31 | var testURL = 'https://www.google.com'; 32 | 33 | document.querySelector("#tabwithfallback").addEventListener('click', function(ev) { 34 | cordova.plugins.browsertab.isAvailable(function(result) { 35 | if (!result) { 36 | cordova.InAppBrowser.open(testURL, '_system'); 37 | } else { 38 | cordova.plugins.browsertab.openUrl( 39 | testURL, 40 | function(successResp) {}, 41 | function(failureResp) { 42 | error.textContent = "failed to launch browser tab"; 43 | error.style.display = ''; 44 | }); 45 | } 46 | }, 47 | function(isAvailableError) { 48 | error.textContent = "failed to query availability of in-app browser tab"; 49 | error.style.display = ''; 50 | }); 51 | }); 52 | 53 | ## Customization 54 | 55 | Plugin can be customized by providing parameters during installation. 56 | You can customize i.e. custom tab background color by passing `CUSTOM_TAB_COLOR_RGB` variable in string RGB format: 57 | 58 | ```bash 59 | cordova plugin add cordova-plugin-browsertab --variable CUSTOM_TAB_COLOR_RGB="#ff0000" 60 | ``` 61 | 62 | List of available parameters: 63 | 64 | * **CUSTOM_TAB_COLOR_RGB** - Customize custom tab background color. Pass value as a RGB string `#RRGGBB`. 65 | Supported by Android only at the moment. 66 | 67 | ## Building 68 | 69 | Install Cordova if you haven't already: 70 | 71 | npm install -g cordova 72 | 73 | Then from the root directory: 74 | 75 | cd demo 76 | cordova platform add ios 77 | cordova run ios 78 | cordova platform add android 79 | cordova run android 80 | 81 | ## Development 82 | 83 | During development if you want to make changes to the plugin you need to force 84 | a rebuild and add the plugin from source, like so (from the demo directory): 85 | 86 | cordova plugin remove cordova-plugin-browsertab 87 | cordova plugin add ../plugin 88 | 89 | To refresh the platform build: 90 | 91 | cordova platform remove ios 92 | cordova platform add ios 93 | 94 | To set breakpoints, etc, open the project in the code editor: 95 | 96 | cordova platform add ios 97 | open platforms/ios/BrowserTabDemo.xcworkspace 98 | -------------------------------------------------------------------------------- /plugin/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-browsertab", 3 | "version": "0.2.1", 4 | "description": "In-app browser tabs for Android and iOS", 5 | "cordova": { 6 | "id": "cordova-plugin-browsertab", 7 | "platforms": [ 8 | "android" 9 | ] 10 | }, 11 | "keywords": [ 12 | "sfsafariviewcontroller", 13 | "custom-tabs", 14 | "browser-tabs", 15 | "browser" 16 | ], 17 | "author": "Google, Inc.", 18 | "license": "Apache-2.0" 19 | } 20 | -------------------------------------------------------------------------------- /plugin/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | cordova-plugin-browsertab 8 | 9 | This plugin provides an interface to in-app browser tabs that exist on 10 | some mobile platforms, specifically Custom Tabs on Android and 11 | SFSafariViewController on iOS. 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | $CUSTOM_TAB_COLOR_RGB 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /plugin/src/android/BrowserTab.gradle: -------------------------------------------------------------------------------- 1 | def minSdkVersion = 16 2 | 3 | if(cdvMinSdkVersion == null) { 4 | ext.cdvMinSdkVersion = minSdkVersion; 5 | } else if (cdvMinSdkVersion.toInteger() < minSdkVersion) { 6 | ext.cdvMinSdkVersion = minSdkVersion; 7 | } -------------------------------------------------------------------------------- /plugin/src/android/BrowserTab.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 | * in compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 | * express or implied. See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | 15 | package com.google.cordova.plugin.browsertab; 16 | 17 | import android.content.Intent; 18 | import android.content.pm.PackageManager; 19 | import android.content.pm.ResolveInfo; 20 | import android.net.Uri; 21 | import android.support.customtabs.CustomTabsIntent; 22 | import android.util.Log; 23 | 24 | import java.util.Iterator; 25 | import java.util.List; 26 | 27 | import org.apache.cordova.CallbackContext; 28 | import org.apache.cordova.CordovaInterface; 29 | import org.apache.cordova.CordovaPlugin; 30 | import org.apache.cordova.CordovaWebView; 31 | import org.apache.cordova.PluginResult; 32 | 33 | import org.json.JSONArray; 34 | import org.json.JSONException; 35 | import org.json.JSONObject; 36 | 37 | import android.graphics.Color; 38 | 39 | /** 40 | * Cordova plugin which provides the ability to launch a URL in an 41 | * in-app browser tab. On Android, this means using the custom tabs support 42 | * library, if a supporting browser (e.g. Chrome) is available on the device. 43 | */ 44 | public class BrowserTab extends CordovaPlugin { 45 | 46 | public static final int RC_OPEN_URL = 101; 47 | 48 | private static final String LOG_TAG = "BrowserTab"; 49 | 50 | private Color colorParser = new Color(); 51 | 52 | /** 53 | * The service we expect to find on a web browser that indicates it supports custom tabs. 54 | */ 55 | private static final String ACTION_CUSTOM_TABS_CONNECTION = 56 | "android.support.customtabs.action.CustomTabsService"; 57 | 58 | private boolean mFindCalled = false; 59 | private String mCustomTabsBrowser; 60 | 61 | @Override 62 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { 63 | Log.d(LOG_TAG, "executing " + action); 64 | if ("isAvailable".equals(action)) { 65 | isAvailable(callbackContext); 66 | } else if ("openUrl".equals(action)) { 67 | openUrl(args, callbackContext); 68 | } else if ("close".equals(action)) { 69 | // close is a NOP on Android 70 | return true; 71 | } else { 72 | return false; 73 | } 74 | 75 | return true; 76 | } 77 | 78 | private void isAvailable(CallbackContext callbackContext) { 79 | String browserPackage = findCustomTabBrowser(); 80 | Log.d(LOG_TAG, "browser package: " + browserPackage); 81 | callbackContext.sendPluginResult(new PluginResult( 82 | PluginResult.Status.OK, 83 | browserPackage != null)); 84 | } 85 | 86 | private void openUrl(JSONArray args, CallbackContext callbackContext) { 87 | if (args.length() < 1) { 88 | Log.d(LOG_TAG, "openUrl: no url argument received"); 89 | callbackContext.error("URL argument missing"); 90 | return; 91 | } 92 | 93 | String urlStr; 94 | try { 95 | urlStr = args.getString(0); 96 | } catch (JSONException e) { 97 | Log.d(LOG_TAG, "openUrl: failed to parse url argument"); 98 | callbackContext.error("URL argument is not a string"); 99 | return; 100 | } 101 | 102 | String customTabsBrowser = findCustomTabBrowser(); 103 | if (customTabsBrowser == null) { 104 | Log.d(LOG_TAG, "openUrl: no in app browser tab available"); 105 | callbackContext.error("no in app browser tab implementation available"); 106 | } 107 | 108 | // Initialize Builder 109 | CustomTabsIntent.Builder customTabsIntentBuilder = new CustomTabsIntent.Builder(); 110 | 111 | // Set tab color 112 | String tabColor = cordova.getActivity().getString(cordova.getActivity().getResources().getIdentifier("CUSTOM_TAB_COLOR_RGB", "string", cordova.getActivity().getPackageName())); 113 | customTabsIntentBuilder.setToolbarColor(colorParser.parseColor(tabColor)); 114 | 115 | // Create Intent 116 | CustomTabsIntent customTabsIntent = customTabsIntentBuilder.build(); 117 | 118 | // Load URL 119 | customTabsIntent.launchUrl(cordova.getActivity(), Uri.parse(urlStr)); 120 | 121 | Log.d(LOG_TAG, "in app browser call dispatched"); 122 | callbackContext.success(); 123 | } 124 | 125 | private String findCustomTabBrowser() { 126 | if (mFindCalled) { 127 | return mCustomTabsBrowser; 128 | } 129 | 130 | PackageManager pm = cordova.getActivity().getPackageManager(); 131 | Intent webIntent = new Intent( 132 | Intent.ACTION_VIEW, 133 | Uri.parse("http://www.example.com")); 134 | List resolvedActivityList = 135 | pm.queryIntentActivities(webIntent, PackageManager.GET_RESOLVED_FILTER); 136 | 137 | for (ResolveInfo info : resolvedActivityList) { 138 | if (!isFullBrowser(info)) { 139 | continue; 140 | } 141 | 142 | if (hasCustomTabWarmupService(pm, info.activityInfo.packageName)) { 143 | mCustomTabsBrowser = info.activityInfo.packageName; 144 | break; 145 | } 146 | } 147 | 148 | mFindCalled = true; 149 | return mCustomTabsBrowser; 150 | } 151 | 152 | private boolean isFullBrowser(ResolveInfo resolveInfo) { 153 | // The filter must match ACTION_VIEW, CATEGORY_BROWSEABLE, and at least one scheme, 154 | if (!resolveInfo.filter.hasAction(Intent.ACTION_VIEW) 155 | || !resolveInfo.filter.hasCategory(Intent.CATEGORY_BROWSABLE) 156 | || resolveInfo.filter.schemesIterator() == null) { 157 | return false; 158 | } 159 | 160 | // The filter must not be restricted to any particular set of authorities 161 | if (resolveInfo.filter.authoritiesIterator() != null) { 162 | return false; 163 | } 164 | 165 | // The filter must support both HTTP and HTTPS. 166 | boolean supportsHttp = false; 167 | boolean supportsHttps = false; 168 | Iterator schemeIter = resolveInfo.filter.schemesIterator(); 169 | while (schemeIter.hasNext()) { 170 | String scheme = schemeIter.next(); 171 | supportsHttp |= "http".equals(scheme); 172 | supportsHttps |= "https".equals(scheme); 173 | 174 | if (supportsHttp && supportsHttps) { 175 | return true; 176 | } 177 | } 178 | 179 | // at least one of HTTP or HTTPS is not supported 180 | return false; 181 | } 182 | 183 | private boolean hasCustomTabWarmupService(PackageManager pm, String packageName) { 184 | Intent serviceIntent = new Intent(); 185 | serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION); 186 | serviceIntent.setPackage(packageName); 187 | return (pm.resolveService(serviceIntent, 0) != null); 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /plugin/src/android/res/browsertab.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /plugin/src/ios/CBTBrowserTab.h: -------------------------------------------------------------------------------- 1 | /*! @file CBTBrowserTab.h 2 | @brief Browser tab plugin for Cordova 3 | @copyright 4 | Copyright 2016 Google Inc. All Rights Reserved. 5 | @copydetails 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | #import 19 | 20 | @interface CBTBrowserTab : CDVPlugin 21 | 22 | - (void)isAvailable:(CDVInvokedUrlCommand *)command; 23 | - (void)openUrl:(CDVInvokedUrlCommand *)command; 24 | - (void)close:(CDVInvokedUrlCommand *)command; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /plugin/src/ios/CBTBrowserTab.m: -------------------------------------------------------------------------------- 1 | /*! @file CBTBrowserTab.m 2 | @brief Browser tab plugin for Cordova 3 | @copyright 4 | Copyright 2016 Google Inc. All Rights Reserved. 5 | @copydetails 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | http://www.apache.org/licenses/LICENSE-2.0 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import "CBTBrowserTab.h" 18 | 19 | @implementation CBTBrowserTab { 20 | SFSafariViewController *_safariViewController; 21 | } 22 | 23 | - (void)isAvailable:(CDVInvokedUrlCommand *)command { 24 | BOOL available = ([SFSafariViewController class] != nil); 25 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK 26 | messageAsBool:available]; 27 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 28 | } 29 | 30 | - (void)openUrl:(CDVInvokedUrlCommand *)command { 31 | NSString *urlString = command.arguments[0]; 32 | if (urlString == nil) { 33 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR 34 | messageAsString:@"url can't be empty"]; 35 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 36 | return; 37 | } 38 | 39 | NSURL *url = [NSURL URLWithString:urlString]; 40 | if ([SFSafariViewController class] != nil) { 41 | NSString *errorMessage = @"in app browser tab not available"; 42 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR 43 | messageAsString:errorMessage]; 44 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 45 | } 46 | 47 | _safariViewController = [[SFSafariViewController alloc] initWithURL:url]; 48 | [self.viewController presentViewController:_safariViewController animated:YES completion:nil]; 49 | 50 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; 51 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 52 | } 53 | 54 | - (void)close:(CDVInvokedUrlCommand *)command { 55 | if (!_safariViewController) { 56 | return; 57 | } 58 | [_safariViewController dismissViewControllerAnimated:YES completion:nil]; 59 | _safariViewController = nil; 60 | } 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /plugin/www/browsertab.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 | * in compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 | * express or implied. See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | 15 | var exec = require('cordova/exec'); 16 | 17 | exports.isAvailable = function(success, error) { 18 | exec(success, error, 'BrowserTab', 'isAvailable', []); 19 | }; 20 | 21 | exports.openUrl = function(url, opt_error) { 22 | var doNothing = function() {}; 23 | var error = (!opt_error) ? doNothing : opt_error; 24 | exec(doNothing, error, 'BrowserTab', 'openUrl', [url]); 25 | }; 26 | 27 | exports.close = function(opt_error) { 28 | var doNothing = function() {}; 29 | var error = (!opt_error) ? doNothing : opt_error; 30 | exec(doNothing, error, 'BrowserTab', 'close', []); 31 | }; 32 | --------------------------------------------------------------------------------