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 a45fa39bd58d0..68c906948f125 100644 --- a/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java +++ b/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java @@ -119,8 +119,7 @@ public class AppSearchManagerService extends SystemService { } schemasPackageAccessible.put(entry.getKey(), packageIdentifiers); } - AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUserId); impl.setSchema( packageName, databaseName, @@ -153,7 +152,7 @@ public class AppSearchManagerService extends SystemService { verifyUserUnlocked(callingUserId); verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); List schemas = impl.getSchema(packageName, databaseName); List schemaBundles = new ArrayList<>(schemas.size()); for (int i = 0; i < schemas.size(); i++) { @@ -188,7 +187,7 @@ public class AppSearchManagerService extends SystemService { AppSearchBatchResult.Builder resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); for (int i = 0; i < documentBundles.size(); i++) { GenericDocument document = new GenericDocument(documentBundles.get(i)); try { @@ -231,7 +230,7 @@ public class AppSearchManagerService extends SystemService { AppSearchBatchResult.Builder resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { @@ -276,7 +275,7 @@ public class AppSearchManagerService extends SystemService { verifyUserUnlocked(callingUserId); verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); SearchResultPage searchResultPage = impl.query( packageName, @@ -311,7 +310,7 @@ public class AppSearchManagerService extends SystemService { verifyUserUnlocked(callingUserId); verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); SearchResultPage searchResultPage = impl.globalQuery( queryExpression, @@ -342,7 +341,7 @@ public class AppSearchManagerService extends SystemService { try { verifyUserUnlocked(callingUserId); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); SearchResultPage searchResultPage = impl.getNextPage(nextPageToken); invokeCallbackOnResult( callback, @@ -362,7 +361,7 @@ public class AppSearchManagerService extends SystemService { try { verifyUserUnlocked(callingUserId); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); impl.invalidateNextPageToken(nextPageToken); } catch (Throwable t) { Log.e(TAG, "Unable to invalidate the query page token", t); @@ -390,7 +389,7 @@ public class AppSearchManagerService extends SystemService { try { verifyUserUnlocked(callingUserId); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); impl.reportUsage(packageName, databaseName, namespace, uri, usageTimeMillis); invokeCallbackOnResult( callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); @@ -422,7 +421,7 @@ public class AppSearchManagerService extends SystemService { AppSearchBatchResult.Builder resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { @@ -460,7 +459,7 @@ public class AppSearchManagerService extends SystemService { verifyUserUnlocked(callingUserId); verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); impl.removeByQuery( packageName, databaseName, @@ -482,7 +481,7 @@ public class AppSearchManagerService extends SystemService { try { verifyUserUnlocked(callingUserId); AppSearchImpl impl = - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getAppSearchImpl(callingUserId); impl.persistToDisk(); } catch (Throwable t) { Log.e(TAG, "Unable to persist the data to disk", t); @@ -499,7 +498,7 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyUserUnlocked(callingUserId); - mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); + mImplInstanceManager.getOrCreateAppSearchImpl(getContext(), callingUserId); invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); } catch (Throwable t) { invokeCallbackOnError(callback, t); @@ -571,6 +570,7 @@ public class AppSearchManagerService extends SystemService { private void invokeCallbackOnError( IAppSearchBatchResultCallback callback, Throwable throwable) { try { + //TODO(b/175067650) verify ParcelableException could propagate throwable correctly. callback.onSystemError(new ParcelableException(throwable)); } catch (RemoteException e) { Log.e(TAG, "Unable to send error to the callback", e); diff --git a/apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java b/apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java index 82319d4f353b6..8c953d12fd982 100644 --- a/apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java +++ b/apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java @@ -74,7 +74,7 @@ public final class ImplInstanceManager { } /** - * Gets an instance of AppSearchImpl for the given user. + * Gets an instance of AppSearchImpl for the given user, or creates one if none exists. * *

If no AppSearchImpl instance exists for the unlocked user, Icing will be initialized and * one will be created. @@ -84,7 +84,7 @@ public final class ImplInstanceManager { * @return An initialized {@link AppSearchImpl} for this user */ @NonNull - public AppSearchImpl getAppSearchImpl( + public AppSearchImpl getOrCreateAppSearchImpl( @NonNull Context context, @UserIdInt int userId) throws AppSearchException { synchronized (mInstancesLocked) { AppSearchImpl instance = mInstancesLocked.get(userId); @@ -96,6 +96,32 @@ public final class ImplInstanceManager { } } + + /** + * Gets an instance of AppSearchImpl for the given user. + * + *

This method should only be called by an initialized SearchSession, which has been already + * created the AppSearchImpl instance for the given user. + * + * @param userId The multi-user userId of the device user calling AppSearch + * @return An initialized {@link AppSearchImpl} for this user + * @throws IllegalStateException if {@link AppSearchImpl} haven't created for the given user. + */ + @NonNull + public AppSearchImpl getAppSearchImpl(@UserIdInt int userId) { + synchronized (mInstancesLocked) { + AppSearchImpl instance = mInstancesLocked.get(userId); + if (instance == null) { + // Impossible scenario, user cannot call an uninitialized SearchSession, + // getInstance should always find the instance for the given user and never try to + // create an instance for this user again. + throw new IllegalStateException( + "AppSearchImpl has never been created for this user: " + userId); + } + return instance; + } + } + private AppSearchImpl createImpl(@NonNull Context context, @UserIdInt int userId) throws AppSearchException { File appSearchDir = getAppSearchDir(context, userId); @@ -104,6 +130,7 @@ public final class ImplInstanceManager { private static File getAppSearchDir(@NonNull Context context, @UserIdInt int userId) { // See com.android.internal.app.ChooserActivity::getPinnedSharedPrefs + //TODO(b/177685938):Switch from getDataUserCePackageDirectory to getDataSystemCeDirectory File userCeDir = Environment.getDataUserCePackageDirectory( StorageManager.UUID_PRIVATE_INTERNAL, userId, context.getPackageName()); diff --git a/apex/appsearch/testing/Android.bp b/apex/appsearch/testing/Android.bp index eb072afec6961..3c5082ef937db 100644 --- a/apex/appsearch/testing/Android.bp +++ b/apex/appsearch/testing/Android.bp @@ -30,5 +30,8 @@ java_library { "guava", "truth-prebuilt", ], - visibility: ["//cts/tests/appsearch"], + visibility: [ + "//cts/tests/appsearch", + "//vendor:__subpackages__", + ], }