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

Commit 567df6a4 authored by Felipe Leme's avatar Felipe Leme
Browse files

Add shell cmd to disabled default Content Capture service.

Otherwise a bunch of CTS tests fail when the OEM sets a default service.

Test: atest CtsContentCaptureServiceTestCases
Fixes: 123952975

Change-Id: Ia172fbde6670fc5f125425f9dc92a34e8cdfdaed
parent a6a335de
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -77,6 +77,15 @@ public final class ContentCaptureManagerServiceShellCommand extends ShellCommand
            pw.println("    Temporarily (for DURATION ms) changes the service implemtation.");
            pw.println("    To reset, call with just the USER_ID argument.");
            pw.println("");
            pw.println("");
            pw.println("  set default-service-enabled USER_ID [true|false]");
            pw.println("    Enable / disable the default service for the user.");
            pw.println("");
            pw.println("");
            pw.println("  get default-service-enabled USER_ID");
            pw.println("    Checks whether the default service is enabled for the user.");
            pw.println("");
            pw.println("");
            pw.println("  list sessions [--user USER_ID]");
            pw.println("    Lists all pending sessions.");
            pw.println("");
@@ -91,6 +100,8 @@ public final class ContentCaptureManagerServiceShellCommand extends ShellCommand
        switch(what) {
            case "bind-instant-service-allowed":
                return getBindInstantService(pw);
            case "default-service-enabled":
                return getDefaultServiceEnabled(pw);
            default:
                pw.println("Invalid set: " + what);
                return -1;
@@ -105,6 +116,8 @@ public final class ContentCaptureManagerServiceShellCommand extends ShellCommand
                return setBindInstantService(pw);
            case "temporary-service":
                return setTemporaryService(pw);
            case "default-service-enabled":
                return setDefaultServiceEnabled();
            default:
                pw.println("Invalid set: " + what);
                return -1;
@@ -149,6 +162,20 @@ public final class ContentCaptureManagerServiceShellCommand extends ShellCommand
        return 0;
    }

    private int setDefaultServiceEnabled() {
        final int userId = getNextIntArgRequired();
        final boolean enabled = Boolean.parseBoolean(getNextArg());
        mService.setDefaultServiceEnabled(userId, enabled);
        return 0;
    }

    private int getDefaultServiceEnabled(PrintWriter pw) {
        final int userId = getNextIntArgRequired();
        final boolean enabled = mService.isDefaultServiceEnabled(userId);
        pw.println(enabled);
        return 0;
    }

    private int requestDestroy(PrintWriter pw) {
        if (!isNextArgSessions(pw)) {
            return -1;
+40 −0
Original line number Diff line number Diff line
@@ -285,6 +285,46 @@ public abstract class AbstractMasterSystemService<M extends AbstractMasterSystem
        }
    }

    /**
     * Sets whether the default service should be used.
     *
     * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
     * with the test results.
     *
     * @throws SecurityException if caller is not allowed to manage this service's settings.
     */
    public final void setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) {
        Slog.i(mTag, "setDefaultServiceEnabled() for userId " + userId + ": " + enabled);
        enforceCallingPermissionForManagement();

        synchronized (mLock) {
            final S oldService = peekServiceForUserLocked(userId);
            if (oldService != null) {
                oldService.removeSelfFromCacheLocked();
            }
            mServiceNameResolver.setDefaultServiceEnabled(userId, enabled);

            // Must update the service on cache so its initialization code is triggered
            updateCachedServiceLocked(userId);
        }
    }

    /**
     * Checks whether the default service should be used.
     *
     * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
     * with the test results.
     *
     * @throws SecurityException if caller is not allowed to manage this service's settings.
     */
    public final boolean isDefaultServiceEnabled(@UserIdInt int userId) {
        enforceCallingPermissionForManagement();

        synchronized (mLock) {
            return mServiceNameResolver.isDefaultServiceEnabled(userId);
        }
    }

    /**
     * Gets the maximum time the service implementation can be changed.
     *
+41 −4
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.TimeUtils;

import com.android.internal.annotations.GuardedBy;
@@ -60,6 +61,15 @@ public final class FrameworkResourcesServiceNameResolver implements ServiceNameR
    @GuardedBy("mLock")
    private final SparseArray<String> mTemporaryServiceNames = new SparseArray<>();

    /**
     * Map of default services that have been disabled by
     * {@link #setDefaultServiceEnabled(int, boolean)},keyed by {@code userId}.
     *
     * <p>Typically used by Shell command and/or CTS tests.
     */
    @GuardedBy("mLock")
    private final SparseBooleanArray mDefaultServicesDisabled = new SparseBooleanArray();

    /**
     * When the temporary service will expire (and reset back to the default).
     */
