blob: 914edaef847f1921027b5c503ae5b1efdf275cd7 [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;
John Reck8bcafc12011-08-29 16:43:02 -070026import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.util.Log;
kaiyizc4ada322013-07-30 09:58:07 +080028import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070029
Bijan Amirzada41242f22014-03-21 12:12:18 -070030import com.android.browser.R;
31import com.android.browser.reflect.ReflectHelper;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080032
kaiyizc4ada322013-07-30 09:58:07 +080033import java.io.UnsupportedEncodingException;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import java.net.URISyntaxException;
John Reck95a49ff2011-02-08 18:23:22 -080035import java.util.List;
John Reckdb3d43d2011-02-11 11:56:38 -080036import java.util.regex.Matcher;
Michael Kolb8233fac2010-10-26 16:08:53 -070037
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080038import org.codeaurora.swe.WebView;
39
Michael Kolb8233fac2010-10-26 16:08:53 -070040public class UrlHandler {
41
kaiyizc4ada322013-07-30 09:58:07 +080042 private final static String TAG = "UrlHandler";
John Reck35e9dd62011-04-25 09:01:54 -070043
Michael Kolb8233fac2010-10-26 16:08:53 -070044 // Use in overrideUrlLoading
45 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
46 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
47 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
48 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
Vivek Sekharb54614f2014-05-01 19:03:37 -070049 /* package */ final static String SCHEME_MAILTO = "mailto:";
Michael Kolb8233fac2010-10-26 16:08:53 -070050 Controller mController;
51 Activity mActivity;
52
Michael Kolb8233fac2010-10-26 16:08:53 -070053 public UrlHandler(Controller controller) {
54 mController = controller;
55 mActivity = mController.getActivity();
56 }
57
Michael Kolb18eb3772010-12-10 14:29:51 -080058 boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -070059 if (view.isPrivateBrowsingEnabled()) {
60 // Don't allow urls to leave the browser app when in
61 // private browsing mode
Patrick Scottd05faa62010-12-16 09:15:34 -050062 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -070063 }
64
65 if (url.startsWith(SCHEME_WTAI)) {
66 // wtai://wp/mc;number
67 // number=string(phone-number)
68 if (url.startsWith(SCHEME_WTAI_MC)) {
69 Intent intent = new Intent(Intent.ACTION_VIEW,
70 Uri.parse(WebView.SCHEME_TEL +
71 url.substring(SCHEME_WTAI_MC.length())));
72 mActivity.startActivity(intent);
73 // before leaving BrowserActivity, close the empty child tab.
74 // If a new tab is created through JavaScript open to load this
75 // url, we would like to close it as we will load this url in a
76 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -070077 mController.closeEmptyTab();
Michael Kolb8233fac2010-10-26 16:08:53 -070078 return true;
79 }
80 // wtai://wp/sd;dtmf
81 // dtmf=string(dialstring)
82 if (url.startsWith(SCHEME_WTAI_SD)) {
83 // TODO: only send when there is active voice connection
84 return false;
85 }
86 // wtai://wp/ap;number;name
87 // number=string(phone-number)
88 // name=string
89 if (url.startsWith(SCHEME_WTAI_AP)) {
90 // TODO
91 return false;
92 }
93 }
94
95 // The "about:" schemes are internal to the browser; don't want these to
96 // be dispatched to other apps.
97 if (url.startsWith("about:")) {
98 return false;
99 }
100
kaiyiz6e5b3e02013-08-19 20:02:01 +0800101 if (url.startsWith("ae://") && url.endsWith("add-fav")) {
102 mController.startAddMyNavigation(url);
103 return true;
104 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
Vivek Sekharb54614f2014-05-01 19:03:37 -0700106 // add for carrier feature - recognize additional website format
107 // here add to support "mailto:" scheme
108 if (url.startsWith(SCHEME_MAILTO) && handleMailtoTypeUrl(url)) {
109 return true;
110 }
111
112 // add for carrier feature - wap2estore
113 String browserRes = mActivity.getResources().getString(R.string.config_carrier_resource);
114 boolean wap2estore = "ct".equals(browserRes);
115 if (wap2estore && isEstoreTypeUrl(url) && handleEstoreTypeUrl(url)) {
kaiyizc4ada322013-07-30 09:58:07 +0800116 return true;
117 }
118
John Reck8bcafc12011-08-29 16:43:02 -0700119 if (startActivityForUrl(tab, url)) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800120 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 }
122
Russell Brenner14e1ae22011-01-12 14:54:23 -0800123 if (handleMenuClick(tab, url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 return true;
125 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800126
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 return false;
128 }
129
Vivek Sekharb54614f2014-05-01 19:03:37 -0700130 private boolean handleMailtoTypeUrl(String url) {
131 Intent intent;
132 // perform generic parsing of the URI to turn it into an Intent.
kaiyizc4ada322013-07-30 09:58:07 +0800133 try {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700134 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
135 mActivity.startActivity(intent);
136 } catch (URISyntaxException ex) {
137 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
138 return false;
139 } catch (ActivityNotFoundException ex) {
140 Log.w("Browser", "No Activity Found for " + url);
141 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800142 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700143
144 return true;
145 }
146
147 private boolean isEstoreTypeUrl(String url) {
148 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800149 return true;
150 }
151 return false;
152 }
153
Vivek Sekharb54614f2014-05-01 19:03:37 -0700154 private boolean handleEstoreTypeUrl(String url) {
155 if (url.getBytes().length > 256) {
kaiyizc4ada322013-07-30 09:58:07 +0800156 Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700157 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800158 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700159
160 Intent intent;
161 // perform generic parsing of the URI to turn it into an Intent.
kaiyizc4ada322013-07-30 09:58:07 +0800162 try {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700163 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
kaiyizc4ada322013-07-30 09:58:07 +0800164 mActivity.startActivity(intent);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700165 } catch (URISyntaxException ex) {
166 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
167 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800168 } catch (ActivityNotFoundException ex) {
169 String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage);
170 mController.loadUrl(mController.getCurrentTab(), downloadUrl);
171 Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show();
172 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700173
174 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800175 }
176
Vivek Sekharb54614f2014-05-01 19:03:37 -0700177
John Reck8bcafc12011-08-29 16:43:02 -0700178 boolean startActivityForUrl(Tab tab, String url) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800179 Intent intent;
180 // perform generic parsing of the URI to turn it into an Intent.
181 try {
182 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
183 } catch (URISyntaxException ex) {
184 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
185 return false;
186 }
187
188 // check whether the intent can be resolved. If not, we will see
189 // whether we can download it from the Market.
190 if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) {
191 String packagename = intent.getPackage();
192 if (packagename != null) {
193 intent = new Intent(Intent.ACTION_VIEW, Uri
194 .parse("market://search?q=pname:" + packagename));
195 intent.addCategory(Intent.CATEGORY_BROWSABLE);
196 mActivity.startActivity(intent);
197 // before leaving BrowserActivity, close the empty child tab.
198 // If a new tab is created through JavaScript open to load this
199 // url, we would like to close it as we will load this url in a
200 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -0700201 mController.closeEmptyTab();
Russell Brennerd4afde12011-01-07 11:09:36 -0800202 return true;
203 } else {
204 return false;
205 }
206 }
207
208 // sanitize the Intent, ensuring web pages can not bypass browser
209 // security (only access to BROWSABLE activities).
210 intent.addCategory(Intent.CATEGORY_BROWSABLE);
211 intent.setComponent(null);
John Reck8bcafc12011-08-29 16:43:02 -0700212 // Re-use the existing tab if the intent comes back to us
213 if (tab != null) {
214 if (tab.getAppId() == null) {
Michael Kolbf1286a42012-04-17 14:10:52 -0700215 tab.setAppId(mActivity.getPackageName() + "-" + tab.getId());
John Reck8bcafc12011-08-29 16:43:02 -0700216 }
217 intent.putExtra(Browser.EXTRA_APPLICATION_ID, tab.getAppId());
218 }
John Reckdb3d43d2011-02-11 11:56:38 -0800219 // Make sure webkit can handle it internally before checking for specialized
220 // handlers. If webkit can't handle it internally, we need to call
221 // startActivityIfNeeded
222 Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url);
223 if (m.matches() && !isSpecializedHandlerAvailable(intent)) {
John Reck95a49ff2011-02-08 18:23:22 -0800224 return false;
225 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800226 try {
John Reck38b39652012-06-05 09:22:59 -0700227 intent.putExtra(BrowserActivity.EXTRA_DISABLE_URL_OVERRIDE, true);
Russell Brennerd4afde12011-01-07 11:09:36 -0800228 if (mActivity.startActivityIfNeeded(intent, -1)) {
229 // before leaving BrowserActivity, close the empty child tab.
230 // If a new tab is created through JavaScript open to load this
231 // url, we would like to close it as we will load this url in a
232 // different Activity.
John Reck8bcafc12011-08-29 16:43:02 -0700233 mController.closeEmptyTab();
Russell Brennerd4afde12011-01-07 11:09:36 -0800234 return true;
235 }
236 } catch (ActivityNotFoundException ex) {
237 // ignore the error. If no application can handle the URL,
238 // eg about:blank, assume the browser can handle it.
239 }
240
241 return false;
242 }
243
John Reck95a49ff2011-02-08 18:23:22 -0800244 /**
245 * Search for intent handlers that are specific to this URL
246 * aka, specialized apps like google maps or youtube
247 */
248 private boolean isSpecializedHandlerAvailable(Intent intent) {
249 PackageManager pm = mActivity.getPackageManager();
250 List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
251 PackageManager.GET_RESOLVED_FILTER);
252 if (handlers == null || handlers.size() == 0) {
253 return false;
254 }
255 for (ResolveInfo resolveInfo : handlers) {
256 IntentFilter filter = resolveInfo.filter;
257 if (filter == null) {
258 // No intent filter matches this intent?
259 // Error on the side of staying in the browser, ignore
260 continue;
261 }
John Reck1e6a2872011-12-15 14:35:02 -0800262 if (filter.countDataAuthorities() == 0 && filter.countDataPaths() == 0) {
John Reck95a49ff2011-02-08 18:23:22 -0800263 // Generic handler, skip
264 continue;
265 }
266 return true;
267 }
268 return false;
269 }
270
Russell Brenner14e1ae22011-01-12 14:54:23 -0800271 // In case a physical keyboard is attached, handle clicks with the menu key
272 // depressed by opening in a new tab
Russell Brennerca9898e2011-01-21 13:34:02 -0800273 boolean handleMenuClick(Tab tab, String url) {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800274 if (mController.isMenuDown()) {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700275 mController.openTab(url,
276 (tab != null) && tab.isPrivateBrowsingEnabled(),
277 !BrowserSettings.getInstance().openInBackground(), true);
Russell Brenner14e1ae22011-01-12 14:54:23 -0800278 mActivity.closeOptionsMenu();
279 return true;
280 }
281
282 return false;
283 }
284
Michael Kolb8233fac2010-10-26 16:08:53 -0700285}