blob: 37e70f8615a2372bee2031ab64a3b71ed00557de [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
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
17package com.android.browser;
18
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
John Reck95a49ff2011-02-08 18:23:22 -080022import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.pm.PackageManager;
John Reck95a49ff2011-02-08 18:23:22 -080024import android.content.pm.ResolveInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.database.Cursor;
26import android.net.Uri;
Ben Murdocha941f6e2010-12-07 16:09:16 +000027import android.os.AsyncTask;
kaiyizc4ada322013-07-30 09:58:07 +080028import android.os.SystemProperties;
John Reck8bcafc12011-08-29 16:43:02 -070029import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.util.Log;
31import android.webkit.WebView;
kaiyizc4ada322013-07-30 09:58:07 +080032import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070033
kaiyizc4ada322013-07-30 09:58:07 +080034import java.io.UnsupportedEncodingException;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import java.net.URISyntaxException;
John Reck95a49ff2011-02-08 18:23:22 -080036import java.util.List;
John Reckdb3d43d2011-02-11 11:56:38 -080037import java.util.regex.Matcher;
Michael Kolb8233fac2010-10-26 16:08:53 -070038
39/**
40 *
41 */
42public class UrlHandler {
43
kaiyizc4ada322013-07-30 09:58:07 +080044 private final static String TAG = "UrlHandler";
John Reck35e9dd62011-04-25 09:01:54 -070045 static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
46 static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
47
Michael Kolb8233fac2010-10-26 16:08:53 -070048 // Use in overrideUrlLoading
49 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
50 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
51 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
52 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
53
54 Controller mController;
55 Activity mActivity;
56
57 private Boolean mIsProviderPresent = null;
58 private Uri mRlzUri = null;
59
60 public UrlHandler(Controller controller) {
61 mController = controller;
62 mActivity = mController.getActivity();
63 }
64
Michael Kolb18eb3772010-12-10 14:29:51 -080065 boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -070066 if (view.isPrivateBrowsingEnabled()) {
67 // Don't allow urls to leave the browser app when in
68 // private browsing mode
Patrick Scottd05faa62010-12-16 09:15:34 -050069 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -070070 }
71
72 if (url.startsWith(SCHEME_WTAI)) {
73 // wtai://wp/mc;number
74 // number=string(phone-number)
75 if (url.startsWith(SCHEME_WTAI_MC)) {
76 Intent intent = new Intent(Intent.ACTION_VIEW,
77 Uri.parse(WebView.SCHEME_TEL +
78 url.substring(SCHEME_WTAI_MC.length())));
79 mActivity.startActivity(intent);
80 // before leaving BrowserActivity, close the empty child tab.
81 // If a new tab is created through JavaScript open to load this
82 // url, we would like to close it as we will load this url in a
83 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -070084 mController.closeEmptyTab();
Michael Kolb8233fac2010-10-26 16:08:53 -070085 return true;
86 }
87 // wtai://wp/sd;dtmf
88 // dtmf=string(dialstring)
89 if (url.startsWith(SCHEME_WTAI_SD)) {
90 // TODO: only send when there is active voice connection
91 return false;
92 }
93 // wtai://wp/ap;number;name
94 // number=string(phone-number)
95 // name=string
96 if (url.startsWith(SCHEME_WTAI_AP)) {
97 // TODO
98 return false;
99 }
100 }
101
102 // The "about:" schemes are internal to the browser; don't want these to
103 // be dispatched to other apps.
104 if (url.startsWith("about:")) {
105 return false;
106 }
107
108 // If this is a Google search, attempt to add an RLZ string
109 // (if one isn't already present).
110 if (rlzProviderPresent()) {
111 Uri siteUri = Uri.parse(url);
112 if (needsRlzString(siteUri)) {
Ben Murdocha941f6e2010-12-07 16:09:16 +0000113 // Need to look up the RLZ info from a database, so do it in an
114 // AsyncTask. Although we are not overriding the URL load synchronously,
115 // we guarantee that we will handle this URL load after the task executes,
116 // so it's safe to just return true to WebCore now to stop its own loading.
Russell Brenner14e1ae22011-01-12 14:54:23 -0800117 new RLZTask(tab, siteUri, view).execute();
Michael Kolb8233fac2010-10-26 16:08:53 -0700118 return true;
119 }
120 }
121
kaiyizc4ada322013-07-30 09:58:07 +0800122 // add for carrier wap2estore feature
123 boolean wap2estore = SystemProperties.getBoolean(
124 "persist.env.browser.wap2estore", false);
125 if (wap2estore && isEstoreTypeUrl(url)) {
126 handleEstoreTypeUrl(url);
127 return true;
128 }
129
John Reck8bcafc12011-08-29 16:43:02 -0700130 if (startActivityForUrl(tab, url)) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800131 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 }
133
Russell Brenner14e1ae22011-01-12 14:54:23 -0800134 if (handleMenuClick(tab, url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 return true;
136 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800137
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 return false;
139 }
140
kaiyizc4ada322013-07-30 09:58:07 +0800141 private boolean isEstoreTypeUrl(String url) {
142 String utf8Url = null;
143 try {
144 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
145 } catch (UnsupportedEncodingException e) {
146 Log.e(TAG, "err " + e);
147 }
148 if (utf8Url != null && utf8Url.startsWith("estore:")) {
149 return true;
150 }
151 return false;
152 }
153
154 private void handleEstoreTypeUrl(String url) {
155 String utf8Url = null, finalUrl = null;
156 try {
157 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
158 } catch (UnsupportedEncodingException e) {
159 Log.e(TAG, "err " + e);
160 }
161 if (utf8Url != null) {
162 finalUrl = utf8Url;
163 } else {
164 finalUrl = url;
165 }
166 if (finalUrl.replaceFirst("estore:", "").length() > 256) {
167 Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show();
168 return;
169 }
170 Intent intent = new Intent(Intent.ACTION_VIEW);
171 intent.setData(Uri.parse(finalUrl));
172 try {
173 mActivity.startActivity(intent);
174 } catch (ActivityNotFoundException ex) {
175 String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage);
176 mController.loadUrl(mController.getCurrentTab(), downloadUrl);
177 Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show();
178 }
179 }
180
John Reck8bcafc12011-08-29 16:43:02 -0700181 boolean startActivityForUrl(Tab tab, String url) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800182 Intent intent;
183 // perform generic parsing of the URI to turn it into an Intent.
184 try {
185 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
186 } catch (URISyntaxException ex) {
187 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
188 return false;
189 }
190
191 // check whether the intent can be resolved. If not, we will see
192 // whether we can download it from the Market.
193 if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) {
194 String packagename = intent.getPackage();
195 if (packagename != null) {
196 intent = new Intent(Intent.ACTION_VIEW, Uri
197 .parse("market://search?q=pname:" + packagename));
198 intent.addCategory(Intent.CATEGORY_BROWSABLE);
199 mActivity.startActivity(intent);
200 // before leaving BrowserActivity, close the empty child tab.
201 // If a new tab is created through JavaScript open to load this
202 // url, we would like to close it as we will load this url in a
203 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -0700204 mController.closeEmptyTab();
Russell Brennerd4afde12011-01-07 11:09:36 -0800205 return true;
206 } else {
207 return false;
208 }
209 }
210
211 // sanitize the Intent, ensuring web pages can not bypass browser
212 // security (only access to BROWSABLE activities).
213 intent.addCategory(Intent.CATEGORY_BROWSABLE);
214 intent.setComponent(null);
John Reck8bcafc12011-08-29 16:43:02 -0700215 // Re-use the existing tab if the intent comes back to us
216 if (tab != null) {
217 if (tab.getAppId() == null) {
Michael Kolbf1286a42012-04-17 14:10:52 -0700218 tab.setAppId(mActivity.getPackageName() + "-" + tab.getId());
John Reck8bcafc12011-08-29 16:43:02 -0700219 }
220 intent.putExtra(Browser.EXTRA_APPLICATION_ID, tab.getAppId());
221 }
John Reckdb3d43d2011-02-11 11:56:38 -0800222 // Make sure webkit can handle it internally before checking for specialized
223 // handlers. If webkit can't handle it internally, we need to call
224 // startActivityIfNeeded
225 Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url);
226 if (m.matches() && !isSpecializedHandlerAvailable(intent)) {
John Reck95a49ff2011-02-08 18:23:22 -0800227 return false;
228 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800229 try {
John Reck38b39652012-06-05 09:22:59 -0700230 intent.putExtra(BrowserActivity.EXTRA_DISABLE_URL_OVERRIDE, true);
Russell Brennerd4afde12011-01-07 11:09:36 -0800231 if (mActivity.startActivityIfNeeded(intent, -1)) {
232 // before leaving BrowserActivity, close the empty child tab.
233 // If a new tab is created through JavaScript open to load this
234 // url, we would like to close it as we will load this url in a
235 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -0700236 mController.closeEmptyTab();
Russell Brennerd4afde12011-01-07 11:09:36 -0800237 return true;
238 }
239 } catch (ActivityNotFoundException ex) {
240 // ignore the error. If no application can handle the URL,
241 // eg about:blank, assume the browser can handle it.
242 }
243
244 return false;
245 }
246
John Reck95a49ff2011-02-08 18:23:22 -0800247 /**
248 * Search for intent handlers that are specific to this URL
249 * aka, specialized apps like google maps or youtube
250 */
251 private boolean isSpecializedHandlerAvailable(Intent intent) {
252 PackageManager pm = mActivity.getPackageManager();
253 List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
254 PackageManager.GET_RESOLVED_FILTER);
255 if (handlers == null || handlers.size() == 0) {
256 return false;
257 }
258 for (ResolveInfo resolveInfo : handlers) {
259 IntentFilter filter = resolveInfo.filter;
260 if (filter == null) {
261 // No intent filter matches this intent?
262 // Error on the side of staying in the browser, ignore
263 continue;
264 }
John Reck1e6a2872011-12-15 14:35:02 -0800265 if (filter.countDataAuthorities() == 0 && filter.countDataPaths() == 0) {
John Reck95a49ff2011-02-08 18:23:22 -0800266 // Generic handler, skip
267 continue;
268 }
269 return true;
270 }
271 return false;
272 }
273
Russell Brenner14e1ae22011-01-12 14:54:23 -0800274 // In case a physical keyboard is attached, handle clicks with the menu key
275 // depressed by opening in a new tab
Russell Brennerca9898e2011-01-21 13:34:02 -0800276 boolean handleMenuClick(Tab tab, String url) {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800277 if (mController.isMenuDown()) {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700278 mController.openTab(url,
279 (tab != null) && tab.isPrivateBrowsingEnabled(),
280 !BrowserSettings.getInstance().openInBackground(), true);
Russell Brenner14e1ae22011-01-12 14:54:23 -0800281 mActivity.closeOptionsMenu();
282 return true;
283 }
284
285 return false;
286 }
287
Russell Brennerca9898e2011-01-21 13:34:02 -0800288 // TODO: Move this class into Tab, where it can be properly stopped upon
289 // closure of the tab
Ben Murdocha941f6e2010-12-07 16:09:16 +0000290 private class RLZTask extends AsyncTask<Void, Void, String> {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800291 private Tab mTab;
Ben Murdocha941f6e2010-12-07 16:09:16 +0000292 private Uri mSiteUri;
293 private WebView mWebView;
294
Russell Brenner14e1ae22011-01-12 14:54:23 -0800295 public RLZTask(Tab tab, Uri uri, WebView webView) {
296 mTab = tab;
Ben Murdocha941f6e2010-12-07 16:09:16 +0000297 mSiteUri = uri;
298 mWebView = webView;
299 }
300
301 protected String doInBackground(Void... unused) {
302 String result = mSiteUri.toString();
303 Cursor cur = null;
304 try {
305 cur = mActivity.getContentResolver()
306 .query(getRlzUri(), null, null, null, null);
307 if (cur != null && cur.moveToFirst() && !cur.isNull(0)) {
308 result = mSiteUri.buildUpon()
309 .appendQueryParameter("rlz", cur.getString(0))
310 .build().toString();
311 }
312 } finally {
313 if (cur != null) {
314 cur.close();
315 }
316 }
317 return result;
318 }
319
320 protected void onPostExecute(String result) {
Michael Kolbe57c7092011-07-19 11:56:56 -0700321 // abort if we left browser already
322 if (mController.isActivityPaused()) return;
Russell Brennerca9898e2011-01-21 13:34:02 -0800323 // Make sure the Tab was not closed while handling the task
Michael Kolbc831b632011-05-11 09:30:34 -0700324 if (mController.getTabControl().getTabPosition(mTab) != -1) {
Russell Brennerca9898e2011-01-21 13:34:02 -0800325 // If the Activity Manager is not invoked, load the URL directly
John Reck8bcafc12011-08-29 16:43:02 -0700326 if (!startActivityForUrl(mTab, result)) {
Russell Brennerca9898e2011-01-21 13:34:02 -0800327 if (!handleMenuClick(mTab, result)) {
John Reck26b18322011-06-21 13:08:58 -0700328 mController.loadUrl(mTab, result);
Russell Brennerca9898e2011-01-21 13:34:02 -0800329 }
Russell Brenner14e1ae22011-01-12 14:54:23 -0800330 }
331 }
Ben Murdocha941f6e2010-12-07 16:09:16 +0000332 }
333 }
334
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 // Determine whether the RLZ provider is present on the system.
336 private boolean rlzProviderPresent() {
337 if (mIsProviderPresent == null) {
338 PackageManager pm = mActivity.getPackageManager();
John Reck35e9dd62011-04-25 09:01:54 -0700339 mIsProviderPresent = pm.resolveContentProvider(RLZ_PROVIDER, 0) != null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 }
341 return mIsProviderPresent;
342 }
343
344 // Retrieve the RLZ access point string and cache the URI used to
345 // retrieve RLZ values.
346 private Uri getRlzUri() {
347 if (mRlzUri == null) {
348 String ap = mActivity.getResources()
349 .getString(R.string.rlz_access_point);
John Reck35e9dd62011-04-25 09:01:54 -0700350 mRlzUri = Uri.withAppendedPath(RLZ_PROVIDER_URI, ap);
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 }
352 return mRlzUri;
353 }
354
355 // Determine if this URI appears to be for a Google search
356 // and does not have an RLZ parameter.
357 // Taken largely from Chrome source, src/chrome/browser/google_url_tracker.cc
358 private static boolean needsRlzString(Uri uri) {
359 String scheme = uri.getScheme();
360 if (("http".equals(scheme) || "https".equals(scheme)) &&
361 (uri.getQueryParameter("q") != null) &&
362 (uri.getQueryParameter("rlz") == null)) {
363 String host = uri.getHost();
364 if (host == null) {
365 return false;
366 }
367 String[] hostComponents = host.split("\\.");
368
369 if (hostComponents.length < 2) {
370 return false;
371 }
372 int googleComponent = hostComponents.length - 2;
373 String component = hostComponents[googleComponent];
374 if (!"google".equals(component)) {
375 if (hostComponents.length < 3 ||
376 (!"co".equals(component) && !"com".equals(component))) {
377 return false;
378 }
379 googleComponent = hostComponents.length - 3;
380 if (!"google".equals(hostComponents[googleComponent])) {
381 return false;
382 }
383 }
384
385 // Google corp network handling.
386 if (googleComponent > 0 && "corp".equals(
387 hostComponents[googleComponent - 1])) {
388 return false;
389 }
390
391 return true;
392 }
393 return false;
394 }
395
396}