blob: b5aa4a94924196da8ff3c286da88b9aae80aa790 [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
Jeff Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.search;
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000018
Amith Yamasanic1d07a42012-08-14 09:32:02 -070019import android.app.ActivityManager;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070020import android.app.AppGlobals;
Dianne Hackbornfdf5b352014-10-08 17:43:48 -070021import android.app.IActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.ISearchManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010023import android.app.SearchManager;
Bjorn Bringert2126aac2009-12-03 15:48:19 +000024import android.app.SearchableInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.ComponentName;
Narayan Kamathee69ff42011-06-28 12:07:18 +010026import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
28import android.content.Intent;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070029import android.content.pm.IPackageManager;
30import android.content.pm.PackageManager;
Narayan Kamathee69ff42011-06-28 12:07:18 +010031import android.content.pm.ResolveInfo;
32import android.database.ContentObserver;
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070033import android.os.Binder;
Tim Kilbourn0e5f1102015-06-05 16:18:09 -070034import android.os.Bundle;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070035import android.os.Handler;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070036import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070037import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -070038import android.os.UserManager;
Narayan Kamathee69ff42011-06-28 12:07:18 +010039import android.provider.Settings;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010040import android.util.Log;
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070041import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Jeff Sharkey4175be22016-01-09 14:57:45 -070043import com.android.internal.annotations.GuardedBy;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080044import com.android.internal.content.PackageMonitor;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070045import com.android.internal.os.BackgroundThread;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080046import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggi165ce062015-07-06 16:18:11 -070047import com.android.server.LocalServices;
Jeff Sharkey4175be22016-01-09 14:57:45 -070048import com.android.server.SystemService;
Jorim Jaggi165ce062015-07-06 16:18:11 -070049import com.android.server.statusbar.StatusBarManagerInternal;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080050
Amith Yamasani64442c12012-10-07 08:17:46 -070051import java.io.FileDescriptor;
52import java.io.PrintWriter;
Bjorn Bringert6d72e022009-04-29 14:56:12 +010053import java.util.List;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
Jeff Sharkey4175be22016-01-09 14:57:45 -070056 * The search manager service handles the search UI, and maintains a registry of
57 * searchable activities.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 */
Bjorn Bringert444c7272009-07-06 21:32:50 +010059public class SearchManagerService extends ISearchManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private static final String TAG = "SearchManagerService";
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070061 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Jeff Sharkey4175be22016-01-09 14:57:45 -070063 public static class Lifecycle extends SystemService {
64 private SearchManagerService mService;
65
66 public Lifecycle(Context context) {
67 super(context);
68 }
69
70 @Override
71 public void onStart() {
72 mService = new SearchManagerService(getContext());
73 publishBinderService(Context.SEARCH_SERVICE, mService);
74 }
75
76 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070077 public void onUnlockUser(final int userId) {
78 mService.mHandler.post(new Runnable() {
79 @Override
80 public void run() {
81 mService.onUnlockUser(userId);
82 }
83 });
Jeff Sharkey4175be22016-01-09 14:57:45 -070084 }
85
86 @Override
87 public void onCleanupUser(int userHandle) {
88 mService.onCleanupUser(userHandle);
89 }
90 }
91
Bjorn Bringert444c7272009-07-06 21:32:50 +010092 // Context that the service is running in.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private final Context mContext;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010094
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000095 // This field is initialized lazily in getSearchables(), and then never modified.
Jeff Sharkey4175be22016-01-09 14:57:45 -070096 @GuardedBy("mSearchables")
97 private final SparseArray<Searchables> mSearchables = new SparseArray<>();
Narayan Kamathee69ff42011-06-28 12:07:18 +010098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700100 * Initializes the Search Manager service in the provided system context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * Only one instance of this object should be created!
102 *
103 * @param context to use for accessing DB, window manager, etc.
104 */
Satish Sampathf9acde22009-06-04 11:51:17 +0100105 public SearchManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 mContext = context;
Amith Yamasani64442c12012-10-07 08:17:46 -0700107 new MyPackageMonitor().register(context, null, UserHandle.ALL, true);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700108 new GlobalSearchProviderObserver(context.getContentResolver());
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700109 mHandler = BackgroundThread.getHandler();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100110 }
111
Amith Yamasani64442c12012-10-07 08:17:46 -0700112 private Searchables getSearchables(int userId) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700113 return getSearchables(userId, false);
114 }
115
116 private Searchables getSearchables(int userId, boolean forceUpdate) {
117 final long token = Binder.clearCallingIdentity();
Amith Yamasani64442c12012-10-07 08:17:46 -0700118 try {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700119 final UserManager um = mContext.getSystemService(UserManager.class);
120 if (um.getUserInfo(userId) == null) {
121 throw new IllegalStateException("User " + userId + " doesn't exist");
122 }
Jeff Sharkeyce18c812016-04-27 16:00:41 -0600123 if (!um.isUserUnlockingOrUnlocked(userId)) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700124 throw new IllegalStateException("User " + userId + " isn't unlocked");
125 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700126 } finally {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700127 Binder.restoreCallingIdentity(token);
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700128 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700129 synchronized (mSearchables) {
130 Searchables searchables = mSearchables.get(userId);
Amith Yamasani64442c12012-10-07 08:17:46 -0700131 if (searchables == null) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700132 searchables = new Searchables(mContext, userId);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700133 searchables.updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700134 mSearchables.append(userId, searchables);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700135 } else if (forceUpdate) {
136 searchables.updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700137 }
138 return searchables;
139 }
140 }
141
Jeff Sharkey4175be22016-01-09 14:57:45 -0700142 private void onUnlockUser(int userId) {
Jeff Sharkey38e0d0f52016-07-26 09:47:04 -0600143 try {
144 getSearchables(userId, true);
145 } catch (IllegalStateException ignored) {
146 // We're just trying to warm a cache, so we don't mind if the user
147 // was stopped or destroyed before we got here.
148 }
Bjorn Bringert9bc75cb2009-07-13 13:17:27 +0100149 }
150
Jeff Sharkey4175be22016-01-09 14:57:45 -0700151 private void onCleanupUser(int userId) {
152 synchronized (mSearchables) {
153 mSearchables.remove(userId);
Amith Yamasani64442c12012-10-07 08:17:46 -0700154 }
155 }
156
Bjorn Bringert2c7b1972010-05-04 20:44:16 +0100157 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100158 * Refreshes the "searchables" list when packages are added/removed.
159 */
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000160 class MyPackageMonitor extends PackageMonitor {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 @Override
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000163 public void onSomePackagesChanged() {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700164 updateSearchables();
165 }
166
167 @Override
168 public void onPackageModified(String pkg) {
169 updateSearchables();
170 }
171
172 private void updateSearchables() {
Amith Yamasani64442c12012-10-07 08:17:46 -0700173 final int changingUserId = getChangingUserId();
174 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700175 // Update list of searchable activities
176 for (int i = 0; i < mSearchables.size(); i++) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700177 if (changingUserId == mSearchables.keyAt(i)) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700178 mSearchables.valueAt(i).updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700179 break;
180 }
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700181 }
182 }
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000183 // Inform all listeners that the list of searchables has been updated.
184 Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Amith Yamasanie6687942012-10-29 14:29:05 -0700185 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
186 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Amith Yamasani64442c12012-10-07 08:17:46 -0700187 mContext.sendBroadcastAsUser(intent, new UserHandle(changingUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
190
Narayan Kamathee69ff42011-06-28 12:07:18 +0100191 class GlobalSearchProviderObserver extends ContentObserver {
192 private final ContentResolver mResolver;
193
194 public GlobalSearchProviderObserver(ContentResolver resolver) {
195 super(null);
196 mResolver = resolver;
197 mResolver.registerContentObserver(
198 Settings.Secure.getUriFor(Settings.Secure.SEARCH_GLOBAL_SEARCH_ACTIVITY),
199 false /* notifyDescendants */,
200 this);
201 }
202
203 @Override
204 public void onChange(boolean selfChange) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700205 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700206 for (int i = 0; i < mSearchables.size(); i++) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700207 mSearchables.valueAt(i).updateSearchableList();
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700208 }
209 }
Narayan Kamathee69ff42011-06-28 12:07:18 +0100210 Intent intent = new Intent(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
211 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700212 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Narayan Kamathee69ff42011-06-28 12:07:18 +0100213 }
Narayan Kamathee69ff42011-06-28 12:07:18 +0100214 }
215
Bjorn Bringert444c7272009-07-06 21:32:50 +0100216 //
217 // Searchable activities API
218 //
Bjorn Bringerta48a5af2009-05-20 17:58:39 +0100219
220 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100221 * Returns the SearchableInfo for a given activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 *
223 * @param launchActivity The activity from which we're launching this search.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700224 * @return Returns a SearchableInfo record describing the parameters of the search,
225 * or null if no searchable metadata was available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700227 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000228 public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
229 if (launchActivity == null) {
230 Log.e(TAG, "getSearchableInfo(), activity == null");
231 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700233 return getSearchables(UserHandle.getCallingUserId()).getSearchableInfo(launchActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
Satish Sampathf9acde22009-06-04 11:51:17 +0100235
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100236 /**
237 * Returns a list of the searchable activities that can be included in global search.
238 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700239 @Override
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100240 public List<SearchableInfo> getSearchablesInGlobalSearch() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700241 return getSearchables(UserHandle.getCallingUserId()).getSearchablesInGlobalSearchList();
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100242 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100243
Jeff Sharkey4175be22016-01-09 14:57:45 -0700244 @Override
Narayan Kamathee69ff42011-06-28 12:07:18 +0100245 public List<ResolveInfo> getGlobalSearchActivities() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700246 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivities();
Narayan Kamathee69ff42011-06-28 12:07:18 +0100247 }
248
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100249 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000250 * Gets the name of the global search activity.
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100251 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700252 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000253 public ComponentName getGlobalSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700254 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100255 }
256
257 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000258 * Gets the name of the web search activity.
Bjorn Bringert444c7272009-07-06 21:32:50 +0100259 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700260 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000261 public ComponentName getWebSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700262 return getSearchables(UserHandle.getCallingUserId()).getWebSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100263 }
264
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700265 @Override
Jorim Jaggi165ce062015-07-06 16:18:11 -0700266 public void launchAssist(Bundle args) {
267 StatusBarManagerInternal statusBarManager =
268 LocalServices.getService(StatusBarManagerInternal.class);
269 if (statusBarManager != null) {
270 statusBarManager.startAssist(args);
271 }
272 }
273
274 private ComponentName getLegacyAssistComponent(int userHandle) {
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700275 try {
Ben Pietrzak05cb3632012-12-12 11:31:57 -0800276 userHandle = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Jorim Jaggi165ce062015-07-06 16:18:11 -0700277 Binder.getCallingUid(), userHandle, true, false, "getLegacyAssistComponent", null);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700278 IPackageManager pm = AppGlobals.getPackageManager();
279 Intent assistIntent = new Intent(Intent.ACTION_ASSIST);
280 ResolveInfo info =
281 pm.resolveIntent(assistIntent,
Jorim Jaggi165ce062015-07-06 16:18:11 -0700282 assistIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
283 PackageManager.MATCH_DEFAULT_ONLY, userHandle);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700284 if (info != null) {
285 return new ComponentName(
286 info.activityInfo.applicationInfo.packageName,
287 info.activityInfo.name);
288 }
289 } catch (RemoteException re) {
290 // Local call
Jorim Jaggi165ce062015-07-06 16:18:11 -0700291 Log.e(TAG, "RemoteException in getLegacyAssistComponent: " + re);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700292 } catch (Exception e) {
Jorim Jaggi165ce062015-07-06 16:18:11 -0700293 Log.e(TAG, "Exception in getLegacyAssistComponent: " + e);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700294 }
295 return null;
296 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700297
298 @Override
Jorim Jaggi165ce062015-07-06 16:18:11 -0700299 public boolean launchLegacyAssist(String hint, int userHandle, Bundle args) {
300 ComponentName comp = getLegacyAssistComponent(userHandle);
Dianne Hackbornfdf5b352014-10-08 17:43:48 -0700301 if (comp == null) {
302 return false;
303 }
304 long ident = Binder.clearCallingIdentity();
305 try {
306 Intent intent = new Intent(Intent.ACTION_ASSIST);
307 intent.setComponent(comp);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800308 IActivityManager am = ActivityManager.getService();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -0700309 return am.launchAssistIntent(intent, ActivityManager.ASSIST_CONTEXT_BASIC, hint,
Tim Kilbourn0e5f1102015-06-05 16:18:09 -0700310 userHandle, args);
Dianne Hackbornfdf5b352014-10-08 17:43:48 -0700311 } catch (RemoteException e) {
312 } finally {
313 Binder.restoreCallingIdentity(ident);
314 }
315 return true;
316 }
317
318 @Override
Amith Yamasani64442c12012-10-07 08:17:46 -0700319 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkey52801aa2012-10-12 16:06:16 -0700320 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
321
Amith Yamasani64442c12012-10-07 08:17:46 -0700322 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
323 synchronized (mSearchables) {
324 for (int i = 0; i < mSearchables.size(); i++) {
325 ipw.print("\nUser: "); ipw.println(mSearchables.keyAt(i));
326 ipw.increaseIndent();
327 mSearchables.valueAt(i).dump(fd, ipw, args);
328 ipw.decreaseIndent();
329 }
330 }
331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332}