SearchableSources.java revision 25cbd7d054b92df1fe857d155ccc12f8191c12cf
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        mWebSearchSource = createWebSearchSource();
87        addSource(mWebSearchSource);
88    }
89
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
105    private void addSource(Source source) {
106        mSources.put(source.getName(), source);
107    }
108
109    protected Source createWebSearchSource() {
110        return QsbApplication.get(getContext()).getGoogleSource();
111    }
112
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
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