导航菜单

页面标题

页面副标题

Perplexity v2.48.1 - MapboxOkHttpService.java 源代码

正在查看: Perplexity v2.48.1 应用的 MapboxOkHttpService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.mapbox.common.module.okhttp;

import Pm.E;
import Pm.K;
import Pm.N;
import Pm.P;
import Pm.e;
import Pm.z;
import Tm.j;
import android.content.Context;
import android.content.res.Resources;
import com.google.firebase.perf.network.FirebasePerfOkHttpClient;
import com.mapbox.bindgen.Expected;
import com.mapbox.bindgen.ExpectedFactory;
import com.mapbox.common.GetLifecycleStateCallback;
import com.mapbox.common.HttpMethod;
import com.mapbox.common.HttpRequestError;
import com.mapbox.common.HttpRequestErrorType;
import com.mapbox.common.LifecycleMonitorFactory;
import com.mapbox.common.LifecycleMonitorInterface;
import com.mapbox.common.LifecycleMonitoringState;
import com.mapbox.common.LifecycleObserver;
import com.mapbox.common.LifecycleState;
import com.mapbox.common.MapboxSDKCommon;
import com.mapbox.common.NetworkRestriction;
import com.mapbox.common.NetworkStatus;
import com.mapbox.common.OfflineSwitch;
import com.mapbox.common.OfflineSwitchObserver;
import com.mapbox.common.ReachabilityChanged;
import com.mapbox.common.ReachabilityFactory;
import com.mapbox.common.ReachabilityInterface;
import com.mapbox.common.ReadStream;
import com.mapbox.common.ResultCallback;
import com.mapbox.common.http_backend.Request;
import com.mapbox.common.http_backend.RequestObserver;
import com.mapbox.common.http_backend.ResponseData;
import com.mapbox.common.http_backend.Service;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.jvm.internal.Intrinsics;

public class MapboxOkHttpService implements Service {
    private static final String TAG = "MapboxOkHttpService";
    private static final List<String> allowFromBackgroundList = Arrays.asList("https://api.mapbox.com/sdk-sessions/v1", "https://events.mapbox.com", "https://config.mapbox.com", "https://api-events-staging.tilestream.net", "https://api-events-config-staging.tilestream.net", "https://cloudfront-staging.tilestream.net");
    private static final String emptyHostname = "";
    private final AtomicBoolean allowBackgroundRequests;
    private final Map<Long, QueuedRequest> foregroundRequests;
    private final LazyClient httpClient;
    private LifecycleMonitorInterface lifecycleMonitor;
    private LifecycleObserver lifecycleObserver;
    private LifecycleState lifecycleState;
    private NetworkStatus networkStatus;
    private final AtomicBoolean offline;
    private final OfflineSwitchObserver offlineObserver;
    private final AtomicBoolean pauseRequestsOnDemand;
    protected final Map<Long, CallbackWrapper> pendingCalls;
    private final ReachabilityInterface reachability;
    protected final ReachabilityChanged reachabilityChanged;

    public static class AnonymousClass1 {
        static final int[] $SwitchMap$com$mapbox$common$HttpMethod;
        static final int[] $SwitchMap$com$mapbox$common$NetworkRestriction;

