package com.ea.nimble; import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkCapabilities; import android.os.Build; import com.ea.nimble.Error; import com.ea.nimble.IHttpRequest; import com.ea.nimble.Log; import com.ea.nimble.Network; import com.mbridge.msdk.foundation.entity.CampaignEx; import com.unity3d.ads.core.domain.InitializeAndroidBoldSDK; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ class NetworkImpl extends Component implements INetwork, LogSource { private static final String BACKUP_NETWORK_REACHABILITY_CHECK_URL = "https://www.google.com"; private static final int DETECTION_TIMEOUT = 30; private static final String MAIN_NETWORK_REACHABILITY_CHECK_URL = "https://ping1.tnt-ea.com"; private static final int MAX_CONCURRENT_THREADS = 4; private static final int[] PING_INTERVAL = {5, 10, 30, 60}; private static final int QUICK_DETECTION_TIMEOUT = 5; private ExecutorService m_asyncTaskManager; private ConnectivityReceiver m_connectivityReceiver; private NetworkConnection m_detectionConnection; private boolean m_isWifi; private NetworkCallback m_networkCallback; private DetectionState m_networkDetectionState; private int m_pingIndex; private final List m_queue; private Network.Status m_status; private Timer m_timer; private LinkedList m_waitingToExecuteQueue; public enum DetectionState { NONE, VERIFY_REACHABLE_MAIN, VERIFY_UNREACHABLE_MAIN, VERIFY_REACHABLE_BACKUP, PING } @Override // com.ea.nimble.Component public String getComponentId() { return "com.ea.nimble.network"; } @Override // com.ea.nimble.LogSource public String getLogSourceTitle() { return InitializeAndroidBoldSDK.MSG_NETWORK; } public class ConnectivityReceiver extends BroadcastReceiver { private ConnectivityReceiver() { } public /* synthetic */ ConnectivityReceiver(NetworkImpl networkImpl, AnonymousClass1 anonymousClass1) { this(); } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { Log.Helper.LOGD(this, "Network reachability changed!", new Object[0]); synchronized (NetworkImpl.this) { NetworkImpl.this.detect(true); } } } @TargetApi(28) public class NetworkCallback extends ConnectivityManager.NetworkCallback { private NetworkCallback() { } public /* synthetic */ NetworkCallback(NetworkImpl networkImpl, AnonymousClass1 anonymousClass1) { this(); } @Override // android.net.ConnectivityManager.NetworkCallback public void onAvailable(android.net.Network network) { Log.Helper.LOGD(this, "Network reachability changed!", new Object[0]); super.onAvailable(network); synchronized (NetworkImpl.this) { NetworkImpl.this.detect(true); } } @Override // android.net.ConnectivityManager.NetworkCallback public void onLost(android.net.Network network) { Log.Helper.LOGD(this, "Network reachability changed!", new Object[0]); super.onLost(network); synchronized (NetworkImpl.this) { NetworkImpl.this.detect(true); } } } public NetworkImpl() { Log.Helper.LOGFUNC(this); this.m_connectivityReceiver = null; this.m_networkCallback = null; this.m_status = Network.Status.UNKNOWN; this.m_detectionConnection = null; this.m_networkDetectionState = DetectionState.NONE; this.m_pingIndex = 0; this.m_queue = new ArrayList(); } @Override // com.ea.nimble.Component public void setup() { Log.Helper.LOGV(this, "setup", new Object[0]); startWork(); } @Override // com.ea.nimble.Component public void suspend() { synchronized (this) { stopPing(); unregisterNetworkListener(); synchronized (this) { try { Iterator it = new ArrayList(this.m_queue).iterator(); while (it.hasNext()) { ((NetworkConnection) it.next()).cancelForAppSuspend(); } } catch (Throwable th) { throw th; } } Log.Helper.LOGV(this, "suspend", new Object[0]); } Log.Helper.LOGV(this, "suspend", new Object[0]); } @Override // com.ea.nimble.Component public void resume() { Log.Helper.LOGV(this, CampaignEx.JSON_NATIVE_VIDEO_RESUME, new Object[0]); synchronized (this) { detect(true); registerNetworkListener(); } } @Override // com.ea.nimble.Component public void cleanup() { stopWork(); Log.Helper.LOGV(this, "cleanup", new Object[0]); } private void registerNetworkListener() { Log.Helper.LOGFUNC(this); AnonymousClass1 anonymousClass1 = null; if (Build.VERSION.SDK_INT < 28) { if (this.m_connectivityReceiver == null) { Log.Helper.LOGD(this, "Register network reachability listener.", new Object[0]); this.m_connectivityReceiver = new ConnectivityReceiver(this, anonymousClass1); ApplicationEnvironment.getComponent().getApplicationContext().registerReceiver(this.m_connectivityReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); return; } return; } if (this.m_networkCallback == null) { ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationEnvironment.getComponent().getApplicationContext().getSystemService("connectivity"); if (connectivityManager == null) { Log.Helper.LOGE(this, "Can not get ConnectivityManager from context.", new Object[0]); return; } NetworkCallback networkCallback = new NetworkCallback(this, anonymousClass1); this.m_networkCallback = networkCallback; connectivityManager.registerDefaultNetworkCallback(networkCallback); } } private void unregisterNetworkListener() { Log.Helper.LOGFUNC(this); if (Build.VERSION.SDK_INT < 28) { if (this.m_connectivityReceiver != null) { try { ApplicationEnvironment.getComponent().getApplicationContext().unregisterReceiver(this.m_connectivityReceiver); } catch (IllegalArgumentException unused) { Log.Helper.LOGE(this, "Unable to unregister network reachability listener even it does exists", new Object[0]); } this.m_connectivityReceiver = null; return; } return; } if (this.m_networkCallback != null) { ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationEnvironment.getComponent().getApplicationContext().getSystemService("connectivity"); if (connectivityManager == null) { Log.Helper.LOGE(this, "Can not get ConnectivityManager from context.", new Object[0]); } else { connectivityManager.unregisterNetworkCallback(this.m_networkCallback); this.m_networkCallback = null; } } } @Override // com.ea.nimble.INetwork public NetworkConnectionHandle sendGetRequest(URL url, HashMap hashMap, NetworkConnectionCallback networkConnectionCallback) { Log.Helper.LOGPUBLICFUNC(this); HttpRequest httpRequest = new HttpRequest(url); httpRequest.method = IHttpRequest.Method.GET; httpRequest.headers = hashMap; return sendRequest(httpRequest, networkConnectionCallback); } @Override // com.ea.nimble.INetwork public NetworkConnectionHandle sendPostRequest(URL url, HashMap hashMap, byte[] bArr, NetworkConnectionCallback networkConnectionCallback) { Log.Helper.LOGPUBLICFUNC(this); HttpRequest httpRequest = new HttpRequest(url); httpRequest.method = IHttpRequest.Method.POST; httpRequest.headers = hashMap; try { httpRequest.data.write(bArr); } catch (Exception e) { e.printStackTrace(); } return sendRequest(httpRequest, networkConnectionCallback); } @Override // com.ea.nimble.INetwork public NetworkConnectionHandle sendDeleteRequest(URL url, HashMap hashMap, NetworkConnectionCallback networkConnectionCallback) { Log.Helper.LOGPUBLICFUNC(this); HttpRequest httpRequest = new HttpRequest(url); httpRequest.method = IHttpRequest.Method.DELETE; httpRequest.headers = hashMap; return sendRequest(httpRequest, networkConnectionCallback); } @Override // com.ea.nimble.INetwork public NetworkConnectionHandle sendRequest(HttpRequest httpRequest, NetworkConnectionCallback networkConnectionCallback) { Log.Helper.LOGPUBLICFUNC(this); return sendRequest(httpRequest, networkConnectionCallback, null); } @Override // com.ea.nimble.INetwork public NetworkConnectionHandle sendRequest(HttpRequest httpRequest, NetworkConnectionCallback networkConnectionCallback, IOperationalTelemetryDispatch iOperationalTelemetryDispatch) { NetworkConnection networkConnection; Log.Helper.LOGPUBLICFUNC(this); if (httpRequest.runInBackground) { networkConnection = new BackgroundNetworkConnection(this, httpRequest, iOperationalTelemetryDispatch); } else { networkConnection = new NetworkConnection(this, httpRequest, iOperationalTelemetryDispatch); } networkConnection.setCompletionCallback(networkConnectionCallback); URL url = httpRequest.url; if (url == null || !Utility.validString(url.toString())) { networkConnection.finishWithError(new Error(Error.Code.INVALID_ARGUMENT, "Sending request without valid url")); return networkConnection; } if (this.m_status != Network.Status.OK) { networkConnection.finishWithError(new Error(Error.Code.NETWORK_NO_CONNECTION, "No network connection, network status " + this.m_status.toString())); return networkConnection; } synchronized (this) { this.m_queue.add(networkConnection); } ExecutorService executorService = this.m_asyncTaskManager; if (executorService == null || executorService.isShutdown()) { if (this.m_asyncTaskManager != null) { Log.Helper.LOGW(this, "AsyncTaskManager shutdown. Queueing network connection until AsyncTaskManager is started.", new Object[0]); } else { Log.Helper.LOGW(this, "AsyncTaskManager is not ready. Queueing network connection until AsyncTaskManager is started.", new Object[0]); } if (this.m_waitingToExecuteQueue == null) { this.m_waitingToExecuteQueue = new LinkedList<>(); } this.m_waitingToExecuteQueue.add(networkConnection); } else { this.m_asyncTaskManager.execute(networkConnection); } return networkConnection; } @Override // com.ea.nimble.INetwork public synchronized void forceRedetectNetworkStatus() { Log.Helper.LOGPUBLICFUNC(this); detect(true); } @Override // com.ea.nimble.INetwork public Network.Status getStatus() { Log.Helper.LOGPUBLICFUNC(this); return this.m_status; } @Override // com.ea.nimble.INetwork public boolean isNetworkWifi() { Log.Helper.LOGPUBLICFUNC(this); return this.m_isWifi; } public synchronized void removeConnection(NetworkConnection networkConnection) { Log.Helper.LOGFUNC(this); this.m_queue.remove(networkConnection); } /* JADX INFO: Access modifiers changed from: private */ public void detect(boolean z) { Log.Helper.LOGFUNC(this); NetworkConnection networkConnection = this.m_detectionConnection; if (networkConnection != null) { if (!z) { return; } this.m_detectionConnection = null; networkConnection.cancel(); } stopPing(); boolean z2 = this.m_isWifi; if (reachabilityCheck()) { if (this.m_status != Network.Status.DEAD) { setStatus(Network.Status.OK, z2 != this.m_isWifi); } this.m_networkDetectionState = DetectionState.VERIFY_REACHABLE_MAIN; } else { if (this.m_status == Network.Status.UNKNOWN) { setStatus(Network.Status.NONE, false); } this.m_networkDetectionState = DetectionState.VERIFY_UNREACHABLE_MAIN; } verifyReachability(MAIN_NETWORK_REACHABILITY_CHECK_URL, 5.0d); } private boolean reachabilityCheck() { ConnectivityManager connectivityManager; android.net.Network activeNetwork; Log.Helper.LOGFUNC(this); boolean z = false; if (!BaseCore.getInstance().isActive()) { Log.Helper.LOGD(this, "BaseCore not active yet. Postpone reachability check.", new Object[0]); return false; } this.m_isWifi = false; Context applicationContext = ApplicationEnvironment.getComponent().getApplicationContext(); if (applicationContext == null || (connectivityManager = (ConnectivityManager) applicationContext.getSystemService("connectivity")) == null || (activeNetwork = connectivityManager.getActiveNetwork()) == null) { return false; } NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork); if (networkCapabilities != null) { if (!networkCapabilities.hasCapability(12)) { return false; } z = true; if (networkCapabilities.hasTransport(1) || networkCapabilities.hasTransport(3)) { this.m_isWifi = true; } } return z; } private void startPing() { Log.Helper.LOGFUNC(this); if (this.m_pingIndex >= PING_INTERVAL.length) { this.m_pingIndex = r1.length - 1; } Timer timer = new Timer(new TimerTask(this, null)); this.m_timer = timer; timer.schedule(r1[this.m_pingIndex], false); } private void stopPing() { Log.Helper.LOGFUNC(this); Timer timer = this.m_timer; if (timer != null) { timer.cancel(); this.m_timer = null; } } public class TimerTask implements Runnable { private TimerTask() { } public /* synthetic */ TimerTask(NetworkImpl networkImpl, AnonymousClass1 anonymousClass1) { this(); } @Override // java.lang.Runnable public void run() { synchronized (NetworkImpl.this) { NetworkImpl.this.m_timer = null; NetworkImpl.this.verifyReachability(NetworkImpl.MAIN_NETWORK_REACHABILITY_CHECK_URL, 30.0d); } } } /* JADX INFO: Access modifiers changed from: private */ public void verifyReachability(String str, double d) { Log.Helper.LOGFUNC(this); try { HttpRequest httpRequest = new HttpRequest(new URL(str)); httpRequest.timeout = d; httpRequest.method = IHttpRequest.Method.HEAD; NetworkConnection networkConnection = new NetworkConnection(this, httpRequest); this.m_detectionConnection = networkConnection; networkConnection.setCompletionCallback(new NetworkConnectionCallback() { // from class: com.ea.nimble.NetworkImpl$$ExternalSyntheticLambda0 @Override // com.ea.nimble.NetworkConnectionCallback public final void callback(NetworkConnectionHandle networkConnectionHandle) { NetworkImpl.this.lambda$verifyReachability$0(networkConnectionHandle); } }); ExecutorService executorService = this.m_asyncTaskManager; if (executorService == null || executorService.isShutdown()) { Log.Helper.LOGW(this, "AsyncTaskManager is not ready. Queueing network connection until AsyncTaskManager is started.", new Object[0]); if (this.m_waitingToExecuteQueue == null) { this.m_waitingToExecuteQueue = new LinkedList<>(); } this.m_waitingToExecuteQueue.addFirst(this.m_detectionConnection); return; } this.m_asyncTaskManager.execute(this.m_detectionConnection); } catch (MalformedURLException unused) { Log.Helper.LOGE(this, "Invalid url: " + str, new Object[0]); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: onReachabilityVerification, reason: merged with bridge method [inline-methods] */ public synchronized void lambda$verifyReachability$0(NetworkConnectionHandle networkConnectionHandle) { Log.Helper.LOGFUNC(this); Exception error = networkConnectionHandle.getResponse().getError(); if (error == null) { Log.Helper.LOGD(this, "network verified reachable.", new Object[0]); setStatus(Network.Status.OK, false); this.m_detectionConnection = null; return; } if (networkConnectionHandle != this.m_detectionConnection) { return; } this.m_detectionConnection = null; Log.Helper.LOGD(this, "network verified unreachable, ERROR %s for detection state %s", networkConnectionHandle.getResponse().getError(), this.m_networkDetectionState); if (error instanceof Error) { Error error2 = (Error) error; if (error2.getDomain().equals(Error.ERROR_DOMAIN) && error2.isError(Error.Code.NETWORK_OPERATION_CANCELLED)) { Log.Helper.LOGW(this, "Network detection verification connection get cancelled for unknown reason (maybe reasonable for Android)", new Object[0]); } } int i = AnonymousClass1.$SwitchMap$com$ea$nimble$NetworkImpl$DetectionState[this.m_networkDetectionState.ordinal()]; if (i == 1) { this.m_networkDetectionState = DetectionState.VERIFY_REACHABLE_BACKUP; verifyReachability(BACKUP_NETWORK_REACHABILITY_CHECK_URL, 30.0d); } else if (i == 2) { setStatus(Network.Status.NONE, false); } else if (i == 3) { this.m_networkDetectionState = DetectionState.PING; Network.Status status = this.m_status; Network.Status status2 = Network.Status.DEAD; if (status != status2) { setStatus(status2, false); this.m_pingIndex = 0; } startPing(); } else if (i == 4) { this.m_pingIndex++; startPing(); } } /* renamed from: com.ea.nimble.NetworkImpl$1, reason: invalid class name */ public static /* synthetic */ class AnonymousClass1 { static final /* synthetic */ int[] $SwitchMap$com$ea$nimble$NetworkImpl$DetectionState; static { int[] iArr = new int[DetectionState.values().length]; $SwitchMap$com$ea$nimble$NetworkImpl$DetectionState = iArr; try { iArr[DetectionState.VERIFY_REACHABLE_MAIN.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { $SwitchMap$com$ea$nimble$NetworkImpl$DetectionState[DetectionState.VERIFY_UNREACHABLE_MAIN.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { $SwitchMap$com$ea$nimble$NetworkImpl$DetectionState[DetectionState.VERIFY_REACHABLE_BACKUP.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { $SwitchMap$com$ea$nimble$NetworkImpl$DetectionState[DetectionState.PING.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } } } private void setStatus(Network.Status status, boolean z) { Log.Helper.LOGI(this, "Status change %s -> %s", this.m_status, status); if (status != this.m_status || z) { this.m_status = status; Utility.sendBroadcast(Global.NOTIFICATION_NETWORK_STATUS_CHANGE); } } private synchronized void startWork() { try { Log.Helper.LOGFUNC(this); if (this.m_asyncTaskManager != null) { return; } detect(true); registerNetworkListener(); this.m_asyncTaskManager = Executors.newFixedThreadPool(4); LinkedList linkedList = this.m_waitingToExecuteQueue; if (linkedList != null && !linkedList.isEmpty()) { Log.Helper.LOGW(this, "NetworkConnections waiting to execute on new AsyncTaskManager. Executing.", new Object[0]); while (!this.m_waitingToExecuteQueue.isEmpty()) { NetworkConnection poll = this.m_waitingToExecuteQueue.poll(); if (poll != null) { Log.Helper.LOGW(this, "Executing request URL: " + poll.getRequest().url.toString(), new Object[0]); this.m_asyncTaskManager.execute(poll); } else { Log.Helper.LOGE(this, "Could not get queued connection", new Object[0]); } } } } catch (Throwable th) { throw th; } } private void stopWork() { Log.Helper.LOGFUNC(this); synchronized (this) { this.m_detectionConnection = null; stopPing(); unregisterNetworkListener(); } ExecutorService executorService = this.m_asyncTaskManager; if (executorService == null) { return; } try { Iterator it = executorService.shutdownNow().iterator(); while (it.hasNext()) { ((NetworkConnection) it.next()).cancelForAppSuspend(); } this.m_asyncTaskManager.awaitTermination(60L, TimeUnit.SECONDS); } catch (InterruptedException unused) { this.m_asyncTaskManager.shutdownNow(); Thread.currentThread().interrupt(); } this.m_asyncTaskManager = null; } }