Merge "Add support to update the throttle time of silent updates" into sc-dev am: b55a5da715

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14909659

Change-Id: If028f774a8e292f4bd7a4b2fddf2484de6e0174e
This commit is contained in:
Rhed Jao
2021-06-16 07:52:02 +00:00
committed by Automerger Merge Worker
4 changed files with 72 additions and 13 deletions

View File

@@ -63,4 +63,5 @@ interface IPackageInstaller {
void bypassNextStagedInstallerCheck(boolean value); void bypassNextStagedInstallerCheck(boolean value);
void setAllowUnlimitedSilentUpdates(String installerPackageName); void setAllowUnlimitedSilentUpdates(String installerPackageName);
void setSilentUpdatesThrottleTime(long throttleTimeInSeconds);
} }

View File

@@ -1117,6 +1117,17 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements
mSilentUpdatePolicy.setAllowUnlimitedSilentUpdates(installerPackageName); mSilentUpdatePolicy.setAllowUnlimitedSilentUpdates(installerPackageName);
} }
/**
* Set the silent updates throttle time in seconds.
*/
@Override
public void setSilentUpdatesThrottleTime(long throttleTimeInSeconds) {
if (!isCalledBySystemOrShell(Binder.getCallingUid())) {
throw new SecurityException("Caller not allowed to set silent updates throttle time");
}
mSilentUpdatePolicy.setSilentUpdatesThrottleTime(throttleTimeInSeconds);
}
private static int getSessionCount(SparseArray<PackageInstallerSession> sessions, private static int getSessionCount(SparseArray<PackageInstallerSession> sessions,
int installerUid) { int installerUid) {
int count = 0; int count = 0;

View File

@@ -307,8 +307,8 @@ class PackageManagerShellCommand extends ShellCommand {
return runLogVisibility(); return runLogVisibility();
case "bypass-staged-installer-check": case "bypass-staged-installer-check":
return runBypassStagedInstallerCheck(); return runBypassStagedInstallerCheck();
case "allow-unlimited-silent-updates": case "set-silent-updates-policy":
return runAllowUnlimitedSilentUpdates(); return runSetSilentUpdatesPolicy();
default: { default: {
Boolean domainVerificationResult = Boolean domainVerificationResult =
mDomainVerificationShell.runCommand(this, cmd); mDomainVerificationShell.runCommand(this, cmd);
@@ -3041,12 +3041,20 @@ class PackageManagerShellCommand extends ShellCommand {
} }
} }
private int runAllowUnlimitedSilentUpdates() { private int runSetSilentUpdatesPolicy() {
final PrintWriter pw = getOutPrintWriter(); final PrintWriter pw = getOutPrintWriter();
String opt; String opt;
String installerPackageName = null;
Long throttleTimeInSeconds = null;
boolean reset = false; boolean reset = false;
while ((opt = getNextOption()) != null) { while ((opt = getNextOption()) != null) {
switch (opt) { switch (opt) {
case "--allow-unlimited-silent-updates":
installerPackageName = getNextArgRequired();
break;
case "--throttle-time":
throttleTimeInSeconds = Long.parseLong(getNextArgRequired());
break;
case "--reset": case "--reset":
reset = true; reset = true;
break; break;
@@ -3055,10 +3063,24 @@ class PackageManagerShellCommand extends ShellCommand {
return -1; return -1;
} }
} }
if (throttleTimeInSeconds != null && throttleTimeInSeconds < 0) {
pw.println("Error: Invalid value for \"--throttle-time\":" + throttleTimeInSeconds);
return -1;
}
final String installerPackageName = reset ? null : getNextArgRequired();
try { try {
mInterface.getPackageInstaller().setAllowUnlimitedSilentUpdates(installerPackageName); final IPackageInstaller installer = mInterface.getPackageInstaller();
if (reset) {
installer.setAllowUnlimitedSilentUpdates(null /* installerPackageName */);
installer.setSilentUpdatesThrottleTime(-1 /* restore to the default */);
} else {
if (installerPackageName != null) {
installer.setAllowUnlimitedSilentUpdates(installerPackageName);
}
if (throttleTimeInSeconds != null) {
installer.setSilentUpdatesThrottleTime(throttleTimeInSeconds);
}
}
} catch (RemoteException e) { } catch (RemoteException e) {
pw.println("Failure [" pw.println("Failure ["
+ e.getClass().getName() + " - " + e.getClass().getName() + " - "
@@ -3889,11 +3911,14 @@ class PackageManagerShellCommand extends ShellCommand {
pw.println(" --enable: turn on debug logging (default)"); pw.println(" --enable: turn on debug logging (default)");
pw.println(" --disable: turn off debug logging"); pw.println(" --disable: turn off debug logging");
pw.println(""); pw.println("");
pw.println(" allow-unlimited-silent-updates (--reset | <INSTALLER>)"); pw.println(" set-silent-updates-policy [--allow-unlimited-silent-updates <INSTALLER>]");
pw.println(" Allows unlimited silent updated installation requests from the installer"); pw.println(" [--throttle-time <SECONDS>] [--reset]");
pw.println(" without the throttle time."); pw.println(" Sets the policies of the silent updates.");
pw.println(" --reset: clear the allowed installer and tracks of silent updates in"); pw.println(" --allow-unlimited-silent-updates: allows unlimited silent updated");
pw.println(" the system."); pw.println(" installation requests from the installer without the throttle time.");
pw.println(" --throttle-time: update the silent updates throttle time in seconds.");
pw.println(" --reset: restore the installer and throttle time to the default, and");
pw.println(" clear tracks of silent updates in the system.");
pw.println(""); pw.println("");
mDomainVerificationShell.printHelp(pw); mDomainVerificationShell.printHelp(pw);
pw.println(""); pw.println("");

View File

@@ -33,7 +33,8 @@ import java.util.concurrent.TimeUnit;
* in the {@link PackageInstallerSession}. * in the {@link PackageInstallerSession}.
*/ */
public class SilentUpdatePolicy { public class SilentUpdatePolicy {
// A throttle time to prevent the installer from silently updating the same app repeatedly. // The default throttle time to prevent the installer from silently updating the same app
// repeatedly.
private static final long SILENT_UPDATE_THROTTLE_TIME_MS = TimeUnit.SECONDS.toMillis(30); private static final long SILENT_UPDATE_THROTTLE_TIME_MS = TimeUnit.SECONDS.toMillis(30);
// Map to the uptime timestamp for each installer and app of the silent update. // Map to the uptime timestamp for each installer and app of the silent update.
@@ -44,6 +45,9 @@ public class SilentUpdatePolicy {
@GuardedBy("mSilentUpdateInfos") @GuardedBy("mSilentUpdateInfos")
private String mAllowUnlimitedSilentUpdatesInstaller; private String mAllowUnlimitedSilentUpdatesInstaller;
@GuardedBy("mSilentUpdateInfos")
private long mSilentUpdateThrottleTimeMs = SILENT_UPDATE_THROTTLE_TIME_MS;
/** /**
* Checks if the silent update is allowed by the given installer and app package name. * Checks if the silent update is allowed by the given installer and app package name.
* *
@@ -58,7 +62,11 @@ public class SilentUpdatePolicy {
return true; return true;
} }
final long lastSilentUpdatedMs = getTimestampMs(installerPackageName, packageName); final long lastSilentUpdatedMs = getTimestampMs(installerPackageName, packageName);
return SystemClock.uptimeMillis() - lastSilentUpdatedMs > SILENT_UPDATE_THROTTLE_TIME_MS; final long throttleTimeMs;
synchronized (mSilentUpdateInfos) {
throttleTimeMs = mSilentUpdateThrottleTimeMs;
}
return SystemClock.uptimeMillis() - lastSilentUpdatedMs > throttleTimeMs;
} }
/** /**
@@ -99,11 +107,25 @@ public class SilentUpdatePolicy {
} }
} }
/**
* Set the silent updates throttle time in seconds.
*
* @param throttleTimeInSeconds The throttle time to set, or <code>-1</code> to restore the
* value to the default.
*/
void setSilentUpdatesThrottleTime(long throttleTimeInSeconds) {
synchronized (mSilentUpdateInfos) {
mSilentUpdateThrottleTimeMs = throttleTimeInSeconds >= 0
? TimeUnit.SECONDS.toMillis(throttleTimeInSeconds)
: SILENT_UPDATE_THROTTLE_TIME_MS;
}
}
private void pruneLocked(long uptime) { private void pruneLocked(long uptime) {
final int size = mSilentUpdateInfos.size(); final int size = mSilentUpdateInfos.size();
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
final long lastSilentUpdatedMs = mSilentUpdateInfos.valueAt(i); final long lastSilentUpdatedMs = mSilentUpdateInfos.valueAt(i);
if (uptime - lastSilentUpdatedMs > SILENT_UPDATE_THROTTLE_TIME_MS) { if (uptime - lastSilentUpdatedMs > mSilentUpdateThrottleTimeMs) {
mSilentUpdateInfos.removeAt(i); mSilentUpdateInfos.removeAt(i);
} }
} }