        static {
            int[] iArr = new int[HttpMethod.values().length];
            $SwitchMap$com$mapbox$common$HttpMethod = iArr;
            try {
                iArr[HttpMethod.HEAD.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$mapbox$common$HttpMethod[HttpMethod.GET.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$mapbox$common$HttpMethod[HttpMethod.POST.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            int[] iArr2 = new int[NetworkRestriction.values().length];
            $SwitchMap$com$mapbox$common$NetworkRestriction = iArr2;
            try {
                iArr2[NetworkRestriction.NONE.ordinal()] = 1;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$com$mapbox$common$NetworkRestriction[NetworkRestriction.DISALLOW_EXPENSIVE.ordinal()] = 2;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$com$mapbox$common$NetworkRestriction[NetworkRestriction.DISALLOW_ALL.ordinal()] = 3;
            } catch (NoSuchFieldError unused6) {
            }
        }
    }

    public class HttpServiceGetLifecycleStateCallback implements GetLifecycleStateCallback {
        private HttpServiceGetLifecycleStateCallback() {
        }

        @Override
        public void run(Expected<String, LifecycleState> expected) {
            if (expected.isValue()) {
                MapboxOkHttpService.this.lifecycleState = expected.getValue();
            } else {
                MapboxOkHttpService.this.lifecycleState = LifecycleState.UNKNOWN;
            }
        }

        public HttpServiceGetLifecycleStateCallback(MapboxOkHttpService mapboxOkHttpService, AnonymousClass1 anonymousClass1) {
            this();
        }
    }

    public class HttpServiceLifecycleObserver implements LifecycleObserver {
        private HttpServiceLifecycleObserver() {
        }

        @Override
        public void onLifecycleStateChanged(LifecycleState lifecycleState) {
            boolean z = MapboxOkHttpService.this.lifecycleState == LifecycleState.FOREGROUND || MapboxOkHttpService.this.lifecycleState == LifecycleState.MOVING_FOREGROUND;
            MapboxOkHttpService.this.lifecycleState = lifecycleState;
            if (MapboxOkHttpService.this.foregroundRequests.isEmpty() || z || !MapboxOkHttpService.this.canSendForegroundRequest()) {
                return;
            }
            MapboxOkHttpService.this.processForegroundQueue();
        }

        @Override
        public void onMonitoringStateChanged(LifecycleMonitoringState lifecycleMonitoringState, String str) {
            if (str != null || lifecycleMonitoringState == LifecycleMonitoringState.STOPPED) {
                MapboxOkHttpService.this.lifecycleState = LifecycleState.UNKNOWN;
            }
        }

        public HttpServiceLifecycleObserver(MapboxOkHttpService mapboxOkHttpService, AnonymousClass1 anonymousClass1) {
            this();
        }
    }

    public class HttpServiceOfflineSwitchObserver implements OfflineSwitchObserver {
        private HttpServiceOfflineSwitchObserver() {
        }

        @Override
        public void statusChanged(boolean z) {
            MapboxOkHttpService.this.offline.set(!z);
            if (z) {
                return;
            }
            Iterator<CallbackWrapper> it = MapboxOkHttpService.this.pendingCalls.values().iterator();
            while (it.hasNext()) {
                it.next().cancel(new HttpRequestError(HttpRequestErrorType.CONNECTION_ERROR, "Connection lost"));
            }
        }

        public HttpServiceOfflineSwitchObserver(MapboxOkHttpService mapboxOkHttpService, AnonymousClass1 anonymousClass1) {
            this();
        }
    }

    public class QueuedRequest {
        public final e call;
        public final CallbackWrapper callback;

        public QueuedRequest(e eVar, CallbackWrapper callbackWrapper) {
            this.call = eVar;
            this.callback = callbackWrapper;
        }
    }

    public MapboxOkHttpService() {
        ReachabilityInterface reachability = ReachabilityFactory.reachability(emptyHostname);
        this.reachability = reachability;
        this.networkStatus = reachability.currentNetworkStatus();
        HttpServiceOfflineSwitchObserver httpServiceOfflineSwitchObserver = new HttpServiceOfflineSwitchObserver(this, null);
        this.offlineObserver = httpServiceOfflineSwitchObserver;
        this.offline = new AtomicBoolean(false);
        ReachabilityChanged reachabilityChanged = new ReachabilityChanged() {
            @Override
            public final void run(NetworkStatus networkStatus) {
                MapboxOkHttpService.this.lambda$new$0(networkStatus);
            }
        };
        this.reachabilityChanged = reachabilityChanged;
        this.foregroundRequests = new ConcurrentHashMap();
        AtomicBoolean atomicBoolean = new AtomicBoolean(true);
        this.allowBackgroundRequests = atomicBoolean;
        AtomicBoolean atomicBoolean2 = new AtomicBoolean(false);
        this.pauseRequestsOnDemand = atomicBoolean2;
        this.lifecycleMonitor = null;
        this.lifecycleObserver = null;
        this.lifecycleState = LifecycleState.UNKNOWN;
        this.pendingCalls = new ConcurrentHashMap();
        this.httpClient = new LazyClient(null, false);
        OfflineSwitch.getInstance().registerObserver(httpServiceOfflineSwitchObserver);
        reachability.addListener(reachabilityChanged);
        Context context = MapboxSDKCommon.INSTANCE.getContext();
        if (context != null) {
            String packageName = context.getPackageName();
            Resources resources = context.getResources();
            if (resources != null) {
                int identifier = resources.getIdentifier("com.mapbox.common.http.allow_background_requests", "bool", packageName);
                if (identifier != 0) {
                    atomicBoolean.set(resources.getBoolean(identifier));
                }
                int identifier2 = resources.getIdentifier("com.mapbox.common.http.pause_requests_on_demand", "bool", packageName);
                if (identifier2 != 0) {
                    atomicBoolean2.set(resources.getBoolean(identifier2));
                }
            }
        }
        if (!atomicBoolean.get() || atomicBoolean2.get()) {
            disableBackgroundRequests();
        }
    }

    private boolean allowAlways(String str) {
        Iterator<String> it = allowFromBackgroundList.iterator();
        while (it.hasNext()) {
            if (str.startsWith(it.next())) {
                return true;
            }
        }
        return false;
    }

    public static K buildRequest(Request request) {
        Ha.a aVar = new Ha.a(7);
        String url = request.getUrl();
        aVar.d0(url);
        aVar.Z(Object.class, url.toLowerCase(Locale.US));
        for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
            aVar.q(entry.getKey(), entry.getValue());
        }
        if ((request.getFlags() & 1) != 0) {
            aVar.q("Accept-Encoding", "gzip, deflate");
        }
        int i = AnonymousClass1.$SwitchMap$com$mapbox$common$HttpMethod[request.getMethod().ordinal()];
        if (i == 1) {
            aVar.S("HEAD", (N) null);
        } else if (i == 2) {
            aVar.S("GET", (N) null);
        } else if (i == 3) {
            ReadStream body = request.getBody();
            if (body == null) {
                N create = N.create(new byte[0], (E) null);
                Intrinsics.h(create, "body");
                aVar.S("POST", create);
            } else {
                aVar.S("POST", new StreamingRequestBody(body, null));
            }
        }
        return aVar.u();
    }

    public boolean canSendForegroundRequest() {
        LifecycleState lifecycleState = this.lifecycleState;
        return lifecycleState == LifecycleState.FOREGROUND || lifecycleState == LifecycleState.MOVING_FOREGROUND || lifecycleState == LifecycleState.UNKNOWN;
    }

    public static HashMap<String, String> generateOutputHeaders(P p) {
        HashMap<String, String> hashMap = new HashMap<>();
        z zVar = p.Y;
        for (int i = 0; i < zVar.size(); i++) {
            hashMap.put(zVar.d(i).toLowerCase(Locale.US), zVar.i(i));
        }
        return hashMap;
    }

    public void lambda$request$4(long j, RequestObserver requestObserver, Request request, Expected<HttpRequestError, ResponseData> expected) {
        if (expected.isValue()) {
            requestObserver.onResponse(j, expected.getValue());
        } else {
            requestObserver.onFailed(j, expected.getError());
        }
    }

    public void lambda$new$0(NetworkStatus networkStatus) {
        this.networkStatus = networkStatus;
    }

    private boolean needToPauseRequest(Request request) {
        if (canSendForegroundRequest()) {
            return false;
        }
        boolean z = (request.getFlags() & 2) != 0;
        if (this.pauseRequestsOnDemand.get() && z) {
            return true;
        }
        if (this.allowBackgroundRequests.get()) {
            return false;
        }
        return !allowAlways(request.getUrl().toString());
    }

    public void processForegroundQueue() {
        while (!this.foregroundRequests.isEmpty() && canSendForegroundRequest()) {
            Map.Entry<Long, QueuedRequest> next = this.foregroundRequests.entrySet().iterator().next();
            Long key = next.getKey();
            QueuedRequest value = next.getValue();
            FirebasePerfOkHttpClient.enqueue(value.call, value.callback);
            this.foregroundRequests.remove(key);
        }
    }

    @Override
    public void cancelRequest(long j, ResultCallback resultCallback) {
        boolean z;
        CallbackWrapper callbackWrapper = this.pendingCalls.get(Long.valueOf(j));
        if (callbackWrapper != null) {
            callbackWrapper.cancel();
            removeCall(j);
            z = true;
        } else {
            z = false;
        }
        resultCallback.run(z);
    }

    public void disableBackgroundRequests() {
        this.allowBackgroundRequests.set(false);
        if (this.lifecycleMonitor == null || this.lifecycleObserver == null) {
            this.lifecycleMonitor = LifecycleMonitorFactory.getOrCreate();
            AnonymousClass1 anonymousClass1 = null;
            HttpServiceLifecycleObserver httpServiceLifecycleObserver = new HttpServiceLifecycleObserver(this, anonymousClass1);
            this.lifecycleObserver = httpServiceLifecycleObserver;
            this.lifecycleMonitor.registerObserver(httpServiceLifecycleObserver);
            this.lifecycleMonitor.getLifecycleState(new HttpServiceGetLifecycleStateCallback(this, anonymousClass1));
        }
    }

    public void disablePauseRequestsOnDemand() {
        this.pauseRequestsOnDemand.set(false);
    }

    public void enableBackgroundRequests() {
        this.allowBackgroundRequests.set(true);
    }

    public void enablePauseRequestsOnDemand() {
        this.pauseRequestsOnDemand.set(true);
        if (this.lifecycleMonitor == null || this.lifecycleObserver == null) {
            this.lifecycleMonitor = LifecycleMonitorFactory.getOrCreate();
            AnonymousClass1 anonymousClass1 = null;
            HttpServiceLifecycleObserver httpServiceLifecycleObserver = new HttpServiceLifecycleObserver(this, anonymousClass1);
            this.lifecycleObserver = httpServiceLifecycleObserver;
            this.lifecycleMonitor.registerObserver(httpServiceLifecycleObserver);
            this.lifecycleMonitor.getLifecycleState(new HttpServiceGetLifecycleStateCallback(this, anonymousClass1));
        }
    }

    public void removeCall(long j) {
        this.pendingCalls.remove(Long.valueOf(j));
        this.foregroundRequests.remove(Long.valueOf(j));
    }

    @Override
    public long request(Request request, RequestObserver requestObserver) {
        RequestObserver requestObserver2;
        Request request2;
        HttpCallback httpCallback;
        long newId = IdGenerator.getNewId();
        if (this.offline.get()) {
            Expected createError = ExpectedFactory.createError(new HttpRequestError(HttpRequestErrorType.CONNECTION_ERROR, "Not connected"));
            ((ThreadPoolExecutor) this.httpClient.get().w.a()).execute(new b(this, newId, requestObserver, request, createError, 0));
            return newId;
        }
        MapboxOkHttpService mapboxOkHttpService = this;
        int i = AnonymousClass1.$SwitchMap$com$mapbox$common$NetworkRestriction[request.getNetworkRestriction().ordinal()];
        if (i != 2) {
            if (i == 3) {
                Expected createError2 = ExpectedFactory.createError(new HttpRequestError(HttpRequestErrorType.CONNECTION_ERROR, "Not allowed"));
                ExecutorService a = mapboxOkHttpService.httpClient.get().w.a();
                ((ThreadPoolExecutor) a).execute(new b(mapboxOkHttpService, newId, requestObserver, request, createError2, 2));
                return newId;
            }
        } else if (mapboxOkHttpService.networkStatus == NetworkStatus.REACHABLE_VIA_WWAN) {
            Expected createError3 = ExpectedFactory.createError(new HttpRequestError(HttpRequestErrorType.CONNECTION_ERROR, "No connection satisfies network restriction"));
            ExecutorService a2 = mapboxOkHttpService.httpClient.get().w.a();
            ((ThreadPoolExecutor) a2).execute(new b(mapboxOkHttpService, newId, requestObserver, request, createError3, 1));
            return newId;
        }
        try {
            j c = mapboxOkHttpService.httpClient.get().c(buildRequest(request));
            c.Y.g(request.getTimeout(), TimeUnit.SECONDS);
            try {
                httpCallback = new HttpCallback(newId, request, requestObserver, mapboxOkHttpService);
                request2 = request;
                requestObserver2 = requestObserver;
                newId = newId;
                mapboxOkHttpService = this;
            } catch (Exception e) {
                e = e;
                request2 = request;
                requestObserver2 = requestObserver;
                newId = newId;
                mapboxOkHttpService = mapboxOkHttpService;
            }
            try {
                CallbackWrapper callbackWrapper = new CallbackWrapper(mapboxOkHttpService, newId, c, httpCallback);
                mapboxOkHttpService.pendingCalls.put(Long.valueOf(newId), callbackWrapper);
                if (!needToPauseRequest(request2)) {
                    FirebasePerfOkHttpClient.enqueue(c, callbackWrapper);
                    return newId;
                }
                mapboxOkHttpService.foregroundRequests.put(Long.valueOf(newId), new QueuedRequest(c, callbackWrapper));
                return newId;
            } catch (Exception e2) {
                e = e2;
                Expected createError4 = ExpectedFactory.createError(new HttpRequestError(HttpRequestErrorType.OTHER_ERROR, String.valueOf(e.getMessage())));
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) mapboxOkHttpService.httpClient.get().w.a();
                threadPoolExecutor.execute(new b(mapboxOkHttpService, newId, requestObserver2, request2, createError4, 3));
                return newId;
            }
        } catch (Exception e3) {
            e = e3;
            requestObserver2 = requestObserver;
            request2 = request;
        }
    }

    @Override
    public void setMaxRequestsPerHost(byte b) {
        this.httpClient.setMaxRequestsPerHost(b);
    }

    @Override
    public boolean supportsKeepCompression() {
        return true;
    }
}