SearchableSources.java revision 5229b06f00d20aac76cd8519b37f2a579d61c54f
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.pm.PackageManager.NameNotFoundException; 24import android.provider.ContactsContract; 25import android.text.TextUtils; 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 protected Context getContext() { 60 return mContext; 61 } 62 63 protected SearchManager getSearchManager() { 64 return mSearchManager; 65 } 66 67 public Collection<Source> getSources() { 68 return mSources.values(); 69 } 70 71 public Source getSource(String name) { 72 return mSources.get(name); 73 } 74 75 public Source getWebSearchSource() { 76 return mWebSearchSource; 77 } 78 79 /** 80 * Updates the list of suggestion sources. 81 */ 82 public void update() { 83 if (DBG) Log.d(TAG, "update()"); 84 mSources = new HashMap<String,Source>(); 85 86 addSearchableSources(); 87 88 addInternalSources(); 89 90 mWebSearchSource = createWebSearchSource(); 91 if (mWebSearchSource != null) { 92 addSource(mWebSearchSource); 93 } 94 } 95 96 protected void addInternalSources() { 97 } 98 99 private void addSearchableSources() { 100 List<SearchableInfo> searchables = mSearchManager.getSearchablesInGlobalSearch(); 101 if (searchables == null) { 102 Log.e(TAG, "getSearchablesInGlobalSearch() returned null"); 103 return; 104 } 105 for (SearchableInfo searchable : searchables) { 106 SearchableSource source = createSearchableSource(searchable); 107 if (source != null) { 108 if (DBG) Log.d(TAG, "Created source " + source); 109 addSource(source); 110 } 111 } 112 } 113 114 protected void addSource(Source source) { 115 mSources.put(source.getName(), source); 116 } 117 118 protected Source createWebSearchSource() { 119 return QsbApplication.get(getContext()).getGoogleSource(); 120 } 121 122 protected boolean isSearchableContacts(SearchableInfo searchable) { 123 if (DBG) { 124 Log.d(TAG, "isSearchableContacts " + searchable.getSuggestAuthority() + " == " + 125 ContactsContract.AUTHORITY + " ?"); 126 } 127 return TextUtils.equals(ContactsContract.AUTHORITY, searchable.getSuggestAuthority()); 128 } 129 130 protected SearchableSource createSearchableSource(SearchableInfo searchable) { 131 if (searchable == null) return null; 132 try { 133 // special case for contacts which has a different suggestion view factory 134 if (isSearchableContacts(searchable)) { 135 return new ContactsSource(mContext, searchable); 136 } else { 137 return createDefaultSearchableSource(searchable); 138 } 139 } catch (NameNotFoundException ex) { 140 Log.e(TAG, "Source not found: " + ex); 141 return null; 142 } 143 } 144 145 protected SearchableSource createDefaultSearchableSource(SearchableInfo searchable) 146 throws NameNotFoundException { 147 return new SearchableSource(mContext, searchable); 148 } 149 150 public Source createSourceFor(ComponentName component) { 151 SearchableInfo info = mSearchManager.getSearchableInfo(component); 152 SearchableSource source = createSearchableSource(info); 153 if (DBG) Log.d(TAG, "SearchableSource for " + component + ": " + source); 154 return source; 155 } 156} 157