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 package com.android.browser.search; 17 18 import com.android.browser.BrowserSettings; 19 import com.android.browser.InstantSearchEngine; 20 import com.android.browser.R; 21 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.text.TextUtils; 25 import android.util.Log; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 public class SearchEngines { 31 32 private static final String TAG = "SearchEngines"; 33 34 public static SearchEngine getDefaultSearchEngine(Context context) { 35 if (BrowserSettings.getInstance().useInstantSearch()) { 36 return new InstantSearchEngine(context, DefaultSearchEngine.create(context)); 37 } 38 39 return DefaultSearchEngine.create(context); 40 } 41 42 public static List<SearchEngineInfo> getSearchEngineInfos(Context context) { 43 ArrayList<SearchEngineInfo> searchEngineInfos = new ArrayList<SearchEngineInfo>(); 44 Resources res = context.getResources(); 45 String[] searchEngines = res.getStringArray(R.array.search_engines); 46 for (int i = 0; i < searchEngines.length; i++) { 47 String name = searchEngines[i]; 48 SearchEngineInfo info = new SearchEngineInfo(context, name); 49 searchEngineInfos.add(info); 50 } 51 return searchEngineInfos; 52 } 53 54 public static SearchEngine get(Context context, String name) { 55 // TODO: cache 56 SearchEngine defaultSearchEngine = getDefaultSearchEngine(context); 57 if (TextUtils.isEmpty(name) 58 || (defaultSearchEngine != null && name.equals(defaultSearchEngine.getName()))) { 59 return defaultSearchEngine; 60 } 61 SearchEngineInfo searchEngineInfo = getSearchEngineInfo(context, name); 62 if (searchEngineInfo == null) return defaultSearchEngine; 63 return new OpenSearchSearchEngine(context, searchEngineInfo); 64 } 65 66 public static SearchEngineInfo getSearchEngineInfo(Context context, String name) { 67 try { 68 return new SearchEngineInfo(context, name); 69 } catch (IllegalArgumentException exception) { 70 Log.e(TAG, "Cannot load search engine " + name, exception); 71 return null; 72 } 73 } 74 75 } 76