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

Commit 4729a3fb authored by Yi Jiang's avatar Yi Jiang
Browse files

Handles user ids of shell commands.

The rotation resolver's shell commands should be able to specify user id
instead of always using the default user id.

Test: manually with adb shell
Bug: 209688969
Change-Id: I93067eee529695066dd6cf98a0e6c122e17a4fe6
parent 4795c7ff
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.LatencyTracker;
import com.android.server.infra.AbstractPerUserSystemService;

import java.io.PrintWriter;

/**
 * Manages the Rotation Resolver Service on a per-user basis.
 */
@@ -216,6 +218,13 @@ final class RotationResolverManagerPerUserService extends
        mCurrentRequest = null;
    }

    @Override
    @GuardedBy("mLock")
    protected void dumpLocked(@NonNull String prefix, @NonNull PrintWriter pw) {
        super.dumpLocked(prefix, pw);
        dumpInternal(new IndentingPrintWriter(pw, "  "));
    }

    void dumpInternal(IndentingPrintWriter ipw) {
        synchronized (mLock) {
            if (mRemoteService != null) {
+28 −7
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import static com.android.internal.util.FrameworkStatsLog.AUTO_ROTATE_REPORTED__
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.content.ComponentName;
import android.content.Context;
import android.hardware.SensorPrivacyManager;
import android.os.Binder;
@@ -42,7 +43,6 @@ import android.rotationresolver.RotationResolverInternal;
import android.service.rotationresolver.RotationResolutionRequest;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;
import android.util.IndentingPrintWriter;
import android.util.Slog;
import android.view.Surface;

@@ -143,6 +143,30 @@ public class RotationResolverManagerService extends
        return !TextUtils.isEmpty(getServiceConfigPackage(context));
    }

    ComponentName getComponentNameShellCommand(@UserIdInt int userId) {
        synchronized (mLock) {
            final RotationResolverManagerPerUserService service = getServiceForUserLocked(userId);
            if (service != null) {
                return service.getComponentName();
            }
        }
        return null;
    }

    void resolveRotationShellCommand(@UserIdInt int userId,
            RotationResolverInternal.RotationResolverCallbackInternal callbackInternal,
            RotationResolutionRequest request) {
        synchronized (mLock) {
            final RotationResolverManagerPerUserService service = getServiceForUserLocked(userId);
            if (service != null) {
                service.resolveRotationLocked(callbackInternal, request, new CancellationSignal());
            } else {
                Slog.i(TAG, "service not available for user_id: " + userId);
            }
        }
    }


    static String getServiceConfigPackage(Context context) {
        return context.getPackageManager().getRotationResolverPackageName();
    }
@@ -201,9 +225,7 @@ public class RotationResolverManagerService extends
                return;
            }
            synchronized (mLock) {
                final RotationResolverManagerPerUserService service = getServiceForUserLocked(
                        UserHandle.getCallingUserId());
                service.dumpInternal(new IndentingPrintWriter(pw, "  "));
                dumpLocked("", pw);
            }
        }

@@ -214,9 +236,8 @@ public class RotationResolverManagerService extends
                ResultReceiver resultReceiver) {
            mContext.enforceCallingOrSelfPermission(Manifest.permission.MANAGE_ROTATION_RESOLVER,
                    TAG);
            final RotationResolverManagerPerUserService service = getServiceForUserLocked(
                    UserHandle.getCallingUserId());
            new RotationResolverShellCommand(service).exec(this, in, out, err, args, callback,
            new RotationResolverShellCommand(RotationResolverManagerService.this).exec(this, in,
                    out, err, args, callback,
                    resultReceiver);
        }
    }
+18 −16
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server.rotationresolver;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.os.CancellationSignal;
import android.os.ShellCommand;
import android.rotationresolver.RotationResolverInternal.RotationResolverCallbackInternal;
import android.service.rotationresolver.RotationResolutionRequest;
@@ -31,9 +30,9 @@ final class RotationResolverShellCommand extends ShellCommand {
    private static final int INITIAL_RESULT_CODE = -1;

    @NonNull
    private final RotationResolverManagerPerUserService mService;
    private final RotationResolverManagerService mService;

    RotationResolverShellCommand(@NonNull RotationResolverManagerPerUserService service) {
    RotationResolverShellCommand(@NonNull RotationResolverManagerService service) {
        mService = service;
    }

@@ -77,7 +76,7 @@ final class RotationResolverShellCommand extends ShellCommand {
            case "get-bound-package":
                return getBoundPackageName();
            case "set-temporary-service":
                return setTemporaryService(getNextArgRequired());
                return setTemporaryService();
            default:
                return handleDefaultCommands(cmd);
        }
@@ -85,31 +84,33 @@ final class RotationResolverShellCommand extends ShellCommand {

    private int getBoundPackageName() {
        final PrintWriter out = getOutPrintWriter();
        final ComponentName componentName = mService.getComponentName();
        final int userId = Integer.parseInt(getNextArgRequired());
        final ComponentName componentName = mService.getComponentNameShellCommand(userId);
        out.println(componentName == null ? "" : componentName.getPackageName());
        return 0;
    }

    private int setTemporaryService(String serviceName) {
    private int setTemporaryService() {
        final PrintWriter out = getOutPrintWriter();
        final int userId = Integer.parseInt(getNextArgRequired());
        final String serviceName = getNextArg();
        if (serviceName == null) {
            mService.getMaster().resetTemporaryService(mService.getUserId());
            mService.resetTemporaryService(userId);
            out.println("RotationResolverService temporary reset. ");
            return 0;
        }

        final int duration = Integer.parseInt(getNextArgRequired());
        mService.getMaster().setTemporaryService(mService.getUserId(), serviceName, duration);
        mService.setTemporaryService(userId, serviceName, duration);
        out.println("RotationResolverService temporarily set to " + serviceName
                + " for " + duration + "ms");
        return 0;
    }

    private int runResolveRotation() {
        final int userId = Integer.parseInt(getNextArgRequired());
        final RotationResolutionRequest request = new RotationResolutionRequest("",
                Surface.ROTATION_0, Surface.ROTATION_0, true, 2000L);
        mService.resolveRotationLocked(sTestableRotationCallbackInternal, request,
                new CancellationSignal());
        mService.resolveRotationShellCommand(userId, sTestableRotationCallbackInternal, request);
        return 0;
    }

@@ -126,11 +127,12 @@ final class RotationResolverShellCommand extends ShellCommand {
        pw.println("  help");
        pw.println("    Print this help text.");
        pw.println();
        pw.println("  resolve-rotation: request a rotation resolution.");
        pw.println("  resolve-rotation USER_ID: request a rotation resolution.");
        pw.println("  get-last-resolution: show the last rotation resolution result.");
        pw.println("  get-bound-package: print the bound package that implements the service.");
        pw.println("  set-temporary-service [COMPONENT_NAME DURATION]");
        pw.println("  get-bound-package USER_ID:");
        pw.println("    Print the bound package that implements the service.");
        pw.println("  set-temporary-service USER_ID [COMPONENT_NAME DURATION]");
        pw.println("    Temporarily (for DURATION ms) changes the service implementation.");
        pw.println("    To reset, call with no argument.");
        pw.println("    To reset, call with just the USER_ID argument.");
    }
}