cronet: Add internal API to specify Network · grpc/grpc-java@c2eccca
@@ -20,9 +20,12 @@
2020import static com.google.common.base.Preconditions.checkNotNull;
2121import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
222223+import android.net.Network;
24+import android.os.Build;
2325import com.google.common.annotations.VisibleForTesting;
2426import com.google.common.base.Preconditions;
2527import com.google.common.util.concurrent.MoreExecutors;
28+import com.google.errorprone.annotations.CanIgnoreReturnValue;
2629import com.google.errorprone.annotations.DoNotCall;
2730import io.grpc.ChannelCredentials;
2831import io.grpc.ChannelLogger;
@@ -105,6 +108,7 @@ public static CronetChannelBuilder forAddress(String name, int port) {
105108private int trafficStatsTag;
106109private boolean trafficStatsUidSet;
107110private int trafficStatsUid;
111+private Network network;
108112109113private CronetChannelBuilder(String host, int port, CronetEngine cronetEngine) {
110114final class CronetChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
@@ -190,6 +194,13 @@ CronetChannelBuilder setTrafficStatsUid(int uid) {
190194return this;
191195 }
192196197+/** Sets the network ID to use for this channel traffic. */
198+@CanIgnoreReturnValue
199+CronetChannelBuilder bindToNetwork(@Nullable Network network) {
200+this.network = network;
201+return this;
202+ }
203+193204/**
194205 * Provides a custom scheduled executor service.
195206 *
@@ -210,7 +221,12 @@ public CronetChannelBuilder scheduledExecutorService(
210221ClientTransportFactory buildTransportFactory() {
211222return new CronetTransportFactory(
212223new TaggingStreamFactory(
213-cronetEngine, trafficStatsTagSet, trafficStatsTag, trafficStatsUidSet, trafficStatsUid),
224+cronetEngine,
225+trafficStatsTagSet,
226+trafficStatsTag,
227+trafficStatsUidSet,
228+trafficStatsUid,
229+network),
214230MoreExecutors.directExecutor(),
215231scheduledExecutorService,
216232maxMessageSize,
@@ -294,18 +310,21 @@ private static class TaggingStreamFactory extends StreamBuilderFactory {
294310private final int trafficStatsTag;
295311private final boolean trafficStatsUidSet;
296312private final int trafficStatsUid;
313+private final Network network;
297314298315TaggingStreamFactory(
299316CronetEngine cronetEngine,
300317boolean trafficStatsTagSet,
301318int trafficStatsTag,
302319boolean trafficStatsUidSet,
303-int trafficStatsUid) {
320+int trafficStatsUid,
321+Network network) {
304322this.cronetEngine = cronetEngine;
305323this.trafficStatsTagSet = trafficStatsTagSet;
306324this.trafficStatsTag = trafficStatsTag;
307325this.trafficStatsUidSet = trafficStatsUidSet;
308326this.trafficStatsUid = trafficStatsUid;
327+this.network = network;
309328 }
310329311330@Override
@@ -320,6 +339,11 @@ public BidirectionalStream.Builder newBidirectionalStreamBuilder(
320339if (trafficStatsUidSet) {
321340builder.setTrafficStatsUid(trafficStatsUid);
322341 }
342+if (network != null) {
343+if (Build.VERSION.SDK_INT >= 23) {
344+builder.bindToNetwork(network.getNetworkHandle());
345+ }
346+ }
323347return builder;
324348 }
325349 }