SearchManagerService.java revision ab5d96c5daf4bcc9b7a0cde44357454a11a8e48a
1/*
2 * Copyright (C) 2007 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 android.server.search;
18
19import com.android.internal.content.PackageMonitor;
20
21import android.app.ISearchManager;
22import android.app.SearchManager;
23import android.app.SearchableInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.util.Log;
28
29import java.util.List;
30
31/**
32 * The search manager service handles the search UI, and maintains a registry of searchable
33 * activities.
34 */
35public class SearchManagerService extends ISearchManager.Stub {
36
37    // general debugging support
38    private static final String TAG = "SearchManagerService";
39
40    // Context that the service is running in.
41    private final Context mContext;
42
43    // This field is initialized lazily in getSearchables(), and then never modified.
44    private Searchables mSearchables;
45
46    /**
47     * Initializes the Search Manager service in the provided system context.
48     * Only one instance of this object should be created!
49     *
50     * @param context to use for accessing DB, window manager, etc.
51     */
52    public SearchManagerService(Context context)  {
53        mContext = context;
54    }
55
56    private synchronized Searchables getSearchables() {
57        if (mSearchables == null) {
58            mSearchables = new Searchables(mContext);
59            mSearchables.buildSearchableList();
60            new MyPackageMonitor().register(mContext, true);
61        }
62        return mSearchables;
63    }
64
65    /**
66     * Refreshes the "searchables" list when packages are added/removed.
67     */
68    class MyPackageMonitor extends PackageMonitor {
69        @Override
70        public void onSomePackagesChanged() {
71            // Update list of searchable activities
72            getSearchables().buildSearchableList();
73            // Inform all listeners that the list of searchables has been updated.
74            Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
75            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
76            mContext.sendBroadcast(intent);
77        }
78    }
79
80    //
81    // Searchable activities API
82    //
83
84    /**
85     * Returns the SearchableInfo for a given activity.
86     *
87     * @param launchActivity The activity from which we're launching this search.
88     * @return Returns a SearchableInfo record describing the parameters of the search,
89     * or null if no searchable metadata was available.
90     */
91    public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
92        if (launchActivity == null) {
93            Log.e(TAG, "getSearchableInfo(), activity == null");
94            return null;
95        }
96        return getSearchables().getSearchableInfo(launchActivity);
97    }
98
99    /**
100     * Returns a list of the searchable activities that can be included in global search.
101     */
102    public List<SearchableInfo> getSearchablesInGlobalSearch() {
103        return getSearchables().getSearchablesInGlobalSearchList();
104    }
105
106    /**
107     * Gets the name of the global search activity.
108     */
109    public ComponentName getGlobalSearchActivity() {
110        return getSearchables().getGlobalSearchActivity();
111    }
112
113    /**
114     * Gets the name of the web search activity.
115     */
116    public ComponentName getWebSearchActivity() {
117        return getSearchables().getWebSearchActivity();
118    }
119
120}
121