PreloadedTabControl.java revision 29721c2c6cc7f79a52962556c4431b71bb3ce46e
1/*
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
18import android.text.TextUtils;
19import android.util.Log;
20import android.webkit.SearchBox;
21
22import java.util.Map;
23
24/**
25 * Class to manage the controlling of preloaded tab.
26 */
27public class PreloadedTabControl {
28    private static final boolean LOGD_ENABLED = true;//com.android.browser.Browser.LOGD_ENABLED;
29    private static final String LOGTAG = "PreloadedTabControl";
30
31    final Tab mTab;
32    private String mLastQuery;
33    private boolean mDestroyed;
34
35    public PreloadedTabControl(Tab t) {
36        mTab = t;
37    }
38
39    private void maybeSetQuery(String query, SearchBox sb) {
40        if (!TextUtils.equals(mLastQuery, query)) {
41            if (sb != null) {
42                if (LOGD_ENABLED) Log.d(LOGTAG, "Changing searchbox query to " + query);
43                sb.setVerbatim(true);
44                sb.setQuery(query);
45                sb.onchange();
46                mLastQuery = query;
47            } else {
48                if (LOGD_ENABLED) Log.d(LOGTAG, "Cannot set query: no searchbox interface");
49            }
50        }
51    }
52
53    public void setQuery(String query) {
54        maybeSetQuery(query, mTab.getWebView().getSearchBox());
55    }
56
57    public boolean searchBoxSubmit(final String query,
58            final String fallbackUrl, final Map<String, String> fallbackHeaders) {
59        final SearchBox sb = mTab.getWebView().getSearchBox();
60        if (sb == null) {
61            // no searchbox, cannot submit. Fallback to regular tab creation
62            if (LOGD_ENABLED) Log.d(LOGTAG, "No searchbox, cannot submit query");
63            return false;
64        }
65        sb.isSupported(new SearchBox.IsSupportedCallback() {
66            @Override
67            public void searchBoxIsSupported(boolean supported) {
68                if (LOGD_ENABLED) Log.d(LOGTAG, "SearchBox supported: " + supported);
69                if (mDestroyed) {
70                    if (LOGD_ENABLED) Log.d(LOGTAG, "tab has been destroyed");
71                    return;
72                }
73                if (supported) {
74                    maybeSetQuery(query, sb);
75                    if (LOGD_ENABLED) Log.d(LOGTAG, "Submitting query " + query);
76                    sb.onsubmit();
77                } else {
78                    if (LOGD_ENABLED) Log.d(LOGTAG, "SearchBox not supported; falling back");
79                    loadUrl(fallbackUrl, fallbackHeaders);
80                }
81                mTab.getWebView().clearHistory();
82            }
83        });
84        return true;
85    }
86
87    public void loadUrlIfChanged(String url, Map<String, String> headers) {
88        if (!TextUtils.equals(url, mTab.getUrl())) {
89            loadUrl(url, headers);
90        }
91    }
92
93    public void loadUrl(String url, Map<String, String> headers) {
94        if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url);
95        mTab.loadUrl(url, headers);
96    }
97
98    public void destroy() {
99        mDestroyed = true;
100        mTab.destroy();
101    }
102
103    public Tab getTab() {
104        return mTab;
105    }
106
107}
108