blob: 4a2137461801aad73325585e40d0c71cae710c1c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000019import com.android.internal.content.PackageMonitor;
Amith Yamasani64442c12012-10-07 08:17:46 -070020import com.android.internal.util.IndentingPrintWriter;
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000021
Amith Yamasanic1d07a42012-08-14 09:32:02 -070022import android.app.ActivityManager;
23import android.app.ActivityManagerNative;
24import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.app.ISearchManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010026import android.app.SearchManager;
Bjorn Bringert2126aac2009-12-03 15:48:19 +000027import android.app.SearchableInfo;
Bjorn Bringert2c7b1972010-05-04 20:44:16 +010028import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.ComponentName;
Narayan Kamathee69ff42011-06-28 12:07:18 +010030import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.Context;
32import android.content.Intent;
Bjorn Bringert2c7b1972010-05-04 20:44:16 +010033import android.content.IntentFilter;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070034import android.content.pm.IPackageManager;
35import android.content.pm.PackageManager;
Narayan Kamathee69ff42011-06-28 12:07:18 +010036import android.content.pm.ResolveInfo;
37import android.database.ContentObserver;
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070038import android.os.Binder;
Bjorn Bringert2c7b1972010-05-04 20:44:16 +010039import android.os.Process;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070040import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070041import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -070042import android.os.UserManager;
Narayan Kamathee69ff42011-06-28 12:07:18 +010043import android.provider.Settings;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010044import android.util.Log;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070045import android.util.Slog;
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070046import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Amith Yamasani64442c12012-10-07 08:17:46 -070048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Bjorn Bringert6d72e022009-04-29 14:56:12 +010050import java.util.List;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052/**
Bjorn Bringert444c7272009-07-06 21:32:50 +010053 * The search manager service handles the search UI, and maintains a registry of searchable
54 * activities.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
Bjorn Bringert444c7272009-07-06 21:32:50 +010056public class SearchManagerService extends ISearchManager.Stub {
57
58 // general debugging support
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private static final String TAG = "SearchManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Bjorn Bringert444c7272009-07-06 21:32:50 +010061 // Context that the service is running in.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 private final Context mContext;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010063
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000064 // This field is initialized lazily in getSearchables(), and then never modified.
Amith Yamasani64442c12012-10-07 08:17:46 -070065 private final SparseArray<Searchables> mSearchables = new SparseArray<Searchables>();
Narayan Kamathee69ff42011-06-28 12:07:18 +010066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -070068 * Initializes the Search Manager service in the provided system context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * Only one instance of this object should be created!
70 *
71 * @param context to use for accessing DB, window manager, etc.
72 */
Satish Sampathf9acde22009-06-04 11:51:17 +010073 public SearchManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 mContext = context;
Bjorn Bringert2c7b1972010-05-04 20:44:16 +010075 mContext.registerReceiver(new BootCompletedReceiver(),
76 new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
Amith Yamasanie6687942012-10-29 14:29:05 -070077 mContext.registerReceiver(new UserReceiver(),
Amith Yamasani64442c12012-10-07 08:17:46 -070078 new IntentFilter(Intent.ACTION_USER_REMOVED));
79 new MyPackageMonitor().register(context, null, UserHandle.ALL, true);
Bjorn Bringert444c7272009-07-06 21:32:50 +010080 }
81
Amith Yamasani64442c12012-10-07 08:17:46 -070082 private Searchables getSearchables(int userId) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070083 long origId = Binder.clearCallingIdentity();
Amith Yamasani64442c12012-10-07 08:17:46 -070084 try {
85 boolean userExists = ((UserManager) mContext.getSystemService(Context.USER_SERVICE))
86 .getUserInfo(userId) != null;
87 if (!userExists) return null;
88 } finally {
89 Binder.restoreCallingIdentity(origId);
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070090 }
Amith Yamasani64442c12012-10-07 08:17:46 -070091 synchronized (mSearchables) {
92 Searchables searchables = mSearchables.get(userId);
93
94 if (searchables == null) {
95 Log.i(TAG, "Building list of searchable activities for userId=" + userId);
96 searchables = new Searchables(mContext, userId);
97 searchables.buildSearchableList();
98 mSearchables.append(userId, searchables);
99 }
100 return searchables;
101 }
102 }
103
104 private void onUserRemoved(int userId) {
105 if (userId != UserHandle.USER_OWNER) {
106 synchronized (mSearchables) {
107 mSearchables.remove(userId);
108 }
109 }
Bjorn Bringert9bc75cb2009-07-13 13:17:27 +0100110 }
111
Bjorn Bringert444c7272009-07-06 21:32:50 +0100112 /**
Bjorn Bringert2c7b1972010-05-04 20:44:16 +0100113 * Creates the initial searchables list after boot.
114 */
115 private final class BootCompletedReceiver extends BroadcastReceiver {
116 @Override
117 public void onReceive(Context context, Intent intent) {
118 new Thread() {
119 @Override
120 public void run() {
121 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
122 mContext.unregisterReceiver(BootCompletedReceiver.this);
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700123 getSearchables(0);
Bjorn Bringert2c7b1972010-05-04 20:44:16 +0100124 }
125 }.start();
126 }
127 }
128
Amith Yamasani64442c12012-10-07 08:17:46 -0700129 private final class UserReceiver extends BroadcastReceiver {
130 @Override
131 public void onReceive(Context context, Intent intent) {
132 onUserRemoved(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_OWNER));
133 }
134 }
135
Bjorn Bringert2c7b1972010-05-04 20:44:16 +0100136 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100137 * Refreshes the "searchables" list when packages are added/removed.
138 */
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000139 class MyPackageMonitor extends PackageMonitor {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 @Override
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000142 public void onSomePackagesChanged() {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700143 updateSearchables();
144 }
145
146 @Override
147 public void onPackageModified(String pkg) {
148 updateSearchables();
149 }
150
151 private void updateSearchables() {
Amith Yamasani64442c12012-10-07 08:17:46 -0700152 final int changingUserId = getChangingUserId();
153 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700154 // Update list of searchable activities
155 for (int i = 0; i < mSearchables.size(); i++) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700156 if (changingUserId == mSearchables.keyAt(i)) {
157 getSearchables(mSearchables.keyAt(i)).buildSearchableList();
158 break;
159 }
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700160 }
161 }
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000162 // Inform all listeners that the list of searchables has been updated.
163 Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Amith Yamasanie6687942012-10-29 14:29:05 -0700164 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
165 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Amith Yamasani64442c12012-10-07 08:17:46 -0700166 mContext.sendBroadcastAsUser(intent, new UserHandle(changingUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
169
Narayan Kamathee69ff42011-06-28 12:07:18 +0100170 class GlobalSearchProviderObserver extends ContentObserver {
171 private final ContentResolver mResolver;
172
173 public GlobalSearchProviderObserver(ContentResolver resolver) {
174 super(null);
175 mResolver = resolver;
176 mResolver.registerContentObserver(
177 Settings.Secure.getUriFor(Settings.Secure.SEARCH_GLOBAL_SEARCH_ACTIVITY),
178 false /* notifyDescendants */,
179 this);
180 }
181
182 @Override
183 public void onChange(boolean selfChange) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700184 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700185 for (int i = 0; i < mSearchables.size(); i++) {
186 getSearchables(mSearchables.keyAt(i)).buildSearchableList();
187 }
188 }
Narayan Kamathee69ff42011-06-28 12:07:18 +0100189 Intent intent = new Intent(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
190 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700191 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Narayan Kamathee69ff42011-06-28 12:07:18 +0100192 }
193
194 }
195
Bjorn Bringert444c7272009-07-06 21:32:50 +0100196 //
197 // Searchable activities API
198 //
Bjorn Bringerta48a5af2009-05-20 17:58:39 +0100199
200 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100201 * Returns the SearchableInfo for a given activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 *
203 * @param launchActivity The activity from which we're launching this search.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700204 * @return Returns a SearchableInfo record describing the parameters of the search,
205 * or null if no searchable metadata was available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000207 public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
208 if (launchActivity == null) {
209 Log.e(TAG, "getSearchableInfo(), activity == null");
210 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700212 return getSearchables(UserHandle.getCallingUserId()).getSearchableInfo(launchActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
Satish Sampathf9acde22009-06-04 11:51:17 +0100214
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100215 /**
216 * Returns a list of the searchable activities that can be included in global search.
217 */
218 public List<SearchableInfo> getSearchablesInGlobalSearch() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700219 return getSearchables(UserHandle.getCallingUserId()).getSearchablesInGlobalSearchList();
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100220 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100221
Narayan Kamathee69ff42011-06-28 12:07:18 +0100222 public List<ResolveInfo> getGlobalSearchActivities() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700223 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivities();
Narayan Kamathee69ff42011-06-28 12:07:18 +0100224 }
225
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100226 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000227 * Gets the name of the global search activity.
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100228 */
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000229 public ComponentName getGlobalSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700230 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100231 }
232
233 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000234 * Gets the name of the web search activity.
Bjorn Bringert444c7272009-07-06 21:32:50 +0100235 */
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000236 public ComponentName getWebSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700237 return getSearchables(UserHandle.getCallingUserId()).getWebSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100238 }
239
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700240 @Override
241 public ComponentName getAssistIntent(int userHandle) {
242 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700243 if (userHandle != UserHandle.getCallingUserId()) {
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700244 // Requesting a different user, make sure that they have the permission
245 if (ActivityManager.checkComponentPermission(
246 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
247 Binder.getCallingUid(), -1, true)
248 == PackageManager.PERMISSION_GRANTED) {
249 // Translate to the current user id, if caller wasn't aware
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700250 if (userHandle == UserHandle.USER_CURRENT) {
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700251 long identity = Binder.clearCallingIdentity();
252 userHandle = ActivityManagerNative.getDefault().getCurrentUser().id;
253 Binder.restoreCallingIdentity(identity);
254 }
255 } else {
256 String msg = "Permission Denial: "
257 + "Request to getAssistIntent for " + userHandle
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700258 + " but is calling from user " + UserHandle.getCallingUserId()
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700259 + "; this requires "
260 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
261 Slog.w(TAG, msg);
262 return null;
263 }
264 }
265 IPackageManager pm = AppGlobals.getPackageManager();
266 Intent assistIntent = new Intent(Intent.ACTION_ASSIST);
267 ResolveInfo info =
268 pm.resolveIntent(assistIntent,
269 assistIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
270 PackageManager.MATCH_DEFAULT_ONLY, userHandle);
271 if (info != null) {
272 return new ComponentName(
273 info.activityInfo.applicationInfo.packageName,
274 info.activityInfo.name);
275 }
276 } catch (RemoteException re) {
277 // Local call
278 Log.e(TAG, "RemoteException in getAssistIntent: " + re);
279 } catch (Exception e) {
280 Log.e(TAG, "Exception in getAssistIntent: " + e);
281 }
282 return null;
283 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700284
285 @Override
286 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkey52801aa2012-10-12 16:06:16 -0700287 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
288
Amith Yamasani64442c12012-10-07 08:17:46 -0700289 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
290 synchronized (mSearchables) {
291 for (int i = 0; i < mSearchables.size(); i++) {
292 ipw.print("\nUser: "); ipw.println(mSearchables.keyAt(i));
293 ipw.increaseIndent();
294 mSearchables.valueAt(i).dump(fd, ipw, args);
295 ipw.decreaseIndent();
296 }
297 }
298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299}