Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.ActivityNotFoundException; |
| 21 | import android.content.Intent; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 22 | import android.content.IntentFilter; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 24 | import android.content.pm.ResolveInfo; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.util.Log; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 27 | import android.widget.Toast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 28 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 29 | import com.android.browser.R; |
Bijan Amirzada | 3f04dc7 | 2014-06-25 11:48:36 -0700 | [diff] [blame] | 30 | import com.android.browser.platformsupport.Browser; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 31 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 32 | import java.net.URISyntaxException; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 33 | import java.util.List; |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 34 | import java.util.regex.Matcher; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 35 | |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 36 | import org.codeaurora.swe.WebView; |
| 37 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 38 | public class UrlHandler { |
| 39 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 40 | private final static String TAG = "UrlHandler"; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 41 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 42 | // Use in overrideUrlLoading |
| 43 | /* package */ final static String SCHEME_WTAI = "wtai://wp/"; |
| 44 | /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;"; |
| 45 | /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;"; |
| 46 | /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;"; |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 47 | /* package */ final static String SCHEME_MAILTO = "mailto:"; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 48 | Controller mController; |
| 49 | Activity mActivity; |
| 50 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 51 | public UrlHandler(Controller controller) { |
| 52 | mController = controller; |
| 53 | mActivity = mController.getActivity(); |
| 54 | } |
| 55 | |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 56 | boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 57 | if (view.isPrivateBrowsingEnabled()) { |
| 58 | // Don't allow urls to leave the browser app when in |
| 59 | // private browsing mode |
Patrick Scott | d05faa6 | 2010-12-16 09:15:34 -0500 | [diff] [blame] | 60 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (url.startsWith(SCHEME_WTAI)) { |
| 64 | // wtai://wp/mc;number |
| 65 | // number=string(phone-number) |
| 66 | if (url.startsWith(SCHEME_WTAI_MC)) { |
| 67 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 68 | Uri.parse(WebView.SCHEME_TEL + |
| 69 | url.substring(SCHEME_WTAI_MC.length()))); |
| 70 | mActivity.startActivity(intent); |
| 71 | // before leaving BrowserActivity, close the empty child tab. |
| 72 | // If a new tab is created through JavaScript open to load this |
| 73 | // url, we would like to close it as we will load this url in a |
| 74 | // different Activity. |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 75 | mController.closeEmptyTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | // wtai://wp/sd;dtmf |
| 79 | // dtmf=string(dialstring) |
| 80 | if (url.startsWith(SCHEME_WTAI_SD)) { |
| 81 | // TODO: only send when there is active voice connection |
| 82 | return false; |
| 83 | } |
| 84 | // wtai://wp/ap;number;name |
| 85 | // number=string(phone-number) |
| 86 | // name=string |
| 87 | if (url.startsWith(SCHEME_WTAI_AP)) { |
| 88 | // TODO |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // The "about:" schemes are internal to the browser; don't want these to |
| 94 | // be dispatched to other apps. |
| 95 | if (url.startsWith("about:")) { |
| 96 | return false; |
| 97 | } |
| 98 | |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame] | 99 | if (url.startsWith("ae://") && url.endsWith("add-fav")) { |
| 100 | mController.startAddMyNavigation(url); |
| 101 | return true; |
| 102 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 103 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 104 | // add for carrier feature - recognize additional website format |
| 105 | // here add to support "mailto:" scheme |
| 106 | if (url.startsWith(SCHEME_MAILTO) && handleMailtoTypeUrl(url)) { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // add for carrier feature - wap2estore |
Panos Thomas | 4bdb525 | 2014-11-13 16:20:11 -0800 | [diff] [blame] | 111 | boolean wap2estore = BrowserConfig.getInstance(mController.getContext()) |
| 112 | .hasFeature(BrowserConfig.Feature.WAP2ESTORE); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 113 | if (wap2estore && isEstoreTypeUrl(url) && handleEstoreTypeUrl(url)) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 117 | if (startActivityForUrl(tab, url)) { |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 118 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 121 | if (handleMenuClick(tab, url)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 122 | return true; |
| 123 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 124 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 128 | private boolean handleMailtoTypeUrl(String url) { |
| 129 | Intent intent; |
| 130 | // perform generic parsing of the URI to turn it into an Intent. |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 131 | try { |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 132 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 133 | mActivity.startActivity(intent); |
| 134 | } catch (URISyntaxException ex) { |
| 135 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 136 | return false; |
| 137 | } catch (ActivityNotFoundException ex) { |
| 138 | Log.w("Browser", "No Activity Found for " + url); |
| 139 | return false; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 140 | } |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | private boolean isEstoreTypeUrl(String url) { |
| 146 | if (url != null && url.startsWith("estore:")) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 152 | private boolean handleEstoreTypeUrl(String url) { |
| 153 | if (url.getBytes().length > 256) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 154 | Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show(); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 155 | return false; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 156 | } |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 157 | |
| 158 | Intent intent; |
| 159 | // perform generic parsing of the URI to turn it into an Intent. |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 160 | try { |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 161 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 162 | mActivity.startActivity(intent); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 163 | } catch (URISyntaxException ex) { |
| 164 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 165 | return false; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 166 | } catch (ActivityNotFoundException ex) { |
| 167 | String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage); |
| 168 | mController.loadUrl(mController.getCurrentTab(), downloadUrl); |
| 169 | Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show(); |
| 170 | } |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 171 | |
| 172 | return true; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 175 | |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 176 | boolean startActivityForUrl(Tab tab, String url) { |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 177 | Intent intent; |
| 178 | // perform generic parsing of the URI to turn it into an Intent. |
| 179 | try { |
| 180 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 181 | } catch (URISyntaxException ex) { |
| 182 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // check whether the intent can be resolved. If not, we will see |
| 187 | // whether we can download it from the Market. |
| 188 | if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) { |
| 189 | String packagename = intent.getPackage(); |
| 190 | if (packagename != null) { |
Vivek Sekhar | 4071338 | 2014-06-11 14:29:32 -0700 | [diff] [blame] | 191 | try { |
| 192 | intent = new Intent(Intent.ACTION_VIEW, Uri |
| 193 | .parse("market://search?q=pname:" + packagename)); |
| 194 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 195 | mActivity.startActivity(intent); |
| 196 | // before leaving BrowserActivity, close the empty child tab. |
| 197 | // If a new tab is created through JavaScript open to load this |
| 198 | // url, we would like to close it as we will load this url in a |
| 199 | // different Activity. |
| 200 | mController.closeEmptyTab(); |
| 201 | return true; |
| 202 | } catch (ActivityNotFoundException e) { |
| 203 | Log.w(TAG, "Play store not found while searching for : " + packagename); |
| 204 | CharSequence alert = mActivity.getResources().getString( |
| 205 | R.string.msg_no_google_play); |
| 206 | Toast t = Toast.makeText(mActivity , alert, Toast.LENGTH_SHORT); |
| 207 | t.show(); |
| 208 | return false; |
| 209 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 210 | } else { |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // sanitize the Intent, ensuring web pages can not bypass browser |
| 216 | // security (only access to BROWSABLE activities). |
| 217 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 218 | intent.setComponent(null); |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 219 | // Re-use the existing tab if the intent comes back to us |
| 220 | if (tab != null) { |
| 221 | if (tab.getAppId() == null) { |
Michael Kolb | f1286a4 | 2012-04-17 14:10:52 -0700 | [diff] [blame] | 222 | tab.setAppId(mActivity.getPackageName() + "-" + tab.getId()); |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 223 | } |
| 224 | intent.putExtra(Browser.EXTRA_APPLICATION_ID, tab.getAppId()); |
| 225 | } |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 226 | // Make sure webkit can handle it internally before checking for specialized |
| 227 | // handlers. If webkit can't handle it internally, we need to call |
| 228 | // startActivityIfNeeded |
| 229 | Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url); |
| 230 | if (m.matches() && !isSpecializedHandlerAvailable(intent)) { |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 233 | try { |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 234 | intent.putExtra(BrowserActivity.EXTRA_DISABLE_URL_OVERRIDE, true); |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 235 | if (mActivity.startActivityIfNeeded(intent, -1)) { |
| 236 | // before leaving BrowserActivity, close the empty child tab. |
| 237 | // If a new tab is created through JavaScript open to load this |
| 238 | // url, we would like to close it as we will load this url in a |
| 239 | // different Activity. |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 240 | mController.closeEmptyTab(); |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | } catch (ActivityNotFoundException ex) { |
| 244 | // ignore the error. If no application can handle the URL, |
| 245 | // eg about:blank, assume the browser can handle it. |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 251 | /** |
| 252 | * Search for intent handlers that are specific to this URL |
| 253 | * aka, specialized apps like google maps or youtube |
| 254 | */ |
| 255 | private boolean isSpecializedHandlerAvailable(Intent intent) { |
| 256 | PackageManager pm = mActivity.getPackageManager(); |
| 257 | List<ResolveInfo> handlers = pm.queryIntentActivities(intent, |
| 258 | PackageManager.GET_RESOLVED_FILTER); |
| 259 | if (handlers == null || handlers.size() == 0) { |
| 260 | return false; |
| 261 | } |
| 262 | for (ResolveInfo resolveInfo : handlers) { |
| 263 | IntentFilter filter = resolveInfo.filter; |
| 264 | if (filter == null) { |
| 265 | // No intent filter matches this intent? |
| 266 | // Error on the side of staying in the browser, ignore |
| 267 | continue; |
| 268 | } |
John Reck | 1e6a287 | 2011-12-15 14:35:02 -0800 | [diff] [blame] | 269 | if (filter.countDataAuthorities() == 0 && filter.countDataPaths() == 0) { |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 270 | // Generic handler, skip |
| 271 | continue; |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 278 | // In case a physical keyboard is attached, handle clicks with the menu key |
| 279 | // depressed by opening in a new tab |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 280 | boolean handleMenuClick(Tab tab, String url) { |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 281 | if (mController.isMenuDown()) { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 282 | mController.openTab(url, |
| 283 | (tab != null) && tab.isPrivateBrowsingEnabled(), |
| 284 | !BrowserSettings.getInstance().openInBackground(), true); |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 285 | mActivity.closeOptionsMenu(); |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 292 | } |