blob: 8d1b7840462c6f348c016a92b9b073dc6b015cf7 [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
20import com.android.browser.search.SearchEngine;
21import com.android.common.Search;
22import com.android.common.speech.LoggingEvents;
23
24import android.app.Activity;
25import android.app.SearchManager;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.net.Uri;
30import android.os.AsyncTask;
31import android.os.Bundle;
32import android.provider.Browser;
33import android.provider.MediaStore;
34import android.speech.RecognizerResultsIntent;
35import android.text.TextUtils;
36import android.util.Patterns;
37
38import 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)) {
87 mController.bookmarksOrHistoryPicker(false);
88 return;
89 }
John Reck07af2b82011-01-18 14:24:43 -080090 mController.removeComboView();
91
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)
98 || Intent.ACTION_SEARCH.equals(action)
99 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
100 || Intent.ACTION_WEB_SEARCH.equals(action)
101 || activateVoiceSearch) {
102 if (current.isInVoiceSearchMode()) {
103 String title = current.getVoiceDisplayTitle();
104 if (title != null && title.equals(intent.getStringExtra(
105 SearchManager.QUERY))) {
106 // The user submitted the same search as the last voice
107 // search, so do nothing.
108 return;
109 }
110 if (Intent.ACTION_SEARCH.equals(action)
111 && current.voiceSearchSourceIsGoogle()) {
112 Intent logIntent = new Intent(
113 LoggingEvents.ACTION_LOG_EVENT);
114 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
115 LoggingEvents.VoiceSearch.QUERY_UPDATED);
116 logIntent.putExtra(
117 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
118 intent.getDataString());
119 mActivity.sendBroadcast(logIntent);
120 // Note, onPageStarted will revert the voice title bar
121 // When http://b/issue?id=2379215 is fixed, we should update
122 // the title bar here.
123 }
124 }
125 // If this was a search request (e.g. search query directly typed into the address bar),
126 // pass it on to the default web search provider.
127 if (handleWebSearchIntent(mActivity, mController, intent)) {
128 return;
129 }
130
131 UrlData urlData = getUrlDataFromIntent(intent);
132 if (urlData.isEmpty()) {
133 urlData = new UrlData(mSettings.getHomePage());
134 }
135
Leon Scroggins2ed6e312011-02-22 16:37:23 -0500136 if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)) {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700137 mController.openTab(urlData);
Leon Scroggins2ed6e312011-02-22 16:37:23 -0500138 return;
139 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 final String appId = intent
141 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
142 if ((Intent.ACTION_VIEW.equals(action)
143 // If a voice search has no appId, it means that it came
144 // from the browser. In that case, reuse the current tab.
145 || (activateVoiceSearch && appId != null))
146 && !mActivity.getPackageName().equals(appId)
147 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Michael Kolbaf0d3342011-03-11 11:30:16 -0800148 if (activateVoiceSearch) {
Michael Kolbc831b632011-05-11 09:30:34 -0700149 Tab appTab = mTabControl.getTabFromAppId(appId);
Michael Kolbaf0d3342011-03-11 11:30:16 -0800150 if (appTab != null) {
151 mController.reuseTab(appTab, appId, urlData);
152 return;
153 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700154 Tab tab = mController.openTab(urlData);
155 if (tab != null) {
156 tab.setAppId(appId);
157 }
Michael Kolbaf0d3342011-03-11 11:30:16 -0800158 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 } else {
160 // No matching application tab, try to find a regular tab
161 // with a matching url.
Michael Kolbaf0d3342011-03-11 11:30:16 -0800162 Tab appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 if (appTab != null) {
164 if (current != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700165 mController.switchToTab(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 }
167 // Otherwise, we are already viewing the correct tab.
168 } else {
169 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
170 // will be opened in a new tab unless we have reached
171 // MAX_TABS. Then the url will be opened in the current
172 // tab. If a new tab is created, it will have "true" for
173 // exit on close.
Michael Kolb7bcafde2011-05-09 13:55:59 -0700174 Tab tab = mController.openTab(urlData);
175 if (tab != null) {
176 tab.setAppId(appId);
177 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 }
179 }
180 } else {
181 if (!urlData.isEmpty()
182 && urlData.mUrl.startsWith("about:debug")) {
183 if ("about:debug.dom".equals(urlData.mUrl)) {
184 current.getWebView().dumpDomTree(false);
185 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
186 current.getWebView().dumpDomTree(true);
187 } else if ("about:debug.render".equals(urlData.mUrl)) {
188 current.getWebView().dumpRenderTree(false);
189 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
190 current.getWebView().dumpRenderTree(true);
191 } else if ("about:debug.display".equals(urlData.mUrl)) {
192 current.getWebView().dumpDisplayTree();
Cary Clark4f4cb6b2011-01-18 13:03:42 -0500193 } else if ("about:debug.nav".equals(urlData.mUrl)) {
194 current.getWebView().debugDump();
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700196 mSettings.toggleDebugSettings();
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 }
198 return;
199 }
200 // Get rid of the subwindow if it exists
201 mController.dismissSubWindow(current);
202 // If the current Tab is being used as an application tab,
203 // remove the association, since the new Intent means that it is
204 // no longer associated with that application.
205 current.setAppId(null);
206 mController.loadUrlDataIn(current, urlData);
207 }
208 }
209 }
210
211 protected UrlData getUrlDataFromIntent(Intent intent) {
212 String url = "";
213 Map<String, String> headers = null;
Leon Scrogginse1cab102011-01-04 10:50:13 -0500214 if (intent != null
215 && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 final String action = intent.getAction();
217 if (Intent.ACTION_VIEW.equals(action)) {
218 url = UrlUtils.smartUrlFilter(intent.getData());
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 if (url != null && url.startsWith("http")) {
220 final Bundle pairs = intent
221 .getBundleExtra(Browser.EXTRA_HEADERS);
222 if (pairs != null && !pairs.isEmpty()) {
223 Iterator<String> iter = pairs.keySet().iterator();
224 headers = new HashMap<String, String>();
225 while (iter.hasNext()) {
226 String key = iter.next();
227 headers.put(key, pairs.getString(key));
228 }
229 }
230 }
231 } else if (Intent.ACTION_SEARCH.equals(action)
232 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
233 || Intent.ACTION_WEB_SEARCH.equals(action)) {
234 url = intent.getStringExtra(SearchManager.QUERY);
235 if (url != null) {
236 // In general, we shouldn't modify URL from Intent.
237 // But currently, we get the user-typed URL from search box as well.
238 url = UrlUtils.fixUrl(url);
239 url = UrlUtils.smartUrlFilter(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
241 if (url.contains(searchSource)) {
242 String source = null;
243 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
244 if (appData != null) {
245 source = appData.getString(Search.SOURCE);
246 }
247 if (TextUtils.isEmpty(source)) {
248 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
249 }
250 url = url.replace(searchSource, "&source=android-"+source+"&");
251 }
252 }
253 }
254 }
255 return new UrlData(url, headers, intent);
256 }
257
258 /**
259 * Launches the default web search activity with the query parameters if the given intent's data
260 * are identified as plain search terms and not URLs/shortcuts.
261 * @return true if the intent was handled and web search activity was launched, false if not.
262 */
263 static boolean handleWebSearchIntent(Activity activity,
264 Controller controller, Intent intent) {
265 if (intent == null) return false;
266
267 String url = null;
268 final String action = intent.getAction();
269 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
270 action)) {
271 return false;
272 }
273 if (Intent.ACTION_VIEW.equals(action)) {
274 Uri data = intent.getData();
275 if (data != null) url = data.toString();
276 } else if (Intent.ACTION_SEARCH.equals(action)
277 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
278 || Intent.ACTION_WEB_SEARCH.equals(action)) {
279 url = intent.getStringExtra(SearchManager.QUERY);
280 }
281 return handleWebSearchRequest(activity, controller, url,
282 intent.getBundleExtra(SearchManager.APP_DATA),
283 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
284 }
285
286 /**
287 * Launches the default web search activity with the query parameters if the given url string
288 * was identified as plain search terms and not URL/shortcut.
289 * @return true if the request was handled and web search activity was launched, false if not.
290 */
291 private static boolean handleWebSearchRequest(Activity activity,
292 Controller controller, String inUrl, Bundle appData,
293 String extraData) {
John Reck99141272010-12-21 11:34:12 -0800294 if (inUrl == null) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700295
296 // In general, we shouldn't modify URL from Intent.
297 // But currently, we get the user-typed URL from search box as well.
298 String url = UrlUtils.fixUrl(inUrl).trim();
John Reck99141272010-12-21 11:34:12 -0800299 if (TextUtils.isEmpty(url)) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700300
301 // URLs are handled by the regular flow of control, so
302 // return early.
303 if (Patterns.WEB_URL.matcher(url).matches()
304 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
305 return false;
306 }
307
308 final ContentResolver cr = activity.getContentResolver();
309 final String newUrl = url;
310 if (controller == null || controller.getTabControl() == null
311 || controller.getTabControl().getCurrentWebView() == null
312 || !controller.getTabControl().getCurrentWebView()
313 .isPrivateBrowsingEnabled()) {
314 new AsyncTask<Void, Void, Void>() {
315 @Override
316 protected Void doInBackground(Void... unused) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700317 Browser.addSearchUrl(cr, newUrl);
318 return null;
319 }
320 }.execute();
321 }
322
323 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
324 if (searchEngine == null) return false;
325 searchEngine.startSearch(activity, url, appData, extraData);
326
327 return true;
328 }
329
330 /**
331 * A UrlData class to abstract how the content will be set to WebView.
332 * This base class uses loadUrl to show the content.
333 */
334 static class UrlData {
335 final String mUrl;
336 final Map<String, String> mHeaders;
337 final Intent mVoiceIntent;
338
339 UrlData(String url) {
340 this.mUrl = url;
341 this.mHeaders = null;
342 this.mVoiceIntent = null;
343 }
344
345 UrlData(String url, Map<String, String> headers, Intent intent) {
346 this.mUrl = url;
347 this.mHeaders = headers;
348 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
349 .equals(intent.getAction())) {
350 this.mVoiceIntent = intent;
351 } else {
352 this.mVoiceIntent = null;
353 }
354 }
355
356 boolean isEmpty() {
357 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
358 }
359
360 /**
361 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
362 * the title bar as well.
363 */
364 public void loadIn(Tab t) {
365 if (mVoiceIntent != null) {
366 t.activateVoiceSearchMode(mVoiceIntent);
367 } else {
368 t.getWebView().loadUrl(mUrl, mHeaders);
369 }
370 }
371 }
372
373}