package defpackage;

import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.JsonReader;
import com.google.firebase.installations.FirebaseInstallationsException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.json.JSONException;
import org.json.JSONObject;

/* renamed from: ᵨᵺᵕ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C3769 {

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final C4395 f14087 = new C4395(2);

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final Context f14088;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final InterfaceC3290 f14089;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static final Pattern f14086 = Pattern.compile("[0-9]+s");

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public static final Charset f14085 = Charset.forName("UTF-8");

    public C3769(Context context, InterfaceC3290 interfaceC3290) {
        this.f14088 = context;
        this.f14089 = interfaceC3290;
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public static C2637 m8421(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream = httpURLConnection.getInputStream();
        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, f14085));
        C4782 c4782M10401 = C4948.m10401();
        jsonReader.beginObject();
        String strNextString = null;
        String strNextString2 = null;
        String strNextString3 = null;
        C4948 c4948 = null;
        while (jsonReader.hasNext()) {
            String strNextName = jsonReader.nextName();
            if (strNextName.equals("name")) {
                strNextString = jsonReader.nextString();
            } else if (strNextName.equals("fid")) {
                strNextString2 = jsonReader.nextString();
            } else if (strNextName.equals("refreshToken")) {
                strNextString3 = jsonReader.nextString();
            } else if (strNextName.equals("authToken")) {
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    String strNextName2 = jsonReader.nextName();
                    if (strNextName2.equals("token")) {
                        c4782M10401.f17768 = jsonReader.nextString();
                    } else if (strNextName2.equals("expiresIn")) {
                        c4782M10401.f17767 = Long.valueOf(m8428(jsonReader.nextString()));
                    } else {
                        jsonReader.skipValue();
                    }
                }
                C4948 c4948M10102 = c4782M10401.m10102();
                jsonReader.endObject();
                c4948 = c4948M10102;
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        inputStream.close();
        return new C2637(strNextString, strNextString2, strNextString3, c4948, 1);
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public static void m8422(HttpURLConnection httpURLConnection, String str, String str2) throws JSONException, IOException {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("fid", str);
            jSONObject.put("appId", str2);
            jSONObject.put("authVersion", "FIS_v2");
            jSONObject.put("sdkVersion", "a:18.0.0");
            m8424(httpURLConnection, jSONObject.toString().getBytes("UTF-8"));
        } catch (JSONException e) {
            C2707.m6713(e);
        }
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static C4948 m8423(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream = httpURLConnection.getInputStream();
        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, f14085));
        C4782 c4782M10401 = C4948.m10401();
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String strNextName = jsonReader.nextName();
            if (strNextName.equals("token")) {
                c4782M10401.f17768 = jsonReader.nextString();
            } else if (strNextName.equals("expiresIn")) {
                c4782M10401.f17767 = Long.valueOf(m8428(jsonReader.nextString()));
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        inputStream.close();
        c4782M10401.f17765 = 1;
        return c4782M10401.m10102();
    }

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public static void m8424(HttpURLConnection httpURLConnection, byte[] bArr) throws IOException {
        OutputStream outputStream = httpURLConnection.getOutputStream();
        if (outputStream == null) {
            C0910.m3045("Cannot send request to FIS servers. No OutputStream available.");
            return;
        }
        GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream);
        try {
            gZIPOutputStream.write(bArr);
        } finally {
            try {
                gZIPOutputStream.close();
                outputStream.close();
            } catch (IOException unused) {
            }
        }
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static URL m8425(String str) {
        try {
            return new URL("https://firebaseinstallations.googleapis.com/v1/".concat(str));
        } catch (MalformedURLException e) {
            throw new FirebaseInstallationsException(e.getMessage());
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public static void m8426(HttpURLConnection httpURLConnection, String str) throws IOException {
        InputStream errorStream = httpURLConnection.getErrorStream();
        String str2 = null;
        if (errorStream != null) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(errorStream, f14085));
            try {
                StringBuilder sb = new StringBuilder();
                while (true) {
                    String line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    sb.append(line);
                    sb.append('\n');
                }
                str2 = String.format("Error when communicating with the Firebase Installations server API. HTTP response: [%d %s: %s]", Integer.valueOf(httpURLConnection.getResponseCode()), httpURLConnection.getResponseMessage(), sb);
            } catch (IOException unused) {
            } catch (Throwable th) {
                try {
                    bufferedReader.close();
                } catch (IOException unused2) {
                }
                throw th;
            }
            try {
                bufferedReader.close();
            } catch (IOException unused3) {
            }
        }
        if (TextUtils.isEmpty(str2)) {
            return;
        }
        TextUtils.isEmpty(str);
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public static void m8427(HttpURLConnection httpURLConnection) throws JSONException, IOException {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("sdkVersion", "a:18.0.0");
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("installation", jSONObject);
            m8424(httpURLConnection, jSONObject2.toString().getBytes("UTF-8"));
        } catch (JSONException e) {
            C2707.m6713(e);
        }
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static long m8428(String str) {
        ᵩʛʽ.ˏᵙ("Invalid Expiration Timestamp.", f14086.matcher(str).matches());
        if (str == null || str.length() == 0) {
            return 0L;
        }
        return Long.parseLong(str.substring(0, str.length() - 1));
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final HttpURLConnection m8429(URL url, String str) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.addRequestProperty("Content-Type", "application/json");
            httpURLConnection.addRequestProperty("Accept", "application/json");
            httpURLConnection.addRequestProperty("Content-Encoding", "gzip");
            httpURLConnection.addRequestProperty("Cache-Control", "no-cache");
            Context context = this.f14088;
            httpURLConnection.addRequestProperty("X-Android-Package", context.getPackageName());
            InterfaceC1056 interfaceC1056 = (InterfaceC1056) this.f14089.get();
            if (interfaceC1056 != null) {
                try {
                    httpURLConnection.addRequestProperty("x-firebase-client", (String) ʼٴ.ᵴᵳ(((C1054) interfaceC1056).m3423()));
                } catch (InterruptedException unused) {
                    Thread.currentThread().interrupt();
                } catch (ExecutionException unused2) {
                }
            }
            String str2 = null;
            try {
                byte[] bArr = ᵳʿˉ.ᵨᵳʝ(context, context.getPackageName());
                if (bArr == null) {
                    context.getPackageName();
                } else {
                    str2 = ᵭʝʜ.ᵻᵢﹳᵰ(bArr);
                }
            } catch (PackageManager.NameNotFoundException unused3) {
                context.getPackageName();
            }
            httpURLConnection.addRequestProperty("X-Android-Cert", str2);
            httpURLConnection.addRequestProperty("x-goog-api-key", str);
            return httpURLConnection;
        } catch (IOException unused4) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
        }
    }
}
