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