• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.SearchManager;
20 import android.app.SearchableInfo;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.PackageManager.NameNotFoundException;
24 import android.util.Log;
25 
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
29 
30 /**
31  * Maintains a list of search sources.
32  */
33 public 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      */
SearchableSources(Context context)52     public SearchableSources(Context context) {
53         mContext = context;
54         mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
55     }
56 
getContext()57     protected Context getContext() {
58         return mContext;
59     }
60 
getSearchManager()61     protected SearchManager getSearchManager() {
62         return mSearchManager;
63     }
64 
getSources()65     public Collection<Source> getSources() {
66         return mSources.values();
67     }
68 
getSource(String name)69     public Source getSource(String name) {
70         return mSources.get(name);
71     }
72 
getWebSearchSource()73     public Source getWebSearchSource() {
74         return mWebSearchSource;
75     }
76 
77     /**
78      * Updates the list of suggestion sources.
79      */
update()80     public void update() {
81         if (DBG) Log.d(TAG, "update()");
82         mSources = new HashMap<String,Source>();
83 
84         addSearchableSources();
85 
86         mWebSearchSource = createWebSearchSource();
87         addSource(mWebSearchSource);
88     }
89 
addSearchableSources()90     private void addSearchableSources() {
91         List<SearchableInfo> searchables = mSearchManager.getSearchablesInGlobalSearch();
92         if (searchables == null) {
93             Log.e(TAG, "getSearchablesInGlobalSearch() returned null");
94             return;
95         }
96         for (SearchableInfo searchable : searchables) {
97             SearchableSource source = createSearchableSource(searchable);
98             if (source != null) {
99                 if (DBG) Log.d(TAG, "Created source " + source);
100                 addSource(source);
101             }
102         }
103     }
104 
addSource(Source source)105     private void addSource(Source source) {
106         mSources.put(source.getName(), source);
107     }
108 
createWebSearchSource()109     protected Source createWebSearchSource() {
110         return QsbApplication.get(getContext()).getGoogleSource();
111     }
112 
createSearchableSource(SearchableInfo searchable)113     protected SearchableSource createSearchableSource(SearchableInfo searchable) {
114         if (searchable == null) return null;
115         try {
116             return new SearchableSource(mContext, searchable);
117         } catch (NameNotFoundException ex) {
118             Log.e(TAG, "Source not found: " + ex);
119             return null;
120         }
121     }
122 
createSourceFor(ComponentName component)123     public Source createSourceFor(ComponentName component) {
124         SearchableInfo info = mSearchManager.getSearchableInfo(component);
125         SearchableSource source = createSearchableSource(info);
126         if (DBG) Log.d(TAG, "SearchableSource for " + component + ": " + source);
127         return source;
128     }
129 }
130