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

Commit 1e577bb2 authored by Yi Jiang's avatar Yi Jiang Committed by Android (Google) Code Review
Browse files

Merge "Simplifies the service resolving logic in RRMS" into sc-dev

parents 8a67239a cb800aa3
Loading
Loading
Loading
Loading
+26 −66
Original line number Diff line number Diff line
@@ -19,24 +19,20 @@ package com.android.server.rotationresolver;
import static android.service.rotationresolver.RotationResolverService.ROTATION_RESULT_FAILURE_CANCELLED;

import static com.android.server.rotationresolver.RotationResolverManagerService.RESOLUTION_UNAVAILABLE;
import static com.android.server.rotationresolver.RotationResolverManagerService.getServiceConfigPackage;
import static com.android.server.rotationresolver.RotationResolverManagerService.logRotationStats;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.AppGlobals;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.CancellationSignal;
import android.os.RemoteException;
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;

@@ -65,7 +61,6 @@ final class RotationResolverManagerPerUserService extends
    @GuardedBy("mLock")
    RemoteRotationResolverService mRemoteService;

    private static String sTestingPackage;
    private ComponentName mComponentName;

    RotationResolverManagerPerUserService(@NonNull RotationResolverManagerService main,
@@ -138,18 +133,6 @@ final class RotationResolverManagerPerUserService extends
        }
    }

    /**
     * Set the testing package name.
     *
     * @param packageName the name of the package that implements {@link RotationResolverService}
     *                    and is used for testing only.
     */
    @VisibleForTesting
    void setTestingPackage(String packageName) {
        sTestingPackage = packageName;
        mComponentName = resolveRotationResolverService(getContext());
    }

    /**
     * get the currently bound component name.
     */
@@ -158,63 +141,40 @@ final class RotationResolverManagerPerUserService extends
        return mComponentName;
    }

    /**
     * Provides rotation resolver service component name at runtime, making sure it's provided
     * by the system.
     */
    static ComponentName resolveRotationResolverService(Context context) {
        String resolvedPackage;
        int flags = PackageManager.MATCH_SYSTEM_ONLY;
        if (!TextUtils.isEmpty(sTestingPackage)) {
            // Testing Package is set.
            resolvedPackage = sTestingPackage;
            flags = PackageManager.GET_META_DATA;
        } else {
            final String serviceConfigPackage = getServiceConfigPackage(context);
            if (!TextUtils.isEmpty(serviceConfigPackage)) {
                resolvedPackage = serviceConfigPackage;
            } else {
                return null;
            }
        }

        final Intent intent = new Intent(
                RotationResolverService.SERVICE_INTERFACE).setPackage(resolvedPackage);

        final ResolveInfo resolveInfo = context.getPackageManager().resolveServiceAsUser(intent,
                flags, context.getUserId());
        if (resolveInfo == null || resolveInfo.serviceInfo == null) {
            Slog.wtf(TAG, String.format("Service %s not found in package %s",
                    RotationResolverService.SERVICE_INTERFACE, resolvedPackage));
            return null;
        }

        final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
        final String permission = serviceInfo.permission;
        if (Manifest.permission.BIND_ROTATION_RESOLVER_SERVICE.equals(permission)) {
            Slog.i(TAG, String.format("Successfully bound the service from package: %s",
                    resolvedPackage));
            return serviceInfo.getComponentName();
        }
        Slog.e(TAG, String.format(
                "Service %s should require %s permission. Found %s permission",
                serviceInfo.getComponentName(),
                Manifest.permission.BIND_ROTATION_RESOLVER_SERVICE,
                serviceInfo.permission));
        return null;
    }


    /** Resolves and sets up the rotation resolver service if it had not been done yet. */
    @GuardedBy("mLock")
    @VisibleForTesting
    boolean isServiceAvailableLocked() {
        if (mComponentName == null) {
            mComponentName = resolveRotationResolverService(getContext());
            mComponentName = updateServiceInfoLocked();
        }
        return mComponentName != null;
    }

    @Override // from PerUserSystemService
    protected ServiceInfo newServiceInfoLocked(@NonNull ComponentName serviceComponent)
            throws PackageManager.NameNotFoundException {
        ServiceInfo serviceInfo;
        try {
            serviceInfo = AppGlobals.getPackageManager().getServiceInfo(serviceComponent,
                    PackageManager.GET_META_DATA, mUserId);
            if (serviceInfo != null) {
                final String permission = serviceInfo.permission;
                if (!Manifest.permission.BIND_ROTATION_RESOLVER_SERVICE.equals(permission)) {
                    throw new SecurityException(String.format(
                            "Service %s requires %s permission. Found %s permission",
                            serviceInfo.getComponentName(),
                            Manifest.permission.BIND_ROTATION_RESOLVER_SERVICE,
                            serviceInfo.permission));
                }
            }
        } catch (RemoteException e) {
            throw new PackageManager.NameNotFoundException(
                    "Could not get service for " + serviceComponent);
        }
        return serviceInfo;
    }

    @GuardedBy("mLock")
    private void cancelLocked() {
+15 −16
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.os.CancellationSignal;
import android.os.ShellCommand;
import android.rotationresolver.RotationResolverInternal.RotationResolverCallbackInternal;
import android.service.rotationresolver.RotationResolutionRequest;
import android.text.TextUtils;
import android.view.Surface;

import java.io.PrintWriter;
@@ -75,12 +74,10 @@ final class RotationResolverShellCommand extends ShellCommand {
                return runResolveRotation();
            case "get-last-resolution":
                return getLastResolution();
            case "set-testing-package":
                return setTestRotationResolverPackage(getNextArgRequired());
            case "get-bound-package":
                return getBoundPackageName();
            case "clear-testing-package":
                return resetTestRotationResolverPackage();
            case "set-temporary-service":
                return setTemporaryService(getNextArgRequired());
            default:
                return handleDefaultCommands(cmd);
        }
@@ -93,17 +90,18 @@ final class RotationResolverShellCommand extends ShellCommand {
        return 0;
    }

    private int setTestRotationResolverPackage(String testingPackage) {
        if (!TextUtils.isEmpty((testingPackage))) {
            mService.setTestingPackage(testingPackage);
            sTestableRotationCallbackInternal.reset();
        }
    private int setTemporaryService(String serviceName) {
        final PrintWriter out = getOutPrintWriter();
        if (serviceName == null) {
            mService.getMaster().resetTemporaryService(mService.getUserId());
            out.println("RotationResolverService temporary reset. ");
            return 0;
        }

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

@@ -130,8 +128,9 @@ final class RotationResolverShellCommand extends ShellCommand {
        pw.println();
        pw.println("  resolve-rotation: request a rotation resolution.");
        pw.println("  get-last-resolution: show the last rotation resolution result.");
        pw.println("  set-testing-package: Set the testing package that implements the service.");
        pw.println("  get-bound-package: print the bound package that implements the service.");
        pw.println("  clear-testing-package: reset the testing package.");
        pw.println("  set-temporary-service [COMPONENT_NAME DURATION]");
        pw.println("    Temporarily (for DURATION ms) changes the service implementation.");
        pw.println("    To reset, call with no argument.");
    }
}