blob: c76197d4c2bd02741c98664c10662640b1970b12 [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 Kolbe28b3472011-08-04 16:54:31 -070035import com.android.browser.search.SearchEngine;
36import com.android.common.Search;
37import com.android.common.speech.LoggingEvents;
38
Michael Kolb8233fac2010-10-26 16:08:53 -070039import java.util.HashMap;
40import java.util.Iterator;
41import java.util.Map;
42
43/**
44 * Handle all browser related intents
45 */
46public class IntentHandler {
47
48 // "source" parameter for Google search suggested by the browser
49 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
50 // "source" parameter for Google search from unknown source
51 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
52
53 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
54
55 private Activity mActivity;
56 private Controller mController;
57 private TabControl mTabControl;
58 private BrowserSettings mSettings;
59
60 public IntentHandler(Activity browser, Controller controller) {
61 mActivity = browser;
62 mController = controller;
63 mTabControl = mController.getTabControl();
64 mSettings = controller.getSettings();
65 }
66
67 void onNewIntent(Intent intent) {
68 Tab current = mTabControl.getCurrentTab();
69 // When a tab is closed on exit, the current tab index is set to -1.
70 // Reset before proceed as Browser requires the current tab to be set.
71 if (current == null) {
72 // Try to reset the tab in case the index was incorrect.
73 current = mTabControl.getTab(0);
74 if (current == null) {
75 // No tabs at all so just ignore this intent.
76 return;
77 }
78 mController.setActiveTab(current);
Michael Kolb8233fac2010-10-26 16:08:53 -070079 }
80 final String action = intent.getAction();
81 final int flags = intent.getFlags();
82 if (Intent.ACTION_MAIN.equals(action) ||
83 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
84 // just resume the browser
85 return;
86 }
John Reck439c9a52010-12-14 10:04:39 -080087 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(action)) {
88 mController.bookmarksOrHistoryPicker(false);
89 return;
90 }
John Reck07af2b82011-01-18 14:24:43 -080091
Michael Kolb8233fac2010-10-26 16:08:53 -070092 // In case the SearchDialog is open.
93 ((SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE))
94 .stopSearch();
95 boolean activateVoiceSearch = RecognizerResultsIntent
96 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
97 if (Intent.ACTION_VIEW.equals(action)
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -050098 || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
Michael Kolb8233fac2010-10-26 16:08:53 -070099 || Intent.ACTION_SEARCH.equals(action)
100 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
101 || Intent.ACTION_WEB_SEARCH.equals(action)
102 || activateVoiceSearch) {
103 if (current.isInVoiceSearchMode()) {
104 String title = current.getVoiceDisplayTitle();
105 if (title != null && title.equals(intent.getStringExtra(
106 SearchManager.QUERY))) {
107 // The user submitted the same search as the last voice
108 // search, so do nothing.
109 return;
110 }
111 if (Intent.ACTION_SEARCH.equals(action)
112 && current.voiceSearchSourceIsGoogle()) {
113 Intent logIntent = new Intent(
114 LoggingEvents.ACTION_LOG_EVENT);
115 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
116 LoggingEvents.VoiceSearch.QUERY_UPDATED);
117 logIntent.putExtra(
118 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
119 intent.getDataString());
120 mActivity.sendBroadcast(logIntent);
121 // Note, onPageStarted will revert the voice title bar
122 // When http://b/issue?id=2379215 is fixed, we should update
123 // the title bar here.
124 }
125 }
126 // If this was a search request (e.g. search query directly typed into the address bar),
127 // pass it on to the default web search provider.
128 if (handleWebSearchIntent(mActivity, mController, intent)) {
129 return;
130 }
131
132 UrlData urlData = getUrlDataFromIntent(intent);
133 if (urlData.isEmpty()) {
134 urlData = new UrlData(mSettings.getHomePage());
135 }
136
Michael Kolb14612442011-06-24 13:06:29 -0700137 if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)
138 || urlData.isPreloaded()) {
139 Tab t = mController.openTab(urlData);
Leon Scroggins2ed6e312011-02-22 16:37:23 -0500140 return;
141 }
John Reckdb22ec42011-06-29 11:31:24 -0700142 /*
143 * TODO: Don't allow javascript URIs
144 * 0) If this is a javascript: URI, *always* open a new tab
145 * 1) If this is a voice search, re-use tab for appId
146 * If there is no appId, use current tab
147 * 2) If the URL is already opened, switch to that tab
148 * 3-phone) Reuse tab with same appId
149 * 3-tablet) Open new tab
150 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 final String appId = intent
152 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
John Reck26b18322011-06-21 13:08:58 -0700153 if (!TextUtils.isEmpty(urlData.mUrl) &&
154 urlData.mUrl.startsWith("javascript:")) {
155 // Always open javascript: URIs in new tabs
156 mController.openTab(urlData);
157 return;
158 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 if ((Intent.ACTION_VIEW.equals(action)
160 // If a voice search has no appId, it means that it came
161 // from the browser. In that case, reuse the current tab.
162 || (activateVoiceSearch && appId != null))
John Reckdb22ec42011-06-29 11:31:24 -0700163 && !mActivity.getPackageName().equals(appId)) {
164 if (activateVoiceSearch || !BrowserActivity.isTablet(mActivity)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700165 Tab appTab = mTabControl.getTabFromAppId(appId);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800166 if (appTab != null) {
John Reck26b18322011-06-21 13:08:58 -0700167 mController.reuseTab(appTab, urlData);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800168 return;
Michael Kolbaf0d3342011-03-11 11:30:16 -0800169 }
John Reckdb22ec42011-06-29 11:31:24 -0700170 }
171 // No matching application tab, try to find a regular tab
172 // with a matching url.
173 Tab appTab = mTabControl.findTabWithUrl(urlData.mUrl);
174 if (appTab != null) {
175 // Transfer ownership
176 appTab.setAppId(appId);
177 if (current != appTab) {
178 mController.switchToTab(appTab);
179 }
180 // Otherwise, we are already viewing the correct tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700182 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
183 // will be opened in a new tab unless we have reached
184 // MAX_TABS. Then the url will be opened in the current
185 // tab. If a new tab is created, it will have "true" for
186 // exit on close.
187 Tab tab = mController.openTab(urlData);
188 if (tab != null) {
189 tab.setAppId(appId);
Michael Kolbe28b3472011-08-04 16:54:31 -0700190 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
191 tab.setCloseOnBack(true);
192 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 }
194 }
195 } else {
196 if (!urlData.isEmpty()
197 && urlData.mUrl.startsWith("about:debug")) {
198 if ("about:debug.dom".equals(urlData.mUrl)) {
199 current.getWebView().dumpDomTree(false);
200 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
201 current.getWebView().dumpDomTree(true);
202 } else if ("about:debug.render".equals(urlData.mUrl)) {
203 current.getWebView().dumpRenderTree(false);
204 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
205 current.getWebView().dumpRenderTree(true);
206 } else if ("about:debug.display".equals(urlData.mUrl)) {
207 current.getWebView().dumpDisplayTree();
Cary Clark4f4cb6b2011-01-18 13:03:42 -0500208 } else if ("about:debug.nav".equals(urlData.mUrl)) {
209 current.getWebView().debugDump();
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700211 mSettings.toggleDebugSettings();
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 }
213 return;
214 }
215 // Get rid of the subwindow if it exists
216 mController.dismissSubWindow(current);
217 // If the current Tab is being used as an application tab,
218 // remove the association, since the new Intent means that it is
219 // no longer associated with that application.
220 current.setAppId(null);
221 mController.loadUrlDataIn(current, urlData);
222 }
223 }
224 }
225
Michael Kolb14612442011-06-24 13:06:29 -0700226 protected static UrlData getUrlDataFromIntent(Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 String url = "";
228 Map<String, String> headers = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100229 PreloadedTabControl preloaded = null;
230 String preloadedSearchBoxQuery = null;
Leon Scrogginse1cab102011-01-04 10:50:13 -0500231 if (intent != null
232 && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 final String action = intent.getAction();
Jeff Hamiltonfd77aaa2011-06-17 16:19:33 -0500234 if (Intent.ACTION_VIEW.equals(action) ||
235 NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 url = UrlUtils.smartUrlFilter(intent.getData());
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 if (url != null && url.startsWith("http")) {
238 final Bundle pairs = intent
239 .getBundleExtra(Browser.EXTRA_HEADERS);
240 if (pairs != null && !pairs.isEmpty()) {
241 Iterator<String> iter = pairs.keySet().iterator();
242 headers = new HashMap<String, String>();
243 while (iter.hasNext()) {
244 String key = iter.next();
245 headers.put(key, pairs.getString(key));
246 }
247 }
248 }
Michael Kolb14612442011-06-24 13:06:29 -0700249 if (intent.hasExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID)) {
250 String id = intent.getStringExtra(PreloadRequestReceiver.EXTRA_PRELOAD_ID);
Mathew Inwood29721c22011-06-29 17:55:24 +0100251 preloadedSearchBoxQuery = intent.getStringExtra(
252 PreloadRequestReceiver.EXTRA_SEARCHBOX_SETQUERY);
Michael Kolb14612442011-06-24 13:06:29 -0700253 preloaded = Preloader.getInstance().getPreloadedTab(id);
254 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 } else if (Intent.ACTION_SEARCH.equals(action)
256 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
257 || Intent.ACTION_WEB_SEARCH.equals(action)) {
258 url = intent.getStringExtra(SearchManager.QUERY);
259 if (url != null) {
260 // In general, we shouldn't modify URL from Intent.
261 // But currently, we get the user-typed URL from search box as well.
262 url = UrlUtils.fixUrl(url);
263 url = UrlUtils.smartUrlFilter(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
265 if (url.contains(searchSource)) {
266 String source = null;
267 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
268 if (appData != null) {
269 source = appData.getString(Search.SOURCE);
270 }
271 if (TextUtils.isEmpty(source)) {
272 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
273 }
274 url = url.replace(searchSource, "&source=android-"+source+"&");
275 }
276 }
277 }
278 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100279 return new UrlData(url, headers, intent, preloaded, preloadedSearchBoxQuery);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 }
281
282 /**
283 * Launches the default web search activity with the query parameters if the given intent's data
284 * are identified as plain search terms and not URLs/shortcuts.
285 * @return true if the intent was handled and web search activity was launched, false if not.
286 */
287 static boolean handleWebSearchIntent(Activity activity,
288 Controller controller, Intent intent) {
289 if (intent == null) return false;
290
291 String url = null;
292 final String action = intent.getAction();
293 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
294 action)) {
295 return false;
296 }
297 if (Intent.ACTION_VIEW.equals(action)) {
298 Uri data = intent.getData();
299 if (data != null) url = data.toString();
300 } else if (Intent.ACTION_SEARCH.equals(action)
301 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
302 || Intent.ACTION_WEB_SEARCH.equals(action)) {
303 url = intent.getStringExtra(SearchManager.QUERY);
304 }
305 return handleWebSearchRequest(activity, controller, url,
306 intent.getBundleExtra(SearchManager.APP_DATA),
307 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
308 }
309
310 /**
311 * Launches the default web search activity with the query parameters if the given url string
312 * was identified as plain search terms and not URL/shortcut.
313 * @return true if the request was handled and web search activity was launched, false if not.
314 */
315 private static boolean handleWebSearchRequest(Activity activity,
316 Controller controller, String inUrl, Bundle appData,
317 String extraData) {
John Reck99141272010-12-21 11:34:12 -0800318 if (inUrl == null) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700319
320 // In general, we shouldn't modify URL from Intent.
321 // But currently, we get the user-typed URL from search box as well.
322 String url = UrlUtils.fixUrl(inUrl).trim();
John Reck99141272010-12-21 11:34:12 -0800323 if (TextUtils.isEmpty(url)) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700324
325 // URLs are handled by the regular flow of control, so
326 // return early.
327 if (Patterns.WEB_URL.matcher(url).matches()
328 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
329 return false;
330 }
331
332 final ContentResolver cr = activity.getContentResolver();
333 final String newUrl = url;
334 if (controller == null || controller.getTabControl() == null
335 || controller.getTabControl().getCurrentWebView() == null
336 || !controller.getTabControl().getCurrentWebView()
337 .isPrivateBrowsingEnabled()) {
338 new AsyncTask<Void, Void, Void>() {
339 @Override
340 protected Void doInBackground(Void... unused) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 Browser.addSearchUrl(cr, newUrl);
342 return null;
343 }
344 }.execute();
345 }
346
347 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
348 if (searchEngine == null) return false;
349 searchEngine.startSearch(activity, url, appData, extraData);
350
351 return true;
352 }
353
354 /**
355 * A UrlData class to abstract how the content will be set to WebView.
356 * This base class uses loadUrl to show the content.
357 */
358 static class UrlData {
359 final String mUrl;
360 final Map<String, String> mHeaders;
361 final Intent mVoiceIntent;
Mathew Inwood29721c22011-06-29 17:55:24 +0100362 final PreloadedTabControl mPreloadedTab;
363 final String mSearchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700364
365 UrlData(String url) {
366 this.mUrl = url;
367 this.mHeaders = null;
368 this.mVoiceIntent = null;
Michael Kolb14612442011-06-24 13:06:29 -0700369 this.mPreloadedTab = null;
Mathew Inwood29721c22011-06-29 17:55:24 +0100370 this.mSearchBoxQueryToSubmit = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 }
372
373 UrlData(String url, Map<String, String> headers, Intent intent) {
Mathew Inwood29721c22011-06-29 17:55:24 +0100374 this(url, headers, intent, null, null);
Michael Kolb14612442011-06-24 13:06:29 -0700375 }
376
Mathew Inwood29721c22011-06-29 17:55:24 +0100377 UrlData(String url, Map<String, String> headers, Intent intent,
378 PreloadedTabControl preloaded, String searchBoxQueryToSubmit) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 this.mUrl = url;
380 this.mHeaders = headers;
381 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
382 .equals(intent.getAction())) {
383 this.mVoiceIntent = intent;
384 } else {
385 this.mVoiceIntent = null;
386 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100387 this.mPreloadedTab = preloaded;
388 this.mSearchBoxQueryToSubmit = searchBoxQueryToSubmit;
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 }
390
391 boolean isEmpty() {
392 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
393 }
Michael Kolb14612442011-06-24 13:06:29 -0700394
395 boolean isPreloaded() {
396 return mPreloadedTab != null;
397 }
398
Mathew Inwood29721c22011-06-29 17:55:24 +0100399 PreloadedTabControl getPreloadedTab() {
Michael Kolb14612442011-06-24 13:06:29 -0700400 return mPreloadedTab;
401 }
Mathew Inwood29721c22011-06-29 17:55:24 +0100402
403 String getSearchBoxQueryToSubmit() {
404 return mSearchBoxQueryToSubmit;
405 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 }
407
408}