what-the-bank/sources/io/grpc/android/AndroidChannelBuilder.java

261 lines
9.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import com.google.common.base.Preconditions;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
import io.grpc.ForwardingChannelBuilder;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.grpc.internal.GrpcUtil;
import java.util.concurrent.TimeUnit;
/* loaded from: classes6.dex */
public final class AndroidChannelBuilder extends ForwardingChannelBuilder<AndroidChannelBuilder> {
private static final String LOG_TAG = "AndroidChannelBuilder";
private static final Class<?> OKHTTP_CHANNEL_BUILDER_CLASS = findOkHttp();
private Context context;
private final ManagedChannelBuilder<?> delegateBuilder;
private static Class<?> findOkHttp() {
try {
return Class.forName("io.grpc.okhttp.OkHttpChannelBuilder");
} catch (ClassNotFoundException unused) {
return null;
}
}
public static AndroidChannelBuilder forTarget(String str) {
return new AndroidChannelBuilder(str);
}
public static AndroidChannelBuilder forAddress(String str, int i) {
return forTarget(GrpcUtil.authorityFromHostAndPort(str, i));
}
@Deprecated
public static AndroidChannelBuilder fromBuilder(ManagedChannelBuilder<?> managedChannelBuilder) {
return usingBuilder(managedChannelBuilder);
}
public static AndroidChannelBuilder usingBuilder(ManagedChannelBuilder<?> managedChannelBuilder) {
return new AndroidChannelBuilder(managedChannelBuilder);
}
private AndroidChannelBuilder(String str) {
Class<?> cls = OKHTTP_CHANNEL_BUILDER_CLASS;
if (cls == null) {
throw new UnsupportedOperationException("No ManagedChannelBuilder found on the classpath");
}
try {
this.delegateBuilder = (ManagedChannelBuilder) cls.getMethod("forTarget", String.class).invoke(null, str);
} catch (Exception e) {
throw new RuntimeException("Failed to create ManagedChannelBuilder", e);
}
}
private AndroidChannelBuilder(ManagedChannelBuilder<?> managedChannelBuilder) {
this.delegateBuilder = (ManagedChannelBuilder) Preconditions.checkNotNull(managedChannelBuilder, "delegateBuilder");
}
@Override // io.grpc.ForwardingChannelBuilder, io.grpc.ManagedChannelBuilder
public final ManagedChannel build() {
return new AndroidChannel(this.delegateBuilder.build(), this.context);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static final class AndroidChannel extends ManagedChannel {
private final ConnectivityManager connectivityManager;
private final Context context;
private final ManagedChannel delegate;
private final Object lock = new Object();
private Runnable unregisterRunnable;
AndroidChannel(ManagedChannel managedChannel, Context context) {
this.delegate = managedChannel;
this.context = context;
if (context != null) {
this.connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
try {
configureNetworkMonitoring();
return;
} catch (SecurityException unused) {
return;
}
}
this.connectivityManager = null;
}
private void configureNetworkMonitoring() {
if (this.connectivityManager != null) {
DefaultNetworkCallback defaultNetworkCallback = new DefaultNetworkCallback();
this.connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback);
this.unregisterRunnable = new Runnable(this, defaultNetworkCallback) { // from class: io.grpc.android.AndroidChannelBuilder.AndroidChannel.1
final AndroidChannel this$0;
final DefaultNetworkCallback val$defaultNetworkCallback;
{
this.this$0 = this;
this.val$defaultNetworkCallback = defaultNetworkCallback;
}
@Override // java.lang.Runnable
public void run() {
this.this$0.connectivityManager.unregisterNetworkCallback(this.val$defaultNetworkCallback);
}
};
} else {
NetworkReceiver networkReceiver = new NetworkReceiver();
this.context.registerReceiver(networkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
this.unregisterRunnable = new Runnable(this, networkReceiver) { // from class: io.grpc.android.AndroidChannelBuilder.AndroidChannel.2
final AndroidChannel this$0;
final NetworkReceiver val$networkReceiver;
{
this.this$0 = this;
this.val$networkReceiver = networkReceiver;
}
@Override // java.lang.Runnable
public void run() {
this.this$0.context.unregisterReceiver(this.val$networkReceiver);
}
};
}
}
private void unregisterNetworkListener() {
synchronized (this.lock) {
Runnable runnable = this.unregisterRunnable;
if (runnable != null) {
runnable.run();
this.unregisterRunnable = null;
}
}
}
@Override // io.grpc.ManagedChannel
public final ManagedChannel shutdown() {
unregisterNetworkListener();
return this.delegate.shutdown();
}
@Override // io.grpc.ManagedChannel
public final boolean isShutdown() {
return this.delegate.isShutdown();
}
@Override // io.grpc.ManagedChannel
public final boolean isTerminated() {
return this.delegate.isTerminated();
}
@Override // io.grpc.ManagedChannel
public final ManagedChannel shutdownNow() {
unregisterNetworkListener();
return this.delegate.shutdownNow();
}
@Override // io.grpc.ManagedChannel
public final boolean awaitTermination(long j, TimeUnit timeUnit) throws InterruptedException {
return this.delegate.awaitTermination(j, timeUnit);
}
@Override // io.grpc.Channel
public final <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
return this.delegate.newCall(methodDescriptor, callOptions);
}
@Override // io.grpc.Channel
public final String authority() {
return this.delegate.authority();
}
@Override // io.grpc.ManagedChannel
public final ConnectivityState getState(boolean z) {
return this.delegate.getState(z);
}
@Override // io.grpc.ManagedChannel
public final void notifyWhenStateChanged(ConnectivityState connectivityState, Runnable runnable) {
this.delegate.notifyWhenStateChanged(connectivityState, runnable);
}
@Override // io.grpc.ManagedChannel
public final void resetConnectBackoff() {
this.delegate.resetConnectBackoff();
}
@Override // io.grpc.ManagedChannel
public final void enterIdle() {
this.delegate.enterIdle();
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class DefaultNetworkCallback extends ConnectivityManager.NetworkCallback {
final AndroidChannel this$0;
private DefaultNetworkCallback(AndroidChannel androidChannel) {
this.this$0 = androidChannel;
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onAvailable(Network network) {
this.this$0.delegate.enterIdle();
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onBlockedStatusChanged(Network network, boolean z) {
if (z) {
return;
}
this.this$0.delegate.enterIdle();
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class NetworkReceiver extends BroadcastReceiver {
private boolean isConnected;
final AndroidChannel this$0;
private NetworkReceiver(AndroidChannel androidChannel) {
this.this$0 = androidChannel;
this.isConnected = false;
}
@Override // android.content.BroadcastReceiver
public void onReceive(Context context, Intent intent) {
NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
boolean z = this.isConnected;
boolean z2 = activeNetworkInfo != null && activeNetworkInfo.isConnected();
this.isConnected = z2;
if (!z2 || z) {
return;
}
this.this$0.delegate.enterIdle();
}
}
}
@Override // io.grpc.ForwardingChannelBuilder
public final ManagedChannelBuilder<?> delegate() {
return this.delegateBuilder;
}
public final AndroidChannelBuilder context(Context context) {
this.context = context;
return this;
}
}