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

Commit 3a4cb2dd authored by Shashwat Razdan's avatar Shashwat Razdan
Browse files

Adding multiple provider support in AbstractMasterSystemService

Bug: 216509090
Test: CTS tests, tested on physical device
Change-Id: I1da7f54bc8aaba15980aa90babaddc0b699c64e7
parent 0ef57a21
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -4130,9 +4130,9 @@
          This service must be trusted, as it can be activated without explicit consent of the user.
          If no service with the specified name exists on the device, cloudsearch will be disabled.
          Example: "com.android.intelligence/.CloudSearchService"
          config_defaultCloudSearchService is for the single provider case.
          config_defaultCloudSearchServices is for the multiple provider case.
    -->
    <string name="config_defaultCloudSearchService" translatable="false"></string>
    <string-array name="config_defaultCloudSearchServices"></string-array>

    <!-- The package name for the system's translation service.
     This service must be trusted, as it can be activated without explicit consent of the user.
+1 −1
Original line number Diff line number Diff line
@@ -3674,7 +3674,7 @@
  <java-symbol type="string" name="notification_channel_network_status" />
  <java-symbol type="string" name="notification_channel_network_alerts" />
  <java-symbol type="string" name="notification_channel_network_available" />
  <java-symbol type="string" name="config_defaultCloudSearchService" />
  <java-symbol type="array" name="config_defaultCloudSearchServices" />
  <java-symbol type="string" name="notification_channel_vpn" />
  <java-symbol type="string" name="notification_channel_device_admin" />
  <java-symbol type="string" name="notification_channel_alerts" />
+44 −13
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ import com.android.server.infra.FrameworkResourcesServiceNameResolver;
import com.android.server.wm.ActivityTaskManagerInternal;

import java.io.FileDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
@@ -62,7 +64,7 @@ public class CloudSearchManagerService extends

    public CloudSearchManagerService(Context context) {
        super(context, new FrameworkResourcesServiceNameResolver(context,
                        R.string.config_defaultCloudSearchService), null,
                        R.array.config_defaultCloudSearchServices, true), null,
                PACKAGE_UPDATE_POLICY_NO_REFRESH | PACKAGE_RESTART_POLICY_NO_REFRESH);
        mActivityTaskManagerInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
        mContext = context;
@@ -70,7 +72,25 @@ public class CloudSearchManagerService extends

    @Override
    protected CloudSearchPerUserService newServiceLocked(int resolvedUserId, boolean disabled) {
        return new CloudSearchPerUserService(this, mLock, resolvedUserId);
        return new CloudSearchPerUserService(this, mLock, resolvedUserId, "");
    }

    @Override
    protected List<CloudSearchPerUserService> newServiceListLocked(int resolvedUserId,
            boolean disabled, String[] serviceNames) {
        if (serviceNames == null) {
            return new ArrayList<>();
        }
        List<CloudSearchPerUserService> serviceList =
                new ArrayList<>(serviceNames.length);
        for (int i = 0; i < serviceNames.length; i++) {
            if (serviceNames[i] == null) {
                continue;
            }
            serviceList.add(new CloudSearchPerUserService(this, mLock, resolvedUserId,
                    serviceNames[i]));
        }
        return serviceList;
    }

    @Override
@@ -111,19 +131,28 @@ public class CloudSearchManagerService extends
                @NonNull ICloudSearchManagerCallback callBack) {
            searchRequest.setSource(
                    mContext.getPackageManager().getNameForUid(Binder.getCallingUid()));
            runForUserLocked("search", searchRequest.getRequestId(), (service) ->
                    service.onSearchLocked(searchRequest, callBack));
            runForUser("search", (service) -> {
                synchronized (service.mLock) {
                    service.onSearchLocked(searchRequest, callBack);
                }
            });
        }

        @Override
        public void returnResults(IBinder token, String requestId, SearchResponse response) {
            runForUserLocked("returnResults", requestId, (service) ->
                    service.onReturnResultsLocked(token, requestId, response));
            runForUser("returnResults", (service) -> {
                synchronized (service.mLock) {
                    service.onReturnResultsLocked(token, requestId, response);
                }
            });
        }

        public void destroy(@NonNull SearchRequest searchRequest) {
            runForUserLocked("destroyCloudSearchSession", searchRequest.getRequestId(),
                    (service) -> service.onDestroyLocked(searchRequest.getRequestId()));
            runForUser("destroyCloudSearchSession", (service) -> {
                synchronized (service.mLock) {
                    service.onDestroyLocked(searchRequest.getRequestId());
                }
            });
        }

        public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
