Merge "Check opportunistic quota on metered networks."

This commit is contained in:
TreeHugger Robot
2021-06-22 17:08:16 +00:00
committed by Android (Google) Code Review
2 changed files with 39 additions and 7 deletions

View File

@@ -633,23 +633,34 @@ public final class ConnectivityController extends RestrictingController implemen
}
}
private static boolean isRelaxedSatisfied(JobStatus jobStatus, Network network,
private boolean isRelaxedSatisfied(JobStatus jobStatus, Network network,
NetworkCapabilities capabilities, Constants constants) {
// Only consider doing this for unrestricted prefetching jobs
if (!jobStatus.getJob().isPrefetch() || jobStatus.getStandbyBucket() == RESTRICTED_INDEX) {
return false;
}
final long estDownloadBytes = jobStatus.getEstimatedNetworkDownloadBytes();
if (estDownloadBytes <= 0) {
// Need to at least know the estimated download bytes for a prefetch job.
return false;
}
// See if we match after relaxing any unmetered request
final NetworkCapabilities.Builder builder =
copyCapabilities(jobStatus.getJob().getRequiredNetwork());
builder.removeCapability(NET_CAPABILITY_NOT_METERED);
if (builder.build().satisfiedByNetworkCapabilities(capabilities)) {
// TODO: treat this as "maybe" response; need to check quotas
return jobStatus.getFractionRunTime() > constants.CONN_PREFETCH_RELAX_FRAC;
} else {
return false;
if (builder.build().satisfiedByNetworkCapabilities(capabilities)
&& jobStatus.getFractionRunTime() > constants.CONN_PREFETCH_RELAX_FRAC) {
final long opportunisticQuotaBytes =
mNetPolicyManagerInternal.getSubscriptionOpportunisticQuota(
network, NetworkPolicyManagerInternal.QUOTA_TYPE_JOBS);
final long estUploadBytes = jobStatus.getEstimatedNetworkUploadBytes();
final long estimatedBytes = estDownloadBytes
+ (estUploadBytes == JobInfo.NETWORK_BYTES_UNKNOWN ? 0 : estUploadBytes);
return opportunisticQuotaBytes >= estimatedBytes;
}
return false;
}
@VisibleForTesting

View File

@@ -299,8 +299,17 @@ public class ConnectivityControllerTest {
final JobStatus earlyPrefetch = createJobStatus(job, now - 1000, now + 2000);
final JobStatus latePrefetch = createJobStatus(job, now - 2000, now + 1000);
job.setEstimatedNetworkBytes(JobInfo.NETWORK_BYTES_UNKNOWN, DataUnit.MEBIBYTES.toBytes(1));
final JobStatus latePrefetchUnknownDown = createJobStatus(job, now - 2000, now + 1000);
job.setEstimatedNetworkBytes(DataUnit.MEBIBYTES.toBytes(1), JobInfo.NETWORK_BYTES_UNKNOWN);
final JobStatus latePrefetchUnknownUp = createJobStatus(job, now - 2000, now + 1000);
final ConnectivityController controller = new ConnectivityController(mService);
when(mNetPolicyManagerInternal.getSubscriptionOpportunisticQuota(
any(), eq(NetworkPolicyManagerInternal.QUOTA_TYPE_JOBS)))
.thenReturn(0L);
// Unmetered network is whenever
{
final Network net = mock(Network.class);
@@ -312,9 +321,11 @@ public class ConnectivityControllerTest {
assertTrue(controller.isSatisfied(late, net, caps, mConstants));
assertTrue(controller.isSatisfied(earlyPrefetch, net, caps, mConstants));
assertTrue(controller.isSatisfied(latePrefetch, net, caps, mConstants));
assertTrue(controller.isSatisfied(latePrefetchUnknownDown, net, caps, mConstants));
assertTrue(controller.isSatisfied(latePrefetchUnknownUp, net, caps, mConstants));
}
// Metered network is only when prefetching and late
// Metered network is only when prefetching, late, and in opportunistic quota
{
final Network net = mock(Network.class);
final NetworkCapabilities caps = createCapabilitiesBuilder()
@@ -323,7 +334,17 @@ public class ConnectivityControllerTest {
assertFalse(controller.isSatisfied(early, net, caps, mConstants));
assertFalse(controller.isSatisfied(late, net, caps, mConstants));
assertFalse(controller.isSatisfied(earlyPrefetch, net, caps, mConstants));
assertFalse(controller.isSatisfied(latePrefetch, net, caps, mConstants));
assertFalse(controller.isSatisfied(latePrefetchUnknownDown, net, caps, mConstants));
assertFalse(controller.isSatisfied(latePrefetchUnknownUp, net, caps, mConstants));
when(mNetPolicyManagerInternal.getSubscriptionOpportunisticQuota(
any(), eq(NetworkPolicyManagerInternal.QUOTA_TYPE_JOBS)))
.thenReturn(9876543210L);
assertTrue(controller.isSatisfied(latePrefetch, net, caps, mConstants));
// Only relax restrictions when we at least know the estimated download bytes.
assertFalse(controller.isSatisfied(latePrefetchUnknownDown, net, caps, mConstants));
assertTrue(controller.isSatisfied(latePrefetchUnknownUp, net, caps, mConstants));
}
}