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 | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 19 | import static android.os.Process.INVALID_UID; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 20 | import static android.os.UserHandle.USER_NULL; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 21 | |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 22 | import android.annotation.ElapsedRealtimeLong; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 24 | import android.annotation.UserIdInt; |
| 25 | import android.app.ActivityManager; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 26 | import android.app.appsearch.AppSearchBatchResult; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 27 | import android.app.appsearch.AppSearchMigrationHelper; |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 28 | import android.app.appsearch.AppSearchResult; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 29 | import android.app.appsearch.AppSearchSchema; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 30 | import android.app.appsearch.GenericDocument; |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 31 | import android.app.appsearch.GetSchemaResponse; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 32 | import android.app.appsearch.IAppSearchBatchResultCallback; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 33 | import android.app.appsearch.IAppSearchManager; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 34 | import android.app.appsearch.IAppSearchResultCallback; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 35 | import android.app.appsearch.PackageIdentifier; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 36 | import android.app.appsearch.SearchResultPage; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 37 | import android.app.appsearch.SearchSpec; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 38 | import android.app.appsearch.SetSchemaResponse; |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 39 | import android.app.appsearch.StorageInfo; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 41 | import android.content.Context; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 42 | import android.content.Intent; |
| 43 | import android.content.IntentFilter; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManagerInternal; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageStats; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 48 | import android.os.Bundle; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 49 | import android.os.ParcelFileDescriptor; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 50 | import android.os.RemoteException; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 51 | import android.os.SystemClock; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 52 | import android.os.UserHandle; |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 53 | import android.os.UserManager; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 54 | import android.util.ArrayMap; |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 55 | import android.util.ArraySet; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 56 | import android.util.Log; |
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); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 131 | |
| 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 Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 142 | } |
| 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 Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 149 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 150 | if (userId == USER_NULL) { |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 151 | Log.e(TAG, "userId is missing in the intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | handleUserRemoved(userId); |
| 155 | break; |
| 156 | default: |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 157 | Log.e(TAG, "Received unknown intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 158 | } |
| 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 Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 176 | mLoggerInstanceManager.removePlatformLoggerForUser(userId); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 177 | Log.i(TAG, "Removed AppSearchImpl instance for user: " + userId); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 178 | } catch (Throwable t) { |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 179 | 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 Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 223 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 226 | @Override |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 227 | public void onUserUnlocking(@NonNull TargetUser user) { |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 228 | synchronized (mUnlockedUserIdsLocked) { |
| 229 | mUnlockedUserIdsLocked.add(user.getUserIdentifier()); |
| 230 | } |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 233 | private void verifyUserUnlocked(int callingUserId) { |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 234 | if (isUserLocked(callingUserId)) { |
| 235 | throw new IllegalStateException("User " + callingUserId + " is locked or not running."); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private boolean isUserLocked(int callingUserId) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 240 | synchronized (mUnlockedUserIdsLocked) { |
| 241 | // First, check the local copy. |
| 242 | if (mUnlockedUserIdsLocked.contains(callingUserId)) { |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 243 | return false; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 244 | } |
| 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 Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame^] | 247 | return !mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(callingUserId)); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 251 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 252 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 253 | public void setSchema( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 254 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 255 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 256 | @NonNull List<Bundle> schemaBundles, |
Alexander Dorokhine | 315cca6 | 2021-03-04 12:34:41 -0800 | [diff] [blame] | 257 | @NonNull List<String> schemasNotDisplayedBySystem, |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 258 | @NonNull Map<String, List<Bundle>> schemasPackageAccessibleBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 259 | boolean forceOverride, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 260 | int schemaVersion, |
Terry Wang | c10610d | 2021-03-21 13:21:24 -0700 | [diff] [blame] | 261 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 262 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 263 | Objects.requireNonNull(packageName); |
| 264 | Objects.requireNonNull(databaseName); |
| 265 | Objects.requireNonNull(schemaBundles); |
| 266 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 267 | int callingUid = Binder.getCallingUid(); |
| 268 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 269 | 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 Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 276 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 277 | 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 Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 302 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 303 | }); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 307 | public void getSchema( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 308 | @NonNull String packageName, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 309 | @NonNull String databaseName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 310 | @UserIdInt int userId, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 311 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 312 | Objects.requireNonNull(packageName); |
| 313 | Objects.requireNonNull(databaseName); |
| 314 | Objects.requireNonNull(callback); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 315 | int callingUid = Binder.getCallingUid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 316 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 317 | 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 Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void getNamespaces( |
| 335 | @NonNull String packageName, |
| 336 | @NonNull String databaseName, |
| 337 | @UserIdInt int userId, |
| 338 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 339 | Objects.requireNonNull(packageName); |
| 340 | Objects.requireNonNull(databaseName); |
| 341 | Objects.requireNonNull(callback); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 342 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 343 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 344 | 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 Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 360 | public void putDocuments( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 361 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 362 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 363 | @NonNull List<Bundle> documentBundles, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 364 | @UserIdInt int userId, |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 365 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 366 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 367 | Objects.requireNonNull(packageName); |
| 368 | Objects.requireNonNull(databaseName); |
| 369 | Objects.requireNonNull(documentBundles); |
| 370 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 371 | int callingUid = Binder.getCallingUid(); |
| 372 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 373 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 374 | 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 Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 379 | 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 Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 386 | logger = mLoggerInstanceManager.getPlatformLogger(callingUserId); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 387 | for (int i = 0; i < documentBundles.size(); i++) { |
| 388 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
| 389 | try { |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 390 | impl.putDocument(packageName, databaseName, document, logger); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 391 | resultBuilder.setSuccess(document.getId(), /*result=*/ null); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 392 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 393 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 394 | resultBuilder.setResult(document.getId(), |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 395 | throwableToFailedResult(t)); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 396 | AppSearchResult<Void> result = throwableToFailedResult(t); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 397 | resultBuilder.setResult(document.getId(), result); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 398 | // for failures, we would just log the one for last failure |
| 399 | statusCode = result.getResultCode(); |
| 400 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 401 | } |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 402 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 403 | // Now that the batch has been written. Persist the newly written data. |
| 404 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 405 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 406 | } catch (Throwable t) { |
| 407 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 408 | } 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 Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 428 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 429 | }); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 430 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 431 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 432 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 433 | public void getDocuments( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 434 | @NonNull String packageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 435 | @NonNull String databaseName, |
| 436 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 437 | @NonNull List<String> ids, |
Alexander Dorokhine | 87cdd15 | 2021-01-20 15:41:25 -0800 | [diff] [blame] | 438 | @NonNull Map<String, List<String>> typePropertyPaths, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 439 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 440 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 441 | Objects.requireNonNull(packageName); |
| 442 | Objects.requireNonNull(databaseName); |
| 443 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 444 | Objects.requireNonNull(ids); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 445 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 446 | int callingUid = Binder.getCallingUid(); |
| 447 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 448 | 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 Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 456 | for (int i = 0; i < ids.size(); i++) { |
| 457 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 458 | try { |
| 459 | GenericDocument document = |
| 460 | impl.getDocument( |
| 461 | packageName, |
| 462 | databaseName, |
| 463 | namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 464 | id, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 465 | typePropertyPaths); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 466 | resultBuilder.setSuccess(id, document.getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 467 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 468 | resultBuilder.setResult(id, throwableToFailedResult(t)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 469 | } |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 470 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 471 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 472 | } catch (Throwable t) { |
| 473 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 474 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 475 | }); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 476 | } |
| 477 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 478 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 479 | public void query( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 480 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 481 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 482 | @NonNull String queryExpression, |
| 483 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 484 | @UserIdInt int userId, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 485 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 486 | Objects.requireNonNull(packageName); |
| 487 | Objects.requireNonNull(databaseName); |
| 488 | Objects.requireNonNull(queryExpression); |
| 489 | Objects.requireNonNull(searchSpecBundle); |
| 490 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 491 | int callingUid = Binder.getCallingUid(); |
| 492 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 493 | 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 Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 514 | @Override |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 515 | public void globalQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 516 | @NonNull String packageName, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 517 | @NonNull String queryExpression, |
| 518 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 519 | @UserIdInt int userId, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 520 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 521 | Objects.requireNonNull(packageName); |
| 522 | Objects.requireNonNull(queryExpression); |
| 523 | Objects.requireNonNull(searchSpecBundle); |
| 524 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 525 | int callingUid = Binder.getCallingUid(); |
| 526 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 527 | 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 Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 548 | @Override |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 549 | public void getNextPage( |
| 550 | long nextPageToken, |
| 551 | @UserIdInt int userId, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 552 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 553 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 554 | int callingUid = Binder.getCallingUid(); |
| 555 | int callingUserId = handleIncomingUser(userId, callingUid); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 556 | // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally |
| 557 | // opened it |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 558 | 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 Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 574 | public void invalidateNextPageToken(long nextPageToken, @UserIdInt int userId) { |
| 575 | int callingUid = Binder.getCallingUid(); |
| 576 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 577 | 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 | }); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 587 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 588 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 589 | @Override |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 590 | 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 Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 600 | 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 Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 621 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 622 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 623 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 624 | } catch (Throwable t) { |
| 625 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 626 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 627 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 628 | } |
| 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 Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 639 | EXECUTOR.execute(() -> { |
| 640 | try { |
| 641 | verifyCallingPackage(callingUid, packageName); |
| 642 | AppSearchImpl impl = |
| 643 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 644 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 645 | 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 Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 661 | migrationFailureBundles.add(new SetSchemaResponse.MigrationFailure( |
| 662 | document.getNamespace(), |
| 663 | document.getId(), |
| 664 | document.getSchemaType(), |
| 665 | AppSearchResult.throwableToFailedResult(t)) |
| 666 | .getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 667 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 668 | } |
| 669 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 670 | impl.persistToDisk(PersistType.Code.FULL); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 671 | invokeCallbackOnResult(callback, |
| 672 | AppSearchResult.newSuccessfulResult(migrationFailureBundles)); |
| 673 | } catch (Throwable t) { |
| 674 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 675 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 676 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | @Override |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 680 | public void reportUsage( |
| 681 | @NonNull String packageName, |
| 682 | @NonNull String databaseName, |
| 683 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 684 | @NonNull String documentId, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 685 | long usageTimeMillis, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 686 | boolean systemUsage, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 687 | @UserIdInt int userId, |
| 688 | @NonNull IAppSearchResultCallback callback) { |
| 689 | Objects.requireNonNull(databaseName); |
| 690 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 691 | Objects.requireNonNull(documentId); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 692 | Objects.requireNonNull(callback); |
| 693 | int callingUid = Binder.getCallingUid(); |
| 694 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 695 | EXECUTOR.execute(() -> { |
| 696 | try { |
| 697 | verifyUserUnlocked(callingUserId); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 698 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 699 | 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 Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 706 | packageName, databaseName, namespace, documentId, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 707 | usageTimeMillis, systemUsage); |
| 708 | invokeCallbackOnResult( |
| 709 | callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); |
| 710 | } catch (Throwable t) { |
| 711 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 712 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 713 | }); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | @Override |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 717 | public void removeByDocumentId( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 718 | @NonNull String packageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 719 | @NonNull String databaseName, |
| 720 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 721 | @NonNull List<String> ids, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 722 | @UserIdInt int userId, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 723 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 724 | Objects.requireNonNull(packageName); |
| 725 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 726 | Objects.requireNonNull(ids); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 727 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 728 | int callingUid = Binder.getCallingUid(); |
| 729 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 730 | 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 Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 738 | for (int i = 0; i < ids.size(); i++) { |
| 739 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 740 | try { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 741 | impl.remove(packageName, databaseName, namespace, id); |
| 742 | resultBuilder.setSuccess(id, /*result= */ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 743 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 744 | resultBuilder.setResult(id, throwableToFailedResult(t)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 745 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 746 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 747 | // Now that the batch has been written. Persist the newly written data. |
| 748 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 749 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 750 | } catch (Throwable t) { |
| 751 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 752 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 753 | }); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 757 | public void removeByQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 758 | @NonNull String packageName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 759 | @NonNull String databaseName, |
| 760 | @NonNull String queryExpression, |
| 761 | @NonNull Bundle searchSpecBundle, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 762 | @UserIdInt int userId, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 763 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 764 | Objects.requireNonNull(packageName); |
| 765 | Objects.requireNonNull(databaseName); |
| 766 | Objects.requireNonNull(queryExpression); |
| 767 | Objects.requireNonNull(searchSpecBundle); |
| 768 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 769 | int callingUid = Binder.getCallingUid(); |
| 770 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 771 | 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 Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 782 | // Now that the batch has been written. Persist the newly written data. |
| 783 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 784 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 785 | } catch (Throwable t) { |
| 786 | invokeCallbackOnError(callback, t); |
| 787 | } |
| 788 | }); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 791 | @Override |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 792 | public void getStorageInfo( |
| 793 | @NonNull String packageName, |
| 794 | @NonNull String databaseName, |
| 795 | @UserIdInt int userId, |
| 796 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 797 | Objects.requireNonNull(packageName); |
| 798 | Objects.requireNonNull(databaseName); |
| 799 | Objects.requireNonNull(callback); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 800 | int callingUid = Binder.getCallingUid(); |
| 801 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 802 | 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 Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | @Override |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 820 | public void persistToDisk(@UserIdInt int userId) { |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 821 | int callingUid = Binder.getCallingUid(); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 822 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 823 | EXECUTOR.execute(() -> { |
| 824 | try { |
| 825 | verifyUserUnlocked(callingUserId); |
| 826 | AppSearchImpl impl = |
| 827 | mImplInstanceManager.getAppSearchImpl(callingUserId); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 828 | impl.persistToDisk(PersistType.Code.FULL); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 829 | } catch (Throwable t) { |
| 830 | Log.e(TAG, "Unable to persist the data to disk", t); |
| 831 | } |
| 832 | }); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 836 | public void initialize(@UserIdInt int userId, @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 837 | Objects.requireNonNull(callback); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 838 | int callingUid = Binder.getCallingUid(); |
| 839 | int callingUserId = handleIncomingUser(userId, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 840 | EXECUTOR.execute(() -> { |
| 841 | try { |
| 842 | verifyUserUnlocked(callingUserId); |
| 843 | mImplInstanceManager.getOrCreateAppSearchImpl(mContext, callingUserId); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 844 | mLoggerInstanceManager.getOrCreatePlatformLogger(getContext(), callingUserId); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 845 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 846 | } catch (Throwable t) { |
| 847 | invokeCallbackOnError(callback, t); |
| 848 | } |
| 849 | }); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 852 | private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 853 | Objects.requireNonNull(callingPackage); |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 854 | if (mPackageManagerInternal.getPackageUid( |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 855 | callingPackage, /*flags=*/ 0, UserHandle.getUserId(callingUid)) |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 856 | != callingUid) { |
| 857 | throw new SecurityException( |
| 858 | "Specified calling package [" |
| 859 | + callingPackage |
| 860 | + "] does not match the calling uid " |
| 861 | + callingUid); |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 862 | } |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 865 | /** Invokes the {@link IAppSearchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 866 | private void invokeCallbackOnResult( |
| 867 | IAppSearchResultCallback callback, AppSearchResult<?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 868 | try { |
| 869 | callback.onResult(result); |
| 870 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 871 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 872 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 873 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 874 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 875 | /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 876 | private void invokeCallbackOnResult( |
| 877 | IAppSearchBatchResultCallback callback, AppSearchBatchResult<?, ?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 878 | try { |
| 879 | callback.onResult(result); |
| 880 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 881 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 882 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 886 | * Invokes the {@link IAppSearchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 887 | * |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 888 | * <p>The throwable is convert to a {@link AppSearchResult}; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 889 | */ |
| 890 | private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) { |
| 891 | try { |
| 892 | callback.onResult(throwableToFailedResult(throwable)); |
| 893 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 894 | Log.e(TAG, "Unable to send result to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 899 | * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 900 | * |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 901 | * <p>The throwable is converted to {@link AppSearchResult}. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 902 | */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 903 | private void invokeCallbackOnError( |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 904 | @NonNull IAppSearchBatchResultCallback callback, @NonNull Throwable throwable) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 905 | try { |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 906 | callback.onSystemError(throwableToFailedResult(throwable)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 907 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 908 | Log.e(TAG, "Unable to send error to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 909 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 910 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 911 | } |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 912 | |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 913 | // 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 Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 916 | private static int handleIncomingUser(@UserIdInt int userId, int callingUid) { |
| 917 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 918 | return ActivityManager.handleIncomingUser( |
| 919 | callingPid, |
| 920 | callingUid, |
| 921 | userId, |
| 922 | /*allowAll=*/ false, |
| 923 | /*requireFull=*/ false, |
| 924 | /*name=*/ null, |
| 925 | /*callerPackage=*/ null); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 926 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 927 | |
| 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 Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 975 | } |