导航菜单

页面标题

页面副标题

CallApp v2.226 - HttpUtils.java 源代码

正在查看: CallApp v2.226 应用的 HttpUtils.java JAVA 源代码文件

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


package com.callapp.contacts.util.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.StatFs;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.callapp.common.util.UrlUtils;
import com.callapp.contacts.CallAppApplication;
import com.callapp.contacts.R;
import com.callapp.contacts.manager.CallAppRemoteConfigManager;
import com.callapp.contacts.manager.ExceptionManager;
import com.callapp.contacts.manager.Singletons;
import com.callapp.contacts.manager.cache.CacheManager;
import com.callapp.contacts.manager.preferences.Prefs;
import com.callapp.contacts.util.CLog;
import com.callapp.contacts.util.CrashlyticsUtils;
import com.callapp.contacts.util.IoUtils;
import com.callapp.contacts.util.http.HttpRequestParams;
import com.callapp.framework.util.CollectionUtils;
import com.callapp.framework.util.StringUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.CertificatePinner;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import org.json.JSONArray;
import org.json.JSONObject;
import s4.y;

public class HttpUtils {
    public static final OkHttpClient e;
    public static final CertificatePinner g;
    public static final MediaType a = MediaType.parse("application/json; charset=utf-8");
    public static final UserAgentInterceptor b = new UserAgentInterceptor(null);
    public static final OkHttpExceptionInterceptor c = new OkHttpExceptionInterceptor(0);
    public static final String d = "=";
    public static final OkHttpClient f = new OkHttpClient();

    public interface HttpResponseHandler {
        void a(String str, Response response);

        void b(String str, Response response);
    }

    public static abstract class HttpResponseHandlerImpl implements HttpResponseHandler {
        public int a;

        @Override
        public void a(String str, Response response) {
            if (response != null) {
                response.body().string();
                this.a = response.code();
            }
        }

        @Override
        public void b(String str, Response response) {
            if (response != null) {
                this.a = response.code();
            }
        }

        public int getResponseCode() {
            return this.a;
        }
    }

    public static class OkHttpExceptionInterceptor implements Interceptor {
        public OkHttpExceptionInterceptor(int i) {
            this();
        }

        public final Response intercept(Interceptor.Chain chain) {
            try {
                return chain.proceed(chain.request());
            } catch (Throwable th) {
                if (th instanceof IOException) {
                    throw th;
                }
                throw new IOException(th);
            }
        }

        private OkHttpExceptionInterceptor() {
        }
    }

    public static class UserAgentInterceptor implements Interceptor {
        public String a;

        public UserAgentInterceptor(String str) {
            this.a = str;
        }

        public final Response intercept(Interceptor.Chain chain) {
            if (this.a == null) {
                this.a = HttpUtils.j(CallAppApplication.get());
            }
            Request request = chain.request();
            return request.header("User-Agent") == null ? chain.proceed(request.newBuilder().addHeader("User-Agent", this.a).build()) : chain.proceed(request);
        }
    }

