SearchableSources.java revision 21bff9bbf4286907b01d3153bff2fbd6b5ec5df8
1/* 2 * Copyright (C) 2009 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; 18 19import android.app.SearchManager; 20import android.app.SearchableInfo; 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.Intent; 24import android.content.pm.PackageManager; 25import android.content.pm.PackageManager.NameNotFoundException; 26import android.util.Log; 27 28import java.util.Collection; 29import java.util.HashMap; 30import java.util.List; 31 32/** 33 * Maintains a list of search sources. 34 */ 35public class SearchableSources implements Sources { 36 37 // set to true to enable the more verbose debug logging for this file 38 private static final boolean DBG = false; 39 private static final String TAG = "QSB.SearchableSources"; 40 41 private final Context mContext; 42 private final SearchManager mSearchManager; 43 44 // All suggestion sources, by name. 45 private HashMap<String, Source> mSources; 46 47 // The web search source to use. 48 private Source mWebSearchSource; 49 50 /** 51 * 52 * @param context Used for looking up source information etc. 53 */ 54 public SearchableSources(Context context) { 55 mContext = context; 56 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); 57 } 58 59 public Collection<Source> getSources() { 60 return mSources.values(); 61 } 62 63 public Source getSource(String name) { 64 return mSources.get(name); 65 } 66 67 public Source getWebSearchSource() { 68 return mWebSearchSource; 69 } 70 71 /** 72 * Updates the list of suggestion sources. 73 */ 74 public void update() { 75 if (DBG) Log.d(TAG, "update()"); 76 mSources = new HashMap<String,Source>(); 77 78 addSearchableSources(); 79 80 mWebSearchSource = createWebSearchSource(); 81 addSource(mWebSearchSource); 82 } 83 84 private void addSearchableSources() { 85 List<SearchableInfo> searchables = mSearchManager.getSearchablesInGlobalSearch(); 86 if (searchables == null) { 87 Log.e(TAG, "getSearchablesInGlobalSearch() returned null"); 88 return; 89 } 90 for (SearchableInfo searchable : searchables) { 91 SearchableSource source = createSearchableSource(searchable); 92 if (source != null) { 93 if (DBG) Log.d(TAG, "Created source " + source); 94 addSource(source); 95 } 96 } 97 } 98 99 private void addSource(Source source) { 100 mSources.put(source.getName(), source); 101 } 102 103 private Source createWebSearchSource() { 104 ComponentName name = getWebSearchComponent(); 105 SearchableInfo webSearchable = mSearchManager.getSearchableInfo(name); 106 if (webSearchable == null) { 107 Log.e(TAG, "Web search source " + name + " is not searchable."); 108 return null; 109 } 110 return createSearchableSource(webSearchable); 111 } 112 113 private ComponentName getWebSearchComponent() { 114 // Looks for an activity in the current package that handles ACTION_WEB_SEARCH. 115 // This indirect method is used to allow easy replacement of the web 116 // search activity when extending this package. 117 Intent webSearchIntent = new Intent(Intent.ACTION_WEB_SEARCH); 118 webSearchIntent.setPackage(mContext.getPackageName()); 119 PackageManager pm = mContext.getPackageManager(); 120 return webSearchIntent.resolveActivity(pm); 121 } 122 123 private SearchableSource createSearchableSource(SearchableInfo searchable) { 124 if (searchable == null) return null; 125 try { 126 return new SearchableSource(mContext, searchable); 127 } catch (NameNotFoundException ex) { 128 Log.e(TAG, "Source not found: " + ex); 129 return null; 130 } 131 } 132} 133