blob: 2a34aba049af2b4fbe62fdc2c07c10855bfbddc2 [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 }
90 if (BrowserActivity.ACTION_SHOW_BROWSER.equals(action)) {
91 mController.removeComboView();
92 return;
93 }
Michael Kolb8233fac2010-10-26 16:08:53 -070094 // In case the SearchDialog is open.
95 ((SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE))
96 .stopSearch();
97 boolean activateVoiceSearch = RecognizerResultsIntent
98 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
99 if (Intent.ACTION_VIEW.equals(action)
100 || 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
138 final String appId = intent
139 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
140 if ((Intent.ACTION_VIEW.equals(action)
141 // If a voice search has no appId, it means that it came
142 // from the browser. In that case, reuse the current tab.
143 || (activateVoiceSearch && appId != null))
144 && !mActivity.getPackageName().equals(appId)
145 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
146 Tab appTab = mTabControl.getTabFromId(appId);
147 if (appTab != null) {
148 mController.reuseTab(appTab, appId, urlData);
149 return;
150 } else {
151 // No matching application tab, try to find a regular tab
152 // with a matching url.
153 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
154 if (appTab != null) {
155 if (current != appTab) {
156 mController.switchToTab(mTabControl.getTabIndex(appTab));
157 }
158 // Otherwise, we are already viewing the correct tab.
159 } else {
160 // 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.
Michael Kolb18eb3772010-12-10 14:29:51 -0800165 mController.openTabAndShow(null, urlData, true, appId);
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 }
167 }
168 } else {
169 if (!urlData.isEmpty()
170 && urlData.mUrl.startsWith("about:debug")) {
171 if ("about:debug.dom".equals(urlData.mUrl)) {
172 current.getWebView().dumpDomTree(false);
173 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
174 current.getWebView().dumpDomTree(true);
175 } else if ("about:debug.render".equals(urlData.mUrl)) {
176 current.getWebView().dumpRenderTree(false);
177 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
178 current.getWebView().dumpRenderTree(true);
179 } else if ("about:debug.display".equals(urlData.mUrl)) {
180 current.getWebView().dumpDisplayTree();
Cary Clark4f4cb6b2011-01-18 13:03:42 -0500181 } else if ("about:debug.nav".equals(urlData.mUrl)) {
182 current.getWebView().debugDump();
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 } else {
184 mSettings.toggleDebugSettings();
185 }
186 return;
187 }
188 // Get rid of the subwindow if it exists
189 mController.dismissSubWindow(current);
190 // If the current Tab is being used as an application tab,
191 // remove the association, since the new Intent means that it is
192 // no longer associated with that application.
193 current.setAppId(null);
194 mController.loadUrlDataIn(current, urlData);
195 }
196 }
197 }
198
199 protected UrlData getUrlDataFromIntent(Intent intent) {
200 String url = "";
201 Map<String, String> headers = null;
Leon Scrogginse1cab102011-01-04 10:50:13 -0500202 if (intent != null
203 && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 final String action = intent.getAction();
205 if (Intent.ACTION_VIEW.equals(action)) {
206 url = UrlUtils.smartUrlFilter(intent.getData());
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 if (url != null && url.startsWith("http")) {
208 final Bundle pairs = intent
209 .getBundleExtra(Browser.EXTRA_HEADERS);
210 if (pairs != null && !pairs.isEmpty()) {
211 Iterator<String> iter = pairs.keySet().iterator();
212 headers = new HashMap<String, String>();
213 while (iter.hasNext()) {
214 String key = iter.next();
215 headers.put(key, pairs.getString(key));
216 }
217 }
218 }
219 } else if (Intent.ACTION_SEARCH.equals(action)
220 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
221 || Intent.ACTION_WEB_SEARCH.equals(action)) {
222 url = intent.getStringExtra(SearchManager.QUERY);
223 if (url != null) {
224 // In general, we shouldn't modify URL from Intent.
225 // But currently, we get the user-typed URL from search box as well.
226 url = UrlUtils.fixUrl(url);
227 url = UrlUtils.smartUrlFilter(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
229 if (url.contains(searchSource)) {
230 String source = null;
231 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
232 if (appData != null) {
233 source = appData.getString(Search.SOURCE);
234 }
235 if (TextUtils.isEmpty(source)) {
236 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
237 }
238 url = url.replace(searchSource, "&source=android-"+source+"&");
239 }
240 }
241 }
242 }
243 return new UrlData(url, headers, intent);
244 }
245
246 /**
247 * Launches the default web search activity with the query parameters if the given intent's data
248 * are identified as plain search terms and not URLs/shortcuts.
249 * @return true if the intent was handled and web search activity was launched, false if not.
250 */
251 static boolean handleWebSearchIntent(Activity activity,
252 Controller controller, Intent intent) {
253 if (intent == null) return false;
254
255 String url = null;
256 final String action = intent.getAction();
257 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
258 action)) {
259 return false;
260 }
261 if (Intent.ACTION_VIEW.equals(action)) {
262 Uri data = intent.getData();
263 if (data != null) url = data.toString();
264 } else if (Intent.ACTION_SEARCH.equals(action)
265 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
266 || Intent.ACTION_WEB_SEARCH.equals(action)) {
267 url = intent.getStringExtra(SearchManager.QUERY);
268 }
269 return handleWebSearchRequest(activity, controller, url,
270 intent.getBundleExtra(SearchManager.APP_DATA),
271 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
272 }
273
274 /**
275 * Launches the default web search activity with the query parameters if the given url string
276 * was identified as plain search terms and not URL/shortcut.
277 * @return true if the request was handled and web search activity was launched, false if not.
278 */
279 private static boolean handleWebSearchRequest(Activity activity,
280 Controller controller, String inUrl, Bundle appData,
281 String extraData) {
John Reck99141272010-12-21 11:34:12 -0800282 if (inUrl == null) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700283
284 // In general, we shouldn't modify URL from Intent.
285 // But currently, we get the user-typed URL from search box as well.
286 String url = UrlUtils.fixUrl(inUrl).trim();
John Reck99141272010-12-21 11:34:12 -0800287 if (TextUtils.isEmpty(url)) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
289 // URLs are handled by the regular flow of control, so
290 // return early.
291 if (Patterns.WEB_URL.matcher(url).matches()
292 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
293 return false;
294 }
295
296 final ContentResolver cr = activity.getContentResolver();
297 final String newUrl = url;
298 if (controller == null || controller.getTabControl() == null
299 || controller.getTabControl().getCurrentWebView() == null
300 || !controller.getTabControl().getCurrentWebView()
301 .isPrivateBrowsingEnabled()) {
302 new AsyncTask<Void, Void, Void>() {
303 @Override
304 protected Void doInBackground(Void... unused) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700305 Browser.addSearchUrl(cr, newUrl);
306 return null;
307 }
308 }.execute();
309 }
310
311 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
312 if (searchEngine == null) return false;
313 searchEngine.startSearch(activity, url, appData, extraData);
314
315 return true;
316 }
317
318 /**
319 * A UrlData class to abstract how the content will be set to WebView.
320 * This base class uses loadUrl to show the content.
321 */
322 static class UrlData {
323 final String mUrl;
324 final Map<String, String> mHeaders;
325 final Intent mVoiceIntent;
326
327 UrlData(String url) {
328 this.mUrl = url;
329 this.mHeaders = null;
330 this.mVoiceIntent = null;
331 }
332
333 UrlData(String url, Map<String, String> headers, Intent intent) {
334 this.mUrl = url;
335 this.mHeaders = headers;
336 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
337 .equals(intent.getAction())) {
338 this.mVoiceIntent = intent;
339 } else {
340 this.mVoiceIntent = null;
341 }
342 }
343
344 boolean isEmpty() {
345 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
346 }
347
348 /**
349 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
350 * the title bar as well.
351 */
352 public void loadIn(Tab t) {
353 if (mVoiceIntent != null) {
354 t.activateVoiceSearchMode(mVoiceIntent);
355 } else {
356 t.getWebView().loadUrl(mUrl, mHeaders);
357 }
358 }
359 }
360
361}