Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package com.android.browser; |
| 19 | |
| 20 | import com.android.browser.search.SearchEngine; |
| 21 | import com.android.common.Search; |
| 22 | import com.android.common.speech.LoggingEvents; |
| 23 | |
| 24 | import android.app.Activity; |
| 25 | import android.app.SearchManager; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.net.Uri; |
Jeff Hamilton | fd77aaa | 2011-06-17 16:19:33 -0500 | [diff] [blame] | 30 | import android.nfc.NfcAdapter; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
| 32 | import android.os.Bundle; |
| 33 | import android.provider.Browser; |
| 34 | import android.provider.MediaStore; |
| 35 | import android.speech.RecognizerResultsIntent; |
| 36 | import android.text.TextUtils; |
| 37 | import android.util.Patterns; |
| 38 | |
| 39 | import java.util.HashMap; |
| 40 | import java.util.Iterator; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | /** |
| 44 | * Handle all browser related intents |
| 45 | */ |
| 46 | public 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 79 | } |
| 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 Reck | 439c9a5 | 2010-12-14 10:04:39 -0800 | [diff] [blame] | 87 | if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(action)) { |
| 88 | mController.bookmarksOrHistoryPicker(false); |
| 89 | return; |
| 90 | } |
John Reck | 07af2b8 | 2011-01-18 14:24:43 -0800 | [diff] [blame] | 91 | mController.removeComboView(); |
| 92 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 93 | // 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 Hamilton | fd77aaa | 2011-06-17 16:19:33 -0500 | [diff] [blame] | 99 | || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 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 | |
Leon Scroggins | 2ed6e31 | 2011-02-22 16:37:23 -0500 | [diff] [blame] | 138 | if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)) { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 139 | mController.openTab(urlData); |
Leon Scroggins | 2ed6e31 | 2011-02-22 16:37:23 -0500 | [diff] [blame] | 140 | return; |
| 141 | } |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame^] | 142 | /* |
| 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 151 | final String appId = intent |
| 152 | .getStringExtra(Browser.EXTRA_APPLICATION_ID); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 153 | 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 159 | 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 Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame^] | 163 | && !mActivity.getPackageName().equals(appId)) { |
| 164 | if (activateVoiceSearch || !BrowserActivity.isTablet(mActivity)) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 165 | Tab appTab = mTabControl.getTabFromAppId(appId); |
Michael Kolb | af0d334 | 2011-03-11 11:30:16 -0800 | [diff] [blame] | 166 | if (appTab != null) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 167 | mController.reuseTab(appTab, urlData); |
Michael Kolb | af0d334 | 2011-03-11 11:30:16 -0800 | [diff] [blame] | 168 | return; |
Michael Kolb | af0d334 | 2011-03-11 11:30:16 -0800 | [diff] [blame] | 169 | } |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame^] | 170 | } |
| 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 181 | } else { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame^] | 182 | // 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } else { |
| 193 | if (!urlData.isEmpty() |
| 194 | && urlData.mUrl.startsWith("about:debug")) { |
| 195 | if ("about:debug.dom".equals(urlData.mUrl)) { |
| 196 | current.getWebView().dumpDomTree(false); |
| 197 | } else if ("about:debug.dom.file".equals(urlData.mUrl)) { |
| 198 | current.getWebView().dumpDomTree(true); |
| 199 | } else if ("about:debug.render".equals(urlData.mUrl)) { |
| 200 | current.getWebView().dumpRenderTree(false); |
| 201 | } else if ("about:debug.render.file".equals(urlData.mUrl)) { |
| 202 | current.getWebView().dumpRenderTree(true); |
| 203 | } else if ("about:debug.display".equals(urlData.mUrl)) { |
| 204 | current.getWebView().dumpDisplayTree(); |
Cary Clark | 4f4cb6b | 2011-01-18 13:03:42 -0500 | [diff] [blame] | 205 | } else if ("about:debug.nav".equals(urlData.mUrl)) { |
| 206 | current.getWebView().debugDump(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 207 | } else { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 208 | mSettings.toggleDebugSettings(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 209 | } |
| 210 | return; |
| 211 | } |
| 212 | // Get rid of the subwindow if it exists |
| 213 | mController.dismissSubWindow(current); |
| 214 | // If the current Tab is being used as an application tab, |
| 215 | // remove the association, since the new Intent means that it is |
| 216 | // no longer associated with that application. |
| 217 | current.setAppId(null); |
| 218 | mController.loadUrlDataIn(current, urlData); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | protected UrlData getUrlDataFromIntent(Intent intent) { |
| 224 | String url = ""; |
| 225 | Map<String, String> headers = null; |
Leon Scroggins | e1cab10 | 2011-01-04 10:50:13 -0500 | [diff] [blame] | 226 | if (intent != null |
| 227 | && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 228 | final String action = intent.getAction(); |
Jeff Hamilton | fd77aaa | 2011-06-17 16:19:33 -0500 | [diff] [blame] | 229 | if (Intent.ACTION_VIEW.equals(action) || |
| 230 | NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 231 | url = UrlUtils.smartUrlFilter(intent.getData()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 232 | if (url != null && url.startsWith("http")) { |
| 233 | final Bundle pairs = intent |
| 234 | .getBundleExtra(Browser.EXTRA_HEADERS); |
| 235 | if (pairs != null && !pairs.isEmpty()) { |
| 236 | Iterator<String> iter = pairs.keySet().iterator(); |
| 237 | headers = new HashMap<String, String>(); |
| 238 | while (iter.hasNext()) { |
| 239 | String key = iter.next(); |
| 240 | headers.put(key, pairs.getString(key)); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } else if (Intent.ACTION_SEARCH.equals(action) |
| 245 | || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action) |
| 246 | || Intent.ACTION_WEB_SEARCH.equals(action)) { |
| 247 | url = intent.getStringExtra(SearchManager.QUERY); |
| 248 | if (url != null) { |
| 249 | // In general, we shouldn't modify URL from Intent. |
| 250 | // But currently, we get the user-typed URL from search box as well. |
| 251 | url = UrlUtils.fixUrl(url); |
| 252 | url = UrlUtils.smartUrlFilter(url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 253 | String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&"; |
| 254 | if (url.contains(searchSource)) { |
| 255 | String source = null; |
| 256 | final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA); |
| 257 | if (appData != null) { |
| 258 | source = appData.getString(Search.SOURCE); |
| 259 | } |
| 260 | if (TextUtils.isEmpty(source)) { |
| 261 | source = GOOGLE_SEARCH_SOURCE_UNKNOWN; |
| 262 | } |
| 263 | url = url.replace(searchSource, "&source=android-"+source+"&"); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return new UrlData(url, headers, intent); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Launches the default web search activity with the query parameters if the given intent's data |
| 273 | * are identified as plain search terms and not URLs/shortcuts. |
| 274 | * @return true if the intent was handled and web search activity was launched, false if not. |
| 275 | */ |
| 276 | static boolean handleWebSearchIntent(Activity activity, |
| 277 | Controller controller, Intent intent) { |
| 278 | if (intent == null) return false; |
| 279 | |
| 280 | String url = null; |
| 281 | final String action = intent.getAction(); |
| 282 | if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals( |
| 283 | action)) { |
| 284 | return false; |
| 285 | } |
| 286 | if (Intent.ACTION_VIEW.equals(action)) { |
| 287 | Uri data = intent.getData(); |
| 288 | if (data != null) url = data.toString(); |
| 289 | } else if (Intent.ACTION_SEARCH.equals(action) |
| 290 | || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action) |
| 291 | || Intent.ACTION_WEB_SEARCH.equals(action)) { |
| 292 | url = intent.getStringExtra(SearchManager.QUERY); |
| 293 | } |
| 294 | return handleWebSearchRequest(activity, controller, url, |
| 295 | intent.getBundleExtra(SearchManager.APP_DATA), |
| 296 | intent.getStringExtra(SearchManager.EXTRA_DATA_KEY)); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Launches the default web search activity with the query parameters if the given url string |
| 301 | * was identified as plain search terms and not URL/shortcut. |
| 302 | * @return true if the request was handled and web search activity was launched, false if not. |
| 303 | */ |
| 304 | private static boolean handleWebSearchRequest(Activity activity, |
| 305 | Controller controller, String inUrl, Bundle appData, |
| 306 | String extraData) { |
John Reck | 9914127 | 2010-12-21 11:34:12 -0800 | [diff] [blame] | 307 | if (inUrl == null) return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 308 | |
| 309 | // In general, we shouldn't modify URL from Intent. |
| 310 | // But currently, we get the user-typed URL from search box as well. |
| 311 | String url = UrlUtils.fixUrl(inUrl).trim(); |
John Reck | 9914127 | 2010-12-21 11:34:12 -0800 | [diff] [blame] | 312 | if (TextUtils.isEmpty(url)) return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 313 | |
| 314 | // URLs are handled by the regular flow of control, so |
| 315 | // return early. |
| 316 | if (Patterns.WEB_URL.matcher(url).matches() |
| 317 | || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) { |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | final ContentResolver cr = activity.getContentResolver(); |
| 322 | final String newUrl = url; |
| 323 | if (controller == null || controller.getTabControl() == null |
| 324 | || controller.getTabControl().getCurrentWebView() == null |
| 325 | || !controller.getTabControl().getCurrentWebView() |
| 326 | .isPrivateBrowsingEnabled()) { |
| 327 | new AsyncTask<Void, Void, Void>() { |
| 328 | @Override |
| 329 | protected Void doInBackground(Void... unused) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 330 | Browser.addSearchUrl(cr, newUrl); |
| 331 | return null; |
| 332 | } |
| 333 | }.execute(); |
| 334 | } |
| 335 | |
| 336 | SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine(); |
| 337 | if (searchEngine == null) return false; |
| 338 | searchEngine.startSearch(activity, url, appData, extraData); |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * A UrlData class to abstract how the content will be set to WebView. |
| 345 | * This base class uses loadUrl to show the content. |
| 346 | */ |
| 347 | static class UrlData { |
| 348 | final String mUrl; |
| 349 | final Map<String, String> mHeaders; |
| 350 | final Intent mVoiceIntent; |
| 351 | |
| 352 | UrlData(String url) { |
| 353 | this.mUrl = url; |
| 354 | this.mHeaders = null; |
| 355 | this.mVoiceIntent = null; |
| 356 | } |
| 357 | |
| 358 | UrlData(String url, Map<String, String> headers, Intent intent) { |
| 359 | this.mUrl = url; |
| 360 | this.mHeaders = headers; |
| 361 | if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS |
| 362 | .equals(intent.getAction())) { |
| 363 | this.mVoiceIntent = intent; |
| 364 | } else { |
| 365 | this.mVoiceIntent = null; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | boolean isEmpty() { |
| 370 | return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0); |
| 371 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | } |