Add setting that controls network rate limit

The INGRESS_RATE_LIMIT_BYTES_PER_SECOND setting controls the rate limit
for internet networks. If set to -1, no rate limit applies.  There is
one global rate limit that will be applied to all networks with
NET_CAPABILITY_INTERNET.

Test: atest ConnectivitySettingsManagerTest
Bug: 157552970
Change-Id: Ia82aa867686d484ce46734f76d4a48bf864eff84
This commit is contained in:
Patrick Rohr
2022-01-03 15:55:52 +01:00
parent e7036bec68
commit a20843638f
3 changed files with 60 additions and 0 deletions

View File

@@ -383,6 +383,14 @@ public class ConnectivitySettingsManager {
public static final String UIDS_ALLOWED_ON_RESTRICTED_NETWORKS =
"uids_allowed_on_restricted_networks";
/**
* A global rate limit that applies to all networks with NET_CAPABILITY_INTERNET when enabled.
*
* @hide
*/
public static final String INGRESS_RATE_LIMIT_BYTES_PER_SECOND =
"ingress_rate_limit_bytes_per_second";
/**
* Get mobile data activity timeout from {@link Settings}.
*
@@ -1071,4 +1079,37 @@ public class ConnectivitySettingsManager {
Settings.Global.putString(context.getContentResolver(), UIDS_ALLOWED_ON_RESTRICTED_NETWORKS,
uids);
}
/**
* Get the global network bandwidth rate limit.
*
* The limit is only applicable to networks that provide internet connectivity. If the setting
* is unset, it defaults to -1.
*
* @param context The {@link Context} to query the setting.
* @return The rate limit in number of bytes per second or -1 if disabled.
*/
public static long getIngressRateLimitInBytesPerSecond(@NonNull Context context) {
return Settings.Global.getLong(context.getContentResolver(),
INGRESS_RATE_LIMIT_BYTES_PER_SECOND, -1);
}
/**
* Set the global network bandwidth rate limit.
*
* The limit is only applicable to networks that provide internet connectivity.
*
* @param context The {@link Context} to set the setting.
* @param rateLimitInBytesPerSec The rate limit in number of bytes per second or -1 to disable.
*/
public static void setIngressRateLimitInBytesPerSecond(@NonNull Context context,
@IntRange(from = -1, to = Integer.MAX_VALUE) long rateLimitInBytesPerSec) {
if (rateLimitInBytesPerSec < -1) {
throw new IllegalArgumentException(
"Rate limit must be within the range [-1, Integer.MAX_VALUE]");
}
Settings.Global.putLong(context.getContentResolver(),
INGRESS_RATE_LIMIT_BYTES_PER_SECOND,
rateLimitInBytesPerSec);
}
}