SearchBaseUrlHelper.java revision b1cdcbce5d9c5edb20309c59b7cb52f76bbfac72
1/* 2 * Copyright (C) 2010 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 */ 16 17package com.android.quicksearchbox.google; 18 19import com.android.quicksearchbox.R; 20import com.android.quicksearchbox.SearchSettings; 21import com.android.quicksearchbox.util.HttpHelper; 22 23import android.content.Context; 24import android.os.AsyncTask; 25import android.os.Handler; 26import android.util.Log; 27 28import java.util.Locale; 29 30/** 31 * Helper to build the base URL for all search requests. 32 */ 33public class SearchBaseUrlHelper { 34 private static final boolean DBG = false; 35 private static final String TAG = "QSB.SearchBaseUrlHelper"; 36 37 private static final String DOMAIN_CHECK_URL = 38 "https://www.google.com/searchdomaincheck?format=domain"; 39 40 private static final long SEARCH_BASE_URL_EXPIRY_MS = 24 * 3600 * 1000L; 41 42 private final HttpHelper mHttpHelper; 43 private final Context mContext; 44 private final SearchSettings mSearchSettings; 45 46 /** 47 * Note that this constructor will spawn a thread to issue a HTTP 48 * request if shouldUseGoogleCom is false. 49 */ 50 public SearchBaseUrlHelper(Context context, HttpHelper helper, 51 SearchSettings searchSettings) { 52 mHttpHelper = helper; 53 mContext = context; 54 mSearchSettings = searchSettings; 55 56 maybeUpdateBaseUrlSetting(false); 57 } 58 59 /** 60 * Update the base search url, either: 61 * (a) it has never been set (first run) 62 * (b) it has expired 63 * (c) if the caller forces an update by setting the "force" parameter. 64 * 65 * @param force if true, then the URL is reset whether or not it has 66 * expired. 67 */ 68 public void maybeUpdateBaseUrlSetting(boolean force) { 69 long lastUpdateTime = mSearchSettings.getSearchBaseUrlApplyTime(); 70 long currentTime = System.currentTimeMillis(); 71 72 if (force || lastUpdateTime == -1 || 73 currentTime - lastUpdateTime >= SEARCH_BASE_URL_EXPIRY_MS) { 74 if (mSearchSettings.shouldUseGoogleCom()) { 75 setSearchBaseUrl(getDefaultBaseUrl()); 76 } else { 77 checkSearchDomain(); 78 } 79 } 80 } 81 82 /** 83 * @return the base url for searches. 84 */ 85 public String getSearchBaseUrl() { 86 return mSearchSettings.getSearchBaseUrl(); 87 } 88 89 /** 90 * Issue a request to google.com/searchdomaincheck to retrieve the base 91 * URL for search requests. 92 */ 93 private void checkSearchDomain() { 94 final HttpHelper.GetRequest request = new HttpHelper.GetRequest(DOMAIN_CHECK_URL); 95 96 new AsyncTask<Void, Void, Void>() { 97 @Override 98 protected Void doInBackground(Void ... params) { 99 if (DBG) Log.d(TAG, "Starting request to /searchdomaincheck"); 100 String domain; 101 try { 102 domain = mHttpHelper.get(request); 103 } catch (Exception e) { 104 if (DBG) Log.d(TAG, "Request to /searchdomaincheck failed : " + e); 105 // Swallow any exceptions thrown by the HTTP helper, in 106 // this rare case, we just use the default URL. 107 setSearchBaseUrl(getDefaultBaseUrl()); 108 109 return null; 110 } 111 112 String searchDomain = mContext.getResources().getString( 113 R.string.google_search_base_pattern, domain, 114 GoogleSearch.getLanguage(Locale.getDefault())); 115 setSearchBaseUrl(searchDomain); 116 return null; 117 } 118 }.execute(); 119 } 120 121 private String getDefaultBaseUrl() { 122 return (mContext.getResources().getString( 123 R.string.google_search_base, 124 GoogleSearch.getLanguage(Locale.getDefault()))); 125 } 126 127 private void setSearchBaseUrl(String searchDomain) { 128 if (DBG) Log.d(TAG, "Setting search domain to : " + searchDomain); 129 130 mSearchSettings.setSearchBaseUrl(searchDomain); 131 } 132}