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

Commit f192dbf7 authored by Terry Wang's avatar Terry Wang Committed by Android (Google) Code Review
Browse files

Merge "Handle user stopping in AppSearch." into sc-dev

parents 83f7129f 9a950ce7
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -234,6 +234,18 @@ public class AppSearchManagerService extends SystemService {
        }
    }

    @Override
    public void onUserStopping(@NonNull TargetUser user) {
        synchronized (mUnlockedUserIdsLocked) {
            mUnlockedUserIdsLocked.remove(user.getUserIdentifier());
            try {
                mImplInstanceManager.closeAndRemoveAppSearchImplForUser(user.getUserIdentifier());
            } catch (Throwable t) {
                Log.e(TAG, "Error handling user stopping.", t);
            }
        }
    }

    private void verifyUserUnlocked(int callingUserId) {
        if (isUserLocked(callingUserId)) {
            throw new IllegalStateException("User " + callingUserId + " is locked or not running.");
+18 −0
Original line number Diff line number Diff line
@@ -117,10 +117,28 @@ public final class ImplInstanceManager {
     */
    public void removeAppSearchImplForUser(@UserIdInt int userId) {
        synchronized (mInstancesLocked) {
            // no need to close and persist data to disk since we are removing them now.
            mInstancesLocked.remove(userId);
        }
    }

    /**
     * Close and remove an instance of {@link AppSearchImpl} for the given user.
     *
     * <p>All mutation apply to this {@link AppSearchImpl} will be persisted to disk.
     *
     * @param userId The multi-user userId of the user that need to be removed.
     */
    public void closeAndRemoveAppSearchImplForUser(@UserIdInt int userId) {
        synchronized (mInstancesLocked) {
            AppSearchImpl appSearchImpl = mInstancesLocked.get(userId);
            if (appSearchImpl != null) {
                appSearchImpl.close();
                mInstancesLocked.remove(userId);
            }
        }
    }

    /**
     * Gets an instance of AppSearchImpl for the given user.
     *
+16 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.appsearch.testing;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.appsearch.AppSearchBatchResult;
import android.app.appsearch.AppSearchManager;
import android.app.appsearch.AppSearchResult;
@@ -37,6 +38,7 @@ import android.app.appsearch.SetSchemaResponse;
import android.app.appsearch.StorageInfo;
import android.app.appsearch.exceptions.AppSearchException;
import android.content.Context;
import android.os.UserHandle;

import androidx.test.core.app.ApplicationProvider;

@@ -58,18 +60,29 @@ public class AppSearchSessionShimImpl implements AppSearchSessionShim {
    private final AppSearchSession mAppSearchSession;
    private final ExecutorService mExecutor;

    /** Creates the SearchSessionShim with given SearchContext. */
    @NonNull
    public static ListenableFuture<AppSearchSessionShim> createSearchSession(
            @NonNull AppSearchManager.SearchContext searchContext) {
        return createSearchSession(searchContext, Executors.newCachedThreadPool());
        Context context = ApplicationProvider.getApplicationContext();
        return createSearchSession(context, searchContext, Executors.newCachedThreadPool());
    }

    /** Creates the SearchSessionShim with given SearchContext for the given user. */
    @NonNull
    public static ListenableFuture<AppSearchSessionShim> createSearchSession(
            @NonNull AppSearchManager.SearchContext searchContext, @UserIdInt int userId) {
        Context context = ApplicationProvider.getApplicationContext()
                .createContextAsUser(new UserHandle(userId), /*flags=*/ 0);
        return createSearchSession(context, searchContext, Executors.newCachedThreadPool());
    }

    /**  Creates the SearchSession with given ExecutorService. */
    /**  Creates the SearchSession with given Context and ExecutorService. */
    @NonNull
    public static ListenableFuture<AppSearchSessionShim> createSearchSession(
            @NonNull Context context,
            @NonNull AppSearchManager.SearchContext searchContext,
            @NonNull ExecutorService executor) {
        Context context = ApplicationProvider.getApplicationContext();
        AppSearchManager appSearchManager = context.getSystemService(AppSearchManager.class);
        SettableFuture<AppSearchResult<AppSearchSession>> future = SettableFuture.create();
        appSearchManager.createSearchSession(searchContext, executor, future::set);