blob: b23dc7d8c90c26571e58be07c54f0eb57f3e15f2 [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.net.Uri;
26import android.util.Log;
27import android.webkit.WebView;
28
29import java.net.URISyntaxException;
John Reck95a49ff2011-02-08 18:23:22 -080030import java.util.List;
John Reckdb3d43d2011-02-11 11:56:38 -080031import java.util.regex.Matcher;
Michael Kolb8233fac2010-10-26 16:08:53 -070032
33/**
34 *
35 */
36public 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 Kolb8233fac2010-10-26 16:08:53 -070047 public UrlHandler(Controller controller) {
48 mController = controller;
49 mActivity = mController.getActivity();
50 }
51
Michael Kolb18eb3772010-12-10 14:29:51 -080052 boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -070053 if (view.isPrivateBrowsingEnabled()) {
54 // Don't allow urls to leave the browser app when in
55 // private browsing mode
Patrick Scottd05faa62010-12-16 09:15:34 -050056 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -070057 }
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 Brennerd4afde12011-01-07 11:09:36 -080095 if (startActivityForUrl(url)) {
96 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -070097 }
98
Russell Brenner14e1ae22011-01-12 14:54:23 -080099 if (handleMenuClick(tab, url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 return true;
101 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800102
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 return false;
104 }
105
Russell Brennerca9898e2011-01-21 13:34:02 -0800106 boolean startActivityForUrl(String url) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800107 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 Reckdb3d43d2011-02-11 11:56:38 -0800140 // 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 Reck95a49ff2011-02-08 18:23:22 -0800145 return false;
146 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800147 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 Reck95a49ff2011-02-08 18:23:22 -0800164 /**
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 Brenner14e1ae22011-01-12 14:54:23 -0800191 // In case a physical keyboard is attached, handle clicks with the menu key
192 // depressed by opening in a new tab
Russell Brennerca9898e2011-01-21 13:34:02 -0800193 boolean handleMenuClick(Tab tab, String url) {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800194 if (mController.isMenuDown()) {
195 mController.openTab(tab, url, false);
196 mActivity.closeOptionsMenu();
197 return true;
198 }
199
200 return false;
201 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700202}