blob: 40f08f00ae2bf355d808c3cb7db3d5a5cdcc2c2c [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
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;
kaiyizc4ada322013-07-30 09:58:07 +080027import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070028
Bijan Amirzada41242f22014-03-21 12:12:18 -070029import com.android.browser.R;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070030import com.android.browser.platformsupport.Browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080031
Michael Kolb8233fac2010-10-26 16:08:53 -070032import java.net.URISyntaxException;
John Reck95a49ff2011-02-08 18:23:22 -080033import java.util.List;
John Reckdb3d43d2011-02-11 11:56:38 -080034import java.util.regex.Matcher;
Michael Kolb8233fac2010-10-26 16:08:53 -070035
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080036import org.codeaurora.swe.WebView;
37
Michael Kolb8233fac2010-10-26 16:08:53 -070038public class UrlHandler {
39
kaiyizc4ada322013-07-30 09:58:07 +080040 private final static String TAG = "UrlHandler";
John Reck35e9dd62011-04-25 09:01:54 -070041
Michael Kolb8233fac2010-10-26 16:08:53 -070042 // 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 Sekharb54614f2014-05-01 19:03:37 -070047 /* package */ final static String SCHEME_MAILTO = "mailto:";
Michael Kolb8233fac2010-10-26 16:08:53 -070048 Controller mController;
49 Activity mActivity;
50
Michael Kolb8233fac2010-10-26 16:08:53 -070051 public UrlHandler(Controller controller) {
52 mController = controller;
53 mActivity = mController.getActivity();
54 }
55
Michael Kolb18eb3772010-12-10 14:29:51 -080056 boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -070057 if (view.isPrivateBrowsingEnabled()) {
58 // Don't allow urls to leave the browser app when in
59 // private browsing mode
Patrick Scottd05faa62010-12-16 09:15:34 -050060 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -070061 }
62
Vivek Sekharcb3f7232015-12-10 20:02:46 -080063 if (url.startsWith(WebView.SCHEME_TEL)) {
64 Intent intent = new Intent(Intent.ACTION_VIEW,
65 Uri.parse(WebView.SCHEME_TEL +
66 Uri.encode(url.substring(WebView.SCHEME_TEL.length()))));
67 mActivity.startActivity(intent);
68 return true;
69 }
Michael Kolb8233fac2010-10-26 16:08:53 -070070 if (url.startsWith(SCHEME_WTAI)) {
71 // wtai://wp/mc;number
72 // number=string(phone-number)
73 if (url.startsWith(SCHEME_WTAI_MC)) {
74 Intent intent = new Intent(Intent.ACTION_VIEW,
75 Uri.parse(WebView.SCHEME_TEL +
Vivek Sekharcb3f7232015-12-10 20:02:46 -080076 Uri.encode(url.substring(SCHEME_WTAI_MC.length()))));
Michael Kolb8233fac2010-10-26 16:08:53 -070077 mActivity.startActivity(intent);
78 // before leaving BrowserActivity, close the empty child tab.
79 // If a new tab is created through JavaScript open to load this
80 // url, we would like to close it as we will load this url in a
81 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -070082 mController.closeEmptyTab();
Michael Kolb8233fac2010-10-26 16:08:53 -070083 return true;
84 }
85 // wtai://wp/sd;dtmf
86 // dtmf=string(dialstring)
87 if (url.startsWith(SCHEME_WTAI_SD)) {
88 // TODO: only send when there is active voice connection
89 return false;
90 }
91 // wtai://wp/ap;number;name
92 // number=string(phone-number)
93 // name=string
94 if (url.startsWith(SCHEME_WTAI_AP)) {
95 // TODO
96 return false;
97 }
98 }
99
100 // The "about:" schemes are internal to the browser; don't want these to
101 // be dispatched to other apps.
102 if (url.startsWith("about:")) {
103 return false;
104 }
105
kaiyiz6e5b3e02013-08-19 20:02:01 +0800106 if (url.startsWith("ae://") && url.endsWith("add-fav")) {
107 mController.startAddMyNavigation(url);
108 return true;
109 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
Vivek Sekharb54614f2014-05-01 19:03:37 -0700111 // add for carrier feature - recognize additional website format
112 // here add to support "mailto:" scheme
113 if (url.startsWith(SCHEME_MAILTO) && handleMailtoTypeUrl(url)) {
114 return true;
115 }
116
117 // add for carrier feature - wap2estore
Panos Thomas4bdb5252014-11-13 16:20:11 -0800118 boolean wap2estore = BrowserConfig.getInstance(mController.getContext())
119 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700120 if (wap2estore && isEstoreTypeUrl(url) && handleEstoreTypeUrl(url)) {
kaiyizc4ada322013-07-30 09:58:07 +0800121 return true;
122 }
123
John Reck8bcafc12011-08-29 16:43:02 -0700124 if (startActivityForUrl(tab, url)) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800125 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 }
127
Russell Brenner14e1ae22011-01-12 14:54:23 -0800128 if (handleMenuClick(tab, url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 return true;
130 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800131
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 return false;
133 }
134
Vivek Sekharb54614f2014-05-01 19:03:37 -0700135 private boolean handleMailtoTypeUrl(String url) {
136 Intent intent;
137 // perform generic parsing of the URI to turn it into an Intent.
kaiyizc4ada322013-07-30 09:58:07 +0800138 try {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
140 mActivity.startActivity(intent);
141 } catch (URISyntaxException ex) {
142 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
143 return false;
144 } catch (ActivityNotFoundException ex) {
145 Log.w("Browser", "No Activity Found for " + url);
146 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800147 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700148
149 return true;
150 }
151
152 private boolean isEstoreTypeUrl(String url) {
153 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800154 return true;
155 }
156 return false;
157 }
158
Vivek Sekharb54614f2014-05-01 19:03:37 -0700159 private boolean handleEstoreTypeUrl(String url) {
160 if (url.getBytes().length > 256) {
kaiyizc4ada322013-07-30 09:58:07 +0800161 Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700162 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800163 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700164
165 Intent intent;
166 // perform generic parsing of the URI to turn it into an Intent.
kaiyizc4ada322013-07-30 09:58:07 +0800167 try {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700168 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
kaiyizc4ada322013-07-30 09:58:07 +0800169 mActivity.startActivity(intent);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700170 } catch (URISyntaxException ex) {
171 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
172 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800173 } catch (ActivityNotFoundException ex) {
174 String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage);
175 mController.loadUrl(mController.getCurrentTab(), downloadUrl);
176 Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show();
177 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700178
179 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800180 }
181
Vivek Sekharb54614f2014-05-01 19:03:37 -0700182
John Reck8bcafc12011-08-29 16:43:02 -0700183 boolean startActivityForUrl(Tab tab, String url) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800184 Intent intent;
185 // perform generic parsing of the URI to turn it into an Intent.
186 try {
187 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
188 } catch (URISyntaxException ex) {
189 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
190 return false;
191 }
192
193 // check whether the intent can be resolved. If not, we will see
194 // whether we can download it from the Market.
195 if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) {
196 String packagename = intent.getPackage();
197 if (packagename != null) {
Vivek Sekhar40713382014-06-11 14:29:32 -0700198 try {
199 intent = new Intent(Intent.ACTION_VIEW, Uri
200 .parse("market://search?q=pname:" + packagename));
201 intent.addCategory(Intent.CATEGORY_BROWSABLE);
202 mActivity.startActivity(intent);
203 // before leaving BrowserActivity, close the empty child tab.
204 // If a new tab is created through JavaScript open to load this
205 // url, we would like to close it as we will load this url in a
206 // different Activity.
207 mController.closeEmptyTab();
208 return true;
209 } catch (ActivityNotFoundException e) {
210 Log.w(TAG, "Play store not found while searching for : " + packagename);
211 CharSequence alert = mActivity.getResources().getString(
212 R.string.msg_no_google_play);
213 Toast t = Toast.makeText(mActivity , alert, Toast.LENGTH_SHORT);
214 t.show();
215 return false;
216 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800217 } else {
218 return false;
219 }
220 }
221
222 // sanitize the Intent, ensuring web pages can not bypass browser
223 // security (only access to BROWSABLE activities).
224 intent.addCategory(Intent.CATEGORY_BROWSABLE);
225 intent.setComponent(null);
John Reck8bcafc12011-08-29 16:43:02 -0700226 // Re-use the existing tab if the intent comes back to us
227 if (tab != null) {
228 if (tab.getAppId() == null) {
Michael Kolbf1286a42012-04-17 14:10:52 -0700229 tab.setAppId(mActivity.getPackageName() + "-" + tab.getId());
John Reck8bcafc12011-08-29 16:43:02 -0700230 }
231 intent.putExtra(Browser.EXTRA_APPLICATION_ID, tab.getAppId());
232 }
John Reckdb3d43d2011-02-11 11:56:38 -0800233 // Make sure webkit can handle it internally before checking for specialized
234 // handlers. If webkit can't handle it internally, we need to call
235 // startActivityIfNeeded
236 Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url);
237 if (m.matches() && !isSpecializedHandlerAvailable(intent)) {
John Reck95a49ff2011-02-08 18:23:22 -0800238 return false;
239 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800240 try {
John Reck38b39652012-06-05 09:22:59 -0700241 intent.putExtra(BrowserActivity.EXTRA_DISABLE_URL_OVERRIDE, true);
Russell Brennerd4afde12011-01-07 11:09:36 -0800242 if (mActivity.startActivityIfNeeded(intent, -1)) {
243 // before leaving BrowserActivity, close the empty child tab.
244 // If a new tab is created through JavaScript open to load this
245 // url, we would like to close it as we will load this url in a
246 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -0700247 mController.closeEmptyTab();
Russell Brennerd4afde12011-01-07 11:09:36 -0800248 return true;
249 }
250 } catch (ActivityNotFoundException ex) {
251 // ignore the error. If no application can handle the URL,
252 // eg about:blank, assume the browser can handle it.
253 }
254
255 return false;
256 }
257
John Reck95a49ff2011-02-08 18:23:22 -0800258 /**
259 * Search for intent handlers that are specific to this URL
260 * aka, specialized apps like google maps or youtube
261 */
262 private boolean isSpecializedHandlerAvailable(Intent intent) {
263 PackageManager pm = mActivity.getPackageManager();
264 List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
265 PackageManager.GET_RESOLVED_FILTER);
266 if (handlers == null || handlers.size() == 0) {
267 return false;
268 }
269 for (ResolveInfo resolveInfo : handlers) {
270 IntentFilter filter = resolveInfo.filter;
271 if (filter == null) {
272 // No intent filter matches this intent?
273 // Error on the side of staying in the browser, ignore
274 continue;
275 }
John Reck1e6a2872011-12-15 14:35:02 -0800276 if (filter.countDataAuthorities() == 0 && filter.countDataPaths() == 0) {
John Reck95a49ff2011-02-08 18:23:22 -0800277 // Generic handler, skip
278 continue;
279 }
280 return true;
281 }
282 return false;
283 }
284
Russell Brenner14e1ae22011-01-12 14:54:23 -0800285 // In case a physical keyboard is attached, handle clicks with the menu key
286 // depressed by opening in a new tab
Russell Brennerca9898e2011-01-21 13:34:02 -0800287 boolean handleMenuClick(Tab tab, String url) {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800288 if (mController.isMenuDown()) {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700289 mController.openTab(url,
290 (tab != null) && tab.isPrivateBrowsingEnabled(),
291 !BrowserSettings.getInstance().openInBackground(), true);
Russell Brenner14e1ae22011-01-12 14:54:23 -0800292 mActivity.closeOptionsMenu();
293 return true;
294 }
295
296 return false;
297 }
298
Michael Kolb8233fac2010-10-26 16:08:53 -0700299}