Loading apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java +42 −29 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ public class AppSearchManagerService extends SystemService { public void onStart() { publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); mImplInstanceManager = new ImplInstanceManager(getContext()); mImplInstanceManager = ImplInstanceManager.getInstance(getContext()); } private class Stub extends IAppSearchManager.Stub { Loading Loading @@ -102,7 +102,8 @@ public class AppSearchManagerService extends SystemService { } schemasPackageAccessible.put(entry.getKey(), packageIdentifiers); } AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.setSchema( packageName, databaseName, Loading Loading @@ -133,7 +134,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); List<AppSearchSchema> schemas = impl.getSchema(packageName, databaseName); List<Bundle> schemaBundles = new ArrayList<>(schemas.size()); for (int i = 0; i < schemas.size(); i++) { Loading Loading @@ -166,7 +168,8 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Void> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < documentBundles.size(); i++) { GenericDocument document = new GenericDocument(documentBundles.get(i)); try { Loading Loading @@ -207,12 +210,18 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Bundle> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { GenericDocument document = impl.getDocument(packageName, databaseName, namespace, uri, typePropertyPaths); GenericDocument document = impl.getDocument( packageName, databaseName, namespace, uri, typePropertyPaths); resultBuilder.setSuccess(uri, document.getBundle()); } catch (Throwable t) { resultBuilder.setResult(uri, throwableToFailedResult(t)); Loading Loading @@ -245,7 +254,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.query( packageName, Loading Loading @@ -278,8 +288,10 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); SearchResultPage searchResultPage = impl.globalQuery( AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.globalQuery( queryExpression, new SearchSpec(searchSpecBundle), packageName, Loading @@ -306,7 +318,8 @@ public class AppSearchManagerService extends SystemService { // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally // opened it try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.getNextPage(nextPageToken); invokeCallbackOnResult( callback, Loading @@ -324,7 +337,8 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.invalidateNextPageToken(nextPageToken); } catch (Throwable t) { Log.e(TAG, "Unable to invalidate the query page token", t); Loading @@ -350,15 +364,11 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); impl.reportUsage( packageName, databaseName, namespace, uri, usageTimeMillis); invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.reportUsage(packageName, databaseName, namespace, uri, usageTimeMillis); invokeCallbackOnResult( callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); } catch (Throwable t) { invokeCallbackOnError(callback, t); } finally { Loading @@ -385,7 +395,8 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Void> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { Loading Loading @@ -421,7 +432,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.removeByQuery( packageName, databaseName, Loading @@ -441,7 +453,8 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.persistToDisk(); } catch (Throwable t) { Log.e(TAG, "Unable to persist the data to disk", t); Loading @@ -457,7 +470,7 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { mImplInstanceManager.getInstance(callingUserId); mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); } catch (Throwable t) { invokeCallbackOnError(callback, t); Loading apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java +35 −15 Original line number Diff line number Diff line Loading @@ -41,14 +41,33 @@ import java.io.File; public final class ImplInstanceManager { private static final String APP_SEARCH_DIR = "appSearch"; private static final SparseArray<AppSearchImpl> sInstances = new SparseArray<>(); private static ImplInstanceManager sImplInstanceManager; private final Context mContext; private final SparseArray<AppSearchImpl> mInstances = new SparseArray<>(); private final String mGlobalQuerierPackage; public ImplInstanceManager(@NonNull Context context) { mContext = context; mGlobalQuerierPackage = getGlobalAppSearchDataQuerierPackageName(mContext); private ImplInstanceManager(@NonNull String globalQuerierPackage) { mGlobalQuerierPackage = globalQuerierPackage; } /** * Gets an instance of ImplInstanceManager to be used. * * <p>If no instance has been initialized yet, a new one will be created. Otherwise, the * existing instance will be returned. */ @NonNull public static ImplInstanceManager getInstance(@NonNull Context context) { if (sImplInstanceManager == null) { synchronized (ImplInstanceManager.class) { if (sImplInstanceManager == null) { sImplInstanceManager = new ImplInstanceManager( getGlobalAppSearchDataQuerierPackageName(context)); } } } return sImplInstanceManager; } /** Loading @@ -57,30 +76,30 @@ public final class ImplInstanceManager { * <p>If no AppSearchImpl instance exists for this user, Icing will be initialized and one will * be created. * * @param context The context * @param userId The multi-user userId of the device user calling AppSearch * @return An initialized {@link AppSearchImpl} for this user */ @NonNull public AppSearchImpl getInstance(@UserIdInt int userId) public AppSearchImpl getAppSearchImpl(@NonNull Context context, @UserIdInt int userId) throws AppSearchException { AppSearchImpl instance = sInstances.get(userId); AppSearchImpl instance = mInstances.get(userId); if (instance == null) { synchronized (ImplInstanceManager.class) { instance = sInstances.get(userId); instance = mInstances.get(userId); if (instance == null) { instance = createImpl(userId); sInstances.put(userId, instance); instance = createImpl(context, userId); mInstances.put(userId, instance); } } } return instance; } private AppSearchImpl createImpl(@UserIdInt int userId) private AppSearchImpl createImpl(@NonNull Context context, @UserIdInt int userId) throws AppSearchException { File appSearchDir = getAppSearchDir(mContext, userId); return AppSearchImpl.create( appSearchDir, mContext, userId, mGlobalQuerierPackage); File appSearchDir = getAppSearchDir(context, userId); return AppSearchImpl.create(appSearchDir, context, userId, mGlobalQuerierPackage); } private static File getAppSearchDir(@NonNull Context context, @UserIdInt int userId) { Loading @@ -96,7 +115,8 @@ public final class ImplInstanceManager { * * @param context Context of the system service. */ private static String getGlobalAppSearchDataQuerierPackageName(Context context) { @NonNull private static String getGlobalAppSearchDataQuerierPackageName(@NonNull Context context) { String globalAppSearchDataQuerierPackage = context.getString(R.string.config_globalAppSearchDataQuerierPackage); try { Loading Loading
apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java +42 −29 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ public class AppSearchManagerService extends SystemService { public void onStart() { publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); mImplInstanceManager = new ImplInstanceManager(getContext()); mImplInstanceManager = ImplInstanceManager.getInstance(getContext()); } private class Stub extends IAppSearchManager.Stub { Loading Loading @@ -102,7 +102,8 @@ public class AppSearchManagerService extends SystemService { } schemasPackageAccessible.put(entry.getKey(), packageIdentifiers); } AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.setSchema( packageName, databaseName, Loading Loading @@ -133,7 +134,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); List<AppSearchSchema> schemas = impl.getSchema(packageName, databaseName); List<Bundle> schemaBundles = new ArrayList<>(schemas.size()); for (int i = 0; i < schemas.size(); i++) { Loading Loading @@ -166,7 +168,8 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Void> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < documentBundles.size(); i++) { GenericDocument document = new GenericDocument(documentBundles.get(i)); try { Loading Loading @@ -207,12 +210,18 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Bundle> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { GenericDocument document = impl.getDocument(packageName, databaseName, namespace, uri, typePropertyPaths); GenericDocument document = impl.getDocument( packageName, databaseName, namespace, uri, typePropertyPaths); resultBuilder.setSuccess(uri, document.getBundle()); } catch (Throwable t) { resultBuilder.setResult(uri, throwableToFailedResult(t)); Loading Loading @@ -245,7 +254,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.query( packageName, Loading Loading @@ -278,8 +288,10 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); SearchResultPage searchResultPage = impl.globalQuery( AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.globalQuery( queryExpression, new SearchSpec(searchSpecBundle), packageName, Loading @@ -306,7 +318,8 @@ public class AppSearchManagerService extends SystemService { // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally // opened it try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); SearchResultPage searchResultPage = impl.getNextPage(nextPageToken); invokeCallbackOnResult( callback, Loading @@ -324,7 +337,8 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.invalidateNextPageToken(nextPageToken); } catch (Throwable t) { Log.e(TAG, "Unable to invalidate the query page token", t); Loading @@ -350,15 +364,11 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); impl.reportUsage( packageName, databaseName, namespace, uri, usageTimeMillis); invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.reportUsage(packageName, databaseName, namespace, uri, usageTimeMillis); invokeCallbackOnResult( callback, AppSearchResult.newSuccessfulResult(/*result=*/ null)); } catch (Throwable t) { invokeCallbackOnError(callback, t); } finally { Loading @@ -385,7 +395,8 @@ public class AppSearchManagerService extends SystemService { verifyCallingPackage(callingUid, packageName); AppSearchBatchResult.Builder<String, Void> resultBuilder = new AppSearchBatchResult.Builder<>(); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); for (int i = 0; i < uris.size(); i++) { String uri = uris.get(i); try { Loading Loading @@ -421,7 +432,8 @@ public class AppSearchManagerService extends SystemService { final long callingIdentity = Binder.clearCallingIdentity(); try { verifyCallingPackage(callingUid, packageName); AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.removeByQuery( packageName, databaseName, Loading @@ -441,7 +453,8 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId); AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); impl.persistToDisk(); } catch (Throwable t) { Log.e(TAG, "Unable to persist the data to disk", t); Loading @@ -457,7 +470,7 @@ public class AppSearchManagerService extends SystemService { int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { mImplInstanceManager.getInstance(callingUserId); mImplInstanceManager.getAppSearchImpl(getContext(), callingUserId); invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); } catch (Throwable t) { invokeCallbackOnError(callback, t); Loading
apex/appsearch/service/java/com/android/server/appsearch/ImplInstanceManager.java +35 −15 Original line number Diff line number Diff line Loading @@ -41,14 +41,33 @@ import java.io.File; public final class ImplInstanceManager { private static final String APP_SEARCH_DIR = "appSearch"; private static final SparseArray<AppSearchImpl> sInstances = new SparseArray<>(); private static ImplInstanceManager sImplInstanceManager; private final Context mContext; private final SparseArray<AppSearchImpl> mInstances = new SparseArray<>(); private final String mGlobalQuerierPackage; public ImplInstanceManager(@NonNull Context context) { mContext = context; mGlobalQuerierPackage = getGlobalAppSearchDataQuerierPackageName(mContext); private ImplInstanceManager(@NonNull String globalQuerierPackage) { mGlobalQuerierPackage = globalQuerierPackage; } /** * Gets an instance of ImplInstanceManager to be used. * * <p>If no instance has been initialized yet, a new one will be created. Otherwise, the * existing instance will be returned. */ @NonNull public static ImplInstanceManager getInstance(@NonNull Context context) { if (sImplInstanceManager == null) { synchronized (ImplInstanceManager.class) { if (sImplInstanceManager == null) { sImplInstanceManager = new ImplInstanceManager( getGlobalAppSearchDataQuerierPackageName(context)); } } } return sImplInstanceManager; } /** Loading @@ -57,30 +76,30 @@ public final class ImplInstanceManager { * <p>If no AppSearchImpl instance exists for this user, Icing will be initialized and one will * be created. * * @param context The context * @param userId The multi-user userId of the device user calling AppSearch * @return An initialized {@link AppSearchImpl} for this user */ @NonNull public AppSearchImpl getInstance(@UserIdInt int userId) public AppSearchImpl getAppSearchImpl(@NonNull Context context, @UserIdInt int userId) throws AppSearchException { AppSearchImpl instance = sInstances.get(userId); AppSearchImpl instance = mInstances.get(userId); if (instance == null) { synchronized (ImplInstanceManager.class) { instance = sInstances.get(userId); instance = mInstances.get(userId); if (instance == null) { instance = createImpl(userId); sInstances.put(userId, instance); instance = createImpl(context, userId); mInstances.put(userId, instance); } } } return instance; } private AppSearchImpl createImpl(@UserIdInt int userId) private AppSearchImpl createImpl(@NonNull Context context, @UserIdInt int userId) throws AppSearchException { File appSearchDir = getAppSearchDir(mContext, userId); return AppSearchImpl.create( appSearchDir, mContext, userId, mGlobalQuerierPackage); File appSearchDir = getAppSearchDir(context, userId); return AppSearchImpl.create(appSearchDir, context, userId, mGlobalQuerierPackage); } private static File getAppSearchDir(@NonNull Context context, @UserIdInt int userId) { Loading @@ -96,7 +115,8 @@ public final class ImplInstanceManager { * * @param context Context of the system service. */ private static String getGlobalAppSearchDataQuerierPackageName(Context context) { @NonNull private static String getGlobalAppSearchDataQuerierPackageName(@NonNull Context context) { String globalAppSearchDataQuerierPackage = context.getString(R.string.config_globalAppSearchDataQuerierPackage); try { Loading