@@ -99,12 +109,18 @@ public final class FrameworkResourcesServiceNameResolver implements ServiceNameR
            final String temporaryName = mTemporaryServiceNames.get(userId);
            if (temporaryName != null) {
                // Always log it, as it should only be used on CTS or during development
                Slog.w(TAG, "getComponentName(): using temporary name " + temporaryName
                Slog.w(TAG, "getServiceName(): using temporary name " + temporaryName
                        + " for user " + userId);
                return temporaryName;
            } else {
                return getDefaultServiceName(userId);
            }
            final boolean disabled = mDefaultServicesDisabled.get(userId);
            if (disabled) {
                // Always log it, as it should only be used on CTS or during development
                Slog.w(TAG, "getServiceName(): temporary name not set and default disabled for "
                        + "user " + userId);
                return null;
            }
            return getDefaultServiceName(userId);
        }
    }

@@ -157,6 +173,24 @@ public final class FrameworkResourcesServiceNameResolver implements ServiceNameR
        }
    }

    @Override
    public void setDefaultServiceEnabled(int userId, boolean enabled) {
        synchronized (mLock) {
            if (enabled) {
                mDefaultServicesDisabled.removeAt(userId);
            } else {
                mDefaultServicesDisabled.put(userId, true);
            }
        }
    }

    @Override
    public boolean isDefaultServiceEnabled(int userId) {
        synchronized (mLock) {
            return mDefaultServicesDisabled.get(userId);
        }
    }

    @Override
    public String toString() {
        return "FrameworkResourcesServiceNamer[temps=" + mTemporaryServiceNames + "]";
@@ -168,6 +202,7 @@ public final class FrameworkResourcesServiceNameResolver implements ServiceNameR
        synchronized (mLock) {
            pw.print("FrameworkResourcesServiceNamer: resId="); pw.print(mResourceId);
            pw.print(", numberTemps="); pw.print(mTemporaryServiceNames.size());
            pw.print(", enabledDefaults="); pw.print(mDefaultServicesDisabled.size());
        }
    }

@@ -181,7 +216,9 @@ public final class FrameworkResourcesServiceNameResolver implements ServiceNameR
                final long ttl = mTemporaryServiceExpiration - SystemClock.elapsedRealtime();
                pw.print(" (expires in "); TimeUtils.formatDuration(ttl, pw); pw.print("), ");
            }
            pw.print("defaultName="); pw.println(getDefaultServiceName(userId));
            pw.print("defaultName="); pw.print(getDefaultServiceName(userId));
            final boolean disabled = mDefaultServicesDisabled.get(userId);
            pw.println(disabled ? " (disabled)" : " (enabled)");
        }
    }

+30 −0
Original line number Diff line number Diff line
@@ -107,6 +107,36 @@ public interface ServiceNameResolver {
        throw new UnsupportedOperationException("temporary user not supported");
    }

    /**
     * Sets whether the default service should be used when the temporary service is not set.
     *
     * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
     * with the test results.
     *
     * @param userId user handle
     * @param enabled whether the default service should be used when the temporary service is not
     * set
     *
     * @throws UnsupportedOperationException if not implemented.
     */
    default void setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) {
        throw new UnsupportedOperationException("changing default service not supported");
    }

    /**
     * Checks whether the default service should be used when the temporary service is not set.
     *
     * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
     * with the test results.
     *
     * @param userId user handle
     *
     * @throws UnsupportedOperationException if not implemented.
     */
    default boolean isDefaultServiceEnabled(@UserIdInt int userId) {
        throw new UnsupportedOperationException("checking default service not supported");
    }

    /**
     * Dumps the generic info in just one line (without calling {@code println}.
     */