package com.appsflyer.internal;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.TrafficStats;
import com.appsflyer.AFFacebookDeferredDeeplink;
import com.appsflyer.AFLogger;
import com.appsflyer.AppsFlyerLibCore;
import com.appsflyer.ServerConfigHandler;
import com.facebook.GraphRequest;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;

public final class ab {

    /* renamed from: ʻ  reason: contains not printable characters */
    public boolean f1961;

    /* renamed from: ʽ  reason: contains not printable characters */
    public WeakReference<Context> f1962;

    /* renamed from: ˊ  reason: contains not printable characters */
    public boolean f1963;

    /* renamed from: ˋ  reason: contains not printable characters */
    public String f1964 = "";

    /* renamed from: ˎ  reason: contains not printable characters */
    public String f1965;

    /* renamed from: ˏ  reason: contains not printable characters */
    public boolean f1966 = true;

    /* renamed from: ॱ  reason: contains not printable characters */
    public boolean f1967 = true;

    public ab(Context context, boolean z) {
        this.f1962 = new WeakReference<>(context);
        this.f1963 = z;
    }

    /* JADX WARNING: Removed duplicated region for block: B:40:0x013b  */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x014f  */
    /* renamed from: ॱ  reason: contains not printable characters */
    public final HttpURLConnection m1163(String str) {
        HttpURLConnection httpURLConnection;
        Throwable th;
        if (this.f1963) {
            return null;
        }
        try {
            URL url = new URL(str);
            if (this.f1966) {
                if (ae.f1979 == null) {
                    ae.f1979 = new ae();
                }
                ae.f1979.m1180("server_request", url.toString(), this.f1965);
                int length = this.f1965.getBytes("UTF-8").length;
                StringBuilder sb = new StringBuilder("call = ");
                sb.append(url);
                sb.append("; size = ");
                sb.append(length);
                sb.append(" byte");
                sb.append(length > 1 ? "s" : "");
                sb.append("; body = ");
                sb.append(this.f1965);
                AFFacebookDeferredDeeplink.AnonymousClass3.m1075(sb.toString());
            }
            TrafficStats.setThreadStatsTag("AppsFlyer".hashCode());
            httpURLConnection = (HttpURLConnection) url.openConnection();
            try {
                httpURLConnection.setReadTimeout(30000);
                httpURLConnection.setConnectTimeout(30000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestProperty(GraphRequest.CONTENT_TYPE_HEADER, "application/json");
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                bufferedWriter.write(this.f1965);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                httpURLConnection.connect();
                int responseCode = httpURLConnection.getResponseCode();
                if (this.f1967) {
                    AppsFlyerLibCore.getInstance();
                    this.f1964 = AppsFlyerLibCore.m1128(httpURLConnection);
                }
                if (this.f1966) {
                    if (ae.f1979 == null) {
                        ae.f1979 = new ae();
                    }
                    ae.f1979.m1180("server_response", url.toString(), String.valueOf(responseCode), this.f1964);
                }
                if (responseCode == 200) {
                    AFLogger.afInfoLog("Status 200 ok");
                    Context context = this.f1962.get();
                    if (url.toString().startsWith(ServerConfigHandler.getUrl("https://%sregister.%s/api/v4.11/androidevent?buildnumber=5.0.0&app_id=")) && context != null) {
                        SharedPreferences.Editor edit = AppsFlyerLibCore.m1100(context).edit();
                        edit.putBoolean("sentRegisterRequestToAF", true);
                        edit.apply();
                        AFLogger.afDebugLog("Successfully registered for Uninstall Tracking");
                    }
                } else {
                    this.f1961 = true;
                }
            } catch (Throwable th2) {
                th = th2;
                AFLogger.afErrorLog("Error while calling ".concat(String.valueOf(str)), th);
                this.f1961 = true;
                if (this.f1961) {
                }
                return httpURLConnection;
            }
        } catch (Throwable th3) {
            th = th3;
            httpURLConnection = null;
            AFLogger.afErrorLog("Error while calling ".concat(String.valueOf(str)), th);
            this.f1961 = true;
            if (this.f1961) {
            }
            return httpURLConnection;
        }
        if (this.f1961) {
            StringBuilder sb2 = new StringBuilder("Connection error: ");
            sb2.append(this.f1964);
            AFLogger.afInfoLog(sb2.toString());
        } else {
            StringBuilder sb3 = new StringBuilder("Connection call succeeded: ");
            sb3.append(this.f1964);
            AFLogger.afInfoLog(sb3.toString());
        }
        return httpURLConnection;
    }
}
