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