blob: 91ed6cd4a638a264c7764fe6ac600e0f118a1432 [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;
Terry Wang623e3b02021-02-02 20:27:33 -080024import android.app.appsearch.AppSearchMigrationHelper;
Alexander Dorokhine969f4462020-03-05 15:54:19 -080025import android.app.appsearch.AppSearchResult;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070026import android.app.appsearch.AppSearchSchema;
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -070027import android.app.appsearch.GenericDocument;
Alexander Dorokhine9795b512021-03-23 22:06:59 -070028import android.app.appsearch.GetSchemaResponse;
Terry Wangdbd1dca2020-11-03 17:03:56 -080029import android.app.appsearch.IAppSearchBatchResultCallback;
Terry Wangfebbead2019-10-17 17:05:18 -070030import android.app.appsearch.IAppSearchManager;
Terry Wangdbd1dca2020-11-03 17:03:56 -080031import android.app.appsearch.IAppSearchResultCallback;
Alexander Dorokhineab789062021-01-11 21:00:00 -080032import android.app.appsearch.PackageIdentifier;
Terry Wang26b9e5c2020-10-23 02:05:01 -070033import android.app.appsearch.SearchResultPage;
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -070034import android.app.appsearch.SearchSpec;
Terry Wang623e3b02021-02-02 20:27:33 -080035import android.app.appsearch.SetSchemaResponse;
Terry Wangfebbead2019-10-17 17:05:18 -070036import android.content.Context;
Cassie Wang0c62d992021-01-15 14:39:30 -080037import android.content.pm.PackageManagerInternal;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080038import android.os.Binder;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070039import android.os.Bundle;
Terry Wang623e3b02021-02-02 20:27:33 -080040import android.os.ParcelFileDescriptor;
Terry Wangdbd1dca2020-11-03 17:03:56 -080041import android.os.ParcelableException;
42import android.os.RemoteException;
Cassie Wang0c62d992021-01-15 14:39:30 -080043import android.os.UserHandle;
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070044import android.os.UserManager;
Alexander Dorokhineab789062021-01-11 21:00:00 -080045import android.util.ArrayMap;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080046import android.util.ArraySet;
Terry Wangdbd1dca2020-11-03 17:03:56 -080047import android.util.Log;
Terry Wangfebbead2019-10-17 17:05:18 -070048
Cassie Wang15c86972021-02-09 13:43:25 -080049import com.android.internal.annotations.GuardedBy;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080050import com.android.internal.util.Preconditions;
Cassie Wang0c62d992021-01-15 14:39:30 -080051import com.android.server.LocalServices;
Terry Wangfebbead2019-10-17 17:05:18 -070052import com.android.server.SystemService;
Alexander Dorokhinef660d8f2020-10-29 22:37:00 -070053import com.android.server.appsearch.external.localstorage.AppSearchImpl;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080054
Terry Wang623e3b02021-02-02 20:27:33 -080055import java.io.DataInputStream;
56import java.io.DataOutputStream;
57import java.io.EOFException;
58import java.io.FileInputStream;
59import java.io.FileOutputStream;
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080060import java.util.ArrayList;
Alexander Dorokhine18465842020-01-21 01:08:57 -080061import java.util.List;
Alexander Dorokhineab789062021-01-11 21:00:00 -080062import java.util.Map;
Alexander Dorokhined18f8842021-01-20 15:26:13 -080063import java.util.Objects;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080064import java.util.Set;
Alexander Dorokhine18465842020-01-21 01:08:57 -080065
Cassie Wang0c62d992021-01-15 14:39:30 -080066/** TODO(b/142567528): add comments when implement this class */
Terry Wangfebbead2019-10-17 17:05:18 -070067public class AppSearchManagerService extends SystemService {
Alexander Dorokhineebd37742020-09-22 15:02:26 -070068 private static final String TAG = "AppSearchManagerService";
Cassie Wang0c62d992021-01-15 14:39:30 -080069 private PackageManagerInternal mPackageManagerInternal;
Cassie Wang21c2d6a2021-01-20 23:59:55 -080070 private ImplInstanceManager mImplInstanceManager;
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070071 private UserManager mUserManager;
Terry Wangfebbead2019-10-17 17:05:18 -070072
Cassie Wang15c86972021-02-09 13:43:25 -080073 // Cache of unlocked user ids so we don't have to query UserManager service each time. The
74 // "locked" suffix refers to the fact that access to the field should be locked; unrelated to
75 // the unlocked status of user ids.
76 @GuardedBy("mUnlockedUserIdsLocked")
77 private final Set<Integer> mUnlockedUserIdsLocked = new ArraySet<>();
Cassie Wang9ba9ae12021-02-01 16:39:37 -080078
Terry Wangfebbead2019-10-17 17:05:18 -070079 public AppSearchManagerService(Context context) {
80 super(context);
81 }
82
83 @Override
84 public void onStart() {
85 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
Cassie Wang0c62d992021-01-15 14:39:30 -080086 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Cassie Wang308304c2021-01-21 11:13:49 -080087 mImplInstanceManager = ImplInstanceManager.getInstance(getContext());
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070088 mUserManager = getContext().getSystemService(UserManager.class);
Terry Wangfebbead2019-10-17 17:05:18 -070089 }
90
Cassie Wang9ba9ae12021-02-01 16:39:37 -080091 @Override
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070092 public void onUserUnlocking(@NonNull TargetUser user) {
Cassie Wang15c86972021-02-09 13:43:25 -080093 synchronized (mUnlockedUserIdsLocked) {
94 mUnlockedUserIdsLocked.add(user.getUserIdentifier());
95 }
Cassie Wang9ba9ae12021-02-01 16:39:37 -080096 }
97
Terry Wangfebbead2019-10-17 17:05:18 -070098 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080099 @Override
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800100 public void setSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -0800101 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700102 @NonNull String databaseName,
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700103 @NonNull List<Bundle> schemaBundles,
Alexander Dorokhine315cca62021-03-04 12:34:41 -0800104 @NonNull List<String> schemasNotDisplayedBySystem,
Alexander Dorokhineab789062021-01-11 21:00:00 -0800105 @NonNull Map<String, List<Bundle>> schemasPackageAccessibleBundles,
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800106 boolean forceOverride,
Terry Wangf2093072020-11-30 04:47:19 -0800107 @UserIdInt int userId,
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700108 int schemaVersion,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800109 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800110 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700111 Preconditions.checkNotNull(databaseName);
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700112 Preconditions.checkNotNull(schemaBundles);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800113 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800114 int callingUid = Binder.getCallingUid();
115 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600116 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800117 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800118 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800119 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine6a99f942020-12-04 02:57:22 -0800120 List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size());
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700121 for (int i = 0; i < schemaBundles.size(); i++) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700122 schemas.add(new AppSearchSchema(schemaBundles.get(i)));
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700123 }
Alexander Dorokhineab789062021-01-11 21:00:00 -0800124 Map<String, List<PackageIdentifier>> schemasPackageAccessible =
125 new ArrayMap<>(schemasPackageAccessibleBundles.size());
126 for (Map.Entry<String, List<Bundle>> entry :
127 schemasPackageAccessibleBundles.entrySet()) {
128 List<PackageIdentifier> packageIdentifiers =
129 new ArrayList<>(entry.getValue().size());
Cassie Wange8569db2021-02-05 00:05:25 -0800130 for (int i = 0; i < entry.getValue().size(); i++) {
Alexander Dorokhineab789062021-01-11 21:00:00 -0800131 packageIdentifiers.add(new PackageIdentifier(entry.getValue().get(i)));
132 }
133 schemasPackageAccessible.put(entry.getKey(), packageIdentifiers);
134 }
Terry Wang9d8f4272021-02-04 21:01:10 -0800135 AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUserId);
Terry Wang623e3b02021-02-02 20:27:33 -0800136 SetSchemaResponse setSchemaResponse = impl.setSchema(
Alexander Dorokhineab789062021-01-11 21:00:00 -0800137 packageName,
138 databaseName,
139 schemas,
Alexander Dorokhine315cca62021-03-04 12:34:41 -0800140 schemasNotDisplayedBySystem,
Alexander Dorokhineab789062021-01-11 21:00:00 -0800141 schemasPackageAccessible,
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700142 forceOverride,
143 schemaVersion);
Terry Wang623e3b02021-02-02 20:27:33 -0800144 invokeCallbackOnResult(callback,
145 AppSearchResult.newSuccessfulResult(setSchemaResponse.getBundle()));
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800146 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800147 invokeCallbackOnError(callback, t);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -0800148 } finally {
149 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800150 }
151 }
152
153 @Override
Terry Wang83a24932020-12-09 21:00:18 -0800154 public void getSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -0800155 @NonNull String packageName,
Terry Wang83a24932020-12-09 21:00:18 -0800156 @NonNull String databaseName,
Terry Wangf2093072020-11-30 04:47:19 -0800157 @UserIdInt int userId,
Terry Wang83a24932020-12-09 21:00:18 -0800158 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800159 Preconditions.checkNotNull(packageName);
Terry Wang83a24932020-12-09 21:00:18 -0800160 Preconditions.checkNotNull(databaseName);
161 Preconditions.checkNotNull(callback);
162 int callingUid = Binder.getCallingUidOrThrow();
Terry Wangf2093072020-11-30 04:47:19 -0800163 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wang83a24932020-12-09 21:00:18 -0800164 final long callingIdentity = Binder.clearCallingIdentity();
165 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800166 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800167 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800168 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800169 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700170 GetSchemaResponse response = impl.getSchema(packageName, databaseName);
Cassie Wang0c62d992021-01-15 14:39:30 -0800171 invokeCallbackOnResult(
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700172 callback, AppSearchResult.newSuccessfulResult(response.getBundle()));
173 } catch (Throwable t) {
174 invokeCallbackOnError(callback, t);
175 } finally {
176 Binder.restoreCallingIdentity(callingIdentity);
177 }
178 }
179
180 @Override
181 public void getNamespaces(
182 @NonNull String packageName,
183 @NonNull String databaseName,
184 @UserIdInt int userId,
185 @NonNull IAppSearchResultCallback callback) {
186 Preconditions.checkNotNull(packageName);
187 Preconditions.checkNotNull(databaseName);
188 Preconditions.checkNotNull(callback);
189 int callingUid = Binder.getCallingUidOrThrow();
190 int callingUserId = handleIncomingUser(userId, callingUid);
191 final long callingIdentity = Binder.clearCallingIdentity();
192 try {
193 verifyUserUnlocked(callingUserId);
194 verifyCallingPackage(callingUid, packageName);
195 AppSearchImpl impl =
196 mImplInstanceManager.getAppSearchImpl(callingUserId);
197 List<String> namespaces = impl.getNamespaces(packageName, databaseName);
198 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(namespaces));
Terry Wang83a24932020-12-09 21:00:18 -0800199 } catch (Throwable t) {
200 invokeCallbackOnError(callback, t);
201 } finally {
202 Binder.restoreCallingIdentity(callingIdentity);
203 }
204 }
205
206 @Override
Alexander Dorokhine18465842020-01-21 01:08:57 -0800207 public void putDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800208 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700209 @NonNull String databaseName,
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700210 @NonNull List<Bundle> documentBundles,
Terry Wangf2093072020-11-30 04:47:19 -0800211 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800212 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800213 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700214 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700215 Preconditions.checkNotNull(documentBundles);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800216 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800217 int callingUid = Binder.getCallingUid();
218 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600219 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800220 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800221 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800222 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine18465842020-01-21 01:08:57 -0800223 AppSearchBatchResult.Builder<String, Void> resultBuilder =
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800224 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800225 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800226 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700227 for (int i = 0; i < documentBundles.size(); i++) {
228 GenericDocument document = new GenericDocument(documentBundles.get(i));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800229 try {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800230 // TODO(b/173451571): reduce burden of binder thread by enqueue request onto
231 // a separate thread.
Alexander Dorokhine3488b9a2021-03-16 23:57:49 -0700232 impl.putDocument(packageName, databaseName, document, /*logger=*/ null);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700233 resultBuilder.setSuccess(document.getUri(), /*result=*/ null);
Alexander Dorokhine18465842020-01-21 01:08:57 -0800234 } catch (Throwable t) {
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800235 resultBuilder.setResult(document.getUri(), throwableToFailedResult(t));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800236 }
237 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800238 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800239 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800240 invokeCallbackOnError(callback, t);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800241 } finally {
242 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800243 }
244 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800245
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800246 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800247 public void getDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800248 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800249 @NonNull String databaseName,
250 @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800251 @NonNull List<String> uris,
Alexander Dorokhine87cdd152021-01-20 15:41:25 -0800252 @NonNull Map<String, List<String>> typePropertyPaths,
Terry Wangf2093072020-11-30 04:47:19 -0800253 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800254 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800255 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700256 Preconditions.checkNotNull(databaseName);
257 Preconditions.checkNotNull(namespace);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800258 Preconditions.checkNotNull(uris);
259 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800260 int callingUid = Binder.getCallingUid();
261 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600262 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800263 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800264 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800265 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700266 AppSearchBatchResult.Builder<String, Bundle> resultBuilder =
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800267 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800268 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800269 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800270 for (int i = 0; i < uris.size(); i++) {
271 String uri = uris.get(i);
272 try {
Cassie Wang308304c2021-01-21 11:13:49 -0800273 GenericDocument document =
274 impl.getDocument(
275 packageName,
276 databaseName,
277 namespace,
278 uri,
279 typePropertyPaths);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700280 resultBuilder.setSuccess(uri, document.getBundle());
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800281 } catch (Throwable t) {
282 resultBuilder.setResult(uri, throwableToFailedResult(t));
283 }
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800284 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800285 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800286 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800287 invokeCallbackOnError(callback, t);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800288 } finally {
289 Binder.restoreCallingIdentity(callingIdentity);
290 }
291 }
292
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800293 // TODO(sidchhabra): Do this in a threadpool.
294 @Override
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800295 public void query(
Cassie Wang0c62d992021-01-15 14:39:30 -0800296 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700297 @NonNull String databaseName,
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700298 @NonNull String queryExpression,
299 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800300 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700301 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800302 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700303 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700304 Preconditions.checkNotNull(queryExpression);
305 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800306 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800307 int callingUid = Binder.getCallingUid();
308 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600309 final long callingIdentity = Binder.clearCallingIdentity();
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800310 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 =
Terry Wang9d8f4272021-02-04 21:01:10 -0800314 mImplInstanceManager.getAppSearchImpl(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800315 SearchResultPage searchResultPage =
316 impl.query(
317 packageName,
318 databaseName,
319 queryExpression,
320 new SearchSpec(searchSpecBundle));
321 invokeCallbackOnResult(
322 callback,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700323 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800324 } catch (Throwable t) {
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700325 invokeCallbackOnError(callback, t);
326 } finally {
327 Binder.restoreCallingIdentity(callingIdentity);
328 }
329 }
330
Terry Wangf2093072020-11-30 04:47:19 -0800331 @Override
Terry Wangbfbfcac2020-11-06 15:46:44 -0800332 public void globalQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800333 @NonNull String packageName,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800334 @NonNull String queryExpression,
335 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800336 @UserIdInt int userId,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800337 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800338 Preconditions.checkNotNull(packageName);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800339 Preconditions.checkNotNull(queryExpression);
340 Preconditions.checkNotNull(searchSpecBundle);
341 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800342 int callingUid = Binder.getCallingUid();
343 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800344 final long callingIdentity = Binder.clearCallingIdentity();
345 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800346 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800347 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800348 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800349 mImplInstanceManager.getAppSearchImpl(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800350 SearchResultPage searchResultPage =
351 impl.globalQuery(
352 queryExpression,
353 new SearchSpec(searchSpecBundle),
354 packageName,
355 callingUid);
Cassie Wang0c62d992021-01-15 14:39:30 -0800356 invokeCallbackOnResult(
357 callback,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800358 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
359 } catch (Throwable t) {
360 invokeCallbackOnError(callback, t);
361 } finally {
362 Binder.restoreCallingIdentity(callingIdentity);
363 }
364 }
365
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700366 @Override
Cassie Wang0c62d992021-01-15 14:39:30 -0800367 public void getNextPage(
368 long nextPageToken,
369 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700370 @NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800371 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800372 int callingUid = Binder.getCallingUid();
373 int callingUserId = handleIncomingUser(userId, callingUid);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700374 final long callingIdentity = Binder.clearCallingIdentity();
375 // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally
376 // opened it
377 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800378 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800379 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800380 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700381 SearchResultPage searchResultPage = impl.getNextPage(nextPageToken);
Cassie Wang0c62d992021-01-15 14:39:30 -0800382 invokeCallbackOnResult(
383 callback,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700384 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
385 } catch (Throwable t) {
386 invokeCallbackOnError(callback, t);
387 } finally {
388 Binder.restoreCallingIdentity(callingIdentity);
389 }
390 }
391
392 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800393 public void invalidateNextPageToken(long nextPageToken, @UserIdInt int userId) {
394 int callingUid = Binder.getCallingUid();
395 int callingUserId = handleIncomingUser(userId, callingUid);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700396 final long callingIdentity = Binder.clearCallingIdentity();
397 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800398 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800399 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800400 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700401 impl.invalidateNextPageToken(nextPageToken);
402 } catch (Throwable t) {
Terry Wang2da17852020-12-16 19:59:08 -0800403 Log.e(TAG, "Unable to invalidate the query page token", t);
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800404 } finally {
405 Binder.restoreCallingIdentity(callingIdentity);
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800406 }
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800407 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800408
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700409 @Override
Terry Wang623e3b02021-02-02 20:27:33 -0800410 public void writeQueryResultsToFile(
411 @NonNull String packageName,
412 @NonNull String databaseName,
413 @NonNull ParcelFileDescriptor fileDescriptor,
414 @NonNull String queryExpression,
415 @NonNull Bundle searchSpecBundle,
416 @UserIdInt int userId,
417 @NonNull IAppSearchResultCallback callback) {
418 int callingUid = Binder.getCallingUid();
419 int callingUserId = handleIncomingUser(userId, callingUid);
420 final long callingIdentity = Binder.clearCallingIdentity();
421 try {
422 verifyCallingPackage(callingUid, packageName);
423 AppSearchImpl impl =
424 mImplInstanceManager.getAppSearchImpl(callingUserId);
425 // we don't need to append the file. The file is always brand new.
426 try (DataOutputStream outputStream = new DataOutputStream(
427 new FileOutputStream(fileDescriptor.getFileDescriptor()))) {
428 SearchResultPage searchResultPage = impl.query(
429 packageName,
430 databaseName,
431 queryExpression,
432 new SearchSpec(searchSpecBundle));
433 while (!searchResultPage.getResults().isEmpty()) {
434 for (int i = 0; i < searchResultPage.getResults().size(); i++) {
435 AppSearchMigrationHelper.writeBundleToOutputStream(
436 outputStream, searchResultPage.getResults().get(i)
437 .getGenericDocument().getBundle());
438 }
439 searchResultPage = impl.getNextPage(searchResultPage.getNextPageToken());
440 }
441 }
442 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
443 } catch (Throwable t) {
444 invokeCallbackOnError(callback, t);
445 } finally {
446 Binder.restoreCallingIdentity(callingIdentity);
447 }
448 }
449
450 @Override
451 public void putDocumentsFromFile(
452 @NonNull String packageName,
453 @NonNull String databaseName,
454 @NonNull ParcelFileDescriptor fileDescriptor,
455 @UserIdInt int userId,
456 @NonNull IAppSearchResultCallback callback) {
457 int callingUid = Binder.getCallingUid();
458 int callingUserId = handleIncomingUser(userId, callingUid);
459 final long callingIdentity = Binder.clearCallingIdentity();
460 try {
461 verifyCallingPackage(callingUid, packageName);
462 AppSearchImpl impl =
463 mImplInstanceManager.getAppSearchImpl(callingUserId);
464
465 GenericDocument document;
466 ArrayList<Bundle> migrationFailureBundles = new ArrayList<>();
467 try (DataInputStream inputStream = new DataInputStream(
468 new FileInputStream(fileDescriptor.getFileDescriptor()))) {
469 while (true) {
470 try {
471 document = AppSearchMigrationHelper
472 .readDocumentFromInputStream(inputStream);
473 } catch (EOFException e) {
474 // nothing wrong, we just finish the reading.
475 break;
476 }
477 try {
478 impl.putDocument(packageName, databaseName, document, /*logger=*/ null);
479 } catch (Throwable t) {
480 migrationFailureBundles.add(
481 new SetSchemaResponse.MigrationFailure.Builder()
482 .setNamespace(document.getNamespace())
483 .setSchemaType(document.getSchemaType())
484 .setUri(document.getUri())
485 .setAppSearchResult(
486 AppSearchResult.throwableToFailedResult(t))
487 .build().getBundle());
488 }
489 }
490 }
491 impl.persistToDisk();
492 invokeCallbackOnResult(callback,
493 AppSearchResult.newSuccessfulResult(migrationFailureBundles));
494 } catch (Throwable t) {
495 invokeCallbackOnError(callback, t);
496 } finally {
497 Binder.restoreCallingIdentity(callingIdentity);
498 }
499 }
500
501 @Override
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800502 public void reportUsage(
503 @NonNull String packageName,
504 @NonNull String databaseName,
505 @NonNull String namespace,
506 @NonNull String uri,
507 long usageTimeMillis,
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700508 boolean systemUsage,
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800509 @UserIdInt int userId,
510 @NonNull IAppSearchResultCallback callback) {
511 Objects.requireNonNull(databaseName);
512 Objects.requireNonNull(namespace);
513 Objects.requireNonNull(uri);
514 Objects.requireNonNull(callback);
515 int callingUid = Binder.getCallingUid();
516 int callingUserId = handleIncomingUser(userId, callingUid);
517 final long callingIdentity = Binder.clearCallingIdentity();
518 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800519 verifyUserUnlocked(callingUserId);
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700520
521 if (systemUsage) {
522 // TODO(b/183031844): Validate that the call comes from the system
523 }
524
Cassie Wang308304c2021-01-21 11:13:49 -0800525 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800526 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700527 impl.reportUsage(
528 packageName, databaseName, namespace, uri,
529 usageTimeMillis, systemUsage);
Cassie Wang308304c2021-01-21 11:13:49 -0800530 invokeCallbackOnResult(
531 callback, AppSearchResult.newSuccessfulResult(/*result=*/ null));
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800532 } catch (Throwable t) {
533 invokeCallbackOnError(callback, t);
534 } finally {
535 Binder.restoreCallingIdentity(callingIdentity);
536 }
537 }
538
539 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800540 public void removeByUri(
Cassie Wang0c62d992021-01-15 14:39:30 -0800541 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800542 @NonNull String databaseName,
543 @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800544 @NonNull List<String> uris,
Terry Wangf2093072020-11-30 04:47:19 -0800545 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800546 @NonNull IAppSearchBatchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800547 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700548 Preconditions.checkNotNull(databaseName);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700549 Preconditions.checkNotNull(uris);
550 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800551 int callingUid = Binder.getCallingUid();
552 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600553 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700554 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800555 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800556 verifyCallingPackage(callingUid, packageName);
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800557 AppSearchBatchResult.Builder<String, Void> resultBuilder =
558 new AppSearchBatchResult.Builder<>();
Cassie Wang308304c2021-01-21 11:13:49 -0800559 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800560 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700561 for (int i = 0; i < uris.size(); i++) {
562 String uri = uris.get(i);
563 try {
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800564 impl.remove(packageName, databaseName, namespace, uri);
Cassie Wang0c62d992021-01-15 14:39:30 -0800565 resultBuilder.setSuccess(uri, /*result= */ null);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700566 } catch (Throwable t) {
567 resultBuilder.setResult(uri, throwableToFailedResult(t));
568 }
569 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800570 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700571 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800572 invokeCallbackOnError(callback, t);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700573 } finally {
574 Binder.restoreCallingIdentity(callingIdentity);
575 }
576 }
577
578 @Override
Terry Wang26b9e5c2020-10-23 02:05:01 -0700579 public void removeByQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800580 @NonNull String packageName,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700581 @NonNull String databaseName,
582 @NonNull String queryExpression,
583 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800584 @UserIdInt int userId,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700585 @NonNull IAppSearchResultCallback callback) {
Cassie Wang0c62d992021-01-15 14:39:30 -0800586 Preconditions.checkNotNull(packageName);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700587 Preconditions.checkNotNull(databaseName);
588 Preconditions.checkNotNull(queryExpression);
589 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800590 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800591 int callingUid = Binder.getCallingUid();
592 int callingUserId = handleIncomingUser(userId, callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600593 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700594 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800595 verifyUserUnlocked(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800596 verifyCallingPackage(callingUid, packageName);
Cassie Wang308304c2021-01-21 11:13:49 -0800597 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800598 mImplInstanceManager.getAppSearchImpl(callingUserId);
Cassie Wang0c62d992021-01-15 14:39:30 -0800599 impl.removeByQuery(
600 packageName,
601 databaseName,
602 queryExpression,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700603 new SearchSpec(searchSpecBundle));
604 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700605 } catch (Throwable t) {
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700606 invokeCallbackOnError(callback, t);
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700607 } finally {
608 Binder.restoreCallingIdentity(callingIdentity);
609 }
610 }
611
Terry Wangdbd1dca2020-11-03 17:03:56 -0800612 @Override
Terry Wang2da17852020-12-16 19:59:08 -0800613 public void persistToDisk(@UserIdInt int userId) {
614 int callingUid = Binder.getCallingUidOrThrow();
615 int callingUserId = handleIncomingUser(userId, callingUid);
616 final long callingIdentity = Binder.clearCallingIdentity();
617 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800618 verifyUserUnlocked(callingUserId);
Cassie Wang308304c2021-01-21 11:13:49 -0800619 AppSearchImpl impl =
Terry Wang9d8f4272021-02-04 21:01:10 -0800620 mImplInstanceManager.getAppSearchImpl(callingUserId);
Terry Wang2da17852020-12-16 19:59:08 -0800621 impl.persistToDisk();
622 } catch (Throwable t) {
623 Log.e(TAG, "Unable to persist the data to disk", t);
624 } finally {
625 Binder.restoreCallingIdentity(callingIdentity);
626 }
627 }
628
629 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800630 public void initialize(@UserIdInt int userId, @NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800631 Preconditions.checkNotNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800632 int callingUid = Binder.getCallingUid();
633 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800634 final long callingIdentity = Binder.clearCallingIdentity();
635 try {
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800636 verifyUserUnlocked(callingUserId);
Terry Wang9d8f4272021-02-04 21:01:10 -0800637 mImplInstanceManager.getOrCreateAppSearchImpl(getContext(), callingUserId);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800638 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
639 } catch (Throwable t) {
640 invokeCallbackOnError(callback, t);
641 } finally {
642 Binder.restoreCallingIdentity(callingIdentity);
643 }
644 }
645
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800646 private void verifyUserUnlocked(int callingUserId) {
Cassie Wang15c86972021-02-09 13:43:25 -0800647 synchronized (mUnlockedUserIdsLocked) {
Pinyao Tingd5c2ed92021-03-18 14:51:54 -0700648 // First, check the local copy.
649 if (mUnlockedUserIdsLocked.contains(callingUserId)) {
650 return;
651 }
652 // If the local copy says the user is locked, check with UM for the actual state,
653 // since the user might just have been unlocked.
654 if (!mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(callingUserId))) {
Cassie Wang15c86972021-02-09 13:43:25 -0800655 throw new IllegalStateException(
656 "User " + callingUserId + " is locked or not running.");
657 }
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800658 }
659 }
660
Cassie Wang0c62d992021-01-15 14:39:30 -0800661 private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) {
662 Preconditions.checkNotNull(callingPackage);
663 if (mPackageManagerInternal.getPackageUid(
664 callingPackage, /*flags=*/ 0, UserHandle.getUserId(callingUid))
665 != callingUid) {
666 throw new SecurityException(
667 "Specified calling package ["
668 + callingPackage
669 + "] does not match the calling uid "
670 + callingUid);
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700671 }
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700672 }
673
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800674 /** Invokes the {@link IAppSearchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800675 private void invokeCallbackOnResult(
676 IAppSearchResultCallback callback, AppSearchResult<?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800677 try {
678 callback.onResult(result);
679 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800680 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800681 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800682 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800683
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800684 /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800685 private void invokeCallbackOnResult(
686 IAppSearchBatchResultCallback callback, AppSearchBatchResult<?, ?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800687 try {
688 callback.onResult(result);
689 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800690 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800691 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800692 }
693
694 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800695 * Invokes the {@link IAppSearchResultCallback} with an throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800696 *
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800697 * <p>The throwable is convert to a {@link AppSearchResult};
Terry Wangdbd1dca2020-11-03 17:03:56 -0800698 */
699 private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) {
700 try {
701 callback.onResult(throwableToFailedResult(throwable));
702 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800703 Log.e(TAG, "Unable to send result to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800704 }
705 }
706
707 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800708 * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800709 *
710 * <p>The throwable is converted to {@link ParcelableException}.
711 */
Cassie Wang0c62d992021-01-15 14:39:30 -0800712 private void invokeCallbackOnError(
713 IAppSearchBatchResultCallback callback, Throwable throwable) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800714 try {
Terry Wang9d8f4272021-02-04 21:01:10 -0800715 //TODO(b/175067650) verify ParcelableException could propagate throwable correctly.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800716 callback.onSystemError(new ParcelableException(throwable));
717 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800718 Log.e(TAG, "Unable to send error to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800719 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800720 }
Terry Wangfebbead2019-10-17 17:05:18 -0700721 }
Terry Wangf2093072020-11-30 04:47:19 -0800722
Cassie Wang0c62d992021-01-15 14:39:30 -0800723 // TODO(b/173553485) verifying that the caller has permission to access target user's data
724 // TODO(b/173553485) Handle ACTION_USER_REMOVED broadcast
725 // TODO(b/173553485) Implement SystemService.onUserStopping()
Terry Wangf2093072020-11-30 04:47:19 -0800726 private static int handleIncomingUser(@UserIdInt int userId, int callingUid) {
727 int callingPid = Binder.getCallingPid();
Cassie Wang0c62d992021-01-15 14:39:30 -0800728 return ActivityManager.handleIncomingUser(
729 callingPid,
730 callingUid,
731 userId,
732 /*allowAll=*/ false,
733 /*requireFull=*/ false,
734 /*name=*/ null,
735 /*callerPackage=*/ null);
Terry Wangf2093072020-11-30 04:47:19 -0800736 }
Terry Wangfebbead2019-10-17 17:05:18 -0700737}