blob: b0eff63d7c2e6ffd0ef4d5bd009405622e8afef3 [file] [log] [blame]
Mathew Inwood29721c22011-06-29 17:55:24 +01001/*
2 * Copyright (C) 2011 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 */
16package com.android.browser;
17
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010018import android.app.PendingIntent;
Mathew Inwoode1dbb952011-07-08 17:27:38 +010019import android.net.Uri;
Mathew Inwood29721c22011-06-29 17:55:24 +010020import android.text.TextUtils;
21import android.util.Log;
22import android.webkit.SearchBox;
23
24import java.util.Map;
Mathew Inwooda829d552011-09-02 14:16:25 +010025import java.util.regex.Pattern;
Mathew Inwood29721c22011-06-29 17:55:24 +010026
27/**
28 * Class to manage the controlling of preloaded tab.
29 */
30public class PreloadedTabControl {
Mathew Inwoode1dbb952011-07-08 17:27:38 +010031 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Mathew Inwood29721c22011-06-29 17:55:24 +010032 private static final String LOGTAG = "PreloadedTabControl";
33
34 final Tab mTab;
35 private String mLastQuery;
36 private boolean mDestroyed;
37
38 public PreloadedTabControl(Tab t) {
Mathew Inwoode1dbb952011-07-08 17:27:38 +010039 if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.<init>");
Mathew Inwood29721c22011-06-29 17:55:24 +010040 mTab = t;
41 }
42
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010043 private boolean maybeSetQuery(final String query, SearchBox sb) {
Mathew Inwood29721c22011-06-29 17:55:24 +010044 if (!TextUtils.equals(mLastQuery, query)) {
45 if (sb != null) {
46 if (LOGD_ENABLED) Log.d(LOGTAG, "Changing searchbox query to " + query);
47 sb.setVerbatim(true);
48 sb.setQuery(query);
Mathew Inwoode1dbb952011-07-08 17:27:38 +010049 sb.onchange(new SearchBox.SearchBoxListener() {
50 @Override
51 public void onChangeComplete(boolean called) {
52 if (mDestroyed) return;
53 if (LOGD_ENABLED) Log.d(LOGTAG, "Changed searchbox query: " + called);
54 if (called) {
55 mLastQuery = query;
56 }
57 }
58 });
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010059 return true;
Mathew Inwood29721c22011-06-29 17:55:24 +010060 } else {
61 if (LOGD_ENABLED) Log.d(LOGTAG, "Cannot set query: no searchbox interface");
62 }
63 }
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010064 return false;
Mathew Inwood29721c22011-06-29 17:55:24 +010065 }
66
67 public void setQuery(String query) {
68 maybeSetQuery(query, mTab.getWebView().getSearchBox());
69 }
70
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010071 public boolean searchBoxSubmit(final String query, final String fallbackUrl,
72 final Map<String, String> fallbackHeaders, final PendingIntent onLoadCompleteIntent) {
Mathew Inwood29721c22011-06-29 17:55:24 +010073 final SearchBox sb = mTab.getWebView().getSearchBox();
74 if (sb == null) {
75 // no searchbox, cannot submit. Fallback to regular tab creation
76 if (LOGD_ENABLED) Log.d(LOGTAG, "No searchbox, cannot submit query");
77 return false;
78 }
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010079 final boolean newQuery = maybeSetQuery(query, sb);
Mathew Inwoode1dbb952011-07-08 17:27:38 +010080 if (LOGD_ENABLED) Log.d(LOGTAG, "Submitting query " + query);
Mathew Inwooda829d552011-09-02 14:16:25 +010081 final String currentUrl = mTab.getUrl();
Mathew Inwoode1dbb952011-07-08 17:27:38 +010082 sb.onsubmit(new SearchBox.SearchBoxListener() {
Mathew Inwood29721c22011-06-29 17:55:24 +010083 @Override
Mathew Inwoode1dbb952011-07-08 17:27:38 +010084 public void onSubmitComplete(boolean called) {
85 if (mDestroyed) return;
86 if (LOGD_ENABLED) Log.d(LOGTAG, "Query submitted: " + called);
87 if (!called) {
88 if (LOGD_ENABLED) Log.d(LOGTAG, "Query not submitted; falling back");
Mathew Inwood29721c22011-06-29 17:55:24 +010089 loadUrl(fallbackUrl, fallbackHeaders);
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010090
Mathew Inwood1dd8e822011-08-03 14:34:29 +010091 // make sure that the failed, preloaded URL is cleared from the back stack
Mathew Inwooda829d552011-09-02 14:16:25 +010092 mTab.clearBackStackWhenItemAdded(Pattern.compile(
93 "^" + Pattern.quote(fallbackUrl) + "$"));
Mathew Inwoodb4e831b2011-09-05 18:58:48 +010094 // When setting the search box query, preloadAttempted=true implies that the
95 // the query was prefetched using the searchbox API. This is the case if we
96 // the query is not new.
97 registerLoadCompleteListener(!newQuery, false, onLoadCompleteIntent);
Mathew Inwooda829d552011-09-02 14:16:25 +010098 } else {
99 // ignore the next fragment change, to avoid leaving a blank page in the browser
100 // after the query has been submitted.
101 String currentWithoutFragment = Uri.parse(currentUrl)
102 .buildUpon()
103 .fragment(null)
104 .toString();
105 mTab.clearBackStackWhenItemAdded(
106 Pattern.compile(
107 "^" +
108 Pattern.quote(currentWithoutFragment) +
109 "(\\#.*)?" +
110 "$"));
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100111 registerLoadCompleteListener(!newQuery, true, onLoadCompleteIntent);
Mathew Inwood29721c22011-06-29 17:55:24 +0100112 }
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100113 }});
Mathew Inwood29721c22011-06-29 17:55:24 +0100114 return true;
115 }
116
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100117 private void registerLoadCompleteListener(
118 final boolean queryPreloaded,
119 final boolean preloadSucceeded,
120 final PendingIntent pendingIntent) {
121 if (pendingIntent == null) {
122 return;
123 }
124 mTab.setOnPageLoadCompleteListener(null, new Tab.OnPageLoadCompleteListener(){
125 @Override
126 public void onPageLoadComplete() {
127 IntentHandler.sendPageLoadCompletePendingIntent(mTab.mContext, pendingIntent,
128 queryPreloaded, preloadSucceeded);
129 }});
130 }
131
Mathew Inwooddffa72f2011-08-10 12:37:43 +0100132 public void searchBoxCancel() {
133 SearchBox sb = mTab.getWebView().getSearchBox();
134 if (sb != null) {
135 mLastQuery = null;
136 sb.oncancel(new SearchBox.SearchBoxListener(){
137 @Override
138 public void onCancelComplete(boolean called) {
139 if (LOGD_ENABLED) Log.d(LOGTAG, "Query cancelled: " + called);
140 }
141 });
142 }
143 }
144
Mathew Inwood29721c22011-06-29 17:55:24 +0100145 public void loadUrlIfChanged(String url, Map<String, String> headers) {
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100146 String currentUrl = mTab.getUrl();
147 if (!TextUtils.isEmpty(currentUrl)) {
148 try {
149 // remove fragment:
150 currentUrl = Uri.parse(currentUrl).buildUpon().fragment(null).build().toString();
151 } catch (UnsupportedOperationException e) {
152 // carry on
153 }
154 }
155 if (LOGD_ENABLED) Log.d(LOGTAG, "loadUrlIfChanged\nnew: " + url + "\nold: " +currentUrl);
156 if (!TextUtils.equals(url, currentUrl)) {
Mathew Inwood29721c22011-06-29 17:55:24 +0100157 loadUrl(url, headers);
158 }
159 }
160
161 public void loadUrl(String url, Map<String, String> headers) {
162 if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url);
163 mTab.loadUrl(url, headers);
164 }
165
166 public void destroy() {
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100167 if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.destroy");
Mathew Inwood29721c22011-06-29 17:55:24 +0100168 mDestroyed = true;
169 mTab.destroy();
170 }
171
172 public Tab getTab() {
173 return mTab;
174 }
175
176}