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