From 751415fb9f07255e4286ea290448a3e5fc0090a6 Mon Sep 17 00:00:00 2001 From: Cassie Wang Date: Wed, 24 Mar 2021 09:23:18 -0700 Subject: [PATCH] Implement getStorageInfo. This retrieves storage info for a certain package and database. Bug: 182909475 Test: atest -m -c --rebuild-module-info CtsAppSearchTestCases FrameworksCoreTests: android.app.appsearch FrameworksServicesTests: com.android.server.appsearch Change-Id: I524e0b2ada919981f50722f01ecd3f62b2ae4f43 --- .../app/appsearch/AppSearchSession.java | 24 +++++++++++++-- .../app/appsearch/IAppSearchManager.aidl | 16 ++++++++++ .../appsearch/AppSearchManagerService.java | 29 +++++++++++++++++++ .../server/pm/BaseShortcutManagerTest.java | 6 ++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/apex/appsearch/framework/java/android/app/appsearch/AppSearchSession.java b/apex/appsearch/framework/java/android/app/appsearch/AppSearchSession.java index 0f6468a62794e..00312aec10293 100644 --- a/apex/appsearch/framework/java/android/app/appsearch/AppSearchSession.java +++ b/apex/appsearch/framework/java/android/app/appsearch/AppSearchSession.java @@ -641,8 +641,28 @@ public final class AppSearchSession implements Closeable { Objects.requireNonNull(executor); Objects.requireNonNull(callback); Preconditions.checkState(!mIsClosed, "AppSearchSession has already been closed"); - // TODO(b/182909475): Implement getStorageInfo - throw new UnsupportedOperationException(); + try { + mService.getStorageInfo( + mPackageName, + mDatabaseName, + mUserId, + new IAppSearchResultCallback.Stub() { + public void onResult(AppSearchResult result) { + executor.execute(() -> { + if (result.isSuccess()) { + Bundle responseBundle = (Bundle) result.getResultValue(); + StorageInfo response = + new StorageInfo(responseBundle); + callback.accept(AppSearchResult.newSuccessfulResult(response)); + } else { + callback.accept(result); + } + }); + } + }); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } /** diff --git a/apex/appsearch/framework/java/android/app/appsearch/IAppSearchManager.aidl b/apex/appsearch/framework/java/android/app/appsearch/IAppSearchManager.aidl index 48c397f324ec4..a8ac27c11089c 100644 --- a/apex/appsearch/framework/java/android/app/appsearch/IAppSearchManager.aidl +++ b/apex/appsearch/framework/java/android/app/appsearch/IAppSearchManager.aidl @@ -306,6 +306,22 @@ interface IAppSearchManager { in int userId, in IAppSearchResultCallback callback); + /** + * Gets the storage info. + * + * @param packageName The name of the package to get the storage info for. + * @param databaseName The databaseName to get the storage info for. + * @param userId Id of the calling user + * @param callback {@link IAppSearchResultCallback#onResult} will be called with an + * {@link AppSearchResult}<{@link Bundle}>, where the value is a + * {@link StorageInfo}. + */ + void getStorageInfo( + in String packageName, + in String databaseName, + in int userId, + in IAppSearchResultCallback callback); + /** * Persists all update/delete requests to the disk. * diff --git a/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java b/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java index 91ed6cd4a638a..991dda7cacd2d 100644 --- a/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java +++ b/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java @@ -33,6 +33,7 @@ import android.app.appsearch.PackageIdentifier; import android.app.appsearch.SearchResultPage; import android.app.appsearch.SearchSpec; import android.app.appsearch.SetSchemaResponse; +import android.app.appsearch.StorageInfo; import android.content.Context; import android.content.pm.PackageManagerInternal; import android.os.Binder; @@ -609,6 +610,34 @@ public class AppSearchManagerService extends SystemService { } } + @Override + public void getStorageInfo( + @NonNull String packageName, + @NonNull String databaseName, + @UserIdInt int userId, + @NonNull IAppSearchResultCallback callback) { + Preconditions.checkNotNull(packageName); + Preconditions.checkNotNull(databaseName); + Preconditions.checkNotNull(callback); + int callingUid = Binder.getCallingUid(); + int callingUserId = handleIncomingUser(userId, callingUid); + final long callingIdentity = Binder.clearCallingIdentity(); + try { + verifyUserUnlocked(callingUserId); + verifyCallingPackage(callingUid, packageName); + AppSearchImpl impl = + mImplInstanceManager.getAppSearchImpl(callingUserId); + StorageInfo storageInfo = impl.getStorageInfoForDatabase(packageName, databaseName); + Bundle storageInfoBundle = storageInfo.getBundle(); + invokeCallbackOnResult( + callback, AppSearchResult.newSuccessfulResult(storageInfoBundle)); + } catch (Throwable t) { + invokeCallbackOnError(callback, t); + } finally { + Binder.restoreCallingIdentity(callingIdentity); + } + } + @Override public void persistToDisk(@UserIdInt int userId) { int callingUid = Binder.getCallingUidOrThrow(); diff --git a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java index 02b8dddae7c19..6e1e6ffe53b23 100644 --- a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java @@ -847,6 +847,12 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { callback.onResult(AppSearchResult.newSuccessfulResult(null)); } + @Override + public void getStorageInfo(String packageName, String databaseName, int userId, + IAppSearchResultCallback callback) throws RemoteException { + ignore(callback); + } + @Override public void persistToDisk(int userId) throws RemoteException {