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 17 package com.android.quicksearchbox; 18 19 import com.android.quicksearchbox.util.NamedTaskExecutor; 20 21 import android.app.SearchManager; 22 import android.app.SearchableInfo; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.pm.PackageManager.NameNotFoundException; 26 import android.os.Handler; 27 import android.util.Log; 28 29 import java.util.Collection; 30 import java.util.HashMap; 31 import java.util.List; 32 33 /** 34 * Maintains a list of search sources. 35 */ 36 public class SearchableSources implements Sources { 37 38 // set to true to enable the more verbose debug logging for this file 39 private static final boolean DBG = false; 40 private static final String TAG = "QSB.SearchableSources"; 41 42 private final Context mContext; 43 private final SearchManager mSearchManager; 44 private final Handler mUiThread; 45 private final Config mConfig; 46 47 // All suggestion sources, by name. 48 private HashMap<String, Source> mSources; 49 50 // The web search source to use. 51 private Source mWebSearchSource; 52 53 private final NamedTaskExecutor mIconLoaderExecutor; 54 55 /** 56 * 57 * @param context Used for looking up source information etc. 58 */ SearchableSources(Context context, Handler uiThread, NamedTaskExecutor iconLoader, Config config)59 public SearchableSources(Context context, Handler uiThread, 60 NamedTaskExecutor iconLoader, Config config) { 61 mContext = context; 62 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); 63 mUiThread = uiThread; 64 mIconLoaderExecutor = iconLoader; 65 mConfig = config; 66 } 67 getContext()68 protected Context getContext() { 69 return mContext; 70 } 71 getUiThreadHandler()72 protected Handler getUiThreadHandler() { 73 return mUiThread; 74 } 75 getSearchManager()76 protected SearchManager getSearchManager() { 77 return mSearchManager; 78 } 79 getIconLoaderExecutor()80 protected NamedTaskExecutor getIconLoaderExecutor() { 81 return mIconLoaderExecutor; 82 } 83 getConfig()84 protected Config getConfig() { 85 return mConfig; 86 } 87 getSources()88 public Collection<Source> getSources() { 89 return mSources.values(); 90 } 91 getSource(String name)92 public Source getSource(String name) { 93 return mSources.get(name); 94 } 95 getWebSearchSource()96 public Source getWebSearchSource() { 97 return mWebSearchSource; 98 } 99 100 /** 101 * Updates the list of suggestion sources. 102 */ update()103 public void update() { 104 if (DBG) Log.d(TAG, "update()"); 105 mSources = new HashMap<String,Source>(); 106 107 addSearchableSources(); 108 109 addInternalSources(); 110 111 mWebSearchSource = createWebSearchSource(); 112 if (mWebSearchSource != null) { 113 addSource(mWebSearchSource); 114 } 115 } 116 addInternalSources()117 protected void addInternalSources() { 118 } 119 addSearchableSources()120 private void addSearchableSources() { 121 List<SearchableInfo> searchables = mSearchManager.getSearchablesInGlobalSearch(); 122 if (searchables == null) { 123 Log.e(TAG, "getSearchablesInGlobalSearch() returned null"); 124 return; 125 } 126 for (SearchableInfo searchable : searchables) { 127 SearchableSource source = createSearchableSource(searchable); 128 if (source != null) { 129 if (DBG) Log.d(TAG, "Created source " + source); 130 addSource(source); 131 } 132 } 133 } 134 addSource(Source source)135 protected void addSource(Source source) { 136 mSources.put(source.getName(), source); 137 } 138 createWebSearchSource()139 protected Source createWebSearchSource() { 140 return QsbApplication.get(getContext()).getGoogleSource(); 141 } 142 createSearchableSource(SearchableInfo searchable)143 protected SearchableSource createSearchableSource(SearchableInfo searchable) { 144 if (searchable == null) return null; 145 try { 146 return new SearchableSource(mContext, searchable, getUiThreadHandler(), 147 getIconLoaderExecutor()); 148 } catch (NameNotFoundException ex) { 149 Log.e(TAG, "Source not found: " + ex); 150 return null; 151 } 152 } 153 createSourceFor(ComponentName component)154 public Source createSourceFor(ComponentName component) { 155 SearchableInfo info = mSearchManager.getSearchableInfo(component); 156 SearchableSource source = createSearchableSource(info); 157 if (DBG) Log.d(TAG, "SearchableSource for " + component + ": " + source); 158 return source; 159 } 160 } 161