Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d80bdbd authored by Cassie Wang's avatar Cassie Wang Committed by Android (Google) Code Review
Browse files

Merge "Fix locking issues." into sc-dev

parents 1c916e81 446c9e63
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
import com.android.server.SystemService;
@@ -58,8 +59,11 @@ public class AppSearchManagerService extends SystemService {
    private PackageManagerInternal mPackageManagerInternal;
    private ImplInstanceManager mImplInstanceManager;

    // Cache of unlocked user ids so we don't have to query UserManager service each time.
    private final Set<Integer> mUnlockedUserIds = new ArraySet<>();
    // Cache of unlocked user ids so we don't have to query UserManager service each time. The
    // "locked" suffix refers to the fact that access to the field should be locked; unrelated to
    // the unlocked status of user ids.
    @GuardedBy("mUnlockedUserIdsLocked")
    private final Set<Integer> mUnlockedUserIdsLocked = new ArraySet<>();

    public AppSearchManagerService(Context context) {
        super(context);
@@ -74,7 +78,9 @@ public class AppSearchManagerService extends SystemService {

    @Override
    public void onUserUnlocked(@NonNull TargetUser user) {
        mUnlockedUserIds.add(user.getUserIdentifier());
        synchronized (mUnlockedUserIdsLocked) {
            mUnlockedUserIdsLocked.add(user.getUserIdentifier());
        }
    }

    private class Stub extends IAppSearchManager.Stub {
@@ -503,11 +509,13 @@ public class AppSearchManagerService extends SystemService {
        }

        private void verifyUserUnlocked(int callingUserId) {
            if (!mUnlockedUserIds.contains(callingUserId)) {
            synchronized (mUnlockedUserIdsLocked) {
                if (!mUnlockedUserIdsLocked.contains(callingUserId)) {
                    throw new IllegalStateException(
                            "User " + callingUserId + " is locked or not running.");
                }
            }
        }

        private void verifyCallingPackage(int callingUid, @NonNull String callingPackage) {
            Preconditions.checkNotNull(callingPackage);
+12 −12
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.storage.StorageManager;
import android.util.SparseArray;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.server.appsearch.external.localstorage.AppSearchImpl;

import java.io.File;
@@ -43,7 +44,9 @@ public final class ImplInstanceManager {

    private static ImplInstanceManager sImplInstanceManager;

    private final SparseArray<AppSearchImpl> mInstances = new SparseArray<>();
    @GuardedBy("mInstancesLocked")
    private final SparseArray<AppSearchImpl> mInstancesLocked = new SparseArray<>();

    private final String mGlobalQuerierPackage;

    private ImplInstanceManager(@NonNull String globalQuerierPackage) {
@@ -81,20 +84,17 @@ public final class ImplInstanceManager {
     * @return An initialized {@link AppSearchImpl} for this user
     */
    @NonNull
    public AppSearchImpl getAppSearchImpl(@NonNull Context context, @UserIdInt int userId)
            throws AppSearchException {
        AppSearchImpl instance = mInstances.get(userId);
        if (instance == null) {
            synchronized (ImplInstanceManager.class) {
                instance = mInstances.get(userId);
    public AppSearchImpl getAppSearchImpl(
            @NonNull Context context, @UserIdInt int userId) throws AppSearchException {
        synchronized (mInstancesLocked) {
            AppSearchImpl instance = mInstancesLocked.get(userId);
            if (instance == null) {
                instance = createImpl(context, userId);
                    mInstances.put(userId, instance);
                }
            }
                mInstancesLocked.put(userId, instance);
            }
            return instance;
        }
    }

    private AppSearchImpl createImpl(@NonNull Context context, @UserIdInt int userId)
            throws AppSearchException {