blob: d2482a474bb10085898016dd7f73111dbeec7135 [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 Inwoode1dbb952011-07-08 17:27:38 +010018import android.net.Uri;
Mathew Inwood29721c22011-06-29 17:55:24 +010019import android.text.TextUtils;
20import android.util.Log;
21import android.webkit.SearchBox;
22
23import java.util.Map;
24
25/**
26 * Class to manage the controlling of preloaded tab.
27 */
28public class PreloadedTabControl {
Mathew Inwoode1dbb952011-07-08 17:27:38 +010029 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Mathew Inwood29721c22011-06-29 17:55:24 +010030 private static final String LOGTAG = "PreloadedTabControl";
31
32 final Tab mTab;
33 private String mLastQuery;
34 private boolean mDestroyed;
35
36 public PreloadedTabControl(Tab t) {
Mathew Inwoode1dbb952011-07-08 17:27:38 +010037 if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.<init>");
Mathew Inwood29721c22011-06-29 17:55:24 +010038 mTab = t;
39 }
40
Mathew Inwoode1dbb952011-07-08 17:27:38 +010041 private void maybeSetQuery(final String query, SearchBox sb) {
Mathew Inwood29721c22011-06-29 17:55:24 +010042 if (!TextUtils.equals(mLastQuery, query)) {
43 if (sb != null) {
44 if (LOGD_ENABLED) Log.d(LOGTAG, "Changing searchbox query to " + query);
45 sb.setVerbatim(true);
46 sb.setQuery(query);
Mathew Inwoode1dbb952011-07-08 17:27:38 +010047 sb.onchange(new SearchBox.SearchBoxListener() {
48 @Override
49 public void onChangeComplete(boolean called) {
50 if (mDestroyed) return;
51 if (LOGD_ENABLED) Log.d(LOGTAG, "Changed searchbox query: " + called);
52 if (called) {
53 mLastQuery = query;
54 }
55 }
56 });
Mathew Inwood29721c22011-06-29 17:55:24 +010057 } else {
58 if (LOGD_ENABLED) Log.d(LOGTAG, "Cannot set query: no searchbox interface");
59 }
60 }
61 }
62
63 public void setQuery(String query) {
64 maybeSetQuery(query, mTab.getWebView().getSearchBox());
65 }
66
67 public boolean searchBoxSubmit(final String query,
68 final String fallbackUrl, final Map<String, String> fallbackHeaders) {
69 final SearchBox sb = mTab.getWebView().getSearchBox();
70 if (sb == null) {
71 // no searchbox, cannot submit. Fallback to regular tab creation
72 if (LOGD_ENABLED) Log.d(LOGTAG, "No searchbox, cannot submit query");
73 return false;
74 }
Mathew Inwoode1dbb952011-07-08 17:27:38 +010075 maybeSetQuery(query, sb);
76 if (LOGD_ENABLED) Log.d(LOGTAG, "Submitting query " + query);
77 sb.onsubmit(new SearchBox.SearchBoxListener() {
Mathew Inwood29721c22011-06-29 17:55:24 +010078 @Override
Mathew Inwoode1dbb952011-07-08 17:27:38 +010079 public void onSubmitComplete(boolean called) {
80 if (mDestroyed) return;
81 if (LOGD_ENABLED) Log.d(LOGTAG, "Query submitted: " + called);
82 if (!called) {
83 if (LOGD_ENABLED) Log.d(LOGTAG, "Query not submitted; falling back");
Mathew Inwood29721c22011-06-29 17:55:24 +010084 loadUrl(fallbackUrl, fallbackHeaders);
85 }
86 mTab.getWebView().clearHistory();
Mathew Inwoode1dbb952011-07-08 17:27:38 +010087 }});
Mathew Inwood29721c22011-06-29 17:55:24 +010088 return true;
89 }
90
91 public void loadUrlIfChanged(String url, Map<String, String> headers) {
Mathew Inwoode1dbb952011-07-08 17:27:38 +010092 String currentUrl = mTab.getUrl();
93 if (!TextUtils.isEmpty(currentUrl)) {
94 try {
95 // remove fragment:
96 currentUrl = Uri.parse(currentUrl).buildUpon().fragment(null).build().toString();
97 } catch (UnsupportedOperationException e) {
98 // carry on
99 }
100 }
101 if (LOGD_ENABLED) Log.d(LOGTAG, "loadUrlIfChanged\nnew: " + url + "\nold: " +currentUrl);
102 if (!TextUtils.equals(url, currentUrl)) {
Mathew Inwood29721c22011-06-29 17:55:24 +0100103 loadUrl(url, headers);
104 }
105 }
106
107 public void loadUrl(String url, Map<String, String> headers) {
108 if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url);
109 mTab.loadUrl(url, headers);
110 }
111
112 public void destroy() {
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100113 if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.destroy");
Mathew Inwood29721c22011-06-29 17:55:24 +0100114 mDestroyed = true;
115 mTab.destroy();
116 }
117
118 public Tab getTab() {
119 return mTab;
120 }
121
122}