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 | |
| 17 | package com.android.browser; |
| 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; |
| 27 | import android.webkit.WebView; |
| 28 | |
| 29 | import java.net.URISyntaxException; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 30 | import java.util.List; |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 31 | import java.util.regex.Matcher; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * |
| 35 | */ |
| 36 | public class UrlHandler { |
| 37 | |
| 38 | // Use in overrideUrlLoading |
| 39 | /* package */ final static String SCHEME_WTAI = "wtai://wp/"; |
| 40 | /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;"; |
| 41 | /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;"; |
| 42 | /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;"; |
| 43 | |
| 44 | Controller mController; |
| 45 | Activity mActivity; |
| 46 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 47 | public UrlHandler(Controller controller) { |
| 48 | mController = controller; |
| 49 | mActivity = mController.getActivity(); |
| 50 | } |
| 51 | |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 52 | boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 53 | if (view.isPrivateBrowsingEnabled()) { |
| 54 | // Don't allow urls to leave the browser app when in |
| 55 | // private browsing mode |
Patrick Scott | d05faa6 | 2010-12-16 09:15:34 -0500 | [diff] [blame] | 56 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (url.startsWith(SCHEME_WTAI)) { |
| 60 | // wtai://wp/mc;number |
| 61 | // number=string(phone-number) |
| 62 | if (url.startsWith(SCHEME_WTAI_MC)) { |
| 63 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 64 | Uri.parse(WebView.SCHEME_TEL + |
| 65 | url.substring(SCHEME_WTAI_MC.length()))); |
| 66 | mActivity.startActivity(intent); |
| 67 | // before leaving BrowserActivity, close the empty child tab. |
| 68 | // If a new tab is created through JavaScript open to load this |
| 69 | // url, we would like to close it as we will load this url in a |
| 70 | // different Activity. |
| 71 | mController.closeEmptyChildTab(); |
| 72 | return true; |
| 73 | } |
| 74 | // wtai://wp/sd;dtmf |
| 75 | // dtmf=string(dialstring) |
| 76 | if (url.startsWith(SCHEME_WTAI_SD)) { |
| 77 | // TODO: only send when there is active voice connection |
| 78 | return false; |
| 79 | } |
| 80 | // wtai://wp/ap;number;name |
| 81 | // number=string(phone-number) |
| 82 | // name=string |
| 83 | if (url.startsWith(SCHEME_WTAI_AP)) { |
| 84 | // TODO |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // The "about:" schemes are internal to the browser; don't want these to |
| 90 | // be dispatched to other apps. |
| 91 | if (url.startsWith("about:")) { |
| 92 | return false; |
| 93 | } |
| 94 | |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 95 | if (startActivityForUrl(url)) { |
| 96 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 99 | if (handleMenuClick(tab, url)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 100 | return true; |
| 101 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 102 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 106 | boolean startActivityForUrl(String url) { |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 107 | Intent intent; |
| 108 | // perform generic parsing of the URI to turn it into an Intent. |
| 109 | try { |
| 110 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 111 | } catch (URISyntaxException ex) { |
| 112 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // check whether the intent can be resolved. If not, we will see |
| 117 | // whether we can download it from the Market. |
| 118 | if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) { |
| 119 | String packagename = intent.getPackage(); |
| 120 | if (packagename != null) { |
| 121 | intent = new Intent(Intent.ACTION_VIEW, Uri |
| 122 | .parse("market://search?q=pname:" + packagename)); |
| 123 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 124 | mActivity.startActivity(intent); |
| 125 | // before leaving BrowserActivity, close the empty child tab. |
| 126 | // If a new tab is created through JavaScript open to load this |
| 127 | // url, we would like to close it as we will load this url in a |
| 128 | // different Activity. |
| 129 | mController.closeEmptyChildTab(); |
| 130 | return true; |
| 131 | } else { |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // sanitize the Intent, ensuring web pages can not bypass browser |
| 137 | // security (only access to BROWSABLE activities). |
| 138 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 139 | intent.setComponent(null); |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 140 | // Make sure webkit can handle it internally before checking for specialized |
| 141 | // handlers. If webkit can't handle it internally, we need to call |
| 142 | // startActivityIfNeeded |
| 143 | Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url); |
| 144 | if (m.matches() && !isSpecializedHandlerAvailable(intent)) { |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 145 | return false; |
| 146 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 147 | try { |
| 148 | if (mActivity.startActivityIfNeeded(intent, -1)) { |
| 149 | // before leaving BrowserActivity, close the empty child tab. |
| 150 | // If a new tab is created through JavaScript open to load this |
| 151 | // url, we would like to close it as we will load this url in a |
| 152 | // different Activity. |
| 153 | mController.closeEmptyChildTab(); |
| 154 | return true; |
| 155 | } |
| 156 | } catch (ActivityNotFoundException ex) { |
| 157 | // ignore the error. If no application can handle the URL, |
| 158 | // eg about:blank, assume the browser can handle it. |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Search for intent handlers that are specific to this URL |
| 166 | * aka, specialized apps like google maps or youtube |
| 167 | */ |
| 168 | private boolean isSpecializedHandlerAvailable(Intent intent) { |
| 169 | PackageManager pm = mActivity.getPackageManager(); |
| 170 | List<ResolveInfo> handlers = pm.queryIntentActivities(intent, |
| 171 | PackageManager.GET_RESOLVED_FILTER); |
| 172 | if (handlers == null || handlers.size() == 0) { |
| 173 | return false; |
| 174 | } |
| 175 | for (ResolveInfo resolveInfo : handlers) { |
| 176 | IntentFilter filter = resolveInfo.filter; |
| 177 | if (filter == null) { |
| 178 | // No intent filter matches this intent? |
| 179 | // Error on the side of staying in the browser, ignore |
| 180 | continue; |
| 181 | } |
| 182 | if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) { |
| 183 | // Generic handler, skip |
| 184 | continue; |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 191 | // In case a physical keyboard is attached, handle clicks with the menu key |
| 192 | // depressed by opening in a new tab |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 193 | boolean handleMenuClick(Tab tab, String url) { |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 194 | if (mController.isMenuDown()) { |
| 195 | mController.openTab(tab, url, false); |
| 196 | mActivity.closeOptionsMenu(); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | return false; |
| 201 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 202 | } |