129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood/*
229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * Copyright (C) 2011 The Android Open Source Project
329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood *
429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * Licensed under the Apache License, Version 2.0 (the "License");
529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * you may not use this file except in compliance with the License.
629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * You may obtain a copy of the License at
729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood *
829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood *      http://www.apache.org/licenses/LICENSE-2.0
929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood *
1029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * Unless required by applicable law or agreed to in writing, software
1129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * distributed under the License is distributed on an "AS IS" BASIS,
1229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * See the License for the specific language governing permissions and
1429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * limitations under the License.
1529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood */
1629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodpackage com.android.browser;
1729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
18e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodimport android.net.Uri;
1929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodimport android.text.TextUtils;
2029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodimport android.util.Log;
2129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
2229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodimport java.util.Map;
23a829d55e8af65d08d998b8166e42420dda8cf2ffMathew Inwoodimport java.util.regex.Pattern;
2429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
2529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood/**
2629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood * Class to manage the controlling of preloaded tab.
2729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood */
2829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodpublic class PreloadedTabControl {
29e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood    private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
3029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    private static final String LOGTAG = "PreloadedTabControl";
3129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
3229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    final Tab mTab;
3329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    private String mLastQuery;
3429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    private boolean mDestroyed;
3529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
3629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public PreloadedTabControl(Tab t) {
37e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.<init>");
3829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        mTab = t;
3929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
4029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
4129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public void setQuery(String query) {
4270c7844ff0156926dfa58896c911de71982bd638Selim Gurun        if (LOGD_ENABLED) Log.d(LOGTAG, "Cannot set query: no searchbox interface");
4329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
4429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
459ad1eac377ffa6ffd34afae4c6580b7ba7508c31Mathew Inwood    public boolean searchBoxSubmit(final String query,
469ad1eac377ffa6ffd34afae4c6580b7ba7508c31Mathew Inwood            final String fallbackUrl, final Map<String, String> fallbackHeaders) {
4770c7844ff0156926dfa58896c911de71982bd638Selim Gurun        return false;
4829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
4929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
50dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood    public void searchBoxCancel() {
51dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood    }
52dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood
5329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public void loadUrlIfChanged(String url, Map<String, String> headers) {
54e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        String currentUrl = mTab.getUrl();
55e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        if (!TextUtils.isEmpty(currentUrl)) {
56e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood            try {
57e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood                // remove fragment:
58e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood                currentUrl = Uri.parse(currentUrl).buildUpon().fragment(null).build().toString();
59e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood            } catch (UnsupportedOperationException e) {
60e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood                // carry on
61e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood            }
62e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        }
63e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        if (LOGD_ENABLED) Log.d(LOGTAG, "loadUrlIfChanged\nnew: " + url + "\nold: " +currentUrl);
64e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        if (!TextUtils.equals(url, currentUrl)) {
6529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood            loadUrl(url, headers);
6629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        }
6729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
6829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
6929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public void loadUrl(String url, Map<String, String> headers) {
7029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url);
7129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        mTab.loadUrl(url, headers);
7229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
7329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
7429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public void destroy() {
75e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood        if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.destroy");
7629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        mDestroyed = true;
7729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        mTab.destroy();
7829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
7929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
8029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    public Tab getTab() {
8129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood        return mTab;
8229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    }
8329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood
8429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood}
85