SearchManagerService.java revision 6cf7a325e6e9e70d9858e21fbb438341332ed254
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 android.app.ActivityManagerNative;
20import android.app.IActivityWatcher;
21import android.app.ISearchManager;
22import android.app.ISearchManagerCallback;
23import android.app.SearchManager;
24import android.app.SearchableInfo;
25import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.os.Bundle;
31import android.os.Handler;
32import android.os.RemoteException;
33import android.util.Log;
34
35import java.util.List;
36
37/**
38 * The search manager service handles the search UI, and maintains a registry of searchable
39 * activities.
40 */
41public class SearchManagerService extends ISearchManager.Stub {
42
43    // general debugging support
44    private static final String TAG = "SearchManagerService";
45    private static final boolean DBG = false;
46
47    // Context that the service is running in.
48    private final Context mContext;
49
50    // This field is initialized in ensureSearchablesCreated(), and then never modified.
51    // Only accessed by ensureSearchablesCreated() and getSearchables()
52    private Searchables mSearchables;
53
54    /**
55     * Initializes the Search Manager service in the provided system context.
56     * Only one instance of this object should be created!
57     *
58     * @param context to use for accessing DB, window manager, etc.
59     */
60    public SearchManagerService(Context context)  {
61        mContext = context;
62    }
63
64    private synchronized void ensureSearchablesCreated() {
65        if (mSearchables != null) return;  // already created
66
67        mSearchables = new Searchables(mContext);
68        mSearchables.buildSearchableList();
69
70        IntentFilter packageFilter = new IntentFilter();
71        packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
72        packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
73        packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
74        packageFilter.addDataScheme("package");
75        mContext.registerReceiver(mPackageChangedReceiver, packageFilter);
76        // Register for events related to sdcard installation.
77        IntentFilter sdFilter = new IntentFilter();
78        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
79        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
80        mContext.registerReceiver(mPackageChangedReceiver, sdFilter);
81    }
82
83    private synchronized Searchables getSearchables() {
84        ensureSearchablesCreated();
85        return mSearchables;
86    }
87
88    /**
89     * Refreshes the "searchables" list when packages are added/removed.
90     */
91    private BroadcastReceiver mPackageChangedReceiver = new BroadcastReceiver() {
92        @Override
93        public void onReceive(Context context, Intent intent) {
94            String action = intent.getAction();
95
96            if (Intent.ACTION_PACKAGE_ADDED.equals(action) ||
97                    Intent.ACTION_PACKAGE_REMOVED.equals(action) ||
98                    Intent.ACTION_PACKAGE_CHANGED.equals(action) ||
99                    Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action) ||
100                    Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
101                if (DBG) Log.d(TAG, "Got " + action);
102                // Update list of searchable activities
103                getSearchables().buildSearchableList();
104                broadcastSearchablesChanged();
105            }
106        }
107    };
108
109    /**
110     * Informs all listeners that the list of searchables has been updated.
111     */
112    void broadcastSearchablesChanged() {
113        Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
114        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
115        mContext.sendBroadcast(intent);
116    }
117
118    //
119    // Searchable activities API
120    //
121
122    /**
123     * Returns the SearchableInfo for a given activity.
124     *
125     * @param launchActivity The activity from which we're launching this search.
126     * @return Returns a SearchableInfo record describing the parameters of the search,
127     * or null if no searchable metadata was available.
128     */
129    public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
130        if (launchActivity == null) {
131            Log.e(TAG, "getSearchableInfo(), activity == null");
132            return null;
133        }
134        return getSearchables().getSearchableInfo(launchActivity);
135    }
136
137    /**
138     * Returns a list of the searchable activities that can be included in global search.
139     */
140    public List<SearchableInfo> getSearchablesInGlobalSearch() {
141        return getSearchables().getSearchablesInGlobalSearchList();
142    }
143
144    /**
145     * Gets the name of the global search activity.
146     */
147    public ComponentName getGlobalSearchActivity() {
148        return getSearchables().getGlobalSearchActivity();
149    }
150
151    /**
152     * Gets the name of the web search activity.
153     */
154    public ComponentName getWebSearchActivity() {
155        return getSearchables().getWebSearchActivity();
156    }
157
158}
159