blob: b21c688d5fed61a1cdf7efef9bdd5573bface599 [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;
31import android.speech.RecognizerResultsIntent;
32import android.text.TextUtils;
33import android.util.Patterns;
34
Michael Kolb315d5022011-10-13 12:47:11 -070035import com.android.browser.UI.ComboViews;
Michael Kolbe28b3472011-08-04 16:54:31 -070036import com.android.browser.search.SearchEngine;
37import com.android.common.Search;
38import com.android.common.speech.LoggingEvents;
39
Michael Kolb8233fac2010-10-26 16:08:53 -070040import java.util.HashMap;
41import java.util.Iterator;
42import java.util.Map;
43
44/**
45 * Handle all browser related intents
46 */
47public class IntentHandler {
48
49 // "source" parameter for Google search suggested by the browser
50 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
51 // "source" parameter for Google search from unknown source
52 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
53
54 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
55
56 private Activity mActivity;
57 private Controller mController;
58 private TabControl mTabControl;
59 private BrowserSettings mSettings;
60
61 public IntentHandler(Activity browser, Controller controller) {
62 mActivity = browser;
63 mController = controller;
64 mTabControl = mController.getTabControl();
65 mSettings = controller.getSettings();
66 }
67
68 void onNewIntent(Intent intent) {
69 Tab current = mTabControl.getCurrentTab();
70 // When a tab is closed on exit, the current tab index is set to -1.
71 // Reset before proceed as Browser requires the current tab to be set.
72 if (current == null) {
73 // Try to reset the tab in case the index was incorrect.
74 current = mTabControl.getTab(0);
75 if (current == null) {
76 // No tabs at all so just ignore this intent.
77 return;
78 }
79 mController.setActiveTab(current);
Michael Kolb8233fac2010-10-26 16:08:53 -070080 }
81 final String action = intent.getAction();
82 final int flags = intent.getFlags();
83 if (Intent.ACTION_MAIN.equals(action) ||
84 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
85 // just resume the browser
86 return;
87 }
John Reck439c9a52010-12-14 10:04:39 -080088 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(action)) {
Michael Kolb315d5022011-10-13 12:47:11 -070089 mController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -080090 return;
91 }
John Reck07af2b82011-01-18 14:24:43 -080092
Michael Kolb8233fac2010-10-26 16:08:53 -070093 // In case the SearchDialog is open.
94 ((SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE))
95 .stopSearch();
96 boolean activateVoiceSearch = RecognizerResultsIntent
97 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
98 if (Intent.ACTION_VIEW.equals(action)
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -050099 || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 || Intent.ACTION_SEARCH.equals(action)
101 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
102 || Intent.ACTION_WEB_SEARCH.equals(action)
103 || activateVoiceSearch) {
104 if (current.isInVoiceSearchMode()) {
105 String title = current.getVoiceDisplayTitle();
106 if (title != null && title.equals(intent.getStringExtra(
107 SearchManager.QUERY))) {
108 // The user submitted the same search as the last voice
109 // search, so do nothing.
110 return;
111 }
112 if (Intent.ACTION_SEARCH.equals(action)
113 && current.voiceSearchSourceIsGoogle()) {
114 Intent logIntent = new Intent(
115 LoggingEvents.ACTION_LOG_EVENT);
116 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
117 LoggingEvents.VoiceSearch.QUERY_UPDATED);
118 logIntent.putExtra(
119 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
120 intent.getDataString());
121 mActivity.sendBroadcast(logIntent);
122 // Note, onPageStarted will revert the voice title bar
123 // When http://b/issue?id=2379215 is fixed, we should update
124 // the title bar here.
125 }
126 }
127 // If this was a search request (e.g. search query directly typed into the address bar),
128 // pass it on to the default web search provider.
129 if (handleWebSearchIntent(mActivity, mController, intent)) {
130 return;
131 }
132
133 UrlData urlData = getUrlDataFromIntent(intent);
134 if (urlData.isEmpty()) {
135 urlData = new UrlData(mSettings.getHomePage());
136 }
137
Michael Kolb14612442011-06-24 13:06:29 -0700138 if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)
139 || urlData.isPreloaded()) {
140 Tab t = mController.openTab(urlData);
Leon Scroggins2ed6e312011-02-22 16:37:23 -0500141 return;
142 }
John Reckdb22ec42011-06-29 11:31:24 -0700143 /*
144 * TODO: Don't allow javascript URIs
145 * 0) If this is a javascript: URI, *always* open a new tab
146 * 1) If this is a voice search, re-use tab for appId
147 * If there is no appId, use current tab
148 * 2) If the URL is already opened, switch to that tab
149 * 3-phone) Reuse tab with same appId
150 * 3-tablet) Open new tab
151 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 final String appId = intent
153 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
John Reck26b18322011-06-21 13:08:58 -0700154 if (!TextUtils.isEmpty(urlData.mUrl) &&
155 urlData.mUrl.startsWith("javascript:")) {
156 // Always open javascript: URIs in new tabs
157 mController.openTab(urlData);
158 return;
159 }
Michael Kolbf1286a42012-04-17 14:10:52 -0700160 if (Intent.ACTION_VIEW.equals(action)
161 && (appId != null)
162 && appId.startsWith(mActivity.getPackageName())) {
163 Tab appTab = mTabControl.getTabFromAppId(appId);
164 if ((appTab != null) && (appTab == mController.getCurrentTab())) {
165 mController.switchToTab(appTab);
166 mController.loadUrlDataIn(appTab, urlData);
167 return;
168 }
169 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 if ((Intent.ACTION_VIEW.equals(action)
171 // If a voice search has no appId, it means that it came
172 // from the browser. In that case, reuse the current tab.
173 || (activateVoiceSearch && appId != null))
John Reckdb22ec42011-06-29 11:31:24 -0700174 && !mActivity.getPackageName().equals(appId)) {
175 if (activateVoiceSearch || !BrowserActivity.isTablet(mActivity)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700176 Tab appTab = mTabControl.getTabFromAppId(appId);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800177 if (appTab != null) {
John Reck26b18322011-06-21 13:08:58 -0700178 mController.reuseTab(appTab, urlData);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800179 return;
Michael Kolbaf0d3342011-03-11 11:30:16 -0800180 }
John Reckdb22ec42011-06-29 11:31:24 -0700181 }
182 // No matching application tab, try to find a regular tab
183 // with a matching url.
184 Tab appTab = mTabControl.findTabWithUrl(urlData.mUrl);
185 if (appTab != null) {
186 // Transfer ownership
187 appTab.setAppId(appId);
188 if (current != appTab) {
189 mController.switchToTab(appTab);
190 }
191 // Otherwise, we are already viewing the correct tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700193 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
194 // will be opened in a new tab unless we have reached
195 // MAX_TABS. Then the url will be opened in the current
196 // tab. If a new tab is created, it will have "true" for
197 // exit on close.
198 Tab tab = mController.openTab(urlData);
199 if (tab != null) {
200 tab.setAppId(appId);
Michael Kolbe28b3472011-08-04 16:54:31 -0700201 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
202 tab.setCloseOnBack(true);
203 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 }
205 }
206 } else {
207 if (!urlData.isEmpty()
208 && urlData.mUrl.startsWith("about:debug")) {
209 if ("about:debug.dom".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000210 current.getWebViewClassic().dumpDomTree(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000212 current.getWebViewClassic().dumpDomTree(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 } else if ("about:debug.render".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000214 current.getWebViewClassic().dumpRenderTree(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000216 current.getWebViewClassic().dumpRenderTree(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 } else if ("about:debug.display".equals(urlData.mUrl)) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000218 current.getWebViewClassic().dumpDisplayTree();
Cary Clark4f4cb6b2011-01-18 13:03:42 -0500219 } else if ("about:debug.nav".equals(urlData.mUrl)) {
220 current.getWebView().debugDump();
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700222 mSettings.toggleDebugSettings();
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 }
224 return;
225 }
226 // Get rid of the subwindow if it exists
227 mController.dismissSubWindow(current);
228 // If the current Tab is being used as an application tab,
229 // remove the association, since the new Intent means that it is
230 // no longer associated with that application.
231 current.setAppId(null);
232 mController.loadUrlDataIn(current, urlData);
233 }
234 }
235 }
236
Michael Kolb14612442011-06-24 13:06:29 -0700237 protected static UrlData getUrlDataFromIntent(Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 String url = "";
239 Map<String, String> headers = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100240 PreloadedTabControl preloaded = null;
241 String preloadedSearchBoxQuery = null;
Leon Scrogginse1cab102011-01-04 10:50:13 -0500242 if (intent != null
243 && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 final String action = intent.getAction();
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -0500245 if (Intent.ACTION_VIEW.equals(action) ||
246 NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 url = UrlUtils.smartUrlFilter(intent.getData());
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 if (url != null && url.startsWith("http")) {
249 final Bundle pairs = intent
250 .getBundleExtra(Browser.EXTRA_HEADERS);
251 if (pairs != null && !pairs.isEmpty()) {
252 Iterator<String> iter = pairs.keySet().iterator();
253 headers = new HashMap<String, String>();
254 while (iter.hasNext()) {
255 String key = iter.next();
256 headers.put(key, pairs.getString(key));
257 }
258 }
259 }
Michael Kolb14612442011-06-24 13:06:29 -0700260 if (intent.hasExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID)) {
261 String id = intent.getStringExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID);
Mathew Inwood29721c22011-06-29 17:55:24 +0100262 preloadedSearchBoxQuery = intent.getStringExtra(
263 PreloadRequestReceiver.EXTRA_SEARCHBOX_SETQUERY);
Michael Kolb14612442011-06-24 13:06:29 -0700264 preloaded = Preloader.getInstance().getPreloadedTab(id);
265 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 } else if (Intent.ACTION_SEARCH.equals(action)
267 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
268 || Intent.ACTION_WEB_SEARCH.equals(action)) {
269 url = intent.getStringExtra(SearchManager.QUERY);
270 if (url != null) {
271 // In general, we shouldn't modify URL from Intent.
272 // But currently, we get the user-typed URL from search box as well.
273 url = UrlUtils.fixUrl(url);
274 url = UrlUtils.smartUrlFilter(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
276 if (url.contains(searchSource)) {
277 String source = null;
278 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
279 if (appData != null) {
280 source = appData.getString(Search.SOURCE);
281 }
282 if (TextUtils.isEmpty(source)) {
283 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
284 }
285 url = url.replace(searchSource, "&source=android-"+source+"&");
286 }
287 }
288 }
289 }
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100290 return new UrlData(url, headers, intent, preloaded, preloadedSearchBoxQuery);
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 }
292
293 /**
294 * Launches the default web search activity with the query parameters if the given intent's data
295 * are identified as plain search terms and not URLs/shortcuts.
296 * @return true if the intent was handled and web search activity was launched, false if not.
297 */
298 static boolean handleWebSearchIntent(Activity activity,
299 Controller controller, Intent intent) {
300 if (intent == null) return false;
301
302 String url = null;
303 final String action = intent.getAction();
304 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
305 action)) {
306 return false;
307 }
308 if (Intent.ACTION_VIEW.equals(action)) {
309 Uri data = intent.getData();
310 if (data != null) url = data.toString();
311 } else if (Intent.ACTION_SEARCH.equals(action)
312 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
313 || Intent.ACTION_WEB_SEARCH.equals(action)) {
314 url = intent.getStringExtra(SearchManager.QUERY);
315 }
316 return handleWebSearchRequest(activity, controller, url,
317 intent.getBundleExtra(SearchManager.APP_DATA),
318 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
319 }
320
321 /**
322 * Launches the default web search activity with the query parameters if the given url string
323 * was identified as plain search terms and not URL/shortcut.
324 * @return true if the request was handled and web search activity was launched, false if not.
325 */
326 private static boolean handleWebSearchRequest(Activity activity,
327 Controller controller, String inUrl, Bundle appData,
328 String extraData) {
John Reck99141272010-12-21 11:34:12 -0800329 if (inUrl == null) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700330
331 // In general, we shouldn't modify URL from Intent.
332 // But currently, we get the user-typed URL from search box as well.
333 String url = UrlUtils.fixUrl(inUrl).trim();
John Reck99141272010-12-21 11:34:12 -0800334 if (TextUtils.isEmpty(url)) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700335
336 // URLs are handled by the regular flow of control, so
337 // return early.
338 if (Patterns.WEB_URL.matcher(url).matches()
339 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
340 return false;
341 }
342
343 final ContentResolver cr = activity.getContentResolver();
344 final String newUrl = url;
345 if (controller == null || controller.getTabControl() == null
346 || controller.getTabControl().getCurrentWebView() == null
347 || !controller.getTabControl().getCurrentWebView()
348 .isPrivateBrowsingEnabled()) {
349 new AsyncTask<Void, Void, Void>() {
350 @Override
351 protected Void doInBackground(Void... unused) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 Browser.addSearchUrl(cr, newUrl);
353 return null;
354 }
355 }.execute();
356 }
357
358 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
359 if (searchEngine == null) return false;
360 searchEngine.startSearch(activity, url, appData, extraData);
361
362 return true;
363 }
364
365 /**
366 * A UrlData class to abstract how the content will be set to WebView.
367 * This base class uses loadUrl to show the content.
368 */
369 static class UrlData {
370 final String mUrl;
371 final Map<String, String> mHeaders;
372 final Intent mVoiceIntent;
Mathew Inwood29721c22011-06-29 17:55:24 +0100373 final PreloadedTabControl mPreloadedTab;
374 final String mSearchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700375
376 UrlData(String url) {
377 this.mUrl = url;
378 this.mHeaders = null;
379 this.mVoiceIntent = null;
Michael Kolb14612442011-06-24 13:06:29 -0700380 this.mPreloadedTab = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100381 this.mSearchBoxQueryToSubmit = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 }
383
384 UrlData(String url, Map<String, String> headers, Intent intent) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100385 this(url, headers, intent, null, null);
Michael Kolb14612442011-06-24 13:06:29 -0700386 }
387
Mathew Inwood29721c22011-06-29 17:55:24 +0100388 UrlData(String url, Map<String, String> headers, Intent intent,
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100389 PreloadedTabControl preloaded, String searchBoxQueryToSubmit) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700390 this.mUrl = url;
391 this.mHeaders = headers;
392 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
393 .equals(intent.getAction())) {
394 this.mVoiceIntent = intent;
395 } else {
396 this.mVoiceIntent = null;
397 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100398 this.mPreloadedTab = preloaded;
399 this.mSearchBoxQueryToSubmit = searchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 }
401
402 boolean isEmpty() {
403 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
404 }
Michael Kolb14612442011-06-24 13:06:29 -0700405
406 boolean isPreloaded() {
407 return mPreloadedTab != null;
408 }
409
Mathew Inwood29721c22011-06-29 17:55:24 +0100410 PreloadedTabControl getPreloadedTab() {
Michael Kolb14612442011-06-24 13:06:29 -0700411 return mPreloadedTab;
412 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100413
414 String getSearchBoxQueryToSubmit() {
415 return mSearchBoxQueryToSubmit;
416 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 }
418
419}