Allow network providers to set the linger duration. am: 550b5214d3

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1727821

Change-Id: I2a9872dc3da0e89574ae5bab28dfd3bb4c3f3abf
This commit is contained in:
Chalard Jean
2021-06-08 23:10:08 +00:00
committed by Automerger Merge Worker
7 changed files with 103 additions and 11 deletions

View File

@@ -239,6 +239,7 @@ package android.net {
method public final void sendQosSessionLost(int, int, int);
method public final void sendSocketKeepaliveEvent(int, int);
method @Deprecated public void setLegacySubtype(int, @NonNull String);
method public void setLingerDuration(@NonNull java.time.Duration);
method public void setTeardownDelayMillis(@IntRange(from=0, to=0x1388) int);
method public final void setUnderlyingNetworks(@Nullable java.util.List<android.net.Network>);
method public void unregister();

View File

@@ -42,4 +42,5 @@ oneway interface INetworkAgentRegistry {
void sendQosSessionLost(int qosCallbackId, in QosSession session);
void sendQosCallbackError(int qosCallbackId, int exceptionType);
void sendTeardownDelayMs(int teardownDelayMs);
void sendLingerDuration(int durationMs);
}

View File

@@ -22,6 +22,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.os.Build;
@@ -106,6 +107,9 @@ public abstract class NetworkAgent {
private final String LOG_TAG;
private static final boolean DBG = true;
private static final boolean VDBG = false;
/** @hide */
@TestApi
public static final int MIN_LINGER_TIMER_MS = 2000;
private final ArrayList<RegistryAction> mPreConnectedQueue = new ArrayList<>();
private volatile long mLastBwRefreshTime = 0;
private static final long BW_REFRESH_MIN_WIN_MS = 500;
@@ -391,6 +395,15 @@ public abstract class NetworkAgent {
*/
public static final int CMD_NETWORK_DESTROYED = BASE + 23;
/**
* Sent by the NetworkAgent to ConnectivityService to set the linger duration for this network
* agent.
* arg1 = the linger duration, represents by {@link Duration}.
*
* @hide
*/
public static final int EVENT_LINGER_DURATION_CHANGED = BASE + 24;
private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) {
final NetworkInfo ni = new NetworkInfo(config.legacyType, config.legacySubType,
config.legacyTypeName, config.legacySubTypeName);
@@ -1287,6 +1300,22 @@ public abstract class NetworkAgent {
queueOrSendMessage(ra -> ra.sendQosCallbackError(qosCallbackId, exceptionType));
}
/**
* Set the linger duration for this network agent.
* @param duration the delay between the moment the network becomes unneeded and the
* moment the network is disconnected or moved into the background.
* Note that If this duration has greater than millisecond precision, then
* the internal implementation will drop any excess precision.
*/
public void setLingerDuration(@NonNull final Duration duration) {
Objects.requireNonNull(duration);
final long durationMs = duration.toMillis();
if (durationMs < MIN_LINGER_TIMER_MS || durationMs > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Duration must be within ["
+ MIN_LINGER_TIMER_MS + "," + Integer.MAX_VALUE + "]ms");
}
queueOrSendMessage(ra -> ra.sendLingerDuration((int) durationMs));
}
/** @hide */
protected void log(final String s) {