@@ -134,8 +163,7 @@ public class CloudSearchManagerService extends
                    .exec(this, in, out, err, args, callback, resultReceiver);
        }

        private void runForUserLocked(@NonNull final String func,
                @NonNull final String  requestId,
        private void runForUser(@NonNull final String func,
                @NonNull final Consumer<CloudSearchPerUserService> c) {
            ActivityManagerInternal am = LocalServices.getService(ActivityManagerInternal.class);
            final int userId = am.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
@@ -143,7 +171,7 @@ public class CloudSearchManagerService extends
                    null, null);

            if (DEBUG) {
                Slog.d(TAG, "runForUserLocked:" + func + " from pid=" + Binder.getCallingPid()
                Slog.d(TAG, "runForUser:" + func + " from pid=" + Binder.getCallingPid()
                        + ", uid=" + Binder.getCallingUid());
            }
            Context ctx = getContext();
@@ -160,8 +188,11 @@ public class CloudSearchManagerService extends
            final long origId = Binder.clearCallingIdentity();
            try {
                synchronized (mLock) {
                    final CloudSearchPerUserService service = getServiceForUserLocked(userId);
                    c.accept(service);
                    final List<CloudSearchPerUserService> services =
                            getServiceListForUserLocked(userId);
                    for (int i = 0; i < services.size(); i++) {
                        c.accept(services.get(i));
                    }
                }
            } finally {
                Binder.restoreCallingIdentity(origId);
+6 −1
Original line number Diff line number Diff line
@@ -54,7 +54,12 @@ public class CloudSearchManagerServiceShellCommand extends ShellCommand {
                            return 0;
                        }
                        final int duration = Integer.parseInt(getNextArgRequired());
                        mService.setTemporaryService(userId, serviceName, duration);
                        String[] services = serviceName.split(";");
                        if (services.length == 0) {
                            return 0;
                        } else {
                            mService.setTemporaryServices(userId, services, duration);
                        }
                        pw.println("CloudSearchService temporarily set to " + serviceName
                                + " for " + duration + "ms");
                        break;
+20 −4
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ public class CloudSearchPerUserService extends
    @GuardedBy("mLock")
    private final CircularQueue<String, CloudSearchCallbackInfo> mCallbackQueue =
            new CircularQueue<>(QUEUE_SIZE);
    private final String mServiceName;
    private final ComponentName mRemoteComponentName;
    @Nullable
    @GuardedBy("mLock")
    private RemoteCloudSearchService mRemoteService;
@@ -60,8 +62,10 @@ public class CloudSearchPerUserService extends
    private boolean mZombie;

    protected CloudSearchPerUserService(CloudSearchManagerService master,
            Object lock, int userId) {
            Object lock, int userId, String serviceName) {
        super(master, lock, userId);
        mServiceName = serviceName;
        mRemoteComponentName = ComponentName.unflattenFromString(mServiceName);
    }

    @Override // from PerUserSystemService
@@ -108,7 +112,7 @@ public class CloudSearchPerUserService extends
                ? searchRequest.getSearchConstraints().getString(
                SearchRequest.CONSTRAINT_SEARCH_PROVIDER_FILTER) : "";

        String remoteServicePackageName = getServiceComponentName().getPackageName();
        String remoteServicePackageName = mRemoteComponentName.getPackageName();
        // By default, all providers are marked as wanted.
        boolean wantedProvider = true;
        if (filterList.length() > 0) {
@@ -150,11 +154,19 @@ public class CloudSearchPerUserService extends
    /**
     * Used to return results back to the clients.
     */
    @GuardedBy("mLock")
    public void onReturnResultsLocked(@NonNull IBinder token,
            @NonNull String requestId,
            @NonNull SearchResponse response) {
        if (mRemoteService == null) {
            return;
        }
        ICloudSearchService serviceInterface = mRemoteService.getServiceInterface();
        if (serviceInterface == null || token != serviceInterface.asBinder()) {
            return;
        }
        if (mCallbackQueue.containsKey(requestId)) {
            response.setSource(mRemoteService.getComponentName().getPackageName());
            response.setSource(mServiceName);
            final CloudSearchCallbackInfo sessionInfo = mCallbackQueue.getElement(requestId);
            try {
                if (response.getStatusCode() == SearchResponse.SEARCH_STATUS_OK) {
@@ -163,6 +175,10 @@ public class CloudSearchPerUserService extends
                    sessionInfo.mCallback.onSearchFailed(response);
                }
            } catch (RemoteException e) {
                if (mMaster.debug) {
                    Slog.e(TAG, "Exception in posting results");
                    e.printStackTrace();
                }
                onDestroyLocked(requestId);
            }
        }
@@ -297,7 +313,7 @@ public class CloudSearchPerUserService extends
    @Nullable
    private RemoteCloudSearchService getRemoteServiceLocked() {
        if (mRemoteService == null) {
            final String serviceName = getComponentNameLocked();
            final String serviceName = getComponentNameForMultipleLocked(mServiceName);
            if (serviceName == null) {
                if (mMaster.verbose) {
                    Slog.v(TAG, "getRemoteServiceLocked(): not set");
Loading