Merge "Fix SocketKeepalive APIs which do not meet API review requirement"
am: 8324c3e7e5
Change-Id: Id82be12a54e676c4ad18900ae910e6d08079e62c
This commit is contained in:
@@ -44,6 +44,7 @@ import android.os.INetworkManagementService;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.Messenger;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.Process;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ResultReceiver;
|
||||
@@ -64,6 +65,8 @@ import com.android.internal.util.Protocol;
|
||||
import libcore.net.event.NetworkEventDispatcher;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.net.InetAddress;
|
||||
@@ -1923,14 +1926,22 @@ public class ConnectivityManager {
|
||||
* @return A {@link SocketKeepalive} object that can be used to control the keepalive on the
|
||||
* given socket.
|
||||
**/
|
||||
public SocketKeepalive createSocketKeepalive(@NonNull Network network,
|
||||
public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
|
||||
@NonNull UdpEncapsulationSocket socket,
|
||||
@NonNull InetAddress source,
|
||||
@NonNull InetAddress destination,
|
||||
@NonNull @CallbackExecutor Executor executor,
|
||||
@NonNull Callback callback) {
|
||||
return new NattSocketKeepalive(mService, network, socket.getFileDescriptor(),
|
||||
socket.getResourceId(), source, destination, executor, callback);
|
||||
ParcelFileDescriptor dup;
|
||||
try {
|
||||
dup = ParcelFileDescriptor.dup(socket.getFileDescriptor());
|
||||
} catch (IOException ignored) {
|
||||
// Construct an invalid fd, so that if the user later calls start(), it will fail with
|
||||
// ERROR_INVALID_SOCKET.
|
||||
dup = new ParcelFileDescriptor(new FileDescriptor());
|
||||
}
|
||||
return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source,
|
||||
destination, executor, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1938,9 +1949,9 @@ public class ConnectivityManager {
|
||||
* by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
|
||||
*
|
||||
* @param network The {@link Network} the socket is on.
|
||||
* @param fd The {@link FileDescriptor} that needs to be kept alive. The provided
|
||||
* {@link FileDescriptor} must be bound to a port and the keepalives will be sent from
|
||||
* that port.
|
||||
* @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided
|
||||
* {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent
|
||||
* from that port.
|
||||
* @param source The source address of the {@link UdpEncapsulationSocket}.
|
||||
* @param destination The destination address of the {@link UdpEncapsulationSocket}. The
|
||||
* keepalive packets will always be sent to port 4500 of the given {@code destination}.
|
||||
@@ -1956,14 +1967,22 @@ public class ConnectivityManager {
|
||||
*/
|
||||
@SystemApi
|
||||
@RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
|
||||
public SocketKeepalive createNattKeepalive(@NonNull Network network,
|
||||
@NonNull FileDescriptor fd,
|
||||
public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network,
|
||||
@NonNull ParcelFileDescriptor pfd,
|
||||
@NonNull InetAddress source,
|
||||
@NonNull InetAddress destination,
|
||||
@NonNull @CallbackExecutor Executor executor,
|
||||
@NonNull Callback callback) {
|
||||
return new NattSocketKeepalive(mService, network, fd, INVALID_RESOURCE_ID /* Unused */,
|
||||
source, destination, executor, callback);
|
||||
ParcelFileDescriptor dup;
|
||||
try {
|
||||
dup = pfd.dup();
|
||||
} catch (IOException ignored) {
|
||||
// Construct an invalid fd, so that if the user later calls start(), it will fail with
|
||||
// ERROR_INVALID_SOCKET.
|
||||
dup = new ParcelFileDescriptor(new FileDescriptor());
|
||||
}
|
||||
return new NattSocketKeepalive(mService, network, dup,
|
||||
INVALID_RESOURCE_ID /* Unused */, source, destination, executor, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1987,11 +2006,19 @@ public class ConnectivityManager {
|
||||
*/
|
||||
@SystemApi
|
||||
@RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
|
||||
public SocketKeepalive createSocketKeepalive(@NonNull Network network,
|
||||
public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network,
|
||||
@NonNull Socket socket,
|
||||
@NonNull Executor executor,
|
||||
@NonNull Callback callback) {
|
||||
return new TcpSocketKeepalive(mService, network, socket, executor, callback);
|
||||
ParcelFileDescriptor dup;
|
||||
try {
|
||||
dup = ParcelFileDescriptor.fromSocket(socket);
|
||||
} catch (UncheckedIOException ignored) {
|
||||
// Construct an invalid fd, so that if the user later calls start(), it will fail with
|
||||
// ERROR_INVALID_SOCKET.
|
||||
dup = new ParcelFileDescriptor(new FileDescriptor());
|
||||
}
|
||||
return new TcpSocketKeepalive(mService, network, dup, executor, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3323,7 +3350,7 @@ public class ConnectivityManager {
|
||||
* @param network The {@link Network} whose blocked status has changed.
|
||||
* @param blocked The blocked status of this {@link Network}.
|
||||
*/
|
||||
public void onBlockedStatusChanged(Network network, boolean blocked) {}
|
||||
public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {}
|
||||
|
||||
private NetworkRequest networkRequest;
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
package android.net;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.net.InetAddress;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -31,21 +31,19 @@ public final class NattSocketKeepalive extends SocketKeepalive {
|
||||
|
||||
@NonNull private final InetAddress mSource;
|
||||
@NonNull private final InetAddress mDestination;
|
||||
@NonNull private final FileDescriptor mFd;
|
||||
private final int mResourceId;
|
||||
|
||||
NattSocketKeepalive(@NonNull IConnectivityManager service,
|
||||
@NonNull Network network,
|
||||
@NonNull FileDescriptor fd,
|
||||
@NonNull ParcelFileDescriptor pfd,
|
||||
int resourceId,
|
||||
@NonNull InetAddress source,
|
||||
@NonNull InetAddress destination,
|
||||
@NonNull Executor executor,
|
||||
@NonNull Callback callback) {
|
||||
super(service, network, executor, callback);
|
||||
super(service, network, pfd, executor, callback);
|
||||
mSource = source;
|
||||
mDestination = destination;
|
||||
mFd = fd;
|
||||
mResourceId = resourceId;
|
||||
}
|
||||
|
||||
@@ -53,8 +51,8 @@ public final class NattSocketKeepalive extends SocketKeepalive {
|
||||
void startImpl(int intervalSec) {
|
||||
mExecutor.execute(() -> {
|
||||
try {
|
||||
mService.startNattKeepaliveWithFd(mNetwork, mFd, mResourceId, intervalSec,
|
||||
mCallback,
|
||||
mService.startNattKeepaliveWithFd(mNetwork, mPfd.getFileDescriptor(), mResourceId,
|
||||
intervalSec, mCallback,
|
||||
mSource.getHostAddress(), mDestination.getHostAddress());
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Error starting socket keepalive: ", e);
|
||||
@@ -75,6 +73,5 @@ public final class NattSocketKeepalive extends SocketKeepalive {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,14 +488,14 @@ public abstract class NetworkAgent extends Handler {
|
||||
* Requests that the network hardware send the specified packet at the specified interval.
|
||||
*/
|
||||
protected void startSocketKeepalive(Message msg) {
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests that the network hardware stops sending keepalive packets.
|
||||
*/
|
||||
protected void stopSocketKeepalive(Message msg) {
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -511,7 +511,7 @@ public abstract class NetworkAgent extends Handler {
|
||||
* override this method.
|
||||
*/
|
||||
protected void addKeepalivePacketFilter(Message msg) {
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -520,7 +520,7 @@ public abstract class NetworkAgent extends Handler {
|
||||
* must override this method.
|
||||
*/
|
||||
protected void removeKeepalivePacketFilter(Message msg) {
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
|
||||
onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,8 +21,10 @@ import android.annotation.IntRange;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.os.Binder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -73,10 +75,15 @@ public abstract class SocketKeepalive implements AutoCloseable {
|
||||
/** The target socket is not idle. */
|
||||
public static final int ERROR_SOCKET_NOT_IDLE = -26;
|
||||
|
||||
/** The hardware does not support this request. */
|
||||
public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
|
||||
/** The device does not support this request. */
|
||||
public static final int ERROR_UNSUPPORTED = -30;
|
||||
/** @hide TODO: delete when telephony code has been updated. */
|
||||
public static final int ERROR_HARDWARE_UNSUPPORTED = ERROR_UNSUPPORTED;
|
||||
/** The hardware returned an error. */
|
||||
public static final int ERROR_HARDWARE_ERROR = -31;
|
||||
/** The limitation of resource is reached. */
|
||||
public static final int ERROR_INSUFFICIENT_RESOURCES = -32;
|
||||
|
||||
|
||||
/** @hide */
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@@ -147,15 +154,18 @@ public abstract class SocketKeepalive implements AutoCloseable {
|
||||
|
||||
@NonNull final IConnectivityManager mService;
|
||||
@NonNull final Network mNetwork;
|
||||
@NonNull final ParcelFileDescriptor mPfd;
|
||||
@NonNull final Executor mExecutor;
|
||||
@NonNull final ISocketKeepaliveCallback mCallback;
|
||||
// TODO: remove slot since mCallback could be used to identify which keepalive to stop.
|
||||
@Nullable Integer mSlot;
|
||||
|
||||
SocketKeepalive(@NonNull IConnectivityManager service, @NonNull Network network,
|
||||
@NonNull ParcelFileDescriptor pfd,
|
||||
@NonNull Executor executor, @NonNull Callback callback) {
|
||||
mService = service;
|
||||
mNetwork = network;
|
||||
mPfd = pfd;
|
||||
mExecutor = executor;
|
||||
mCallback = new ISocketKeepaliveCallback.Stub() {
|
||||
@Override
|
||||
@@ -233,6 +243,11 @@ public abstract class SocketKeepalive implements AutoCloseable {
|
||||
@Override
|
||||
public final void close() {
|
||||
stop();
|
||||
try {
|
||||
mPfd.close();
|
||||
} catch (IOException e) {
|
||||
// Nothing much can be done.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,25 +17,22 @@
|
||||
package android.net;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/** @hide */
|
||||
final class TcpSocketKeepalive extends SocketKeepalive {
|
||||
|
||||
private final Socket mSocket;
|
||||
|
||||
TcpSocketKeepalive(@NonNull IConnectivityManager service,
|
||||
@NonNull Network network,
|
||||
@NonNull Socket socket,
|
||||
@NonNull ParcelFileDescriptor pfd,
|
||||
@NonNull Executor executor,
|
||||
@NonNull Callback callback) {
|
||||
super(service, network, executor, callback);
|
||||
mSocket = socket;
|
||||
super(service, network, pfd, executor, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +54,7 @@ final class TcpSocketKeepalive extends SocketKeepalive {
|
||||
void startImpl(int intervalSec) {
|
||||
mExecutor.execute(() -> {
|
||||
try {
|
||||
final FileDescriptor fd = mSocket.getFileDescriptor$();
|
||||
final FileDescriptor fd = mPfd.getFileDescriptor();
|
||||
mService.startTcpKeepalive(mNetwork, fd, intervalSec, mCallback);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "Error starting packet keepalive: ", e);
|
||||
|
||||
Reference in New Issue
Block a user