Merge "Add API to get max action duration."

This commit is contained in:
Kweku Adams
2021-08-05 21:58:55 +00:00
committed by Android (Google) Code Review
2 changed files with 40 additions and 1 deletions

View File

@@ -124,6 +124,11 @@ public interface EconomyManagerInternal {
*/
boolean canPayFor(int userId, @NonNull String pkgName, @NonNull ActionBill bill);
/**
* Returns the maximum duration (in milliseconds) that the specified app can afford the bill,
* based on current prices.
*/
long getMaxDurationMs(int userId, @NonNull String pkgName, @NonNull ActionBill bill);
/**
* Register an {@link AffordabilityChangeListener} to track when an app's ability to afford the

View File

@@ -104,7 +104,6 @@ public class InternalResourceService extends SystemService {
@GuardedBy("mLock")
private long mLastUnusedReclamationTime;
@SuppressWarnings("FieldCanBeLocal")
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Nullable
private String getPackageName(Intent intent) {
@@ -514,6 +513,15 @@ public class InternalResourceService extends SystemService {
}
private final class LocalService implements EconomyManagerInternal {
/**
* Use an extremely large value to indicate that an app can pay for a bill indefinitely.
* The value set here should be large/long enough that there's no reasonable expectation
* of a device operating uninterrupted (or in the exact same state) for that period of time.
* We intentionally don't use Long.MAX_VALUE to avoid potential overflow if a client
* doesn't check the value and just immediately adds it to the current time.
*/
private static final long FOREVER_MS = 27 * 365 * 24 * HOUR_IN_MILLIS;
@Override
public void registerAffordabilityChangeListener(int userId, @NonNull String pkgName,
@NonNull AffordabilityChangeListener listener, @NonNull ActionBill bill) {
@@ -551,6 +559,29 @@ public class InternalResourceService extends SystemService {
}
}
@Override
public long getMaxDurationMs(int userId, @NonNull String pkgName,
@NonNull ActionBill bill) {
if (!mIsEnabled) {
return FOREVER_MS;
}
long totalCostPerSecond = 0;
final List<EconomyManagerInternal.AnticipatedAction> projectedActions =
bill.getAnticipatedActions();
for (int i = 0; i < projectedActions.size(); ++i) {
AnticipatedAction action = projectedActions.get(i);
final long cost =
mCompleteEconomicPolicy.getCostOfAction(action.actionId, userId, pkgName);
totalCostPerSecond += cost;
}
if (totalCostPerSecond == 0) {
return FOREVER_MS;
}
synchronized (mLock) {
return mAgent.getBalanceLocked(userId, pkgName) * 1000 / totalCostPerSecond;
}
}
@Override
public void noteInstantaneousEvent(int userId, @NonNull String pkgName, int eventId,
@Nullable String tag) {
@@ -630,6 +661,9 @@ public class InternalResourceService extends SystemService {
private void dumpInternal(final IndentingPrintWriter pw) {
synchronized (mLock) {
pw.print("Is enabled: ");
pw.println(mIsEnabled);
pw.print("Current battery level: ");
pw.println(mCurrentBatteryLevel);