2020import static com .google .common .base .Preconditions .checkNotNull ;
2121import static io .grpc .internal .GrpcUtil .DEFAULT_MAX_MESSAGE_SIZE ;
2222
23+ 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) {
105108 private int trafficStatsTag ;
106109 private boolean trafficStatsUidSet ;
107110 private int trafficStatsUid ;
111+ private Network network ;
108112
109113 private CronetChannelBuilder (String host , int port , CronetEngine cronetEngine ) {
110114 final class CronetChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
@@ -190,6 +194,13 @@ CronetChannelBuilder setTrafficStatsUid(int uid) {
190194 return this ;
191195 }
192196
197+ /** 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(
210221 ClientTransportFactory buildTransportFactory () {
211222 return new CronetTransportFactory (
212223 new TaggingStreamFactory (
213- cronetEngine , trafficStatsTagSet , trafficStatsTag , trafficStatsUidSet , trafficStatsUid ),
224+ cronetEngine ,
225+ trafficStatsTagSet ,
226+ trafficStatsTag ,
227+ trafficStatsUidSet ,
228+ trafficStatsUid ,
229+ network ),
214230 MoreExecutors .directExecutor (),
215231 scheduledExecutorService ,
216232 maxMessageSize ,
@@ -294,18 +310,21 @@ private static class TaggingStreamFactory extends StreamBuilderFactory {
294310 private final int trafficStatsTag ;
295311 private final boolean trafficStatsUidSet ;
296312 private final int trafficStatsUid ;
313+ private final Network network ;
297314
298315 TaggingStreamFactory (
299316 CronetEngine cronetEngine ,
300317 boolean trafficStatsTagSet ,
301318 int trafficStatsTag ,
302319 boolean trafficStatsUidSet ,
303- int trafficStatsUid ) {
320+ int trafficStatsUid ,
321+ Network network ) {
304322 this .cronetEngine = cronetEngine ;
305323 this .trafficStatsTagSet = trafficStatsTagSet ;
306324 this .trafficStatsTag = trafficStatsTag ;
307325 this .trafficStatsUidSet = trafficStatsUidSet ;
308326 this .trafficStatsUid = trafficStatsUid ;
327+ this .network = network ;
309328 }
310329
311330 @ Override
@@ -320,6 +339,11 @@ public BidirectionalStream.Builder newBidirectionalStreamBuilder(
320339 if (trafficStatsUidSet ) {
321340 builder .setTrafficStatsUid (trafficStatsUid );
322341 }
342+ if (network != null ) {
343+ if (Build .VERSION .SDK_INT >= 23 ) {
344+ builder .bindToNetwork (network .getNetworkHandle ());
345+ }
346+ }
323347 return builder ;
324348 }
325349 }
0 commit comments