blob: bc1a12fac3932ff05f9c1a9f7a85f53014ee2062 [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;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070020import android.app.ActivityTaskManager;
Dianne Hackbornfdf5b352014-10-08 17:43:48 -070021import android.app.IActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070022import android.app.IActivityTaskManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.app.ISearchManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010024import android.app.SearchManager;
Bjorn Bringert2126aac2009-12-03 15:48:19 +000025import android.app.SearchableInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ComponentName;
Narayan Kamathee69ff42011-06-28 12:07:18 +010027import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.Context;
29import android.content.Intent;
Amith Yamasanic1d07a42012-08-14 09:32:02 -070030import 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;
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -070040import android.service.voice.VoiceInteractionService;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010041import android.util.Log;
Amith Yamasani5bb87cd2012-06-14 11:32:13 -070042import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Jeff Sharkey4175be22016-01-09 14:57:45 -070044import com.android.internal.annotations.GuardedBy;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080045import com.android.internal.content.PackageMonitor;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070046import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060047import com.android.internal.util.DumpUtils;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080048import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggi165ce062015-07-06 16:18:11 -070049import com.android.server.LocalServices;
Jeff Sharkey4175be22016-01-09 14:57:45 -070050import com.android.server.SystemService;
Jorim Jaggi165ce062015-07-06 16:18:11 -070051import com.android.server.statusbar.StatusBarManagerInternal;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080052
Amith Yamasani64442c12012-10-07 08:17:46 -070053import java.io.FileDescriptor;
54import java.io.PrintWriter;
Bjorn Bringert6d72e022009-04-29 14:56:12 +010055import java.util.List;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057/**
Jeff Sharkey4175be22016-01-09 14:57:45 -070058 * The search manager service handles the search UI, and maintains a registry of
59 * searchable activities.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 */
Bjorn Bringert444c7272009-07-06 21:32:50 +010061public class SearchManagerService extends ISearchManager.Stub {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 private static final String TAG = "SearchManagerService";
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070063 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Jeff Sharkey4175be22016-01-09 14:57:45 -070065 public static class Lifecycle extends SystemService {
66 private SearchManagerService mService;
67
68 public Lifecycle(Context context) {
69 super(context);
70 }
71
72 @Override
73 public void onStart() {
74 mService = new SearchManagerService(getContext());
75 publishBinderService(Context.SEARCH_SERVICE, mService);
76 }
77
78 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -070079 public void onUnlockUser(final int userId) {
80 mService.mHandler.post(new Runnable() {
81 @Override
82 public void run() {
83 mService.onUnlockUser(userId);
84 }
85 });
Jeff Sharkey4175be22016-01-09 14:57:45 -070086 }
87
88 @Override
89 public void onCleanupUser(int userHandle) {
90 mService.onCleanupUser(userHandle);
91 }
92 }
93
Bjorn Bringert444c7272009-07-06 21:32:50 +010094 // Context that the service is running in.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private final Context mContext;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010096
Bjorn Bringertab5d96c2010-02-23 22:48:46 +000097 // This field is initialized lazily in getSearchables(), and then never modified.
Jeff Sharkey4175be22016-01-09 14:57:45 -070098 @GuardedBy("mSearchables")
99 private final SparseArray<Searchables> mSearchables = new SparseArray<>();
Narayan Kamathee69ff42011-06-28 12:07:18 +0100100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700102 * Initializes the Search Manager service in the provided system context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * Only one instance of this object should be created!
104 *
105 * @param context to use for accessing DB, window manager, etc.
106 */
Satish Sampathf9acde22009-06-04 11:51:17 +0100107 public SearchManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 mContext = context;
Amith Yamasani64442c12012-10-07 08:17:46 -0700109 new MyPackageMonitor().register(context, null, UserHandle.ALL, true);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700110 new GlobalSearchProviderObserver(context.getContentResolver());
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700111 mHandler = BackgroundThread.getHandler();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100112 }
113
Amith Yamasani64442c12012-10-07 08:17:46 -0700114 private Searchables getSearchables(int userId) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700115 return getSearchables(userId, false);
116 }
117
118 private Searchables getSearchables(int userId, boolean forceUpdate) {
119 final long token = Binder.clearCallingIdentity();
Amith Yamasani64442c12012-10-07 08:17:46 -0700120 try {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700121 final UserManager um = mContext.getSystemService(UserManager.class);
122 if (um.getUserInfo(userId) == null) {
123 throw new IllegalStateException("User " + userId + " doesn't exist");
124 }
Jeff Sharkeyce18c812016-04-27 16:00:41 -0600125 if (!um.isUserUnlockingOrUnlocked(userId)) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700126 throw new IllegalStateException("User " + userId + " isn't unlocked");
127 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700128 } finally {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700129 Binder.restoreCallingIdentity(token);
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700130 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700131 synchronized (mSearchables) {
132 Searchables searchables = mSearchables.get(userId);
Amith Yamasani64442c12012-10-07 08:17:46 -0700133 if (searchables == null) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700134 searchables = new Searchables(mContext, userId);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700135 searchables.updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700136 mSearchables.append(userId, searchables);
Jeff Sharkey4175be22016-01-09 14:57:45 -0700137 } else if (forceUpdate) {
138 searchables.updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700139 }
140 return searchables;
141 }
142 }
143
Jeff Sharkey4175be22016-01-09 14:57:45 -0700144 private void onUnlockUser(int userId) {
Jeff Sharkey38e0d0f52016-07-26 09:47:04 -0600145 try {
146 getSearchables(userId, true);
147 } catch (IllegalStateException ignored) {
148 // We're just trying to warm a cache, so we don't mind if the user
149 // was stopped or destroyed before we got here.
150 }
Bjorn Bringert9bc75cb2009-07-13 13:17:27 +0100151 }
152
Jeff Sharkey4175be22016-01-09 14:57:45 -0700153 private void onCleanupUser(int userId) {
154 synchronized (mSearchables) {
155 mSearchables.remove(userId);
Amith Yamasani64442c12012-10-07 08:17:46 -0700156 }
157 }
158
Bjorn Bringert2c7b1972010-05-04 20:44:16 +0100159 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100160 * Refreshes the "searchables" list when packages are added/removed.
161 */
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000162 class MyPackageMonitor extends PackageMonitor {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 @Override
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000165 public void onSomePackagesChanged() {
Amith Yamasani13bc6022011-08-23 12:11:35 -0700166 updateSearchables();
167 }
168
169 @Override
170 public void onPackageModified(String pkg) {
171 updateSearchables();
172 }
173
174 private void updateSearchables() {
Amith Yamasani64442c12012-10-07 08:17:46 -0700175 final int changingUserId = getChangingUserId();
176 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700177 // Update list of searchable activities
178 for (int i = 0; i < mSearchables.size(); i++) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700179 if (changingUserId == mSearchables.keyAt(i)) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700180 mSearchables.valueAt(i).updateSearchableList();
Amith Yamasani64442c12012-10-07 08:17:46 -0700181 break;
182 }
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700183 }
184 }
Bjorn Bringertab5d96c2010-02-23 22:48:46 +0000185 // Inform all listeners that the list of searchables has been updated.
186 Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Amith Yamasanie6687942012-10-29 14:29:05 -0700187 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
188 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Amith Yamasani64442c12012-10-07 08:17:46 -0700189 mContext.sendBroadcastAsUser(intent, new UserHandle(changingUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192
Narayan Kamathee69ff42011-06-28 12:07:18 +0100193 class GlobalSearchProviderObserver extends ContentObserver {
194 private final ContentResolver mResolver;
195
196 public GlobalSearchProviderObserver(ContentResolver resolver) {
197 super(null);
198 mResolver = resolver;
199 mResolver.registerContentObserver(
200 Settings.Secure.getUriFor(Settings.Secure.SEARCH_GLOBAL_SEARCH_ACTIVITY),
201 false /* notifyDescendants */,
202 this);
203 }
204
205 @Override
206 public void onChange(boolean selfChange) {
Amith Yamasani64442c12012-10-07 08:17:46 -0700207 synchronized (mSearchables) {
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700208 for (int i = 0; i < mSearchables.size(); i++) {
Jeff Sharkey4175be22016-01-09 14:57:45 -0700209 mSearchables.valueAt(i).updateSearchableList();
Amith Yamasani5bb87cd2012-06-14 11:32:13 -0700210 }
211 }
Narayan Kamathee69ff42011-06-28 12:07:18 +0100212 Intent intent = new Intent(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
213 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700214 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Narayan Kamathee69ff42011-06-28 12:07:18 +0100215 }
Narayan Kamathee69ff42011-06-28 12:07:18 +0100216 }
217
Bjorn Bringert444c7272009-07-06 21:32:50 +0100218 //
219 // Searchable activities API
220 //
Bjorn Bringerta48a5af2009-05-20 17:58:39 +0100221
222 /**
Bjorn Bringert444c7272009-07-06 21:32:50 +0100223 * Returns the SearchableInfo for a given activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 *
225 * @param launchActivity The activity from which we're launching this search.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700226 * @return Returns a SearchableInfo record describing the parameters of the search,
227 * or null if no searchable metadata was available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700229 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000230 public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
231 if (launchActivity == null) {
232 Log.e(TAG, "getSearchableInfo(), activity == null");
233 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700235 return getSearchables(UserHandle.getCallingUserId()).getSearchableInfo(launchActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
Satish Sampathf9acde22009-06-04 11:51:17 +0100237
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100238 /**
239 * Returns a list of the searchable activities that can be included in global search.
240 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700241 @Override
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100242 public List<SearchableInfo> getSearchablesInGlobalSearch() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700243 return getSearchables(UserHandle.getCallingUserId()).getSearchablesInGlobalSearchList();
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100244 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100245
Jeff Sharkey4175be22016-01-09 14:57:45 -0700246 @Override
Narayan Kamathee69ff42011-06-28 12:07:18 +0100247 public List<ResolveInfo> getGlobalSearchActivities() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700248 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivities();
Narayan Kamathee69ff42011-06-28 12:07:18 +0100249 }
250
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100251 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000252 * Gets the name of the global search activity.
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100253 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700254 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000255 public ComponentName getGlobalSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700256 return getSearchables(UserHandle.getCallingUserId()).getGlobalSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100257 }
258
259 /**
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000260 * Gets the name of the web search activity.
Bjorn Bringert444c7272009-07-06 21:32:50 +0100261 */
Jeff Sharkey4175be22016-01-09 14:57:45 -0700262 @Override
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000263 public ComponentName getWebSearchActivity() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700264 return getSearchables(UserHandle.getCallingUserId()).getWebSearchActivity();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100265 }
266
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700267 @Override
Jorim Jaggi165ce062015-07-06 16:18:11 -0700268 public void launchAssist(Bundle args) {
269 StatusBarManagerInternal statusBarManager =
270 LocalServices.getService(StatusBarManagerInternal.class);
271 if (statusBarManager != null) {
272 statusBarManager.startAssist(args);
273 }
274 }
275
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700276 // Check and return VIS component
Jorim Jaggi165ce062015-07-06 16:18:11 -0700277 private ComponentName getLegacyAssistComponent(int userHandle) {
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700278 try {
Ben Pietrzak05cb3632012-12-12 11:31:57 -0800279 userHandle = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700280 Binder.getCallingUid(), userHandle, true, false, "getLegacyAssistComponent",
281 null);
282 PackageManager pm = mContext.getPackageManager();
283 Intent intentAssistProbe = new Intent(VoiceInteractionService.SERVICE_INTERFACE);
284 List<ResolveInfo> infoListVis = pm.queryIntentServicesAsUser(intentAssistProbe,
285 PackageManager.MATCH_SYSTEM_ONLY, userHandle);
286 if (infoListVis == null || infoListVis.isEmpty()) {
287 return null;
288 } else {
289 ResolveInfo rInfo = infoListVis.get(0);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700290 return new ComponentName(
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700291 rInfo.serviceInfo.applicationInfo.packageName,
292 rInfo.serviceInfo.name);
293
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700294 }
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700295 } catch (Exception e) {
Jorim Jaggi165ce062015-07-06 16:18:11 -0700296 Log.e(TAG, "Exception in getLegacyAssistComponent: " + e);
Amith Yamasanic1d07a42012-08-14 09:32:02 -0700297 }
298 return null;
299 }
Amith Yamasani64442c12012-10-07 08:17:46 -0700300
301 @Override
Jorim Jaggi165ce062015-07-06 16:18:11 -0700302 public boolean launchLegacyAssist(String hint, int userHandle, Bundle args) {
Powei Feng719e70c2017-04-11 23:00:45 +0000303 ComponentName comp = getLegacyAssistComponent(userHandle);
Dianne Hackbornfdf5b352014-10-08 17:43:48 -0700304 if (comp == null) {
305 return false;
306 }
307 long ident = Binder.clearCallingIdentity();
308 try {
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700309 Intent intent = new Intent(VoiceInteractionService.SERVICE_INTERFACE);
Dianne Hackbornfdf5b352014-10-08 17:43:48 -0700310 intent.setComponent(comp);
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700311
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700312 IActivityTaskManager am = ActivityTaskManager.getService();
Sujith Ramakrishnanf8942c02017-07-18 18:35:14 -0700313 if (args != null) {
314 args.putInt(Intent.EXTRA_KEY_EVENT, android.view.KeyEvent.KEYCODE_ASSIST);
315 }
316 intent.putExtras(args);
317
Powei Feng719e70c2017-04-11 23:00:45 +0000318 return am.launchAssistIntent(intent, ActivityManager.ASSIST_CONTEXT_BASIC, hint,
319 userHandle, args);
Dianne Hackbornfdf5b352014-10-08 17:43:48 -0700320 } catch (RemoteException e) {
321 } finally {
322 Binder.restoreCallingIdentity(ident);
323 }
324 return true;
325 }
326
327 @Override
Amith Yamasani64442c12012-10-07 08:17:46 -0700328 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600329 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Sharkey52801aa2012-10-12 16:06:16 -0700330
Amith Yamasani64442c12012-10-07 08:17:46 -0700331 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
332 synchronized (mSearchables) {
333 for (int i = 0; i < mSearchables.size(); i++) {
334 ipw.print("\nUser: "); ipw.println(mSearchables.keyAt(i));
335 ipw.increaseIndent();
336 mSearchables.valueAt(i).dump(fd, ipw, args);
337 ipw.decreaseIndent();
338 }
339 }
340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341}