blob: 2c1b299064ce649be0acaec265f91e185f8cbc45 [file] [log] [blame]
Terry Wangfebbead2019-10-17 17:05:18 -07001/*
sidchhabraa7c8f8a2020-01-16 18:38:17 -08002 * Copyright (C) 2020 The Android Open Source Project
Terry Wangfebbead2019-10-17 17:05:18 -07003 *
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 */
16package com.android.server.appsearch;
17
Terry Wangdbd1dca2020-11-03 17:03:56 -080018import static android.app.appsearch.AppSearchResult.throwableToFailedResult;
19
sidchhabraa7c8f8a2020-01-16 18:38:17 -080020import android.annotation.NonNull;
Terry Wangf2093072020-11-30 04:47:19 -080021import android.annotation.UserIdInt;
22import android.app.ActivityManager;
Alexander Dorokhine18465842020-01-21 01:08:57 -080023import android.app.appsearch.AppSearchBatchResult;
Alexander Dorokhine969f4462020-03-05 15:54:19 -080024import android.app.appsearch.AppSearchResult;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070025import android.app.appsearch.AppSearchSchema;
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -070026import android.app.appsearch.GenericDocument;
Terry Wangdbd1dca2020-11-03 17:03:56 -080027import android.app.appsearch.IAppSearchBatchResultCallback;
Terry Wangfebbead2019-10-17 17:05:18 -070028import android.app.appsearch.IAppSearchManager;
Terry Wangdbd1dca2020-11-03 17:03:56 -080029import android.app.appsearch.IAppSearchResultCallback;
Alexander Dorokhineab789062021-01-11 21:00:00 -080030import android.app.appsearch.PackageIdentifier;
Terry Wang26b9e5c2020-10-23 02:05:01 -070031import android.app.appsearch.SearchResultPage;
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -070032import android.app.appsearch.SearchSpec;
Terry Wangfebbead2019-10-17 17:05:18 -070033import android.content.Context;
Cassie Wang0c62d992021-01-15 14:39:30 -080034import android.content.pm.PackageManagerInternal;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080035import android.os.Binder;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070036import android.os.Bundle;
Terry Wangdbd1dca2020-11-03 17:03:56 -080037import android.os.ParcelableException;
38import android.os.RemoteException;
Cassie Wang0c62d992021-01-15 14:39:30 -080039import android.os.UserHandle;
Alexander Dorokhineab789062021-01-11 21:00:00 -080040import android.util.ArrayMap;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080041import android.util.ArraySet;
Terry Wangdbd1dca2020-11-03 17:03:56 -080042import android.util.Log;
Terry Wangfebbead2019-10-17 17:05:18 -070043
Cassie Wang15c86972021-02-09 13:43:25 -080044import com.android.internal.annotations.GuardedBy;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080045import com.android.internal.util.Preconditions;
Cassie Wang0c62d992021-01-15 14:39:30 -080046import com.android.server.LocalServices;
Terry Wangfebbead2019-10-17 17:05:18 -070047import com.android.server.SystemService;
Alexander Dorokhinef660d8f2020-10-29 22:37:00 -070048import com.android.server.appsearch.external.localstorage.AppSearchImpl;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080049
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080050import java.util.ArrayList;
Alexander Dorokhine18465842020-01-21 01:08:57 -080051import java.util.List;
Alexander Dorokhineab789062021-01-11 21:00:00 -080052import java.util.Map;
Alexander Dorokhined18f8842021-01-20 15:26:13 -080053import java.util.Objects;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080054import java.util.Set;
Alexander Dorokhine18465842020-01-21 01:08:57 -080055
Cassie Wang0c62d992021-01-15 14:39:30 -080056/** TODO(b/142567528): add comments when implement this class */
Terry Wangfebbead2019-10-17 17:05:18 -070057public class AppSearchManagerService extends SystemService {
Alexander Dorokhineebd37742020-09-22 15:02:26 -070058 private static final String TAG = "AppSearchManagerService";
Cassie Wang0c62d992021-01-15 14:39:30 -080059 private PackageManagerInternal mPackageManagerInternal;
Cassie Wang21c2d6a2021-01-20 23:59:55 -080060 private ImplInstanceManager mImplInstanceManager;
Terry Wangfebbead2019-10-17 17:05:18 -070061
Cassie Wang15c86972021-02-09 13:43:25 -080062 // Cache of unlocked user ids so we don't have to query UserManager service each time. The
63 // "locked" suffix refers to the fact that access to the field should be locked; unrelated to
64 // the unlocked status of user ids.
65 @GuardedBy("mUnlockedUserIdsLocked")
66 private final Set<Integer> mUnlockedUserIdsLocked = new ArraySet<>();
Cassie Wang9ba9ae12021-02-01 16:39:37 -080067
Terry Wangfebbead2019-10-17 17:05:18 -070068 public AppSearchManagerService(Context context) {
69 super(context);
70 }
71
72 @Override
73 public void onStart() {
74 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
Cassie Wang0c62d992021-01-15 14:39:30 -080075 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Cassie Wang308304c2021-01-21 11:13:49 -080076 mImplInstanceManager = ImplInstanceManager.getInstance(getContext());
Terry Wangfebbead2019-10-17 17:05:18 -070077 }
78
Cassie Wang9ba9ae12021-02-01 16:39:37 -080079 @Override
80 public void onUserUnlocked(@NonNull TargetUser user) {
Cassie Wang15c86972021-02-09 13:43:25 -080081 synchronized (mUnlockedUserIdsLocked) {
82 mUnlockedUserIdsLocked.add(user.getUserIdentifier());
83 }
Cassie Wang9ba9ae12021-02-01 16:39:37 -080084 }
85
Terry Wangfebbead2019-10-17 17:05:18 -070086 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080087 @Override
Alexander Dorokhine969f4462020-03-05 15:54:19 -080088 public void setSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -080089 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -070090 @NonNull String databaseName,
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070091 @NonNull List<Bundle> schemaBundles,
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080092 @NonNull List<String> schemasNotPlatformSurfaceable,
Alexander Dorokhineab789062021-01-11 21:00:00 -080093 @NonNull Map<String, List<Bundle>> schemasPackageAccessibleBundles,
Alexander Dorokhine969f4462020-03-05 15:54:19 -080094 boolean forceOverride,
Terry Wangf2093072020-11-30 04:47:19 -080095 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -080096 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -080097 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -070098 Preconditions.checkNotNull(databaseName);
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070099 Preconditions.checkNotNull(schemaBundles);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800100 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800101 int callingUid = Binder.getCallingUid();
102 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600103 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800104 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800105 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800106 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine6a99f942020-12-04 02:57:22 -0800107 List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size());
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700108 for (int i = 0; i < schemaBundles.size(); i++) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700109 schemas.add(new AppSearchSchema(schemaBundles.get(i)));
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700110 }
Alexander Dorokhineab789062021-01-11 21:00:00 -0800111 Map<String, List<PackageIdentifier>> schemasPackageAccessible =
112 new ArrayMap<>(schemasPackageAccessibleBundles.size());
113 for (Map.Entry<String, List<Bundle>> entry :
114 schemasPackageAccessibleBundles.entrySet()) {
115 List<PackageIdentifier> packageIdentifiers =
116 new ArrayList<>(entry.getValue().size());
117 for (int i = 0; i < packageIdentifiers.size(); i++) {
118 packageIdentifiers.add(new PackageIdentifier(entry.getValue().get(i)));
119 }
120 schemasPackageAccessible.put(entry.getKey(), packageIdentifiers);
121 }
Cassie Wang308304c2021-01-21 11:13:49 -0800122 AppSearchImpl impl =
123 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhineab789062021-01-11 21:00:00 -0800124 impl.setSchema(
125 packageName,
126 databaseName,
127 schemas,
128 schemasNotPlatformSurfaceable,
129 schemasPackageAccessible,
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800130 forceOverride);
Cassie Wang0c62d992021-01-15 14:39:30 -0800131 invokeCallbackOnResult(
132 callback, AppSearchResult.newSuccessfulResult(/*result=*/ null));
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800133 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800134 invokeCallbackOnError(callback, t);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -0800135 } finally {
136 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800137 }
138 }
139
140 @Override
Terry Wang83a24932020-12-09 21:00:18 -0800141 public void getSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -0800142 @NonNull String packageName,
Terry Wang83a24932020-12-09 21:00:18 -0800143 @NonNull String databaseName,
Terry Wangf2093072020-11-30 04:47:19 -0800144 @UserIdInt int userId,
Terry Wang83a24932020-12-09 21:00:18 -0800145 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800146 Preconditions.checkNotNull(packageName);
Terry Wang83a24932020-12-09 21:00:18 -0800147 Preconditions.checkNotNull(databaseName);
148 Preconditions.checkNotNull(callback);
149 int callingUid = Binder.getCallingUidOrThrow();
Terry Wangf2093072020-11-30 04:47:19 -0800150 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wang83a24932020-12-09 21:00:18 -0800151 final long callingIdentity = Binder.clearCallingIdentity();
152 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800153 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800154 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800155 AppSearchImpl impl =
156 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800157 List<AppSearchSchema> schemas = impl.getSchema(packageName, databaseName);
Terry Wang83a24932020-12-09 21:00:18 -0800158 List<Bundle> schemaBundles = new ArrayList<>(schemas.size());
159 for (int i = 0; i < schemas.size(); i++) {
160 schemaBundles.add(schemas.get(i).getBundle());
161 }
Cassie Wang0c62d992021-01-15 14:39:30 -0800162 invokeCallbackOnResult(
163 callback, AppSearchResult.newSuccessfulResult(schemaBundles));
Terry Wang83a24932020-12-09 21:00:18 -0800164 } catch (Throwable t) {
165 invokeCallbackOnError(callback, t);
166 } finally {
167 Binder.restoreCallingIdentity(callingIdentity);
168 }
169 }
170
171 @Override
Alexander Dorokhine18465842020-01-21 01:08:57 -0800172 public void putDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800173 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700174 @NonNull String databaseName,
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700175 @NonNull List<Bundle> documentBundles,
Terry Wangf2093072020-11-30 04:47:19 -0800176 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800177 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800178 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700179 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700180 Preconditions.checkNotNull(documentBundles);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800181 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800182 int callingUid = Binder.getCallingUid();
183 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600184 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800185 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800186 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800187 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine18465842020-01-21 01:08:57 -0800188 AppSearchBatchResult.Builder<String, Void> resultBuilder =
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800189 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800190 AppSearchImpl impl =
191 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700192 for (int i = 0; i < documentBundles.size(); i++) {
193 GenericDocument document = new GenericDocument(documentBundles.get(i));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800194 try {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800195 // TODO(b/173451571): reduce burden of binder thread by enqueue request onto
196 // a separate thread.
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800197 impl.putDocument(packageName, databaseName, document);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700198 resultBuilder.setSuccess(document.getUri(), /*result=*/ null);
Alexander Dorokhine18465842020-01-21 01:08:57 -0800199 } catch (Throwable t) {
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800200 resultBuilder.setResult(document.getUri(), throwableToFailedResult(t));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800201 }
202 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800203 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800204 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800205 invokeCallbackOnError(callback, t);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800206 } finally {
207 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800208 }
209 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800210
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800211 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800212 public void getDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800213 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800214 @NonNull String databaseName,
215 @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800216 @NonNull List<String> uris,
Alexander Dorokhine87cdd152021-01-20 15:41:25 -0800217 @NonNull Map<String, List<String>> typePropertyPaths,
Terry Wangf2093072020-11-30 04:47:19 -0800218 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800219 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800220 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700221 Preconditions.checkNotNull(databaseName);
222 Preconditions.checkNotNull(namespace);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800223 Preconditions.checkNotNull(uris);
224 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800225 int callingUid = Binder.getCallingUid();
226 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600227 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800228 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800229 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800230 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700231 AppSearchBatchResult.Builder<String, Bundle> resultBuilder =
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800232 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800233 AppSearchImpl impl =
234 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800235 for (int i = 0; i < uris.size(); i++) {
236 String uri = uris.get(i);
237 try {
Cassie Wang308304c2021-01-21 11:13:49 -0800238 GenericDocument document =
239 impl.getDocument(
240 packageName,
241 databaseName,
242 namespace,
243 uri,
244 typePropertyPaths);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700245 resultBuilder.setSuccess(uri, document.getBundle());
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800246 } catch (Throwable t) {
247 resultBuilder.setResult(uri, throwableToFailedResult(t));
248 }
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800249 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800250 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800251 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800252 invokeCallbackOnError(callback, t);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800253 } finally {
254 Binder.restoreCallingIdentity(callingIdentity);
255 }
256 }
257
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800258 // TODO(sidchhabra): Do this in a threadpool.
259 @Override
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800260 public void query(
Cassie Wang0c62d992021-01-15 14:39:30 -0800261 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700262 @NonNull String databaseName,
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700263 @NonNull String queryExpression,
264 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800265 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700266 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800267 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700268 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700269 Preconditions.checkNotNull(queryExpression);
270 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800271 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800272 int callingUid = Binder.getCallingUid();
273 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600274 final long callingIdentity = Binder.clearCallingIdentity();
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800275 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800276 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800277 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800278 AppSearchImpl impl =
279 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800280 SearchResultPage searchResultPage =
281 impl.query(
282 packageName,
283 databaseName,
284 queryExpression,
285 new SearchSpec(searchSpecBundle));
286 invokeCallbackOnResult(
287 callback,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700288 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800289 } catch (Throwable t) {
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700290 invokeCallbackOnError(callback, t);
291 } finally {
292 Binder.restoreCallingIdentity(callingIdentity);
293 }
294 }
295
Terry Wangf2093072020-11-30 04:47:19 -0800296 @Override
Terry Wangbfbfcac2020-11-06 15:46:44 -0800297 public void globalQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800298 @NonNull String packageName,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800299 @NonNull String queryExpression,
300 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800301 @UserIdInt int userId,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800302 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800303 Preconditions.checkNotNull(packageName);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800304 Preconditions.checkNotNull(queryExpression);
305 Preconditions.checkNotNull(searchSpecBundle);
306 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800307 int callingUid = Binder.getCallingUid();
308 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800309 final long callingIdentity = Binder.clearCallingIdentity();
310 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800311 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800312 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800313 AppSearchImpl impl =
314 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
315 SearchResultPage searchResultPage =
316 impl.globalQuery(
317 queryExpression,
318 new SearchSpec(searchSpecBundle),
319 packageName,
320 callingUid);
Cassie Wang0c62d992021-01-15 14:39:30 -0800321 invokeCallbackOnResult(
322 callback,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800323 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
324 } catch (Throwable t) {
325 invokeCallbackOnError(callback, t);
326 } finally {
327 Binder.restoreCallingIdentity(callingIdentity);
328 }
329 }
330
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700331 @Override
Cassie Wang0c62d992021-01-15 14:39:30 -0800332 public void getNextPage(
333 long nextPageToken,
334 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700335 @NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800336 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800337 int callingUid = Binder.getCallingUid();
338 int callingUserId = handleIncomingUser(userId, callingUid);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700339 final long callingIdentity = Binder.clearCallingIdentity();
340 // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally
341 // opened it
342 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800343 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800344 AppSearchImpl impl =
345 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700346 SearchResultPage searchResultPage = impl.getNextPage(nextPageToken);
Cassie Wang0c62d992021-01-15 14:39:30 -0800347 invokeCallbackOnResult(
348 callback,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700349 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
350 } catch (Throwable t) {
351 invokeCallbackOnError(callback, t);
352 } finally {
353 Binder.restoreCallingIdentity(callingIdentity);
354 }
355 }
356
357 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800358 public void invalidateNextPageToken(long nextPageToken, @UserIdInt int userId) {
359 int callingUid = Binder.getCallingUid();
360 int callingUserId = handleIncomingUser(userId, callingUid);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700361 final long callingIdentity = Binder.clearCallingIdentity();
362 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800363 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800364 AppSearchImpl impl =
365 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700366 impl.invalidateNextPageToken(nextPageToken);
367 } catch (Throwable t) {
Terry Wang2da17852020-12-16 19:59:08 -0800368 Log.e(TAG, "Unable to invalidate the query page token", t);
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800369 } finally {
370 Binder.restoreCallingIdentity(callingIdentity);
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800371 }
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800372 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800373
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700374 @Override
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800375 public void reportUsage(
376 @NonNull String packageName,
377 @NonNull String databaseName,
378 @NonNull String namespace,
379 @NonNull String uri,
380 long usageTimeMillis,
381 @UserIdInt int userId,
382 @NonNull IAppSearchResultCallback callback) {
383 Objects.requireNonNull(databaseName);
384 Objects.requireNonNull(namespace);
385 Objects.requireNonNull(uri);
386 Objects.requireNonNull(callback);
387 int callingUid = Binder.getCallingUid();
388 int callingUserId = handleIncomingUser(userId, callingUid);
389 final long callingIdentity = Binder.clearCallingIdentity();
390 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800391 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800392 AppSearchImpl impl =
393 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
394 impl.reportUsage(packageName, databaseName, namespace, uri, usageTimeMillis);
395 invokeCallbackOnResult(
396 callback, AppSearchResult.newSuccessfulResult(/*result=*/ null));
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800397 } catch (Throwable t) {
398 invokeCallbackOnError(callback, t);
399 } finally {
400 Binder.restoreCallingIdentity(callingIdentity);
401 }
402 }
403
404 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800405 public void removeByUri(
Cassie Wang0c62d992021-01-15 14:39:30 -0800406 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800407 @NonNull String databaseName,
408 @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800409 @NonNull List<String> uris,
Terry Wangf2093072020-11-30 04:47:19 -0800410 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800411 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800412 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700413 Preconditions.checkNotNull(databaseName);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700414 Preconditions.checkNotNull(uris);
415 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800416 int callingUid = Binder.getCallingUid();
417 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600418 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700419 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800420 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800421 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800422 AppSearchBatchResult.Builder<String, Void> resultBuilder =
423 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800424 AppSearchImpl impl =
425 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700426 for (int i = 0; i < uris.size(); i++) {
427 String uri = uris.get(i);
428 try {
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800429 impl.remove(packageName, databaseName, namespace, uri);
Cassie Wang0c62d992021-01-15 14:39:30 -0800430 resultBuilder.setSuccess(uri, /*result= */ null);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700431 } catch (Throwable t) {
432 resultBuilder.setResult(uri, throwableToFailedResult(t));
433 }
434 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800435 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700436 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800437 invokeCallbackOnError(callback, t);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700438 } finally {
439 Binder.restoreCallingIdentity(callingIdentity);
440 }
441 }
442
443 @Override
Terry Wang26b9e5c2020-10-23 02:05:01 -0700444 public void removeByQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800445 @NonNull String packageName,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700446 @NonNull String databaseName,
447 @NonNull String queryExpression,
448 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800449 @UserIdInt int userId,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700450 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800451 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700452 Preconditions.checkNotNull(databaseName);
453 Preconditions.checkNotNull(queryExpression);
454 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800455 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800456 int callingUid = Binder.getCallingUid();
457 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600458 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700459 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800460 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800461 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800462 AppSearchImpl impl =
463 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800464 impl.removeByQuery(
465 packageName,
466 databaseName,
467 queryExpression,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700468 new SearchSpec(searchSpecBundle));
469 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700470 } catch (Throwable t) {
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700471 invokeCallbackOnError(callback, t);
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700472 } finally {
473 Binder.restoreCallingIdentity(callingIdentity);
474 }
475 }
476
Terry Wangdbd1dca2020-11-03 17:03:56 -0800477 @Override
Terry Wang2da17852020-12-16 19:59:08 -0800478 public void persistToDisk(@UserIdInt int userId) {
479 int callingUid = Binder.getCallingUidOrThrow();
480 int callingUserId = handleIncomingUser(userId, callingUid);
481 final long callingIdentity = Binder.clearCallingIdentity();
482 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800483 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800484 AppSearchImpl impl =
485 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Terry Wang2da17852020-12-16 19:59:08 -0800486 impl.persistToDisk();
487 } catch (Throwable t) {
488 Log.e(TAG, "Unable to persist the data to disk", t);
489 } finally {
490 Binder.restoreCallingIdentity(callingIdentity);
491 }
492 }
493
494 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800495 public void initialize(@UserIdInt int userId, @NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800496 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800497 int callingUid = Binder.getCallingUid();
498 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800499 final long callingIdentity = Binder.clearCallingIdentity();
500 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800501 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800502 mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800503 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
504 } catch (Throwable t) {
505 invokeCallbackOnError(callback, t);
506 } finally {
507 Binder.restoreCallingIdentity(callingIdentity);
508 }
509 }
510
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800511 private void verifyUserUnlocked(int callingUserId) {
Cassie Wang15c86972021-02-09 13:43:25 -0800512 synchronized (mUnlockedUserIdsLocked) {
513 if (!mUnlockedUserIdsLocked.contains(callingUserId)) {
514 throw new IllegalStateException(
515 "User " + callingUserId + " is locked or not running.");
516 }
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800517 }
518 }
519
Cassie Wang0c62d992021-01-15 14:39:30 -0800520 private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) {
521 Preconditions.checkNotNull(callingPackage);
522 if (mPackageManagerInternal.getPackageUid(
523 callingPackage, /*flags=*/ 0, UserHandle.getUserId(callingUid))
524 != callingUid) {
525 throw new SecurityException(
526 "Specified calling package ["
527 + callingPackage
528 + "] does not match the calling uid "
529 + callingUid);
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700530 }
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700531 }
532
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800533 /** Invokes the {@link IAppSearchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800534 private void invokeCallbackOnResult(
535 IAppSearchResultCallback callback, AppSearchResult<?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800536 try {
537 callback.onResult(result);
538 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800539 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800540 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800541 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800542
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800543 /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800544 private void invokeCallbackOnResult(
545 IAppSearchBatchResultCallback callback, AppSearchBatchResult<?, ?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800546 try {
547 callback.onResult(result);
548 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800549 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800550 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800551 }
552
553 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800554 * Invokes the {@link IAppSearchResultCallback} with an throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800555 *
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800556 * <p>The throwable is convert to a {@link AppSearchResult};
Terry Wangdbd1dca2020-11-03 17:03:56 -0800557 */
558 private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) {
559 try {
560 callback.onResult(throwableToFailedResult(throwable));
561 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800562 Log.e(TAG, "Unable to send result to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800563 }
564 }
565
566 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800567 * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800568 *
569 * <p>The throwable is converted to {@link ParcelableException}.
570 */
Cassie Wang0c62d992021-01-15 14:39:30 -0800571 private void invokeCallbackOnError(
572 IAppSearchBatchResultCallback callback, Throwable throwable) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800573 try {
574 callback.onSystemError(new ParcelableException(throwable));
575 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800576 Log.e(TAG, "Unable to send error to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800577 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800578 }
Terry Wangfebbead2019-10-17 17:05:18 -0700579 }
Terry Wangf2093072020-11-30 04:47:19 -0800580
Cassie Wang0c62d992021-01-15 14:39:30 -0800581 // TODO(b/173553485) verifying that the caller has permission to access target user's data
582 // TODO(b/173553485) Handle ACTION_USER_REMOVED broadcast
583 // TODO(b/173553485) Implement SystemService.onUserStopping()
Terry Wangf2093072020-11-30 04:47:19 -0800584 private static int handleIncomingUser(@UserIdInt int userId, int callingUid) {
585 int callingPid = Binder.getCallingPid();
Cassie Wang0c62d992021-01-15 14:39:30 -0800586 return ActivityManager.handleIncomingUser(
587 callingPid,
588 callingUid,
589 userId,
590 /*allowAll=*/ false,
591 /*requireFull=*/ false,
592 /*name=*/ null,
593 /*callerPackage=*/ null);
Terry Wangf2093072020-11-30 04:47:19 -0800594 }
Terry Wangfebbead2019-10-17 17:05:18 -0700595}