blob: 3f6e8a5a79060416e0b8fe038beed9c53779d7cc [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;
Terry Wange201dc02021-04-16 01:03:20 -070019import static android.os.Process.INVALID_UID;
Terry Wang12dc6c02021-03-31 19:26:16 -070020import static android.os.UserHandle.USER_NULL;
Terry Wangdbd1dca2020-11-03 17:03:56 -080021
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -080022import android.annotation.ElapsedRealtimeLong;
sidchhabraa7c8f8a2020-01-16 18:38:17 -080023import android.annotation.NonNull;
Terry Wangf2093072020-11-30 04:47:19 -080024import android.annotation.UserIdInt;
25import android.app.ActivityManager;
Alexander Dorokhine18465842020-01-21 01:08:57 -080026import android.app.appsearch.AppSearchBatchResult;
Terry Wang623e3b02021-02-02 20:27:33 -080027import android.app.appsearch.AppSearchMigrationHelper;
Alexander Dorokhine969f4462020-03-05 15:54:19 -080028import android.app.appsearch.AppSearchResult;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070029import android.app.appsearch.AppSearchSchema;
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -070030import android.app.appsearch.GenericDocument;
Alexander Dorokhine9795b512021-03-23 22:06:59 -070031import android.app.appsearch.GetSchemaResponse;
Terry Wangdbd1dca2020-11-03 17:03:56 -080032import android.app.appsearch.IAppSearchBatchResultCallback;
Terry Wangfebbead2019-10-17 17:05:18 -070033import android.app.appsearch.IAppSearchManager;
Terry Wangdbd1dca2020-11-03 17:03:56 -080034import android.app.appsearch.IAppSearchResultCallback;
Alexander Dorokhineab789062021-01-11 21:00:00 -080035import android.app.appsearch.PackageIdentifier;
Terry Wang26b9e5c2020-10-23 02:05:01 -070036import android.app.appsearch.SearchResultPage;
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -070037import android.app.appsearch.SearchSpec;
Terry Wang623e3b02021-02-02 20:27:33 -080038import android.app.appsearch.SetSchemaResponse;
Cassie Wang8f0df492021-03-24 09:23:18 -070039import android.app.appsearch.StorageInfo;
Terry Wang12dc6c02021-03-31 19:26:16 -070040import android.content.BroadcastReceiver;
Terry Wangfebbead2019-10-17 17:05:18 -070041import android.content.Context;
Terry Wang12dc6c02021-03-31 19:26:16 -070042import android.content.Intent;
43import android.content.IntentFilter;
Yang Yu0fcd51a2021-04-23 11:25:44 -070044import android.content.pm.PackageManager;
Cassie Wang0c62d992021-01-15 14:39:30 -080045import android.content.pm.PackageManagerInternal;
Yang Yu0fcd51a2021-04-23 11:25:44 -070046import android.content.pm.PackageStats;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080047import android.os.Binder;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070048import android.os.Bundle;
Terry Wang623e3b02021-02-02 20:27:33 -080049import android.os.ParcelFileDescriptor;
Terry Wangdbd1dca2020-11-03 17:03:56 -080050import android.os.RemoteException;
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -080051import android.os.SystemClock;
Cassie Wang0c62d992021-01-15 14:39:30 -080052import android.os.UserHandle;
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070053import android.os.UserManager;
Alexander Dorokhineab789062021-01-11 21:00:00 -080054import android.util.ArrayMap;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080055import android.util.ArraySet;
Terry Wangdbd1dca2020-11-03 17:03:56 -080056import android.util.Log;
Terry Wangfebbead2019-10-17 17:05:18 -070057
Cassie Wang15c86972021-02-09 13:43:25 -080058import com.android.internal.annotations.GuardedBy;
Cassie Wang0c62d992021-01-15 14:39:30 -080059import com.android.server.LocalServices;
Terry Wangfebbead2019-10-17 17:05:18 -070060import com.android.server.SystemService;
Alexander Dorokhinef660d8f2020-10-29 22:37:00 -070061import com.android.server.appsearch.external.localstorage.AppSearchImpl;
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -080062import com.android.server.appsearch.external.localstorage.stats.CallStats;
63import com.android.server.appsearch.stats.LoggerInstanceManager;
64import com.android.server.appsearch.stats.PlatformLogger;
Yang Yu0fcd51a2021-04-23 11:25:44 -070065import com.android.server.usage.StorageStatsManagerInternal;
66import com.android.server.usage.StorageStatsManagerInternal.StorageStatsAugmenter;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080067
Alexander Dorokhinec77f4442021-04-14 09:26:06 -070068import com.google.android.icing.proto.PersistType;
69
Terry Wang623e3b02021-02-02 20:27:33 -080070import java.io.DataInputStream;
71import java.io.DataOutputStream;
72import java.io.EOFException;
73import java.io.FileInputStream;
74import java.io.FileOutputStream;
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080075import java.util.ArrayList;
Alexander Dorokhine18465842020-01-21 01:08:57 -080076import java.util.List;
Alexander Dorokhineab789062021-01-11 21:00:00 -080077import java.util.Map;
Alexander Dorokhined18f8842021-01-20 15:26:13 -080078import java.util.Objects;
Cassie Wang9ba9ae12021-02-01 16:39:37 -080079import java.util.Set;
Terry Wange04ceab2021-03-29 19:25:12 -070080import java.util.concurrent.Executor;
Terry Wangd2186e52021-04-14 13:19:45 -070081import java.util.concurrent.LinkedBlockingQueue;
Terry Wange04ceab2021-03-29 19:25:12 -070082import java.util.concurrent.ThreadPoolExecutor;
83import java.util.concurrent.TimeUnit;
Alexander Dorokhine18465842020-01-21 01:08:57 -080084
Cassie Wang0c62d992021-01-15 14:39:30 -080085/** TODO(b/142567528): add comments when implement this class */
Terry Wangfebbead2019-10-17 17:05:18 -070086public class AppSearchManagerService extends SystemService {
Alexander Dorokhineebd37742020-09-22 15:02:26 -070087 private static final String TAG = "AppSearchManagerService";
Terry Wang12dc6c02021-03-31 19:26:16 -070088 private final Context mContext;
Yang Yu0fcd51a2021-04-23 11:25:44 -070089 private PackageManager mPackageManager;
Cassie Wang0c62d992021-01-15 14:39:30 -080090 private PackageManagerInternal mPackageManagerInternal;
Cassie Wang21c2d6a2021-01-20 23:59:55 -080091 private ImplInstanceManager mImplInstanceManager;
Pinyao Tingd5c2ed92021-03-18 14:51:54 -070092 private UserManager mUserManager;
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -080093 private LoggerInstanceManager mLoggerInstanceManager;
Terry Wangfebbead2019-10-17 17:05:18 -070094
Terry Wange04ceab2021-03-29 19:25:12 -070095 // Never call shutdownNow(). It will cancel the futures it's returned. And since
96 // Executor#execute won't return anything, we will hang forever waiting for the execution.
97 // AppSearch multi-thread execution is guarded by Read & Write Lock in AppSearchImpl, all
98 // mutate requests will need to gain write lock and query requests need to gain read lock.
99 private static final Executor EXECUTOR = new ThreadPoolExecutor(/*corePoolSize=*/1,
100 Runtime.getRuntime().availableProcessors(), /*keepAliveTime*/ 60L, TimeUnit.SECONDS,
Terry Wangd2186e52021-04-14 13:19:45 -0700101 new LinkedBlockingQueue<>());
Terry Wange04ceab2021-03-29 19:25:12 -0700102
Cassie Wang15c86972021-02-09 13:43:25 -0800103 // Cache of unlocked user ids so we don't have to query UserManager service each time. The
104 // "locked" suffix refers to the fact that access to the field should be locked; unrelated to
105 // the unlocked status of user ids.
106 @GuardedBy("mUnlockedUserIdsLocked")
107 private final Set<Integer> mUnlockedUserIdsLocked = new ArraySet<>();
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800108
Terry Wangfebbead2019-10-17 17:05:18 -0700109 public AppSearchManagerService(Context context) {
110 super(context);
Terry Wang12dc6c02021-03-31 19:26:16 -0700111 mContext = context;
Terry Wangfebbead2019-10-17 17:05:18 -0700112 }
113
114 @Override
115 public void onStart() {
116 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
Yang Yu0fcd51a2021-04-23 11:25:44 -0700117 mPackageManager = getContext().getPackageManager();
Cassie Wang0c62d992021-01-15 14:39:30 -0800118 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Terry Wang12dc6c02021-03-31 19:26:16 -0700119 mImplInstanceManager = ImplInstanceManager.getInstance(mContext);
120 mUserManager = mContext.getSystemService(UserManager.class);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800121 mLoggerInstanceManager = LoggerInstanceManager.getInstance();
Terry Wang12dc6c02021-03-31 19:26:16 -0700122 registerReceivers();
Yang Yu0fcd51a2021-04-23 11:25:44 -0700123 LocalServices.getService(StorageStatsManagerInternal.class)
124 .registerStorageStatsAugmenter(new AppSearchStorageStatsAugmenter(), TAG);
Terry Wang12dc6c02021-03-31 19:26:16 -0700125 }
126
127 private void registerReceivers() {
128 mContext.registerReceiverAsUser(new UserActionReceiver(), UserHandle.ALL,
129 new IntentFilter(Intent.ACTION_USER_REMOVED), /*broadcastPermission=*/ null,
130 /*scheduler=*/ null);
Terry Wange201dc02021-04-16 01:03:20 -0700131
132 //TODO(b/145759910) Add a direct callback when user clears the data instead of relying on
133 // broadcasts
134 IntentFilter packageChangedFilter = new IntentFilter();
135 packageChangedFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
136 packageChangedFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
137 packageChangedFilter.addDataScheme("package");
138 packageChangedFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
139 mContext.registerReceiverAsUser(new PackageChangedReceiver(), UserHandle.ALL,
140 packageChangedFilter, /*broadcastPermission=*/ null,
141 /*scheduler=*/ null);
Terry Wang12dc6c02021-03-31 19:26:16 -0700142 }
143
144 private class UserActionReceiver extends BroadcastReceiver {
145 @Override
146 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
147 switch (intent.getAction()) {
148 case Intent.ACTION_USER_REMOVED:
Terry Wange201dc02021-04-16 01:03:20 -0700149 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
Terry Wang12dc6c02021-03-31 19:26:16 -0700150 if (userId == USER_NULL) {
Terry Wange201dc02021-04-16 01:03:20 -0700151 Log.e(TAG, "userId is missing in the intent: " + intent);
Terry Wang12dc6c02021-03-31 19:26:16 -0700152 return;
153 }
154 handleUserRemoved(userId);
155 break;
156 default:
Terry Wange201dc02021-04-16 01:03:20 -0700157 Log.e(TAG, "Received unknown intent: " + intent);
Terry Wang12dc6c02021-03-31 19:26:16 -0700158 }
159 }
160 }
161
162 /**
163 * Handles user removed action.
164 *
165 * <p>Only need to clear the AppSearchImpl instance. The data of AppSearch is saved in the
166 * "credential encrypted" system directory of each user. That directory will be auto-deleted
167 * when a user is removed.
168 *
169 * @param userId The multi-user userId of the user that need to be removed.
170 *
171 * @see android.os.Environment#getDataSystemCeDirectory
172 */
173 private void handleUserRemoved(@UserIdInt int userId) {
174 try {
175 mImplInstanceManager.removeAppSearchImplForUser(userId);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800176 mLoggerInstanceManager.removePlatformLoggerForUser(userId);
Terry Wange201dc02021-04-16 01:03:20 -0700177 Log.i(TAG, "Removed AppSearchImpl instance for user: " + userId);
Terry Wang12dc6c02021-03-31 19:26:16 -0700178 } catch (Throwable t) {
Terry Wange201dc02021-04-16 01:03:20 -0700179 Log.e(TAG, "Unable to remove data for user: " + userId, t);
180 }
181 }
182
183 private class PackageChangedReceiver extends BroadcastReceiver {
184 @Override
185 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
186 switch (intent.getAction()) {
187 case Intent.ACTION_PACKAGE_FULLY_REMOVED:
188 case Intent.ACTION_PACKAGE_DATA_CLEARED:
189 String packageName = intent.getData().getSchemeSpecificPart();
190 if (packageName == null) {
191 Log.e(TAG, "Package name is missing in the intent: " + intent);
192 return;
193 }
194 int uid = intent.getIntExtra(Intent.EXTRA_UID, INVALID_UID);
195 if (uid == INVALID_UID) {
196 Log.e(TAG, "uid is missing in the intent: " + intent);
197 return;
198 }
199 handlePackageRemoved(packageName, uid);
200 break;
201 default:
202 Log.e(TAG, "Received unknown intent: " + intent);
203 }
204 }
205 }
206
207 private void handlePackageRemoved(String packageName, int uid) {
208 int userId = UserHandle.getUserId(uid);
209 try {
210 if (isUserLocked(userId)) {
211 //TODO(b/186151459) clear the uninstalled package data when user is unlocked.
212 return;
213 }
214 if (ImplInstanceManager.getAppSearchDir(userId).exists()) {
215 // Only clear the package's data if AppSearch exists for this user.
216 AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl(mContext,
217 userId);
218 //TODO(b/145759910) clear visibility setting for package.
219 impl.clearPackageData(packageName);
220 }
221 } catch (Throwable t) {
222 Log.e(TAG, "Unable to remove data for package: " + packageName, t);
Terry Wang12dc6c02021-03-31 19:26:16 -0700223 }
Terry Wangfebbead2019-10-17 17:05:18 -0700224 }
225
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800226 @Override
Pinyao Tingd5c2ed92021-03-18 14:51:54 -0700227 public void onUserUnlocking(@NonNull TargetUser user) {
Cassie Wang15c86972021-02-09 13:43:25 -0800228 synchronized (mUnlockedUserIdsLocked) {
229 mUnlockedUserIdsLocked.add(user.getUserIdentifier());
230 }
Cassie Wang9ba9ae12021-02-01 16:39:37 -0800231 }
232
Yang Yu0fcd51a2021-04-23 11:25:44 -0700233 private void verifyUserUnlocked(int callingUserId) {
Terry Wange201dc02021-04-16 01:03:20 -0700234 if (isUserLocked(callingUserId)) {
235 throw new IllegalStateException("User " + callingUserId + " is locked or not running.");
236 }
237 }
238
239 private boolean isUserLocked(int callingUserId) {
Yang Yu0fcd51a2021-04-23 11:25:44 -0700240 synchronized (mUnlockedUserIdsLocked) {
241 // First, check the local copy.
242 if (mUnlockedUserIdsLocked.contains(callingUserId)) {
Terry Wange201dc02021-04-16 01:03:20 -0700243 return false;
Yang Yu0fcd51a2021-04-23 11:25:44 -0700244 }
245 // If the local copy says the user is locked, check with UM for the actual state,
246 // since the user might just have been unlocked.
Terry Wange201dc02021-04-16 01:03:20 -0700247 return !mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(callingUserId));
Yang Yu0fcd51a2021-04-23 11:25:44 -0700248 }
249 }
250
Terry Wangfebbead2019-10-17 17:05:18 -0700251 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800252 @Override
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800253 public void setSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -0800254 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700255 @NonNull String databaseName,
Alexander Dorokhine92ce3532020-10-06 01:39:36 -0700256 @NonNull List<Bundle> schemaBundles,
Alexander Dorokhine315cca62021-03-04 12:34:41 -0800257 @NonNull List<String> schemasNotDisplayedBySystem,
Alexander Dorokhineab789062021-01-11 21:00:00 -0800258 @NonNull Map<String, List<Bundle>> schemasPackageAccessibleBundles,
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800259 boolean forceOverride,
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700260 int schemaVersion,
Terry Wangc10610d2021-03-21 13:21:24 -0700261 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800262 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700263 Objects.requireNonNull(packageName);
264 Objects.requireNonNull(databaseName);
265 Objects.requireNonNull(schemaBundles);
266 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800267 int callingUid = Binder.getCallingUid();
268 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700269 EXECUTOR.execute(() -> {
270 try {
271 verifyUserUnlocked(callingUserId);
272 verifyCallingPackage(callingUid, packageName);
273 List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size());
274 for (int i = 0; i < schemaBundles.size(); i++) {
275 schemas.add(new AppSearchSchema(schemaBundles.get(i)));
Alexander Dorokhineab789062021-01-11 21:00:00 -0800276 }
Terry Wange04ceab2021-03-29 19:25:12 -0700277 Map<String, List<PackageIdentifier>> schemasPackageAccessible =
278 new ArrayMap<>(schemasPackageAccessibleBundles.size());
279 for (Map.Entry<String, List<Bundle>> entry :
280 schemasPackageAccessibleBundles.entrySet()) {
281 List<PackageIdentifier> packageIdentifiers =
282 new ArrayList<>(entry.getValue().size());
283 for (int i = 0; i < entry.getValue().size(); i++) {
284 packageIdentifiers.add(
285 new PackageIdentifier(entry.getValue().get(i)));
286 }
287 schemasPackageAccessible.put(entry.getKey(), packageIdentifiers);
288 }
289 AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUserId);
290 SetSchemaResponse setSchemaResponse = impl.setSchema(
291 packageName,
292 databaseName,
293 schemas,
294 schemasNotDisplayedBySystem,
295 schemasPackageAccessible,
296 forceOverride,
297 schemaVersion);
298 invokeCallbackOnResult(callback,
299 AppSearchResult.newSuccessfulResult(setSchemaResponse.getBundle()));
300 } catch (Throwable t) {
301 invokeCallbackOnError(callback, t);
Alexander Dorokhineab789062021-01-11 21:00:00 -0800302 }
Terry Wange04ceab2021-03-29 19:25:12 -0700303 });
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800304 }
305
306 @Override
Terry Wang83a24932020-12-09 21:00:18 -0800307 public void getSchema(
Cassie Wang0c62d992021-01-15 14:39:30 -0800308 @NonNull String packageName,
Terry Wang83a24932020-12-09 21:00:18 -0800309 @NonNull String databaseName,
Terry Wangf2093072020-11-30 04:47:19 -0800310 @UserIdInt int userId,
Terry Wang83a24932020-12-09 21:00:18 -0800311 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700312 Objects.requireNonNull(packageName);
313 Objects.requireNonNull(databaseName);
314 Objects.requireNonNull(callback);
Cassie Wangb0d60122021-03-30 12:38:46 -0700315 int callingUid = Binder.getCallingUid();
Terry Wangf2093072020-11-30 04:47:19 -0800316 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700317 EXECUTOR.execute(() -> {
318 try {
319 verifyUserUnlocked(callingUserId);
320 verifyCallingPackage(callingUid, packageName);
321 AppSearchImpl impl =
322 mImplInstanceManager.getAppSearchImpl(callingUserId);
323 GetSchemaResponse response = impl.getSchema(packageName, databaseName);
324 invokeCallbackOnResult(
325 callback,
326 AppSearchResult.newSuccessfulResult(response.getBundle()));
327 } catch (Throwable t) {
328 invokeCallbackOnError(callback, t);
329 }
330 });
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700331 }
332
333 @Override
334 public void getNamespaces(
335 @NonNull String packageName,
336 @NonNull String databaseName,
337 @UserIdInt int userId,
338 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700339 Objects.requireNonNull(packageName);
340 Objects.requireNonNull(databaseName);
341 Objects.requireNonNull(callback);
Cassie Wangb0d60122021-03-30 12:38:46 -0700342 int callingUid = Binder.getCallingUid();
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700343 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700344 EXECUTOR.execute(() -> {
345 try {
346 verifyUserUnlocked(callingUserId);
347 verifyCallingPackage(callingUid, packageName);
348 AppSearchImpl impl =
349 mImplInstanceManager.getAppSearchImpl(callingUserId);
350 List<String> namespaces = impl.getNamespaces(packageName, databaseName);
351 invokeCallbackOnResult(callback,
352 AppSearchResult.newSuccessfulResult(namespaces));
353 } catch (Throwable t) {
354 invokeCallbackOnError(callback, t);
355 }
356 });
Terry Wang83a24932020-12-09 21:00:18 -0800357 }
358
359 @Override
Alexander Dorokhine18465842020-01-21 01:08:57 -0800360 public void putDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800361 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700362 @NonNull String databaseName,
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700363 @NonNull List<Bundle> documentBundles,
Terry Wangf2093072020-11-30 04:47:19 -0800364 @UserIdInt int userId,
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800365 @ElapsedRealtimeLong long binderCallStartTimeMillis,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800366 @NonNull IAppSearchBatchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700367 Objects.requireNonNull(packageName);
368 Objects.requireNonNull(databaseName);
369 Objects.requireNonNull(documentBundles);
370 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800371 int callingUid = Binder.getCallingUid();
372 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700373 EXECUTOR.execute(() -> {
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800374 long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime();
375 @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK;
376 PlatformLogger logger = null;
377 int operationSuccessCount = 0;
378 int operationFailureCount = 0;
Terry Wange04ceab2021-03-29 19:25:12 -0700379 try {
380 verifyUserUnlocked(callingUserId);
381 verifyCallingPackage(callingUid, packageName);
382 AppSearchBatchResult.Builder<String, Void> resultBuilder =
383 new AppSearchBatchResult.Builder<>();
384 AppSearchImpl impl =
385 mImplInstanceManager.getAppSearchImpl(callingUserId);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800386 logger = mLoggerInstanceManager.getPlatformLogger(callingUserId);
Terry Wange04ceab2021-03-29 19:25:12 -0700387 for (int i = 0; i < documentBundles.size(); i++) {
388 GenericDocument document = new GenericDocument(documentBundles.get(i));
389 try {
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800390 impl.putDocument(packageName, databaseName, document, logger);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700391 resultBuilder.setSuccess(document.getId(), /*result=*/ null);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800392 ++operationSuccessCount;
Terry Wange04ceab2021-03-29 19:25:12 -0700393 } catch (Throwable t) {
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700394 resultBuilder.setResult(document.getId(),
Terry Wange04ceab2021-03-29 19:25:12 -0700395 throwableToFailedResult(t));
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800396 AppSearchResult<Void> result = throwableToFailedResult(t);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700397 resultBuilder.setResult(document.getId(), result);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800398 // for failures, we would just log the one for last failure
399 statusCode = result.getResultCode();
400 ++operationFailureCount;
Terry Wange04ceab2021-03-29 19:25:12 -0700401 }
Alexander Dorokhine18465842020-01-21 01:08:57 -0800402 }
Alexander Dorokhinec77f4442021-04-14 09:26:06 -0700403 // Now that the batch has been written. Persist the newly written data.
404 impl.persistToDisk(PersistType.Code.LITE);
Terry Wange04ceab2021-03-29 19:25:12 -0700405 invokeCallbackOnResult(callback, resultBuilder.build());
406 } catch (Throwable t) {
407 invokeCallbackOnError(callback, t);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800408 } finally {
409 if (logger != null) {
410 CallStats.Builder cBuilder = new CallStats.Builder(packageName,
411 databaseName)
412 .setCallType(CallStats.CALL_TYPE_PUT_DOCUMENTS)
413 // TODO(b/173532925) check the existing binder call latency chart
414 // is good enough for us:
415 // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4
416 .setEstimatedBinderLatencyMillis(
417 2 * (int) (totalLatencyStartTimeMillis
418 - binderCallStartTimeMillis))
419 .setNumOperationsSucceeded(operationSuccessCount)
420 .setNumOperationsFailed(operationFailureCount);
421 cBuilder.getGeneralStatsBuilder()
422 .setStatusCode(statusCode)
423 .setTotalLatencyMillis(
424 (int) (SystemClock.elapsedRealtime()
425 - totalLatencyStartTimeMillis));
426 logger.logStats(cBuilder.build());
427 }
Alexander Dorokhine18465842020-01-21 01:08:57 -0800428 }
Terry Wange04ceab2021-03-29 19:25:12 -0700429 });
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800430 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800431
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800432 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800433 public void getDocuments(
Cassie Wang0c62d992021-01-15 14:39:30 -0800434 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800435 @NonNull String databaseName,
436 @NonNull String namespace,
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700437 @NonNull List<String> ids,
Alexander Dorokhine87cdd152021-01-20 15:41:25 -0800438 @NonNull Map<String, List<String>> typePropertyPaths,
Terry Wangf2093072020-11-30 04:47:19 -0800439 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800440 @NonNull IAppSearchBatchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700441 Objects.requireNonNull(packageName);
442 Objects.requireNonNull(databaseName);
443 Objects.requireNonNull(namespace);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700444 Objects.requireNonNull(ids);
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700445 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800446 int callingUid = Binder.getCallingUid();
447 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700448 EXECUTOR.execute(() -> {
449 try {
450 verifyUserUnlocked(callingUserId);
451 verifyCallingPackage(callingUid, packageName);
452 AppSearchBatchResult.Builder<String, Bundle> resultBuilder =
453 new AppSearchBatchResult.Builder<>();
454 AppSearchImpl impl =
455 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700456 for (int i = 0; i < ids.size(); i++) {
457 String id = ids.get(i);
Terry Wange04ceab2021-03-29 19:25:12 -0700458 try {
459 GenericDocument document =
460 impl.getDocument(
461 packageName,
462 databaseName,
463 namespace,
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700464 id,
Terry Wange04ceab2021-03-29 19:25:12 -0700465 typePropertyPaths);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700466 resultBuilder.setSuccess(id, document.getBundle());
Terry Wange04ceab2021-03-29 19:25:12 -0700467 } catch (Throwable t) {
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700468 resultBuilder.setResult(id, throwableToFailedResult(t));
Terry Wange04ceab2021-03-29 19:25:12 -0700469 }
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800470 }
Terry Wange04ceab2021-03-29 19:25:12 -0700471 invokeCallbackOnResult(callback, resultBuilder.build());
472 } catch (Throwable t) {
473 invokeCallbackOnError(callback, t);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800474 }
Terry Wange04ceab2021-03-29 19:25:12 -0700475 });
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800476 }
477
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800478 @Override
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800479 public void query(
Cassie Wang0c62d992021-01-15 14:39:30 -0800480 @NonNull String packageName,
Terry Wang6413aee2020-10-07 03:04:58 -0700481 @NonNull String databaseName,
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700482 @NonNull String queryExpression,
483 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800484 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700485 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700486 Objects.requireNonNull(packageName);
487 Objects.requireNonNull(databaseName);
488 Objects.requireNonNull(queryExpression);
489 Objects.requireNonNull(searchSpecBundle);
490 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800491 int callingUid = Binder.getCallingUid();
492 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700493 EXECUTOR.execute(() -> {
494 try {
495 verifyUserUnlocked(callingUserId);
496 verifyCallingPackage(callingUid, packageName);
497 AppSearchImpl impl =
498 mImplInstanceManager.getAppSearchImpl(callingUserId);
499 SearchResultPage searchResultPage =
500 impl.query(
501 packageName,
502 databaseName,
503 queryExpression,
504 new SearchSpec(searchSpecBundle));
505 invokeCallbackOnResult(
506 callback,
507 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
508 } catch (Throwable t) {
509 invokeCallbackOnError(callback, t);
510 }
511 });
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700512 }
513
Terry Wangf2093072020-11-30 04:47:19 -0800514 @Override
Terry Wangbfbfcac2020-11-06 15:46:44 -0800515 public void globalQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800516 @NonNull String packageName,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800517 @NonNull String queryExpression,
518 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800519 @UserIdInt int userId,
Terry Wangbfbfcac2020-11-06 15:46:44 -0800520 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700521 Objects.requireNonNull(packageName);
522 Objects.requireNonNull(queryExpression);
523 Objects.requireNonNull(searchSpecBundle);
524 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800525 int callingUid = Binder.getCallingUid();
526 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700527 EXECUTOR.execute(() -> {
528 try {
529 verifyUserUnlocked(callingUserId);
530 verifyCallingPackage(callingUid, packageName);
531 AppSearchImpl impl =
532 mImplInstanceManager.getAppSearchImpl(callingUserId);
533 SearchResultPage searchResultPage =
534 impl.globalQuery(
535 queryExpression,
536 new SearchSpec(searchSpecBundle),
537 packageName,
538 callingUid);
539 invokeCallbackOnResult(
540 callback,
541 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
542 } catch (Throwable t) {
543 invokeCallbackOnError(callback, t);
544 }
545 });
Terry Wangbfbfcac2020-11-06 15:46:44 -0800546 }
547
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700548 @Override
Cassie Wang0c62d992021-01-15 14:39:30 -0800549 public void getNextPage(
550 long nextPageToken,
551 @UserIdInt int userId,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700552 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700553 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800554 int callingUid = Binder.getCallingUid();
555 int callingUserId = handleIncomingUser(userId, callingUid);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700556 // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally
557 // opened it
Terry Wange04ceab2021-03-29 19:25:12 -0700558 EXECUTOR.execute(() -> {
559 try {
560 verifyUserUnlocked(callingUserId);
561 AppSearchImpl impl =
562 mImplInstanceManager.getAppSearchImpl(callingUserId);
563 SearchResultPage searchResultPage = impl.getNextPage(nextPageToken);
564 invokeCallbackOnResult(
565 callback,
566 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
567 } catch (Throwable t) {
568 invokeCallbackOnError(callback, t);
569 }
570 });
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700571 }
572
573 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800574 public void invalidateNextPageToken(long nextPageToken, @UserIdInt int userId) {
575 int callingUid = Binder.getCallingUid();
576 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700577 EXECUTOR.execute(() -> {
578 try {
579 verifyUserUnlocked(callingUserId);
580 AppSearchImpl impl =
581 mImplInstanceManager.getAppSearchImpl(callingUserId);
582 impl.invalidateNextPageToken(nextPageToken);
583 } catch (Throwable t) {
584 Log.e(TAG, "Unable to invalidate the query page token", t);
585 }
586 });
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800587 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800588
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700589 @Override
Terry Wang623e3b02021-02-02 20:27:33 -0800590 public void writeQueryResultsToFile(
591 @NonNull String packageName,
592 @NonNull String databaseName,
593 @NonNull ParcelFileDescriptor fileDescriptor,
594 @NonNull String queryExpression,
595 @NonNull Bundle searchSpecBundle,
596 @UserIdInt int userId,
597 @NonNull IAppSearchResultCallback callback) {
598 int callingUid = Binder.getCallingUid();
599 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700600 EXECUTOR.execute(() -> {
601 try {
602 verifyCallingPackage(callingUid, packageName);
603 AppSearchImpl impl =
604 mImplInstanceManager.getAppSearchImpl(callingUserId);
605 // we don't need to append the file. The file is always brand new.
606 try (DataOutputStream outputStream = new DataOutputStream(
607 new FileOutputStream(fileDescriptor.getFileDescriptor()))) {
608 SearchResultPage searchResultPage = impl.query(
609 packageName,
610 databaseName,
611 queryExpression,
612 new SearchSpec(searchSpecBundle));
613 while (!searchResultPage.getResults().isEmpty()) {
614 for (int i = 0; i < searchResultPage.getResults().size(); i++) {
615 AppSearchMigrationHelper.writeBundleToOutputStream(
616 outputStream, searchResultPage.getResults().get(i)
617 .getGenericDocument().getBundle());
618 }
619 searchResultPage = impl.getNextPage(
620 searchResultPage.getNextPageToken());
Terry Wang623e3b02021-02-02 20:27:33 -0800621 }
Terry Wang623e3b02021-02-02 20:27:33 -0800622 }
Terry Wange04ceab2021-03-29 19:25:12 -0700623 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
624 } catch (Throwable t) {
625 invokeCallbackOnError(callback, t);
Terry Wang623e3b02021-02-02 20:27:33 -0800626 }
Terry Wange04ceab2021-03-29 19:25:12 -0700627 });
Terry Wang623e3b02021-02-02 20:27:33 -0800628 }
629
630 @Override
631 public void putDocumentsFromFile(
632 @NonNull String packageName,
633 @NonNull String databaseName,
634 @NonNull ParcelFileDescriptor fileDescriptor,
635 @UserIdInt int userId,
636 @NonNull IAppSearchResultCallback callback) {
637 int callingUid = Binder.getCallingUid();
638 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700639 EXECUTOR.execute(() -> {
640 try {
641 verifyCallingPackage(callingUid, packageName);
642 AppSearchImpl impl =
643 mImplInstanceManager.getAppSearchImpl(callingUserId);
Terry Wang623e3b02021-02-02 20:27:33 -0800644
Terry Wange04ceab2021-03-29 19:25:12 -0700645 GenericDocument document;
646 ArrayList<Bundle> migrationFailureBundles = new ArrayList<>();
647 try (DataInputStream inputStream = new DataInputStream(
648 new FileInputStream(fileDescriptor.getFileDescriptor()))) {
649 while (true) {
650 try {
651 document = AppSearchMigrationHelper
652 .readDocumentFromInputStream(inputStream);
653 } catch (EOFException e) {
654 // nothing wrong, we just finish the reading.
655 break;
656 }
657 try {
658 impl.putDocument(packageName, databaseName, document,
659 /*logger=*/ null);
660 } catch (Throwable t) {
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700661 migrationFailureBundles.add(new SetSchemaResponse.MigrationFailure(
662 document.getNamespace(),
663 document.getId(),
664 document.getSchemaType(),
665 AppSearchResult.throwableToFailedResult(t))
666 .getBundle());
Terry Wange04ceab2021-03-29 19:25:12 -0700667 }
Terry Wang623e3b02021-02-02 20:27:33 -0800668 }
669 }
Alexander Dorokhinec77f4442021-04-14 09:26:06 -0700670 impl.persistToDisk(PersistType.Code.FULL);
Terry Wange04ceab2021-03-29 19:25:12 -0700671 invokeCallbackOnResult(callback,
672 AppSearchResult.newSuccessfulResult(migrationFailureBundles));
673 } catch (Throwable t) {
674 invokeCallbackOnError(callback, t);
Terry Wang623e3b02021-02-02 20:27:33 -0800675 }
Terry Wange04ceab2021-03-29 19:25:12 -0700676 });
Terry Wang623e3b02021-02-02 20:27:33 -0800677 }
678
679 @Override
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800680 public void reportUsage(
681 @NonNull String packageName,
682 @NonNull String databaseName,
683 @NonNull String namespace,
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700684 @NonNull String documentId,
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800685 long usageTimeMillis,
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700686 boolean systemUsage,
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800687 @UserIdInt int userId,
688 @NonNull IAppSearchResultCallback callback) {
689 Objects.requireNonNull(databaseName);
690 Objects.requireNonNull(namespace);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700691 Objects.requireNonNull(documentId);
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800692 Objects.requireNonNull(callback);
693 int callingUid = Binder.getCallingUid();
694 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700695 EXECUTOR.execute(() -> {
696 try {
697 verifyUserUnlocked(callingUserId);
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700698
Terry Wange04ceab2021-03-29 19:25:12 -0700699 if (systemUsage) {
700 // TODO(b/183031844): Validate that the call comes from the system
701 }
702
703 AppSearchImpl impl =
704 mImplInstanceManager.getAppSearchImpl(callingUserId);
705 impl.reportUsage(
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700706 packageName, databaseName, namespace, documentId,
Terry Wange04ceab2021-03-29 19:25:12 -0700707 usageTimeMillis, systemUsage);
708 invokeCallbackOnResult(
709 callback, AppSearchResult.newSuccessfulResult(/*result=*/ null));
710 } catch (Throwable t) {
711 invokeCallbackOnError(callback, t);
Alexander Dorokhine9795b512021-03-23 22:06:59 -0700712 }
Terry Wange04ceab2021-03-29 19:25:12 -0700713 });
Alexander Dorokhined18f8842021-01-20 15:26:13 -0800714 }
715
716 @Override
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700717 public void removeByDocumentId(
Cassie Wang0c62d992021-01-15 14:39:30 -0800718 @NonNull String packageName,
Terry Wangf2093072020-11-30 04:47:19 -0800719 @NonNull String databaseName,
720 @NonNull String namespace,
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700721 @NonNull List<String> ids,
Terry Wangf2093072020-11-30 04:47:19 -0800722 @UserIdInt int userId,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800723 @NonNull IAppSearchBatchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700724 Objects.requireNonNull(packageName);
725 Objects.requireNonNull(databaseName);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700726 Objects.requireNonNull(ids);
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700727 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800728 int callingUid = Binder.getCallingUid();
729 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700730 EXECUTOR.execute(() -> {
731 try {
732 verifyUserUnlocked(callingUserId);
733 verifyCallingPackage(callingUid, packageName);
734 AppSearchBatchResult.Builder<String, Void> resultBuilder =
735 new AppSearchBatchResult.Builder<>();
736 AppSearchImpl impl =
737 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700738 for (int i = 0; i < ids.size(); i++) {
739 String id = ids.get(i);
Terry Wange04ceab2021-03-29 19:25:12 -0700740 try {
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700741 impl.remove(packageName, databaseName, namespace, id);
742 resultBuilder.setSuccess(id, /*result= */ null);
Terry Wange04ceab2021-03-29 19:25:12 -0700743 } catch (Throwable t) {
Alexander Dorokhine17f79372021-04-21 11:23:09 -0700744 resultBuilder.setResult(id, throwableToFailedResult(t));
Terry Wange04ceab2021-03-29 19:25:12 -0700745 }
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700746 }
Alexander Dorokhinec77f4442021-04-14 09:26:06 -0700747 // Now that the batch has been written. Persist the newly written data.
748 impl.persistToDisk(PersistType.Code.LITE);
Terry Wange04ceab2021-03-29 19:25:12 -0700749 invokeCallbackOnResult(callback, resultBuilder.build());
750 } catch (Throwable t) {
751 invokeCallbackOnError(callback, t);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700752 }
Terry Wange04ceab2021-03-29 19:25:12 -0700753 });
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700754 }
755
756 @Override
Terry Wang26b9e5c2020-10-23 02:05:01 -0700757 public void removeByQuery(
Cassie Wang0c62d992021-01-15 14:39:30 -0800758 @NonNull String packageName,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700759 @NonNull String databaseName,
760 @NonNull String queryExpression,
761 @NonNull Bundle searchSpecBundle,
Terry Wangf2093072020-11-30 04:47:19 -0800762 @UserIdInt int userId,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700763 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700764 Objects.requireNonNull(packageName);
765 Objects.requireNonNull(databaseName);
766 Objects.requireNonNull(queryExpression);
767 Objects.requireNonNull(searchSpecBundle);
768 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800769 int callingUid = Binder.getCallingUid();
770 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700771 EXECUTOR.execute(() -> {
772 try {
773 verifyUserUnlocked(callingUserId);
774 verifyCallingPackage(callingUid, packageName);
775 AppSearchImpl impl =
776 mImplInstanceManager.getAppSearchImpl(callingUserId);
777 impl.removeByQuery(
778 packageName,
779 databaseName,
780 queryExpression,
781 new SearchSpec(searchSpecBundle));
Alexander Dorokhinec77f4442021-04-14 09:26:06 -0700782 // Now that the batch has been written. Persist the newly written data.
783 impl.persistToDisk(PersistType.Code.LITE);
Terry Wange04ceab2021-03-29 19:25:12 -0700784 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
785 } catch (Throwable t) {
786 invokeCallbackOnError(callback, t);
787 }
788 });
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700789 }
790
Terry Wangdbd1dca2020-11-03 17:03:56 -0800791 @Override
Cassie Wang8f0df492021-03-24 09:23:18 -0700792 public void getStorageInfo(
793 @NonNull String packageName,
794 @NonNull String databaseName,
795 @UserIdInt int userId,
796 @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700797 Objects.requireNonNull(packageName);
798 Objects.requireNonNull(databaseName);
799 Objects.requireNonNull(callback);
Cassie Wang8f0df492021-03-24 09:23:18 -0700800 int callingUid = Binder.getCallingUid();
801 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700802 EXECUTOR.execute(() -> {
803 try {
804 verifyUserUnlocked(callingUserId);
805 verifyCallingPackage(callingUid, packageName);
806 AppSearchImpl impl =
807 mImplInstanceManager.getAppSearchImpl(callingUserId);
808 StorageInfo storageInfo = impl.getStorageInfoForDatabase(packageName,
809 databaseName);
810 Bundle storageInfoBundle = storageInfo.getBundle();
811 invokeCallbackOnResult(
812 callback, AppSearchResult.newSuccessfulResult(storageInfoBundle));
813 } catch (Throwable t) {
814 invokeCallbackOnError(callback, t);
815 }
816 });
Cassie Wang8f0df492021-03-24 09:23:18 -0700817 }
818
819 @Override
Terry Wang2da17852020-12-16 19:59:08 -0800820 public void persistToDisk(@UserIdInt int userId) {
Cassie Wangb0d60122021-03-30 12:38:46 -0700821 int callingUid = Binder.getCallingUid();
Terry Wang2da17852020-12-16 19:59:08 -0800822 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700823 EXECUTOR.execute(() -> {
824 try {
825 verifyUserUnlocked(callingUserId);
826 AppSearchImpl impl =
827 mImplInstanceManager.getAppSearchImpl(callingUserId);
Alexander Dorokhinec77f4442021-04-14 09:26:06 -0700828 impl.persistToDisk(PersistType.Code.FULL);
Terry Wange04ceab2021-03-29 19:25:12 -0700829 } catch (Throwable t) {
830 Log.e(TAG, "Unable to persist the data to disk", t);
831 }
832 });
Terry Wang2da17852020-12-16 19:59:08 -0800833 }
834
835 @Override
Terry Wangf2093072020-11-30 04:47:19 -0800836 public void initialize(@UserIdInt int userId, @NonNull IAppSearchResultCallback callback) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700837 Objects.requireNonNull(callback);
Terry Wangf2093072020-11-30 04:47:19 -0800838 int callingUid = Binder.getCallingUid();
839 int callingUserId = handleIncomingUser(userId, callingUid);
Terry Wange04ceab2021-03-29 19:25:12 -0700840 EXECUTOR.execute(() -> {
841 try {
842 verifyUserUnlocked(callingUserId);
843 mImplInstanceManager.getOrCreateAppSearchImpl(mContext, callingUserId);
Xiaoyu Jinf8cc2012021-03-12 11:00:54 -0800844 mLoggerInstanceManager.getOrCreatePlatformLogger(getContext(), callingUserId);
Terry Wange04ceab2021-03-29 19:25:12 -0700845 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
846 } catch (Throwable t) {
847 invokeCallbackOnError(callback, t);
848 }
849 });
Terry Wangdbd1dca2020-11-03 17:03:56 -0800850 }
851
Cassie Wang0c62d992021-01-15 14:39:30 -0800852 private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) {
Alexander Dorokhine5258ad22021-04-15 10:59:55 -0700853 Objects.requireNonNull(callingPackage);
Cassie Wang0c62d992021-01-15 14:39:30 -0800854 if (mPackageManagerInternal.getPackageUid(
Yang Yu0fcd51a2021-04-23 11:25:44 -0700855 callingPackage, /*flags=*/ 0, UserHandle.getUserId(callingUid))
Cassie Wang0c62d992021-01-15 14:39:30 -0800856 != callingUid) {
857 throw new SecurityException(
858 "Specified calling package ["
859 + callingPackage
860 + "] does not match the calling uid "
861 + callingUid);
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700862 }
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700863 }
864
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800865 /** Invokes the {@link IAppSearchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800866 private void invokeCallbackOnResult(
867 IAppSearchResultCallback callback, AppSearchResult<?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800868 try {
869 callback.onResult(result);
870 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800871 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800872 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800873 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800874
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800875 /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */
Cassie Wang0c62d992021-01-15 14:39:30 -0800876 private void invokeCallbackOnResult(
877 IAppSearchBatchResultCallback callback, AppSearchBatchResult<?, ?> result) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800878 try {
879 callback.onResult(result);
880 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800881 Log.e(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800882 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800883 }
884
885 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800886 * Invokes the {@link IAppSearchResultCallback} with an throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800887 *
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800888 * <p>The throwable is convert to a {@link AppSearchResult};
Terry Wangdbd1dca2020-11-03 17:03:56 -0800889 */
890 private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) {
891 try {
892 callback.onResult(throwableToFailedResult(throwable));
893 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800894 Log.e(TAG, "Unable to send result to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800895 }
896 }
897
898 /**
Alexander Dorokhine8c5ba912020-12-14 22:58:12 -0800899 * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800900 *
Alexander Dorokhineb5d34b12021-04-15 00:32:15 -0700901 * <p>The throwable is converted to {@link AppSearchResult}.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800902 */
Cassie Wang0c62d992021-01-15 14:39:30 -0800903 private void invokeCallbackOnError(
Alexander Dorokhineb5d34b12021-04-15 00:32:15 -0700904 @NonNull IAppSearchBatchResultCallback callback, @NonNull Throwable throwable) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800905 try {
Alexander Dorokhineb5d34b12021-04-15 00:32:15 -0700906 callback.onSystemError(throwableToFailedResult(throwable));
Terry Wangdbd1dca2020-11-03 17:03:56 -0800907 } catch (RemoteException e) {
Terry Wang2da17852020-12-16 19:59:08 -0800908 Log.e(TAG, "Unable to send error to the callback", e);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800909 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800910 }
Terry Wangfebbead2019-10-17 17:05:18 -0700911 }
Terry Wangf2093072020-11-30 04:47:19 -0800912
Cassie Wang0c62d992021-01-15 14:39:30 -0800913 // TODO(b/173553485) verifying that the caller has permission to access target user's data
914 // TODO(b/173553485) Handle ACTION_USER_REMOVED broadcast
915 // TODO(b/173553485) Implement SystemService.onUserStopping()
Terry Wangf2093072020-11-30 04:47:19 -0800916 private static int handleIncomingUser(@UserIdInt int userId, int callingUid) {
917 int callingPid = Binder.getCallingPid();
Cassie Wang0c62d992021-01-15 14:39:30 -0800918 return ActivityManager.handleIncomingUser(
919 callingPid,
920 callingUid,
921 userId,
922 /*allowAll=*/ false,
923 /*requireFull=*/ false,
924 /*name=*/ null,
925 /*callerPackage=*/ null);
Terry Wangf2093072020-11-30 04:47:19 -0800926 }
Yang Yu0fcd51a2021-04-23 11:25:44 -0700927
928 // TODO(b/179160886): Cache the previous storage stats.
929 private class AppSearchStorageStatsAugmenter implements StorageStatsAugmenter {
930 @Override
931 public void augmentStatsForPackage(
932 @NonNull PackageStats stats,
933 @NonNull String packageName,
934 @UserIdInt int userId,
935 boolean callerHasStatsPermission) {
936 Objects.requireNonNull(stats);
937 Objects.requireNonNull(packageName);
938 try {
939 verifyUserUnlocked(userId);
940 AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl(mContext,
941 userId);
942 stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes();
943 } catch (Throwable t) {
944 Log.e(
945 TAG,
946 "Unable to augment storage stats for userId "
947 + userId
948 + " packageName "
949 + packageName,
950 t);
951 }
952 }
953
954 @Override
955 public void augmentStatsForUid(
956 @NonNull PackageStats stats, int uid, boolean callerHasStatsPermission) {
957 Objects.requireNonNull(stats);
958 int userId = UserHandle.getUserId(uid);
959 try {
960 verifyUserUnlocked(userId);
961 String[] packagesForUid = mPackageManager.getPackagesForUid(uid);
962 if (packagesForUid == null) {
963 return;
964 }
965 AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl(mContext,
966 userId);
967 for (String packageName : packagesForUid) {
968 stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes();
969 }
970 } catch (Throwable t) {
971 Log.e(TAG, "Unable to augment storage stats for uid " + uid, t);
972 }
973 }
974 }
Terry Wangfebbead2019-10-17 17:05:18 -0700975}