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

Commit ea77f04a 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 9f8dd75e
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -4150,9 +4150,9 @@
          This service must be trusted, as it can be activated without explicit consent of the user.
          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.
          If no service with the specified name exists on the device, cloudsearch will be disabled.
          Example: "com.android.intelligence/.CloudSearchService"
          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.
    <!-- 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.
     This service must be trusted, as it can be activated without explicit consent of the user.
+1 −1
Original line number Original line Diff line number Diff line
@@ -3675,7 +3675,7 @@
  <java-symbol type="string" name="notification_channel_network_status" />
  <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_alerts" />
  <java-symbol type="string" name="notification_channel_network_available" />
  <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_vpn" />
  <java-symbol type="string" name="notification_channel_device_admin" />
  <java-symbol type="string" name="notification_channel_device_admin" />
  <java-symbol type="string" name="notification_channel_alerts" />
  <java-symbol type="string" name="notification_channel_alerts" />
+44 −13
Original line number Original line Diff line number Diff line
@@ -43,6 +43,8 @@ import com.android.server.infra.FrameworkResourcesServiceNameResolver;
import com.android.server.wm.ActivityTaskManagerInternal;
import com.android.server.wm.ActivityTaskManagerInternal;


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


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


    public CloudSearchManagerService(Context context) {
    public CloudSearchManagerService(Context context) {
        super(context, new FrameworkResourcesServiceNameResolver(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);
                PACKAGE_UPDATE_POLICY_NO_REFRESH | PACKAGE_RESTART_POLICY_NO_REFRESH);
        mActivityTaskManagerInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
        mActivityTaskManagerInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
        mContext = context;
        mContext = context;
@@ -70,7 +72,25 @@ public class CloudSearchManagerService extends


    @Override
    @Override
    protected CloudSearchPerUserService newServiceLocked(int resolvedUserId, boolean disabled) {
    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
    @Override
@@ -111,19 +131,28 @@ public class CloudSearchManagerService extends
                @NonNull ICloudSearchManagerCallback callBack) {
                @NonNull ICloudSearchManagerCallback callBack) {
            searchRequest.setSource(
            searchRequest.setSource(
                    mContext.getPackageManager().getNameForUid(Binder.getCallingUid()));
                    mContext.getPackageManager().getNameForUid(Binder.getCallingUid()));
            runForUserLocked("search", searchRequest.getRequestId(), (service) ->
            runForUser("search", (service) -> {
                    service.onSearchLocked(searchRequest, callBack));
                synchronized (service.mLock) {
                    service.onSearchLocked(searchRequest, callBack);
                }
            });
        }
        }


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


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


        public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
        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);
                    .exec(this, in, out, err, args, callback, resultReceiver);
        }
        }


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


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


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


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


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