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

This commit is contained in:
Yi Jiang
2021-05-10 23:30:48 +00:00
committed by Android (Google) Code Review
2 changed files with 41 additions and 82 deletions

View File

@@ -19,24 +19,20 @@ package com.android.server.rotationresolver;
import static android.service.rotationresolver.RotationResolverService.ROTATION_RESULT_FAILURE_CANCELLED; 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.RESOLUTION_UNAVAILABLE;
import static com.android.server.rotationresolver.RotationResolverManagerService.getServiceConfigPackage;
import static com.android.server.rotationresolver.RotationResolverManagerService.logRotationStats; import static com.android.server.rotationresolver.RotationResolverManagerService.logRotationStats;
import android.Manifest; import android.Manifest;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.UserIdInt; import android.annotation.UserIdInt;
import android.app.AppGlobals;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.os.RemoteException;
import android.rotationresolver.RotationResolverInternal; import android.rotationresolver.RotationResolverInternal;
import android.service.rotationresolver.RotationResolutionRequest; import android.service.rotationresolver.RotationResolutionRequest;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;
import android.util.IndentingPrintWriter; import android.util.IndentingPrintWriter;
import android.util.Slog; import android.util.Slog;
@@ -65,7 +61,6 @@ final class RotationResolverManagerPerUserService extends
@GuardedBy("mLock") @GuardedBy("mLock")
RemoteRotationResolverService mRemoteService; RemoteRotationResolverService mRemoteService;
private static String sTestingPackage;
private ComponentName mComponentName; private ComponentName mComponentName;
RotationResolverManagerPerUserService(@NonNull RotationResolverManagerService main, 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. * get the currently bound component name.
*/ */
@@ -158,63 +141,40 @@ final class RotationResolverManagerPerUserService extends
return mComponentName; 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. */ /** Resolves and sets up the rotation resolver service if it had not been done yet. */
@GuardedBy("mLock") @GuardedBy("mLock")
@VisibleForTesting @VisibleForTesting
boolean isServiceAvailableLocked() { boolean isServiceAvailableLocked() {
if (mComponentName == null) { if (mComponentName == null) {
mComponentName = resolveRotationResolverService(getContext()); mComponentName = updateServiceInfoLocked();
} }
return mComponentName != null; 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") @GuardedBy("mLock")
private void cancelLocked() { private void cancelLocked() {

View File

@@ -23,7 +23,6 @@ import android.os.CancellationSignal;
import android.os.ShellCommand; import android.os.ShellCommand;
import android.rotationresolver.RotationResolverInternal.RotationResolverCallbackInternal; import android.rotationresolver.RotationResolverInternal.RotationResolverCallbackInternal;
import android.service.rotationresolver.RotationResolutionRequest; import android.service.rotationresolver.RotationResolutionRequest;
import android.text.TextUtils;
import android.view.Surface; import android.view.Surface;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -75,12 +74,10 @@ final class RotationResolverShellCommand extends ShellCommand {
return runResolveRotation(); return runResolveRotation();
case "get-last-resolution": case "get-last-resolution":
return getLastResolution(); return getLastResolution();
case "set-testing-package":
return setTestRotationResolverPackage(getNextArgRequired());
case "get-bound-package": case "get-bound-package":
return getBoundPackageName(); return getBoundPackageName();
case "clear-testing-package": case "set-temporary-service":
return resetTestRotationResolverPackage(); return setTemporaryService(getNextArgRequired());
default: default:
return handleDefaultCommands(cmd); return handleDefaultCommands(cmd);
} }
@@ -93,17 +90,18 @@ final class RotationResolverShellCommand extends ShellCommand {
return 0; return 0;
} }
private int setTestRotationResolverPackage(String testingPackage) { private int setTemporaryService(String serviceName) {
if (!TextUtils.isEmpty((testingPackage))) { final PrintWriter out = getOutPrintWriter();
mService.setTestingPackage(testingPackage); if (serviceName == null) {
sTestableRotationCallbackInternal.reset(); mService.getMaster().resetTemporaryService(mService.getUserId());
out.println("RotationResolverService temporary reset. ");
return 0;
} }
return 0;
}
private int resetTestRotationResolverPackage() { final int duration = Integer.parseInt(getNextArgRequired());
mService.setTestingPackage(""); mService.getMaster().setTemporaryService(mService.getUserId(), serviceName, duration);
sTestableRotationCallbackInternal.reset(); out.println("RotationResolverService temporarily set to " + serviceName
+ " for " + duration + "ms");
return 0; return 0;
} }
@@ -130,8 +128,9 @@ final class RotationResolverShellCommand extends ShellCommand {
pw.println(); pw.println();
pw.println(" resolve-rotation: request a rotation resolution."); pw.println(" resolve-rotation: request a rotation resolution.");
pw.println(" get-last-resolution: show the last rotation resolution result."); 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(" 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.");
} }
} }