[DK2]Add new SocketKeepalive.start to dynamically control keepalive
Add SocketKeepalive.start with parameter to enable dynamic
keepalive mode based on the existence of TCP connections.
This supports IPSec mode to notify KeepaliveTracker to disable
keepalive when keepalive is unnecessary to improve battery life.
Keepalive is controlled by periodically TCP socket status check
for both enable and disable. This is a transition commit and
is expected to be updated based on the socket creation or
destroy.
Bug: 259000745
Test: m ; atest FrameworksNetTests
Change-Id: Ie4d598d69a73c4931c7d0b6dfde0e459e5dca6b4
diff --git a/framework/src/android/net/NattSocketKeepalive.java b/framework/src/android/net/NattSocketKeepalive.java
index 56cc923..4d45e70 100644
--- a/framework/src/android/net/NattSocketKeepalive.java
+++ b/framework/src/android/net/NattSocketKeepalive.java
@@ -47,13 +47,39 @@
mResourceId = resourceId;
}
+ /**
+ * Request that keepalive be started with the given {@code intervalSec}.
+ *
+ * When a VPN is running with the network for this keepalive as its underlying network, the
+ * system can monitor the TCP connections on that VPN to determine whether this keepalive is
+ * necessary. To enable this behavior, pass {@link SocketKeepalive#FLAG_AUTOMATIC_ON_OFF} into
+ * the flags. When this is enabled, the system will disable sending keepalive packets when
+ * there are no TCP connections over the VPN(s) running over this network to save battery, and
+ * restart sending them as soon as any TCP connection is opened over one of the VPN networks.
+ * When no VPN is running on top of this network, this flag has no effect, i.e. the keepalives
+ * are always sent with the specified interval.
+ *
+ * Also {@see SocketKeepalive}.
+ *
+ * @param intervalSec The target interval in seconds between keepalive packet transmissions.
+ * The interval should be between 10 seconds and 3600 seconds. Otherwise,
+ * the supplied {@link Callback} will see a call to
+ * {@link Callback#onError(int)} with {@link #ERROR_INVALID_INTERVAL}.
+ * @param flags Flags to enable/disable available options on this keepalive.
+ * @hide
+ */
@Override
- protected void startImpl(int intervalSec) {
+ protected void startImpl(int intervalSec, int flags) {
+ if (0 != (flags & ~FLAG_AUTOMATIC_ON_OFF)) {
+ throw new IllegalArgumentException("Illegal flag value for "
+ + this.getClass().getSimpleName() + " : " + flags);
+ }
+ final boolean automaticOnOffKeepalives = 0 != (flags & FLAG_AUTOMATIC_ON_OFF);
mExecutor.execute(() -> {
try {
mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId,
- intervalSec, mCallback,
- mSource.getHostAddress(), mDestination.getHostAddress());
+ intervalSec, mCallback, mSource.getHostAddress(),
+ mDestination.getHostAddress(), automaticOnOffKeepalives);
} catch (RemoteException e) {
Log.e(TAG, "Error starting socket keepalive: ", e);
throw e.rethrowFromSystemServer();