Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions android/src/main/java/io/grpc/android/AndroidChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Build;
import android.util.Log;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
Expand All @@ -41,7 +42,6 @@
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;

/**
Expand Down Expand Up @@ -81,6 +81,10 @@ public static AndroidChannelBuilder forAddress(String name, int port) {
return forTarget(GrpcUtil.authorityFromHostAndPort(name, port));
}

public static AndroidChannelBuilder fromBuilder(ManagedChannelBuilder builder) {
return new AndroidChannelBuilder(builder);
}

private AndroidChannelBuilder(String target) {
if (OKHTTP_CHANNEL_BUILDER_CLASS == null) {
throw new UnsupportedOperationException("No ManagedChannelBuilder found on the classpath");
Expand All @@ -96,13 +100,23 @@ private AndroidChannelBuilder(String target) {
}
}

private AndroidChannelBuilder(ManagedChannelBuilder delegateBuilder) {
this.delegateBuilder = Preconditions.checkNotNull(delegateBuilder, "delegateBuilder");
}

/** Enables automatic monitoring of the device's network state. */
public AndroidChannelBuilder context(Context context) {
this.context = context;
return this;
}

/** Set the delegate channel builder's transportExecutor. */
/**
* Set the delegate channel builder's transportExecutor.
*
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
* ManagedChannelBuilder instead.
*/
@Deprecated
public AndroidChannelBuilder transportExecutor(@Nullable Executor transportExecutor) {
try {
OKHTTP_CHANNEL_BUILDER_CLASS
Expand All @@ -114,7 +128,13 @@ public AndroidChannelBuilder transportExecutor(@Nullable Executor transportExecu
}
}

/** Set the delegate channel builder's sslSocketFactory. */
/**
* Set the delegate channel builder's sslSocketFactory.
*
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
* ManagedChannelBuilder instead.
*/
@Deprecated
public AndroidChannelBuilder sslSocketFactory(SSLSocketFactory factory) {
try {
OKHTTP_CHANNEL_BUILDER_CLASS
Expand All @@ -126,7 +146,13 @@ public AndroidChannelBuilder sslSocketFactory(SSLSocketFactory factory) {
}
}

/** Set the delegate channel builder's scheduledExecutorService. */
/**
* Set the delegate channel builder's scheduledExecutorService.
*
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
* ManagedChannelBuilder instead.
*/
@Deprecated
public AndroidChannelBuilder scheduledExecutorService(
ScheduledExecutorService scheduledExecutorService) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.grpc.ClientCall;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.okhttp.OkHttpChannelBuilder;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -40,8 +41,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -96,6 +95,13 @@ public void channelBuilderClassFoundReflectively() {
AndroidChannelBuilder.forTarget("target");
}

@Test
public void fromBuilderConstructor() {
OkHttpChannelBuilder wrappedBuilder = OkHttpChannelBuilder.forTarget("target");
AndroidChannelBuilder androidBuilder = AndroidChannelBuilder.fromBuilder(wrappedBuilder);
assertThat(androidBuilder.delegate()).isSameAs(wrappedBuilder);
}

@Test
public void transportExecutor() {
AndroidChannelBuilder.forTarget("target")
Expand Down