Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1 | /* |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.appsearch; |
| 17 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 18 | import static android.app.appsearch.AppSearchResult.throwableToFailedResult; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 19 | import static android.os.UserHandle.USER_NULL; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 20 | |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 21 | import android.annotation.ElapsedRealtimeLong; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 23 | import android.annotation.UserIdInt; |
| 24 | import android.app.ActivityManager; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 25 | import android.app.appsearch.AppSearchBatchResult; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 26 | import android.app.appsearch.AppSearchMigrationHelper; |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 27 | import android.app.appsearch.AppSearchResult; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 28 | import android.app.appsearch.AppSearchSchema; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 29 | import android.app.appsearch.GenericDocument; |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 30 | import android.app.appsearch.GetSchemaResponse; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 31 | import android.app.appsearch.IAppSearchBatchResultCallback; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 32 | import android.app.appsearch.IAppSearchManager; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 33 | import android.app.appsearch.IAppSearchResultCallback; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 34 | import android.app.appsearch.PackageIdentifier; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 35 | import android.app.appsearch.SearchResultPage; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 36 | import android.app.appsearch.SearchSpec; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 37 | import android.app.appsearch.SetSchemaResponse; |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 38 | import android.app.appsearch.StorageInfo; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 39 | import android.content.BroadcastReceiver; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 40 | import android.content.Context; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 43 | import android.content.pm.PackageManager; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManagerInternal; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 45 | import android.content.pm.PackageStats; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 47 | import android.os.Bundle; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 48 | import android.os.ParcelFileDescriptor; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 49 | import android.os.RemoteException; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 50 | import android.os.SystemClock; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 51 | import android.os.UserHandle; |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 52 | import android.os.UserManager; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 53 | import android.util.ArrayMap; |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 54 | import android.util.ArraySet; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 55 | import android.util.Log; |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 56 | import android.util.Slog; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 57 | |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 58 | import com.android.internal.annotations.GuardedBy; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 59 | import com.android.server.LocalServices; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 60 | import com.android.server.SystemService; |
Alexander Dorokhine | f660d8f | 2020-10-29 22:37:00 -0700 | [diff] [blame] | 61 | import com.android.server.appsearch.external.localstorage.AppSearchImpl; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 62 | import com.android.server.appsearch.external.localstorage.stats.CallStats; |
| 63 | import com.android.server.appsearch.stats.LoggerInstanceManager; |
| 64 | import com.android.server.appsearch.stats.PlatformLogger; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 65 | import com.android.server.usage.StorageStatsManagerInternal; |
| 66 | import com.android.server.usage.StorageStatsManagerInternal.StorageStatsAugmenter; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 67 | |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 68 | import com.google.android.icing.proto.PersistType; |
| 69 | |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 70 | import java.io.DataInputStream; |
| 71 | import java.io.DataOutputStream; |
| 72 | import java.io.EOFException; |
| 73 | import java.io.FileInputStream; |
| 74 | import java.io.FileOutputStream; |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 75 | import java.util.ArrayList; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 76 | import java.util.List; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 77 | import java.util.Map; |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 78 | import java.util.Objects; |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 79 | import java.util.Set; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 80 | import java.util.concurrent.Executor; |
Terry Wang | d2186e5 | 2021-04-14 13:19:45 -0700 | [diff] [blame] | 81 | import java.util.concurrent.LinkedBlockingQueue; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 82 | import java.util.concurrent.ThreadPoolExecutor; |
| 83 | import java.util.concurrent.TimeUnit; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 84 | |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 85 | /** TODO(b/142567528): add comments when implement this class */ |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 86 | public class AppSearchManagerService extends SystemService { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 87 | private static final String TAG = "AppSearchManagerService"; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 88 | private final Context mContext; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 89 | private PackageManager mPackageManager; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 90 | private PackageManagerInternal mPackageManagerInternal; |
Cassie Wang | 21c2d6a | 2021-01-20 23:59:55 -0800 | [diff] [blame] | 91 | private ImplInstanceManager mImplInstanceManager; |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 92 | private UserManager mUserManager; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 93 | private LoggerInstanceManager mLoggerInstanceManager; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 94 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 95 | // 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 Wang | d2186e5 | 2021-04-14 13:19:45 -0700 | [diff] [blame] | 101 | new LinkedBlockingQueue<>()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 102 | |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 103 | // 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 Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 108 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 109 | public AppSearchManagerService(Context context) { |
| 110 | super(context); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 111 | mContext = context; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onStart() { |
| 116 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 117 | mPackageManager = getContext().getPackageManager(); |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 118 | mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 119 | mImplInstanceManager = ImplInstanceManager.getInstance(mContext); |
| 120 | mUserManager = mContext.getSystemService(UserManager.class); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 121 | mLoggerInstanceManager = LoggerInstanceManager.getInstance(); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 122 | registerReceivers(); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 123 | LocalServices.getService(StorageStatsManagerInternal.class) |
| 124 | .registerStorageStatsAugmenter(new AppSearchStorageStatsAugmenter(), TAG); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private void registerReceivers() { |
| 128 | mContext.registerReceiverAsUser(new UserActionReceiver(), UserHandle.ALL, |
| 129 | new IntentFilter(Intent.ACTION_USER_REMOVED), /*broadcastPermission=*/ null, |
| 130 | /*scheduler=*/ null); |
| 131 | } |
| 132 | |
| 133 | private class UserActionReceiver extends BroadcastReceiver { |
| 134 | @Override |
| 135 | public void onReceive(@NonNull Context context, @NonNull Intent intent) { |
| 136 | switch (intent.getAction()) { |
| 137 | case Intent.ACTION_USER_REMOVED: |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 138 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 139 | if (userId == USER_NULL) { |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 140 | Slog.e(TAG, "userId is missing in the intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | handleUserRemoved(userId); |
| 144 | break; |
| 145 | default: |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 146 | Slog.e(TAG, "Received unknown intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Handles user removed action. |
| 153 | * |
| 154 | * <p>Only need to clear the AppSearchImpl instance. The data of AppSearch is saved in the |
| 155 | * "credential encrypted" system directory of each user. That directory will be auto-deleted |
| 156 | * when a user is removed. |
| 157 | * |
| 158 | * @param userId The multi-user userId of the user that need to be removed. |
| 159 | * |
| 160 | * @see android.os.Environment#getDataSystemCeDirectory |
| 161 | */ |
| 162 | private void handleUserRemoved(@UserIdInt int userId) { |
| 163 | try { |
| 164 | mImplInstanceManager.removeAppSearchImplForUser(userId); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 165 | mLoggerInstanceManager.removePlatformLoggerForUser(userId); |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 166 | Slog.i(TAG, "Removed AppSearchImpl instance for user: " + userId); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 167 | } catch (Throwable t) { |
Martijn Coenen | 9fdaf0d | 2021-04-15 07:18:26 +0000 | [diff] [blame] | 168 | Slog.e(TAG, "Unable to remove data for user: " + userId, t); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 169 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 172 | @Override |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 173 | public void onUserUnlocking(@NonNull TargetUser user) { |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 174 | synchronized (mUnlockedUserIdsLocked) { |
| 175 | mUnlockedUserIdsLocked.add(user.getUserIdentifier()); |
| 176 | } |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 179 | private void verifyUserUnlocked(int callingUserId) { |
| 180 | synchronized (mUnlockedUserIdsLocked) { |
| 181 | // First, check the local copy. |
| 182 | if (mUnlockedUserIdsLocked.contains(callingUserId)) { |
| 183 | return; |
| 184 | } |
| 185 | // If the local copy says the user is locked, check with UM for the actual state, |
| 186 | // since the user might just have been unlocked. |
| 187 | if (!mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(callingUserId))) { |
| 188 | throw new IllegalStateException( |
| 189 | "User " + callingUserId + " is locked or not running."); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 194 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 195 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 196 | public void setSchema( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 197 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 198 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 199 | @NonNull List<Bundle> schemaBundles, |
Alexander Dorokhine | 315cca6 | 2021-03-04 12:34:41 -0800 | [diff] [blame] | 200 | @NonNull List<String> schemasNotDisplayedBySystem, |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 201 | @NonNull Map<String, List<Bundle>> schemasPackageAccessibleBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 202 | boolean forceOverride, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 203 | int schemaVersion, |
Terry Wang | c10610d | 2021-03-21 13:21:24 -0700 | [diff] [blame] | 204 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 205 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 206 | Objects.requireNonNull(packageName); |
| 207 | Objects.requireNonNull(databaseName); |
| 208 | Objects.requireNonNull(schemaBundles); |
| 209 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 210 | int callingUid = Binder.getCallingUid(); |
| 211 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 212 | EXECUTOR.execute(() -> { |
| 213 | try { |
| 214 | verifyUserUnlocked(callingUserId); |
| 215 | verifyCallingPackage(callingUid, packageName); |
| 216 | List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size()); |
| 217 | for (int i = 0; i < schemaBundles.size(); i++) { |
| 218 | schemas.add(new AppSearchSchema(schemaBundles.get(i))); |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 219 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 220 | Map<String, List<PackageIdentifier>> schemasPackageAccessible = |
| 221 | new ArrayMap<>(schemasPackageAccessibleBundles.size()); |
| 222 | for (Map.Entry<String, List<Bundle>> entry : |
| 223 | schemasPackageAccessibleBundles.entrySet()) { |
| 224 | List<PackageIdentifier> packageIdentifiers = |
| 225 | new ArrayList<>(entry.getValue().size()); |
| 226 | for (int i = 0; i < entry.getValue().size(); i++) { |
| 227 | packageIdentifiers.add( |
| 228 | new PackageIdentifier(entry.getValue().get(i))); |
| 229 | } |
| 230 | schemasPackageAccessible.put(entry.getKey(), packageIdentifiers); |
| 231 | } |
| 232 | AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 233 | SetSchemaResponse setSchemaResponse = impl.setSchema( |
| 234 | packageName, |
| 235 | databaseName, |
| 236 | schemas, |
| 237 | schemasNotDisplayedBySystem, |
| 238 | schemasPackageAccessible, |
| 239 | forceOverride, |
| 240 | schemaVersion); |
| 241 | invokeCallbackOnResult(callback, |
| 242 | AppSearchResult.newSuccessfulResult(setSchemaResponse.getBundle())); |
| 243 | } catch (Throwable t) { |
| 244 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 245 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 246 | }); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 250 | public void getSchema( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 251 | @NonNull String packageName, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 252 | @NonNull String databaseName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 253 | @UserIdInt int userId, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 254 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 255 | Objects.requireNonNull(packageName); |
| 256 | Objects.requireNonNull(databaseName); |
| 257 | Objects.requireNonNull(callback); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 258 | int callingUid = Binder.getCallingUid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 259 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 260 | EXECUTOR.execute(() -> { |
| 261 | try { |
| 262 | verifyUserUnlocked(callingUserId); |
| 263 | verifyCallingPackage(callingUid, packageName); |
| 264 | AppSearchImpl impl = |
| 265 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 266 | GetSchemaResponse response = impl.getSchema(packageName, databaseName); |
| 267 | invokeCallbackOnResult( |
| 268 | callback, |
| 269 | AppSearchResult.newSuccessfulResult(response.getBundle())); |
| 270 | } catch (Throwable t) { |
| 271 | invokeCallbackOnError(callback, t); |
| 272 | } |
| 273 | }); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void getNamespaces( |
| 278 | @NonNull String packageName, |
| 279 | @NonNull String databaseName, |
| 280 | @UserIdInt int userId, |
| 281 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 282 | Objects.requireNonNull(packageName); |
| 283 | Objects.requireNonNull(databaseName); |
| 284 | Objects.requireNonNull(callback); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 285 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 286 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 287 | EXECUTOR.execute(() -> { |
| 288 | try { |
| 289 | verifyUserUnlocked(callingUserId); |
| 290 | verifyCallingPackage(callingUid, packageName); |
| 291 | AppSearchImpl impl = |
| 292 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 293 | List<String> namespaces = impl.getNamespaces(packageName, databaseName); |
| 294 | invokeCallbackOnResult(callback, |
| 295 | AppSearchResult.newSuccessfulResult(namespaces)); |
| 296 | } catch (Throwable t) { |
| 297 | invokeCallbackOnError(callback, t); |
| 298 | } |
| 299 | }); |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 303 | public void putDocuments( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 304 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 305 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 306 | @NonNull List<Bundle> documentBundles, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 307 | @UserIdInt int userId, |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 308 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 309 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 310 | Objects.requireNonNull(packageName); |
| 311 | Objects.requireNonNull(databaseName); |
| 312 | Objects.requireNonNull(documentBundles); |
| 313 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 314 | int callingUid = Binder.getCallingUid(); |
| 315 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 316 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 317 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
| 318 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
| 319 | PlatformLogger logger = null; |
| 320 | int operationSuccessCount = 0; |
| 321 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 322 | try { |
| 323 | verifyUserUnlocked(callingUserId); |
| 324 | verifyCallingPackage(callingUid, packageName); |
| 325 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 326 | new AppSearchBatchResult.Builder<>(); |
| 327 | AppSearchImpl impl = |
| 328 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 329 | logger = mLoggerInstanceManager.getPlatformLogger(callingUserId); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 330 | for (int i = 0; i < documentBundles.size(); i++) { |
| 331 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
| 332 | try { |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 333 | impl.putDocument(packageName, databaseName, document, logger); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 334 | resultBuilder.setSuccess(document.getId(), /*result=*/ null); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 335 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 336 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 337 | resultBuilder.setResult(document.getId(), |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 338 | throwableToFailedResult(t)); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 339 | AppSearchResult<Void> result = throwableToFailedResult(t); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 340 | resultBuilder.setResult(document.getId(), result); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 341 | // for failures, we would just log the one for last failure |
| 342 | statusCode = result.getResultCode(); |
| 343 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 344 | } |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 345 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 346 | // Now that the batch has been written. Persist the newly written data. |
| 347 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 348 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 349 | } catch (Throwable t) { |
| 350 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 351 | } finally { |
| 352 | if (logger != null) { |
| 353 | CallStats.Builder cBuilder = new CallStats.Builder(packageName, |
| 354 | databaseName) |
| 355 | .setCallType(CallStats.CALL_TYPE_PUT_DOCUMENTS) |
| 356 | // TODO(b/173532925) check the existing binder call latency chart |
| 357 | // is good enough for us: |
| 358 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 359 | .setEstimatedBinderLatencyMillis( |
| 360 | 2 * (int) (totalLatencyStartTimeMillis |
| 361 | - binderCallStartTimeMillis)) |
| 362 | .setNumOperationsSucceeded(operationSuccessCount) |
| 363 | .setNumOperationsFailed(operationFailureCount); |
| 364 | cBuilder.getGeneralStatsBuilder() |
| 365 | .setStatusCode(statusCode) |
| 366 | .setTotalLatencyMillis( |
| 367 | (int) (SystemClock.elapsedRealtime() |
| 368 | - totalLatencyStartTimeMillis)); |
| 369 | logger.logStats(cBuilder.build()); |
| 370 | } |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 371 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 372 | }); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 373 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 374 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 375 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 376 | public void getDocuments( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 377 | @NonNull String packageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 378 | @NonNull String databaseName, |
| 379 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 380 | @NonNull List<String> ids, |
Alexander Dorokhine | 87cdd15 | 2021-01-20 15:41:25 -0800 | [diff] [blame] | 381 | @NonNull Map<String, List<String>> typePropertyPaths, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 382 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 383 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 384 | Objects.requireNonNull(packageName); |
| 385 | Objects.requireNonNull(databaseName); |
| 386 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 387 | Objects.requireNonNull(ids); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 388 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 389 | int callingUid = Binder.getCallingUid(); |
| 390 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 391 | EXECUTOR.execute(() -> { |
| 392 | try { |
| 393 | verifyUserUnlocked(callingUserId); |
| 394 | verifyCallingPackage(callingUid, packageName); |
| 395 | AppSearchBatchResult.Builder<String, Bundle> resultBuilder = |
| 396 | new AppSearchBatchResult.Builder<>(); |
| 397 | AppSearchImpl impl = |
| 398 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 399 | for (int i = 0; i < ids.size(); i++) { |
| 400 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 401 | try { |
| 402 | GenericDocument document = |
| 403 | impl.getDocument( |
| 404 | packageName, |
| 405 | databaseName, |
| 406 | namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 407 | id, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 408 | typePropertyPaths); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 409 | resultBuilder.setSuccess(id, document.getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 410 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 411 | resultBuilder.setResult(id, throwableToFailedResult(t)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 412 | } |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 413 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 414 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 415 | } catch (Throwable t) { |
| 416 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 417 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 418 | }); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 419 | } |
| 420 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 421 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 422 | public void query( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 423 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 424 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 425 | @NonNull String queryExpression, |
| 426 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 427 | @UserIdInt int userId, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 428 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 429 | Objects.requireNonNull(packageName); |
| 430 | Objects.requireNonNull(databaseName); |
| 431 | Objects.requireNonNull(queryExpression); |
| 432 | Objects.requireNonNull(searchSpecBundle); |
| 433 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 434 | int callingUid = Binder.getCallingUid(); |
| 435 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 436 | EXECUTOR.execute(() -> { |
| 437 | try { |
| 438 | verifyUserUnlocked(callingUserId); |
| 439 | verifyCallingPackage(callingUid, packageName); |
| 440 | AppSearchImpl impl = |
| 441 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 442 | SearchResultPage searchResultPage = |
| 443 | impl.query( |
| 444 | packageName, |
| 445 | databaseName, |
| 446 | queryExpression, |
| 447 | new SearchSpec(searchSpecBundle)); |
| 448 | invokeCallbackOnResult( |
| 449 | callback, |
| 450 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 451 | } catch (Throwable t) { |
| 452 | invokeCallbackOnError(callback, t); |
| 453 | } |
| 454 | }); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 457 | @Override |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 458 | public void globalQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 459 | @NonNull String packageName, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 460 | @NonNull String queryExpression, |
| 461 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 462 | @UserIdInt int userId, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 463 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 464 | Objects.requireNonNull(packageName); |
| 465 | Objects.requireNonNull(queryExpression); |
| 466 | Objects.requireNonNull(searchSpecBundle); |
| 467 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 468 | int callingUid = Binder.getCallingUid(); |
| 469 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 470 | EXECUTOR.execute(() -> { |
| 471 | try { |
| 472 | verifyUserUnlocked(callingUserId); |
| 473 | verifyCallingPackage(callingUid, packageName); |
| 474 | AppSearchImpl impl = |
| 475 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 476 | SearchResultPage searchResultPage = |
| 477 | impl.globalQuery( |
| 478 | queryExpression, |
| 479 | new SearchSpec(searchSpecBundle), |
| 480 | packageName, |
| 481 | callingUid); |
| 482 | invokeCallbackOnResult( |
| 483 | callback, |
| 484 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 485 | } catch (Throwable t) { |
| 486 | invokeCallbackOnError(callback, t); |
| 487 | } |
| 488 | }); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 491 | @Override |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 492 | public void getNextPage( |
| 493 | long nextPageToken, |
| 494 | @UserIdInt int userId, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 495 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 496 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 497 | int callingUid = Binder.getCallingUid(); |
| 498 | int callingUserId = handleIncomingUser(userId, callingUid); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 499 | // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally |
| 500 | // opened it |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 501 | EXECUTOR.execute(() -> { |
| 502 | try { |
| 503 | verifyUserUnlocked(callingUserId); |
| 504 | AppSearchImpl impl = |
| 505 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 506 | SearchResultPage searchResultPage = impl.getNextPage(nextPageToken); |
| 507 | invokeCallbackOnResult( |
| 508 | callback, |
| 509 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 510 | } catch (Throwable t) { |
| 511 | invokeCallbackOnError(callback, t); |
| 512 | } |
| 513 | }); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 517 | public void invalidateNextPageToken(long nextPageToken, @UserIdInt int userId) { |
| 518 | int callingUid = Binder.getCallingUid(); |
| 519 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 520 | EXECUTOR.execute(() -> { |
| 521 | try { |
| 522 | verifyUserUnlocked(callingUserId); |
| 523 | AppSearchImpl impl = |
| 524 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 525 | impl.invalidateNextPageToken(nextPageToken); |
| 526 | } catch (Throwable t) { |
| 527 | Log.e(TAG, "Unable to invalidate the query page token", t); |
| 528 | } |
| 529 | }); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 530 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 531 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 532 | @Override |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 533 | public void writeQueryResultsToFile( |
| 534 | @NonNull String packageName, |
| 535 | @NonNull String databaseName, |
| 536 | @NonNull ParcelFileDescriptor fileDescriptor, |
| 537 | @NonNull String queryExpression, |
| 538 | @NonNull Bundle searchSpecBundle, |
| 539 | @UserIdInt int userId, |
| 540 | @NonNull IAppSearchResultCallback callback) { |
| 541 | int callingUid = Binder.getCallingUid(); |
| 542 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 543 | EXECUTOR.execute(() -> { |
| 544 | try { |
| 545 | verifyCallingPackage(callingUid, packageName); |
| 546 | AppSearchImpl impl = |
| 547 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 548 | // we don't need to append the file. The file is always brand new. |
| 549 | try (DataOutputStream outputStream = new DataOutputStream( |
| 550 | new FileOutputStream(fileDescriptor.getFileDescriptor()))) { |
| 551 | SearchResultPage searchResultPage = impl.query( |
| 552 | packageName, |
| 553 | databaseName, |
| 554 | queryExpression, |
| 555 | new SearchSpec(searchSpecBundle)); |
| 556 | while (!searchResultPage.getResults().isEmpty()) { |
| 557 | for (int i = 0; i < searchResultPage.getResults().size(); i++) { |
| 558 | AppSearchMigrationHelper.writeBundleToOutputStream( |
| 559 | outputStream, searchResultPage.getResults().get(i) |
| 560 | .getGenericDocument().getBundle()); |
| 561 | } |
| 562 | searchResultPage = impl.getNextPage( |
| 563 | searchResultPage.getNextPageToken()); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 564 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 565 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 566 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 567 | } catch (Throwable t) { |
| 568 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 569 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 570 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public void putDocumentsFromFile( |
| 575 | @NonNull String packageName, |
| 576 | @NonNull String databaseName, |
| 577 | @NonNull ParcelFileDescriptor fileDescriptor, |
| 578 | @UserIdInt int userId, |
| 579 | @NonNull IAppSearchResultCallback callback) { |
| 580 | int callingUid = Binder.getCallingUid(); |
| 581 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 582 | EXECUTOR.execute(() -> { |
| 583 | try { |
| 584 | verifyCallingPackage(callingUid, packageName); |
| 585 | AppSearchImpl impl = |
| 586 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 587 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 588 | GenericDocument document; |
| 589 | ArrayList<Bundle> migrationFailureBundles = new ArrayList<>(); |
| 590 | try (DataInputStream inputStream = new DataInputStream( |
| 591 | new FileInputStream(fileDescriptor.getFileDescriptor()))) { |
| 592 | while (true) { |
| 593 | try { |
| 594 | document = AppSearchMigrationHelper |
| 595 | .readDocumentFromInputStream(inputStream); |
| 596 | } catch (EOFException e) { |
| 597 | // nothing wrong, we just finish the reading. |
| 598 | break; |
| 599 | } |
| 600 | try { |
| 601 | impl.putDocument(packageName, databaseName, document, |
| 602 | /*logger=*/ null); |
| 603 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 604 | migrationFailureBundles.add(new SetSchemaResponse.MigrationFailure( |
| 605 | document.getNamespace(), |
| 606 | document.getId(), |
| 607 | document.getSchemaType(), |
| 608 | AppSearchResult.throwableToFailedResult(t)) |
| 609 | .getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 610 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 613 | impl.persistToDisk(PersistType.Code.FULL); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 614 | invokeCallbackOnResult(callback, |
| 615 | AppSearchResult.newSuccessfulResult(migrationFailureBundles)); |
| 616 | } catch (Throwable t) { |
| 617 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 618 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 619 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 623 | public void reportUsage( |
| 624 | @NonNull String packageName, |
| 625 | @NonNull String databaseName, |
| 626 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 627 | @NonNull String documentId, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 628 | long usageTimeMillis, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 629 | boolean systemUsage, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 630 | @UserIdInt int userId, |
| 631 | @NonNull IAppSearchResultCallback callback) { |
| 632 | Objects.requireNonNull(databaseName); |
| 633 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 634 | Objects.requireNonNull(documentId); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 635 | Objects.requireNonNull(callback); |
| 636 | int callingUid = Binder.getCallingUid(); |
| 637 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 638 | EXECUTOR.execute(() -> { |
| 639 | try { |
| 640 | verifyUserUnlocked(callingUserId); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 641 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 642 | if (systemUsage) { |
| 643 | // TODO(b/183031844): Validate that the call comes from the system |
| 644 | } |
| 645 | |
| 646 | AppSearchImpl impl = |
| 647 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 648 | impl.reportUsage( |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 649 | packageName, databaseName, namespace, documentId, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 650 | usageTimeMillis, systemUsage); |
| 651 | invokeCallbackOnResult( |
| 652 | callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); |
| 653 | } catch (Throwable t) { |
| 654 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 655 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 656 | }); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | @Override |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 660 | public void removeByDocumentId( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 661 | @NonNull String packageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 662 | @NonNull String databaseName, |
| 663 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 664 | @NonNull List<String> ids, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 665 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 666 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 667 | Objects.requireNonNull(packageName); |
| 668 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 669 | Objects.requireNonNull(ids); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 670 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 671 | int callingUid = Binder.getCallingUid(); |
| 672 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 673 | EXECUTOR.execute(() -> { |
| 674 | try { |
| 675 | verifyUserUnlocked(callingUserId); |
| 676 | verifyCallingPackage(callingUid, packageName); |
| 677 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 678 | new AppSearchBatchResult.Builder<>(); |
| 679 | AppSearchImpl impl = |
| 680 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 681 | for (int i = 0; i < ids.size(); i++) { |
| 682 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 683 | try { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 684 | impl.remove(packageName, databaseName, namespace, id); |
| 685 | resultBuilder.setSuccess(id, /*result= */ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 686 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 687 | resultBuilder.setResult(id, throwableToFailedResult(t)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 688 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 689 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 690 | // Now that the batch has been written. Persist the newly written data. |
| 691 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 692 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 693 | } catch (Throwable t) { |
| 694 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 695 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 696 | }); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 700 | public void removeByQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 701 | @NonNull String packageName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 702 | @NonNull String databaseName, |
| 703 | @NonNull String queryExpression, |
| 704 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 705 | @UserIdInt int userId, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 706 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 707 | Objects.requireNonNull(packageName); |
| 708 | Objects.requireNonNull(databaseName); |
| 709 | Objects.requireNonNull(queryExpression); |
| 710 | Objects.requireNonNull(searchSpecBundle); |
| 711 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 712 | int callingUid = Binder.getCallingUid(); |
| 713 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 714 | EXECUTOR.execute(() -> { |
| 715 | try { |
| 716 | verifyUserUnlocked(callingUserId); |
| 717 | verifyCallingPackage(callingUid, packageName); |
| 718 | AppSearchImpl impl = |
| 719 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 720 | impl.removeByQuery( |
| 721 | packageName, |
| 722 | databaseName, |
| 723 | queryExpression, |
| 724 | new SearchSpec(searchSpecBundle)); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 725 | // Now that the batch has been written. Persist the newly written data. |
| 726 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 727 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 728 | } catch (Throwable t) { |
| 729 | invokeCallbackOnError(callback, t); |
| 730 | } |
| 731 | }); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 734 | @Override |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 735 | public void getStorageInfo( |
| 736 | @NonNull String packageName, |
| 737 | @NonNull String databaseName, |
| 738 | @UserIdInt int userId, |
| 739 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 740 | Objects.requireNonNull(packageName); |
| 741 | Objects.requireNonNull(databaseName); |
| 742 | Objects.requireNonNull(callback); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 743 | int callingUid = Binder.getCallingUid(); |
| 744 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 745 | EXECUTOR.execute(() -> { |
| 746 | try { |
| 747 | verifyUserUnlocked(callingUserId); |
| 748 | verifyCallingPackage(callingUid, packageName); |
| 749 | AppSearchImpl impl = |
| 750 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
| 751 | StorageInfo storageInfo = impl.getStorageInfoForDatabase(packageName, |
| 752 | databaseName); |
| 753 | Bundle storageInfoBundle = storageInfo.getBundle(); |
| 754 | invokeCallbackOnResult( |
| 755 | callback, AppSearchResult.newSuccessfulResult(storageInfoBundle)); |
| 756 | } catch (Throwable t) { |
| 757 | invokeCallbackOnError(callback, t); |
| 758 | } |
| 759 | }); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | @Override |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 763 | public void persistToDisk(@UserIdInt int userId) { |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 764 | int callingUid = Binder.getCallingUid(); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 765 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 766 | EXECUTOR.execute(() -> { |
| 767 | try { |
| 768 | verifyUserUnlocked(callingUserId); |
| 769 | AppSearchImpl impl = |
| 770 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 771 | impl.persistToDisk(PersistType.Code.FULL); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 772 | } catch (Throwable t) { |
| 773 | Log.e(TAG, "Unable to persist the data to disk", t); |
| 774 | } |
| 775 | }); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 779 | public void initialize(@UserIdInt int userId, @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 780 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 781 | int callingUid = Binder.getCallingUid(); |
| 782 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 783 | EXECUTOR.execute(() -> { |
| 784 | try { |
| 785 | verifyUserUnlocked(callingUserId); |
| 786 | mImplInstanceManager.getOrCreateAppSearchImpl(mContext, callingUserId); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 787 | mLoggerInstanceManager.getOrCreatePlatformLogger(getContext(), callingUserId); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 788 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 789 | } catch (Throwable t) { |
| 790 | invokeCallbackOnError(callback, t); |
| 791 | } |
| 792 | }); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 795 | private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 796 | Objects.requireNonNull(callingPackage); |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 797 | if (mPackageManagerInternal.getPackageUid( |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 798 | callingPackage, /*flags=*/ 0, UserHandle.getUserId(callingUid)) |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 799 | != callingUid) { |
| 800 | throw new SecurityException( |
| 801 | "Specified calling package [" |
| 802 | + callingPackage |
| 803 | + "] does not match the calling uid " |
| 804 | + callingUid); |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 805 | } |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 808 | /** Invokes the {@link IAppSearchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 809 | private void invokeCallbackOnResult( |
| 810 | IAppSearchResultCallback callback, AppSearchResult<?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 811 | try { |
| 812 | callback.onResult(result); |
| 813 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 814 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 815 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 816 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 817 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 818 | /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 819 | private void invokeCallbackOnResult( |
| 820 | IAppSearchBatchResultCallback callback, AppSearchBatchResult<?, ?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 821 | try { |
| 822 | callback.onResult(result); |
| 823 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 824 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 825 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 829 | * Invokes the {@link IAppSearchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 830 | * |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 831 | * <p>The throwable is convert to a {@link AppSearchResult}; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 832 | */ |
| 833 | private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) { |
| 834 | try { |
| 835 | callback.onResult(throwableToFailedResult(throwable)); |
| 836 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 837 | Log.e(TAG, "Unable to send result to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 842 | * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 843 | * |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 844 | * <p>The throwable is converted to {@link AppSearchResult}. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 845 | */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 846 | private void invokeCallbackOnError( |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 847 | @NonNull IAppSearchBatchResultCallback callback, @NonNull Throwable throwable) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 848 | try { |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 849 | callback.onSystemError(throwableToFailedResult(throwable)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 850 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 851 | Log.e(TAG, "Unable to send error to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 852 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 853 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 854 | } |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 855 | |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 856 | // TODO(b/173553485) verifying that the caller has permission to access target user's data |
| 857 | // TODO(b/173553485) Handle ACTION_USER_REMOVED broadcast |
| 858 | // TODO(b/173553485) Implement SystemService.onUserStopping() |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 859 | private static int handleIncomingUser(@UserIdInt int userId, int callingUid) { |
| 860 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 861 | return ActivityManager.handleIncomingUser( |
| 862 | callingPid, |
| 863 | callingUid, |
| 864 | userId, |
| 865 | /*allowAll=*/ false, |
| 866 | /*requireFull=*/ false, |
| 867 | /*name=*/ null, |
| 868 | /*callerPackage=*/ null); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 869 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame^] | 870 | |
| 871 | // TODO(b/179160886): Cache the previous storage stats. |
| 872 | private class AppSearchStorageStatsAugmenter implements StorageStatsAugmenter { |
| 873 | @Override |
| 874 | public void augmentStatsForPackage( |
| 875 | @NonNull PackageStats stats, |
| 876 | @NonNull String packageName, |
| 877 | @UserIdInt int userId, |
| 878 | boolean callerHasStatsPermission) { |
| 879 | Objects.requireNonNull(stats); |
| 880 | Objects.requireNonNull(packageName); |
| 881 | try { |
| 882 | verifyUserUnlocked(userId); |
| 883 | AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl(mContext, |
| 884 | userId); |
| 885 | stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes(); |
| 886 | } catch (Throwable t) { |
| 887 | Log.e( |
| 888 | TAG, |
| 889 | "Unable to augment storage stats for userId " |
| 890 | + userId |
| 891 | + " packageName " |
| 892 | + packageName, |
| 893 | t); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | @Override |
| 898 | public void augmentStatsForUid( |
| 899 | @NonNull PackageStats stats, int uid, boolean callerHasStatsPermission) { |
| 900 | Objects.requireNonNull(stats); |
| 901 | int userId = UserHandle.getUserId(uid); |
| 902 | try { |
| 903 | verifyUserUnlocked(userId); |
| 904 | String[] packagesForUid = mPackageManager.getPackagesForUid(uid); |
| 905 | if (packagesForUid == null) { |
| 906 | return; |
| 907 | } |
| 908 | AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl(mContext, |
| 909 | userId); |
| 910 | for (String packageName : packagesForUid) { |
| 911 | stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes(); |
| 912 | } |
| 913 | } catch (Throwable t) { |
| 914 | Log.e(TAG, "Unable to augment storage stats for uid " + uid, t); |
| 915 | } |
| 916 | } |
| 917 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 918 | } |