    static {
        g = null;
        if (!CallAppRemoteConfigManager.get().b("DisableSSLPinning")) {
            try {
                JSONObject jSONObject = new JSONObject(CallAppRemoteConfigManager.get().d("domain_certs_pinning"));
                CertificatePinner.Builder builder = new CertificatePinner.Builder();
                Iterator<String> keys = jSONObject.keys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    JSONArray jSONArray = jSONObject.getJSONArray(next);
                    if (jSONArray != null) {
                        for (int i = 0; i < jSONArray.length(); i++) {
                            builder.add(next, new String[]{jSONArray.getString(i)});
                        }
                    }
                }
                g = builder.build();
            } catch (Exception e2) {
                CLog.m("", e2);
                CrashlyticsUtils.b(e2);
            }
        }
        OkHttpClient.Builder builder2 = new OkHttpClient.Builder();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        OkHttpClient.Builder addNetworkInterceptor = builder2.connectTimeout(10000L, timeUnit).readTimeout(10000L, timeUnit).writeTimeout(10000L, timeUnit).addInterceptor(c).addInterceptor(b).addNetworkInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            public final void log(String str) {
                StringUtils.I(HttpUtils.class);
                CLog.a();
            }
        }).setLevel(Prefs.q.get().booleanValue() ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE));
        CertificatePinner certificatePinner = g;
        if (certificatePinner != null) {
            addNetworkInterceptor.certificatePinner(certificatePinner);
        }
        e = addNetworkInterceptor.build();
    }

    public static synchronized boolean a() {
        boolean z;
        NetworkInfo activeNetworkInfo;
        synchronized (HttpUtils.class) {
            ConnectivityManager connectivityManager = (ConnectivityManager) Singletons.b("connectivity");
            if (connectivityManager != null && (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) != null && activeNetworkInfo.isAvailable()) {
                z = activeNetworkInfo.isConnected();
            }
        }
        return z;
    }

    public static boolean b() {
        return a();
    }

    public static void c(Response response) {
        if (response == null || response.body() == null) {
            return;
        }
        try {
            response.body().close();
        } catch (Exception unused) {
        }
    }

    public static Response d(Request request, HttpRequestParams httpRequestParams) {
        int timeoutOverride = httpRequestParams.getTimeoutOverride();
        OkHttpClient okHttpClient = e;
        if (timeoutOverride < 0) {
            return okHttpClient.newCall(request).execute();
        }
        OkHttpClient.Builder newBuilder = okHttpClient.newBuilder();
        long timeoutOverride2 = httpRequestParams.getTimeoutOverride();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        return newBuilder.connectTimeout(timeoutOverride2, timeUnit).readTimeout(httpRequestParams.getTimeoutOverride(), timeUnit).writeTimeout(httpRequestParams.getTimeoutOverride(), timeUnit).build().newCall(request).execute();
    }

    public static DownloadedFile e(File file, String str) {
        Response response;
        Response response2;
        FileOutputStream fileOutputStream;
        Response response3 = null;
        if (a()) {
            try {
                try {
                    response = f(new Request.Builder().url(str).build(), new HttpRequestParams.HttpRequestParamsBuilder(str).a());
                    if (response != null) {
                        try {
                            if (response.isSuccessful() && response.body() != null && response.body().byteStream() != null) {
                                try {
                                    fileOutputStream = new FileOutputStream(file);
                                    try {
                                        InputStream byteStream = response.body().byteStream();
                                        byteStream.getClass();
                                        byte[] bArr = new byte[4096];
                                        long j = 0;
                                        while (true) {
                                            int read = byteStream.read(bArr);
                                            if (read == -1) {
                                                break;
                                            }
                                            fileOutputStream.write(bArr, 0, read);
                                            j += read;
                                        }
                                        boolean z = j == response.body().contentLength();
                                        IoUtils.c(fileOutputStream);
                                        DownloadedFile downloadedFile = new DownloadedFile(file, z);
                                        c(response);
                                        return downloadedFile;
                                    } catch (Throwable th) {
                                        th = th;
                                        IoUtils.c(fileOutputStream);
                                        throw th;
                                    }
                                } catch (Throwable th2) {
                                    th = th2;
                                    fileOutputStream = null;
                                }
                            }
                        } catch (FileNotFoundException e2) {
                            e = e2;
                            File cacheFolder = IoUtils.getCacheFolder();
                            if (((cacheFolder == null || !cacheFolder.isDirectory()) ? -1.0f : IoUtils.k(new StatFs(cacheFolder.getPath()), true)) > 50.0f) {
                                CLog.t(StringUtils.I(HttpUtils.class), e, "IOException in downloadURL(%s)", str);
                            }
                            IoUtils.q();
                        } catch (InterruptedIOException unused) {
                        } catch (RuntimeException e3) {
                            e = e3;
                            CLog.i(StringUtils.I(HttpUtils.class), e, "RuntimeException in downloadURL(%s)", str);
                        } catch (UnknownHostException e4) {
                            e = e4;
                            ExceptionManager.get().b(HttpUtils.class, e, "IOException in downloadURL(%s)", str);
                        } catch (IOException e5) {
                            e = e5;
                            CLog.i(StringUtils.I(HttpUtils.class), e, "IOException in downloadURL(%s)", str);
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                    response3 = response2;
                    c(response3);
                    throw th;
                }
            } catch (FileNotFoundException e6) {
                e = e6;
                response = null;
            } catch (InterruptedIOException unused2) {
                response = null;
            } catch (UnknownHostException e7) {
                e = e7;
                response = null;
            } catch (IOException e8) {
                e = e8;
                response = null;
            } catch (RuntimeException e9) {
                e = e9;
                response = null;
            } catch (Throwable th4) {
                th = th4;
                c(response3);
                throw th;
            }
            c(response);
        }
        return new DownloadedFile(null, false);
    }

    public static Response f(Request request, HttpRequestParams httpRequestParams) {
        if (!a()) {
            return null;
        }
        Response d2 = d(request, httpRequestParams);
        if (httpRequestParams.getIncludeAllResponseCodes() || (d2 != null && d2.isSuccessful())) {
            if (httpRequestParams.getHandler() != null) {
                httpRequestParams.getHandler().b(httpRequestParams.getUrl(), d2);
            }
        } else if (httpRequestParams.getHandler() != null) {
            httpRequestParams.getHandler().a(httpRequestParams.getUrl(), d2);
            return d2;
        }
        return d2;
    }

    public static void g(HttpRequestParams httpRequestParams) {
        try {
            h(httpRequestParams);
        } catch (UnknownHostException e2) {
            ExceptionManager.get().b(HttpUtils.class, e2, "IOException in readInputStream", new Object[0]);
        } catch (IOException e3) {
            CLog.g(StringUtils.I(HttpUtils.class), "IOException in readInputStream", e3);
        }
    }

    public static String getAWSCredentialsUrl() {
        try {
            return getCallappServerPrefix() + "sf?myp=" + URLEncoder.encode(Prefs.a1.get(), "UTF-8") + "&tk=" + Prefs.e1.get() + "&cvc=" + CallAppApplication.get().getVersionCode();
        } catch (UnsupportedEncodingException e2) {
            CLog.e(HttpUtils.class, e2);
            return null;
        }
    }

    public static String getCallAppDomain() {
        return CallAppRemoteConfigManager.get().d("callappDomain");
    }

    public static String getCallAppServerHost() {
        return "s." + getCallAppDomain();
    }

    public static String getCallappServerPrefix() {
        return "https://" + getCallAppServerHost() + "/callapp-server/";
    }

    public static OkHttpClient getExternalClient() {
        return f;
    }

    public static boolean h(HttpRequestParams httpRequestParams) {
        Response response;
        boolean z;
        try {
            response = f(k(httpRequestParams, new Request.Builder()).url(httpRequestParams.getUrl()).build(), httpRequestParams);
            if (response != null) {
                try {
                    if (response.isSuccessful()) {
                        z = true;
                        c(response);
                        return z;
                    }
                } catch (Throwable th) {
                    th = th;
                    c(response);
                    throw th;
                }
            }
            z = false;
            c(response);
            return z;
        } catch (Throwable th2) {
            th = th2;
            response = null;
        }
    }

    public static String i(HttpRequestParams httpRequestParams) {
        String url = httpRequestParams.getUrl();
        String str = null;
        if (StringUtils.t(url)) {
            return null;
        }
        Request build = k(httpRequestParams, new Request.Builder()).url(url).build();
        if (httpRequestParams.getHandler() != null) {
            CLog.f(StringUtils.I(HttpUtils.class), "Handler will not be called !");
        }
        try {
            String d2 = d(build, httpRequestParams);
            if (d2 != null) {
                try {
                    if (d2.isSuccessful()) {
                        str = d2.body().string();
                    }
                } catch (Throwable th) {
                    th = th;
                    str = d2;
                    c(str);
                    throw th;
                }
            }
            c(d2);
            return str;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public static boolean isWiFiConnected() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) CallAppApplication.get().getSystemService("connectivity")).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting() && activeNetworkInfo.getType() == 1;
    }

    public static String j(CallAppApplication callAppApplication) {
        String str;
        String str2 = null;
        try {
        } catch (Exception e2) {
            CLog.f(StringUtils.I(HttpUtils.class), y.c(e2.getClass().getName(), " in getDeviceUserAgentString(): ", e2.getMessage()));
        }
        if (CallAppApplication.get().isUnitTestMode()) {
            Constructor declaredConstructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class);
            declaredConstructor.setAccessible(true);
            str = ((WebSettings) declaredConstructor.newInstance(callAppApplication, null)).getUserAgentString();
        } else {
            str = (String) CacheManager.get().c("defaultUserAgentCacheKey", String.class, false, false);
            if (!StringUtils.x(str)) {
                str2 = WebSettings.getDefaultUserAgent(callAppApplication);
                CacheManager.get().h(String.class, "defaultUserAgentCacheKey", str2, R.integer.default_user_agent_cache_minutes);
                str = str2;
            }
        }
        return StringUtils.t(str) ? "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" : str;
    }

    public static Request.Builder k(HttpRequestParams httpRequestParams, Request.Builder builder) {
        if (CollectionUtils.i(httpRequestParams.getAdditionalHeaders())) {
            for (Map.Entry<String, String> entry : httpRequestParams.getAdditionalHeaders().entrySet()) {
                builder = builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        if (CollectionUtils.i(httpRequestParams.getModifiedHeaders())) {
            for (Map.Entry<String, String> entry2 : httpRequestParams.getModifiedHeaders().entrySet()) {
                builder = builder.header(entry2.getKey(), entry2.getValue());
            }
        }
        return !StringUtils.t(httpRequestParams.getUserAgentOverride()) ? builder.header("User-Agent", httpRequestParams.getUserAgentOverride()) : builder;
    }

    public static Response l(File file, String str) {
        StringBuilder sb2 = new StringBuilder();
        sb2.append(getCallappServerPrefix() + str + "?v=1&myp=" + UrlUtils.a(Prefs.a1.get()));
        sb2.append("&tk=");
        sb2.append(Prefs.e1.get());
        sb2.append("&cvc=");
        sb2.append(CallAppApplication.get().getVersionCode());
        Request build = new Request.Builder().url(sb2.toString()).post(RequestBody.Companion.create(file, a)).header("Content-Encoding", "gzip").build();
        OkHttpClient.Builder newBuilder = e.newBuilder();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        return newBuilder.connectTimeout(0L, timeUnit).readTimeout(0L, timeUnit).writeTimeout(0L, timeUnit).build().newCall(build).execute();
    }

    public static void m(HttpRequestParams httpRequestParams, String str) {
        try {
            Response response = null;
            try {
                response = f(k(httpRequestParams, new Request.Builder()).url(httpRequestParams.getUrl()).post(RequestBody.create((MediaType) null, str)).build(), httpRequestParams);
                if (response != null) {
                    response.isSuccessful();
                }
            } finally {
                c(response);
            }
        } catch (IOException e2) {
            CLog.g(StringUtils.I(HttpUtils.class), "IOException in readString", e2);
        }
    }

    public static int n(HttpRequestParams httpRequestParams) {
        Response response = null;
        try {
            try {
                UTF8FormEncodingBuilder uTF8FormEncodingBuilder = new UTF8FormEncodingBuilder();
                if (CollectionUtils.i(httpRequestParams.getFormParams())) {
                    for (Map.Entry<String, String> entry : httpRequestParams.getFormParams().entrySet()) {
                        String key = entry.getKey();
                        String value = entry.getValue();
                        if (value != null) {
                            uTF8FormEncodingBuilder.a(key, value);
                        } else {
                            Throwable th = new Throwable("postEncodedFormAndReadInputStream null value. Name: " + key + ", url: " + httpRequestParams.getUrl());
                            CLog.m("", th);
                            CrashlyticsUtils.b(th);
                        }
                    }
                }
                response = f(k(httpRequestParams, new Request.Builder()).url(httpRequestParams.getUrl()).post(uTF8FormEncodingBuilder.b()).build(), httpRequestParams);
                int code = response != null ? response.code() : 0;
                c(response);
                return code;
            } catch (IOException e2) {
                CLog.e(HttpUtils.class, e2);
                c(response);
                return 0;
            }
        } catch (Throwable th2) {
            c(response);
            throw th2;
        }
    }

    public static java.lang.String o(java.lang.String r9) {
        throw new UnsupportedOperationException("Method not decompiled: com.callapp.contacts.util.http.HttpUtils.o(java.lang.String):java.lang.String");
    }
}