Merge "Pass global query package name to AppSearchImpl."

This commit is contained in:
Cassie Wang
2021-01-21 17:47:35 +00:00
committed by Android (Google) Code Review
2 changed files with 59 additions and 22 deletions

View File

@@ -54,6 +54,7 @@ import java.util.Objects;
public class AppSearchManagerService extends SystemService {
private static final String TAG = "AppSearchManagerService";
private PackageManagerInternal mPackageManagerInternal;
private ImplInstanceManager mImplInstanceManager;
public AppSearchManagerService(Context context) {
super(context);
@@ -63,6 +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());
}
private class Stub extends IAppSearchManager.Stub {
@@ -100,7 +102,7 @@ public class AppSearchManagerService extends SystemService {
}
schemasPackageAccessible.put(entry.getKey(), packageIdentifiers);
}
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
impl.setSchema(
packageName,
databaseName,
@@ -131,7 +133,7 @@ public class AppSearchManagerService extends SystemService {
final long callingIdentity = Binder.clearCallingIdentity();
try {
verifyCallingPackage(callingUid, packageName);
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
List<AppSearchSchema> schemas = impl.getSchema(packageName, databaseName);
List<Bundle> schemaBundles = new ArrayList<>(schemas.size());
for (int i = 0; i < schemas.size(); i++) {
@@ -164,7 +166,7 @@ public class AppSearchManagerService extends SystemService {
verifyCallingPackage(callingUid, packageName);
AppSearchBatchResult.Builder<String, Void> resultBuilder =
new AppSearchBatchResult.Builder<>();
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
for (int i = 0; i < documentBundles.size(); i++) {
GenericDocument document = new GenericDocument(documentBundles.get(i));
try {
@@ -205,7 +207,7 @@ public class AppSearchManagerService extends SystemService {
verifyCallingPackage(callingUid, packageName);
AppSearchBatchResult.Builder<String, Bundle> resultBuilder =
new AppSearchBatchResult.Builder<>();
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
for (int i = 0; i < uris.size(); i++) {
String uri = uris.get(i);
try {
@@ -243,7 +245,7 @@ public class AppSearchManagerService extends SystemService {
final long callingIdentity = Binder.clearCallingIdentity();
try {
verifyCallingPackage(callingUid, packageName);
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
SearchResultPage searchResultPage =
impl.query(
packageName,
@@ -276,7 +278,7 @@ public class AppSearchManagerService extends SystemService {
final long callingIdentity = Binder.clearCallingIdentity();
try {
verifyCallingPackage(callingUid, packageName);
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
SearchResultPage searchResultPage = impl.globalQuery(
queryExpression,
new SearchSpec(searchSpecBundle),
@@ -304,7 +306,7 @@ public class AppSearchManagerService extends SystemService {
// TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally
// opened it
try {
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
SearchResultPage searchResultPage = impl.getNextPage(nextPageToken);
invokeCallbackOnResult(
callback,
@@ -322,7 +324,7 @@ public class AppSearchManagerService extends SystemService {
int callingUserId = handleIncomingUser(userId, callingUid);
final long callingIdentity = Binder.clearCallingIdentity();
try {
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
impl.invalidateNextPageToken(nextPageToken);
} catch (Throwable t) {
Log.e(TAG, "Unable to invalidate the query page token", t);
@@ -348,7 +350,7 @@ public class AppSearchManagerService extends SystemService {
int callingUserId = handleIncomingUser(userId, callingUid);
final long callingIdentity = Binder.clearCallingIdentity();
try {
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
impl.reportUsage(
packageName,
databaseName,
@@ -383,7 +385,7 @@ public class AppSearchManagerService extends SystemService {
verifyCallingPackage(callingUid, packageName);
AppSearchBatchResult.Builder<String, Void> resultBuilder =
new AppSearchBatchResult.Builder<>();
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
for (int i = 0; i < uris.size(); i++) {
String uri = uris.get(i);
try {
@@ -419,7 +421,7 @@ public class AppSearchManagerService extends SystemService {
final long callingIdentity = Binder.clearCallingIdentity();
try {
verifyCallingPackage(callingUid, packageName);
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
impl.removeByQuery(
packageName,
databaseName,
@@ -439,7 +441,7 @@ public class AppSearchManagerService extends SystemService {
int callingUserId = handleIncomingUser(userId, callingUid);
final long callingIdentity = Binder.clearCallingIdentity();
try {
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
AppSearchImpl impl = mImplInstanceManager.getInstance(callingUserId);
impl.persistToDisk();
} catch (Throwable t) {
Log.e(TAG, "Unable to persist the data to disk", t);
@@ -455,7 +457,7 @@ public class AppSearchManagerService extends SystemService {
int callingUserId = handleIncomingUser(userId, callingUid);
final long callingIdentity = Binder.clearCallingIdentity();
try {
ImplInstanceManager.getInstance(getContext(), callingUserId);
mImplInstanceManager.getInstance(callingUserId);
invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
} catch (Throwable t) {
invokeCallbackOnError(callback, t);

View File

@@ -16,14 +16,19 @@
package com.android.server.appsearch;
import static android.content.pm.PackageManager.MATCH_FACTORY_ONLY;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.appsearch.exceptions.AppSearchException;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.util.SparseArray;
import com.android.internal.R;
import com.android.server.appsearch.external.localstorage.AppSearchImpl;
import java.io.File;
@@ -38,7 +43,13 @@ public final class ImplInstanceManager {
private static final SparseArray<AppSearchImpl> sInstances = new SparseArray<>();
private ImplInstanceManager() {}
private final Context mContext;
private final String mGlobalQuerierPackage;
public ImplInstanceManager(@NonNull Context context) {
mContext = context;
mGlobalQuerierPackage = getGlobalAppSearchDataQuerierPackageName(mContext);
}
/**
* Gets an instance of AppSearchImpl for the given user.
@@ -46,19 +57,18 @@ 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 Android context
* @param userId The multi-user userId of the device user calling AppSearch
* @return An initialized {@link AppSearchImpl} for this user
*/
@NonNull
public static AppSearchImpl getInstance(@NonNull Context context, @UserIdInt int userId)
public AppSearchImpl getInstance(@UserIdInt int userId)
throws AppSearchException {
AppSearchImpl instance = sInstances.get(userId);
if (instance == null) {
synchronized (ImplInstanceManager.class) {
instance = sInstances.get(userId);
if (instance == null) {
instance = createImpl(context, userId);
instance = createImpl(userId);
sInstances.put(userId, instance);
}
}
@@ -66,16 +76,41 @@ public final class ImplInstanceManager {
return instance;
}
private static AppSearchImpl createImpl(@NonNull Context context, @UserIdInt int userId)
private AppSearchImpl createImpl(@UserIdInt int userId)
throws AppSearchException {
File appSearchDir = getAppSearchDir(context, userId);
return AppSearchImpl.create(appSearchDir, context, userId, /*globalQuerierPackage=*/"");
File appSearchDir = getAppSearchDir(mContext, userId);
return AppSearchImpl.create(
appSearchDir, mContext, userId, mGlobalQuerierPackage);
}
private static File getAppSearchDir(@NonNull Context context, @UserIdInt int userId) {
// See com.android.internal.app.ChooserActivity::getPinnedSharedPrefs
File userCeDir = Environment.getDataUserCePackageDirectory(
StorageManager.UUID_PRIVATE_INTERNAL, userId, context.getPackageName());
File userCeDir =
Environment.getDataUserCePackageDirectory(
StorageManager.UUID_PRIVATE_INTERNAL, userId, context.getPackageName());
return new File(userCeDir, APP_SEARCH_DIR);
}
/**
* Returns the global querier package if it's a system package. Otherwise, empty string.
*
* @param context Context of the system service.
*/
private static String getGlobalAppSearchDataQuerierPackageName(Context context) {
String globalAppSearchDataQuerierPackage =
context.getString(R.string.config_globalAppSearchDataQuerierPackage);
try {
if (context.getPackageManager()
.getPackageInfoAsUser(
globalAppSearchDataQuerierPackage,
MATCH_FACTORY_ONLY,
UserHandle.USER_SYSTEM)
== null) {
return "";
}
} catch (PackageManager.NameNotFoundException e) {
return "";
}
return globalAppSearchDataQuerierPackage;
}
}