PreloadedTabControl.java revision dffa72f926d051c089981adf52a44d2c933a0de4
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 Inwoodimport android.webkit.SearchBox; 2229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 2329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwoodimport java.util.Map; 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 41e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private void maybeSetQuery(final String query, SearchBox sb) { 4229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (!TextUtils.equals(mLastQuery, query)) { 4329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (sb != null) { 4429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Changing searchbox query to " + query); 4529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood sb.setVerbatim(true); 4629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood sb.setQuery(query); 47e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood sb.onchange(new SearchBox.SearchBoxListener() { 48e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood @Override 49e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onChangeComplete(boolean called) { 50e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (mDestroyed) return; 51e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Changed searchbox query: " + called); 52e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (called) { 53e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood mLastQuery = query; 54e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 55e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 56e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood }); 5729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } else { 5829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Cannot set query: no searchbox interface"); 5929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 6029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 6129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 6229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 6329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public void setQuery(String query) { 6429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood maybeSetQuery(query, mTab.getWebView().getSearchBox()); 6529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 6629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 6729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public boolean searchBoxSubmit(final String query, 6829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood final String fallbackUrl, final Map<String, String> fallbackHeaders) { 6929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood final SearchBox sb = mTab.getWebView().getSearchBox(); 7029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (sb == null) { 7129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood // no searchbox, cannot submit. Fallback to regular tab creation 7229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "No searchbox, cannot submit query"); 7329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood return false; 7429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 75e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood maybeSetQuery(query, sb); 76e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Submitting query " + query); 77e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood sb.onsubmit(new SearchBox.SearchBoxListener() { 7829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood @Override 79e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onSubmitComplete(boolean called) { 80e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (mDestroyed) return; 81e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Query submitted: " + called); 82e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (!called) { 83e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Query not submitted; falling back"); 8429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood loadUrl(fallbackUrl, fallbackHeaders); 851dd8e82b74b1055fcd572e820a724997550edd33Mathew Inwood // make sure that the failed, preloaded URL is cleared from the back stack 861dd8e82b74b1055fcd572e820a724997550edd33Mathew Inwood mTab.clearBackStackWhenItemAdded(fallbackUrl); 8729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 88e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood }}); 8929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood return true; 9029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 9129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 92dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood public void searchBoxCancel() { 93dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood SearchBox sb = mTab.getWebView().getSearchBox(); 94dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood if (sb != null) { 95dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood mLastQuery = null; 96dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood sb.oncancel(new SearchBox.SearchBoxListener(){ 97dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood @Override 98dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood public void onCancelComplete(boolean called) { 99dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Query cancelled: " + called); 100dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood } 101dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood }); 102dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood } 103dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood } 104dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood 10529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public void loadUrlIfChanged(String url, Map<String, String> headers) { 106e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood String currentUrl = mTab.getUrl(); 107e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (!TextUtils.isEmpty(currentUrl)) { 108e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood try { 109e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood // remove fragment: 110e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood currentUrl = Uri.parse(currentUrl).buildUpon().fragment(null).build().toString(); 111e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } catch (UnsupportedOperationException e) { 112e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood // carry on 113e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 114e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 115e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "loadUrlIfChanged\nnew: " + url + "\nold: " +currentUrl); 116e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (!TextUtils.equals(url, currentUrl)) { 11729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood loadUrl(url, headers); 11829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 11929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 12029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 12129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public void loadUrl(String url, Map<String, String> headers) { 12229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url); 12329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood mTab.loadUrl(url, headers); 12429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 12529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 12629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public void destroy() { 127e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "PreloadedTabControl.destroy"); 12829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood mDestroyed = true; 12929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood mTab.destroy(); 13029721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 13129721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 13229721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood public Tab getTab() { 13329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood return mTab; 13429721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood } 13529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood 13629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood} 137