blob: 47cab70aae0a823afb9499132a3d38be0e8135a0 [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
17
18package com.android.browser;
19
Michael Kolb8233fac2010-10-26 16:08:53 -070020import android.app.Activity;
21import android.app.SearchManager;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.net.Uri;
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -050026import android.nfc.NfcAdapter;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.os.AsyncTask;
28import android.os.Bundle;
29import android.provider.Browser;
30import android.provider.MediaStore;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.text.TextUtils;
32import android.util.Patterns;
33
Michael Kolb315d5022011-10-13 12:47:11 -070034import com.android.browser.UI.ComboViews;
Michael Kolbe28b3472011-08-04 16:54:31 -070035import com.android.browser.search.SearchEngine;
36import com.android.common.Search;
Michael Kolbe28b3472011-08-04 16:54:31 -070037
Michael Kolb8233fac2010-10-26 16:08:53 -070038import java.util.HashMap;
39import java.util.Iterator;
40import java.util.Map;
41
42/**
43 * Handle all browser related intents
44 */
45public class IntentHandler {
46
47 // "source" parameter for Google search suggested by the browser
48 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
49 // "source" parameter for Google search from unknown source
50 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
51
52 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
53
54 private Activity mActivity;
55 private Controller mController;
56 private TabControl mTabControl;
57 private BrowserSettings mSettings;
58
59 public IntentHandler(Activity browser, Controller controller) {
60 mActivity = browser;
61 mController = controller;
62 mTabControl = mController.getTabControl();
63 mSettings = controller.getSettings();
64 }
65
66 void onNewIntent(Intent intent) {
67 Tab current = mTabControl.getCurrentTab();
68 // When a tab is closed on exit, the current tab index is set to -1.
69 // Reset before proceed as Browser requires the current tab to be set.
70 if (current == null) {
71 // Try to reset the tab in case the index was incorrect.
72 current = mTabControl.getTab(0);
73 if (current == null) {
74 // No tabs at all so just ignore this intent.
75 return;
76 }
77 mController.setActiveTab(current);
Michael Kolb8233fac2010-10-26 16:08:53 -070078 }
79 final String action = intent.getAction();
80 final int flags = intent.getFlags();
81 if (Intent.ACTION_MAIN.equals(action) ||
82 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
83 // just resume the browser
84 return;
85 }
John Reck439c9a52010-12-14 10:04:39 -080086 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(action)) {
Michael Kolb315d5022011-10-13 12:47:11 -070087 mController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -080088 return;
89 }
John Reck07af2b82011-01-18 14:24:43 -080090
Michael Kolb8233fac2010-10-26 16:08:53 -070091 // In case the SearchDialog is open.
92 ((SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE))
93 .stopSearch();
Michael Kolb8233fac2010-10-26 16:08:53 -070094 if (Intent.ACTION_VIEW.equals(action)
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -050095 || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
Michael Kolb8233fac2010-10-26 16:08:53 -070096 || Intent.ACTION_SEARCH.equals(action)
97 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Michael Kolb5ff5c8b2012-05-03 11:37:58 -070098 || Intent.ACTION_WEB_SEARCH.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -070099 // If this was a search request (e.g. search query directly typed into the address bar),
100 // pass it on to the default web search provider.
101 if (handleWebSearchIntent(mActivity, mController, intent)) {
102 return;
103 }
104
105 UrlData urlData = getUrlDataFromIntent(intent);
106 if (urlData.isEmpty()) {
107 urlData = new UrlData(mSettings.getHomePage());
108 }
109
Michael Kolb14612442011-06-24 13:06:29 -0700110 if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)
111 || urlData.isPreloaded()) {
112 Tab t = mController.openTab(urlData);
Leon Scroggins2ed6e312011-02-22 16:37:23 -0500113 return;
114 }
John Reckdb22ec42011-06-29 11:31:24 -0700115 /*
116 * TODO: Don't allow javascript URIs
117 * 0) If this is a javascript: URI, *always* open a new tab
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700118 * 1) If the URL is already opened, switch to that tab
119 * 2-phone) Reuse tab with same appId
120 * 2-tablet) Open new tab
John Reckdb22ec42011-06-29 11:31:24 -0700121 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 final String appId = intent
123 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
John Reck26b18322011-06-21 13:08:58 -0700124 if (!TextUtils.isEmpty(urlData.mUrl) &&
125 urlData.mUrl.startsWith("javascript:")) {
126 // Always open javascript: URIs in new tabs
127 mController.openTab(urlData);
128 return;
129 }
Michael Kolbf1286a42012-04-17 14:10:52 -0700130 if (Intent.ACTION_VIEW.equals(action)
131 && (appId != null)
132 && appId.startsWith(mActivity.getPackageName())) {
133 Tab appTab = mTabControl.getTabFromAppId(appId);
134 if ((appTab != null) && (appTab == mController.getCurrentTab())) {
135 mController.switchToTab(appTab);
136 mController.loadUrlDataIn(appTab, urlData);
137 return;
138 }
139 }
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700140 if (Intent.ACTION_VIEW.equals(action)
141 && !mActivity.getPackageName().equals(appId)) {
142 if (!BrowserActivity.isTablet(mActivity)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700143 Tab appTab = mTabControl.getTabFromAppId(appId);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800144 if (appTab != null) {
John Reck26b18322011-06-21 13:08:58 -0700145 mController.reuseTab(appTab, urlData);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800146 return;
Michael Kolbaf0d3342011-03-11 11:30:16 -0800147 }
John Reckdb22ec42011-06-29 11:31:24 -0700148 }
149 // No matching application tab, try to find a regular tab
150 // with a matching url.
151 Tab appTab = mTabControl.findTabWithUrl(urlData.mUrl);
152 if (appTab != null) {
153 // Transfer ownership
154 appTab.setAppId(appId);
155 if (current != appTab) {
156 mController.switchToTab(appTab);
157 }
158 // Otherwise, we are already viewing the correct tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700160 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
161 // will be opened in a new tab unless we have reached
162 // MAX_TABS. Then the url will be opened in the current
163 // tab. If a new tab is created, it will have "true" for
164 // exit on close.
165 Tab tab = mController.openTab(urlData);
166 if (tab != null) {
167 tab.setAppId(appId);
Michael Kolbe28b3472011-08-04 16:54:31 -0700168 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
169 tab.setCloseOnBack(true);
170 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 }
172 }
173 } else {
174 if (!urlData.isEmpty()
175 && urlData.mUrl.startsWith("about:debug")) {
176 if ("about:debug.dom".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000177 current.getWebViewClassic().dumpDomTree(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000179 current.getWebViewClassic().dumpDomTree(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 } else if ("about:debug.render".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000181 current.getWebViewClassic().dumpRenderTree(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000183 current.getWebViewClassic().dumpRenderTree(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 } else if ("about:debug.display".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000185 current.getWebViewClassic().dumpDisplayTree();
Cary Clark4f4cb6b2011-01-18 13:03:42 -0500186 } else if ("about:debug.nav".equals(urlData.mUrl)) {
187 current.getWebView().debugDump();
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700189 mSettings.toggleDebugSettings();
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 }
191 return;
192 }
193 // Get rid of the subwindow if it exists
194 mController.dismissSubWindow(current);
195 // If the current Tab is being used as an application tab,
196 // remove the association, since the new Intent means that it is
197 // no longer associated with that application.
198 current.setAppId(null);
199 mController.loadUrlDataIn(current, urlData);
200 }
201 }
202 }
203
Michael Kolb14612442011-06-24 13:06:29 -0700204 protected static UrlData getUrlDataFromIntent(Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 String url = "";
206 Map<String, String> headers = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100207 PreloadedTabControl preloaded = null;
208 String preloadedSearchBoxQuery = null;
Leon Scrogginse1cab102011-01-04 10:50:13 -0500209 if (intent != null
210 && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 final String action = intent.getAction();
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -0500212 if (Intent.ACTION_VIEW.equals(action) ||
213 NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 url = UrlUtils.smartUrlFilter(intent.getData());
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 if (url != null && url.startsWith("http")) {
216 final Bundle pairs = intent
217 .getBundleExtra(Browser.EXTRA_HEADERS);
218 if (pairs != null && !pairs.isEmpty()) {
219 Iterator<String> iter = pairs.keySet().iterator();
220 headers = new HashMap<String, String>();
221 while (iter.hasNext()) {
222 String key = iter.next();
223 headers.put(key, pairs.getString(key));
224 }
225 }
226 }
Michael Kolb14612442011-06-24 13:06:29 -0700227 if (intent.hasExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID)) {
228 String id = intent.getStringExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID);
Mathew Inwood29721c22011-06-29 17:55:24 +0100229 preloadedSearchBoxQuery = intent.getStringExtra(
230 PreloadRequestReceiver.EXTRA_SEARCHBOX_SETQUERY);
Michael Kolb14612442011-06-24 13:06:29 -0700231 preloaded = Preloader.getInstance().getPreloadedTab(id);
232 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 } else if (Intent.ACTION_SEARCH.equals(action)
234 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
235 || Intent.ACTION_WEB_SEARCH.equals(action)) {
236 url = intent.getStringExtra(SearchManager.QUERY);
237 if (url != null) {
238 // In general, we shouldn't modify URL from Intent.
239 // But currently, we get the user-typed URL from search box as well.
240 url = UrlUtils.fixUrl(url);
241 url = UrlUtils.smartUrlFilter(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
243 if (url.contains(searchSource)) {
244 String source = null;
245 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
246 if (appData != null) {
247 source = appData.getString(Search.SOURCE);
248 }
249 if (TextUtils.isEmpty(source)) {
250 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
251 }
252 url = url.replace(searchSource, "&source=android-"+source+"&");
253 }
254 }
255 }
256 }
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100257 return new UrlData(url, headers, intent, preloaded, preloadedSearchBoxQuery);
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 }
259
260 /**
261 * Launches the default web search activity with the query parameters if the given intent's data
262 * are identified as plain search terms and not URLs/shortcuts.
263 * @return true if the intent was handled and web search activity was launched, false if not.
264 */
265 static boolean handleWebSearchIntent(Activity activity,
266 Controller controller, Intent intent) {
267 if (intent == null) return false;
268
269 String url = null;
270 final String action = intent.getAction();
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 if (Intent.ACTION_VIEW.equals(action)) {
272 Uri data = intent.getData();
273 if (data != null) url = data.toString();
274 } else if (Intent.ACTION_SEARCH.equals(action)
275 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
276 || Intent.ACTION_WEB_SEARCH.equals(action)) {
277 url = intent.getStringExtra(SearchManager.QUERY);
278 }
279 return handleWebSearchRequest(activity, controller, url,
280 intent.getBundleExtra(SearchManager.APP_DATA),
281 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
282 }
283
284 /**
285 * Launches the default web search activity with the query parameters if the given url string
286 * was identified as plain search terms and not URL/shortcut.
287 * @return true if the request was handled and web search activity was launched, false if not.
288 */
289 private static boolean handleWebSearchRequest(Activity activity,
290 Controller controller, String inUrl, Bundle appData,
291 String extraData) {
John Reck99141272010-12-21 11:34:12 -0800292 if (inUrl == null) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700293
294 // In general, we shouldn't modify URL from Intent.
295 // But currently, we get the user-typed URL from search box as well.
296 String url = UrlUtils.fixUrl(inUrl).trim();
John Reck99141272010-12-21 11:34:12 -0800297 if (TextUtils.isEmpty(url)) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700298
299 // URLs are handled by the regular flow of control, so
300 // return early.
301 if (Patterns.WEB_URL.matcher(url).matches()
302 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
303 return false;
304 }
305
306 final ContentResolver cr = activity.getContentResolver();
307 final String newUrl = url;
308 if (controller == null || controller.getTabControl() == null
309 || controller.getTabControl().getCurrentWebView() == null
310 || !controller.getTabControl().getCurrentWebView()
311 .isPrivateBrowsingEnabled()) {
312 new AsyncTask<Void, Void, Void>() {
313 @Override
314 protected Void doInBackground(Void... unused) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700315 Browser.addSearchUrl(cr, newUrl);
316 return null;
317 }
318 }.execute();
319 }
320
321 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
322 if (searchEngine == null) return false;
323 searchEngine.startSearch(activity, url, appData, extraData);
324
325 return true;
326 }
327
328 /**
329 * A UrlData class to abstract how the content will be set to WebView.
330 * This base class uses loadUrl to show the content.
331 */
332 static class UrlData {
333 final String mUrl;
334 final Map<String, String> mHeaders;
Mathew Inwood29721c22011-06-29 17:55:24 +0100335 final PreloadedTabControl mPreloadedTab;
336 final String mSearchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700337
338 UrlData(String url) {
339 this.mUrl = url;
340 this.mHeaders = null;
Michael Kolb14612442011-06-24 13:06:29 -0700341 this.mPreloadedTab = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100342 this.mSearchBoxQueryToSubmit = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 }
344
345 UrlData(String url, Map<String, String> headers, Intent intent) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100346 this(url, headers, intent, null, null);
Michael Kolb14612442011-06-24 13:06:29 -0700347 }
348
Mathew Inwood29721c22011-06-29 17:55:24 +0100349 UrlData(String url, Map<String, String> headers, Intent intent,
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100350 PreloadedTabControl preloaded, String searchBoxQueryToSubmit) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 this.mUrl = url;
352 this.mHeaders = headers;
Mathew Inwood29721c22011-06-29 17:55:24 +0100353 this.mPreloadedTab = preloaded;
354 this.mSearchBoxQueryToSubmit = searchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356
357 boolean isEmpty() {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700358 return (mUrl == null || mUrl.length() == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 }
Michael Kolb14612442011-06-24 13:06:29 -0700360
361 boolean isPreloaded() {
362 return mPreloadedTab != null;
363 }
364
Mathew Inwood29721c22011-06-29 17:55:24 +0100365 PreloadedTabControl getPreloadedTab() {
Michael Kolb14612442011-06-24 13:06:29 -0700366 return mPreloadedTab;
367 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100368
369 String getSearchBoxQueryToSubmit() {
370 return mSearchBoxQueryToSubmit;
371 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700372 }
373
374}