package p138.p176.p291.p323.p325;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.JsonReader;
import android.util.Log;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p253.C4310;
import p138.p176.p177.p242.p246.p253.C4313;
import p138.p176.p177.p242.p246.p255.C4322;
import p138.p176.p291.p319.InterfaceC5902;
import p138.p176.p291.p323.p325.AbstractC5995;
import p138.p176.p291.p323.p325.AbstractC5997;
import p138.p176.p291.p323.p325.C5991;
import p138.p176.p291.p327.InterfaceC6006;

/* JADX INFO: renamed from: ދ.ހ.ށ.ފ.ގ.ހ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C5994 {

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public static final Pattern f16859 = Pattern.compile("[0-9]+s");

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public static final Charset f16860 = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final Context f16861;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final InterfaceC6006 f16862;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final InterfaceC5902 f16863;

    public C5994(Context context, InterfaceC6006 interfaceC6006, InterfaceC5902 interfaceC5902) {
        this.f16861 = context;
        this.f16862 = interfaceC6006;
        this.f16863 = interfaceC5902;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static long m9334(String str) {
        if (!f16859.matcher(str).matches()) {
            throw new IllegalArgumentException("Invalid Expiration Timestamp.");
        }
        if (str == null || str.length() == 0) {
            return 0L;
        }
        return Long.parseLong(str.substring(0, str.length() - 1));
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static JSONObject m9335() throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("sdkVersion", "a:16.2.1");
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("installation", jSONObject);
        return jSONObject2;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static JSONObject m9336(String str, String str2) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("fid", str);
        jSONObject.put("appId", str2);
        jSONObject.put("authVersion", "FIS_v2");
        jSONObject.put("sdkVersion", "a:16.2.1");
        return jSONObject;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final HttpURLConnection m9337(URL url, String str) {
        MessageDigest messageDigestM7210;
        InterfaceC5902.EnumC5903 enumC5903Mo9229;
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(10000);
        httpURLConnection.setReadTimeout(10000);
        httpURLConnection.addRequestProperty("Content-Type", "application/json");
        httpURLConnection.addRequestProperty("Accept", "application/json");
        httpURLConnection.addRequestProperty("Content-Encoding", "gzip");
        httpURLConnection.addRequestProperty("X-Android-Package", this.f16861.getPackageName());
        InterfaceC5902 interfaceC5902 = this.f16863;
        if (interfaceC5902 != null && this.f16862 != null && (enumC5903Mo9229 = interfaceC5902.mo9229("fire-installations-id")) != InterfaceC5902.EnumC5903.NONE) {
            httpURLConnection.addRequestProperty("x-firebase-client", this.f16862.mo9344());
            httpURLConnection.addRequestProperty("x-firebase-client-log-type", Integer.toString(enumC5903Mo9229.f16668));
        }
        String string = null;
        try {
            PackageInfo packageInfo = C4322.m7223(this.f16861).f13621.getPackageManager().getPackageInfo(this.f16861.getPackageName(), 64);
            Signature[] signatureArr = packageInfo.signatures;
            byte[] bArrDigest = (signatureArr == null || signatureArr.length != 1 || (messageDigestM7210 = C4310.m7210("SHA1")) == null) ? null : messageDigestM7210.digest(packageInfo.signatures[0].toByteArray());
            if (bArrDigest == null) {
                Log.e("ContentValues", "Could not get fingerprint hash for package: " + this.f16861.getPackageName());
            } else {
                int length = bArrDigest.length;
                StringBuilder sb = new StringBuilder(length << 1);
                for (int i = 0; i < length; i++) {
                    sb.append(C4313.f13604[(bArrDigest[i] & 240) >>> 4]);
                    sb.append(C4313.f13604[bArrDigest[i] & 15]);
                }
                string = sb.toString();
            }
        } catch (PackageManager.NameNotFoundException e) {
            StringBuilder sbM4889 = C2803.m4889("No such package: ");
            sbM4889.append(this.f16861.getPackageName());
            Log.e("ContentValues", sbM4889.toString(), e);
        }
        httpURLConnection.addRequestProperty("X-Android-Cert", string);
        httpURLConnection.addRequestProperty("x-goog-api-key", str);
        return httpURLConnection;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final AbstractC5995 m9338(HttpURLConnection httpURLConnection) throws IOException {
        JsonReader jsonReader = new JsonReader(new InputStreamReader(httpURLConnection.getInputStream(), f16860));
        AbstractC5997.AbstractC5998 abstractC5998M9342 = AbstractC5997.m9342();
        jsonReader.beginObject();
        String strNextString = null;
        String strNextString2 = null;
        String strNextString3 = null;
        AbstractC5997 abstractC5997Mo9333 = 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")) {
                        ((C5991.C5993) abstractC5998M9342).f16856 = jsonReader.nextString();
                    } else if (strNextName2.equals("expiresIn")) {
                        abstractC5998M9342.mo9332(m9334(jsonReader.nextString()));
                    } else {
                        jsonReader.skipValue();
                    }
                }
                abstractC5997Mo9333 = abstractC5998M9342.mo9333();
                jsonReader.endObject();
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        return new C5989(strNextString, strNextString2, strNextString3, abstractC5997Mo9333, AbstractC5995.EnumC5996.OK, null);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9339(HttpURLConnection httpURLConnection, String str, String str2) throws IOException {
        OutputStream outputStream = httpURLConnection.getOutputStream();
        if (outputStream == null) {
            throw new IOException("Cannot send CreateInstallation request to FIS. No OutputStream available.");
        }
        GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream);
        try {
            try {
                gZIPOutputStream.write(m9336(str, str2).toString().getBytes("UTF-8"));
            } catch (JSONException e) {
                throw new IllegalStateException(e);
            }
        } finally {
            gZIPOutputStream.close();
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final AbstractC5997 m9340(HttpURLConnection httpURLConnection) throws IOException {
        JsonReader jsonReader = new JsonReader(new InputStreamReader(httpURLConnection.getInputStream(), f16860));
        AbstractC5997.AbstractC5998 abstractC5998M9342 = AbstractC5997.m9342();
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String strNextName = jsonReader.nextName();
            if (strNextName.equals("token")) {
                ((C5991.C5993) abstractC5998M9342).f16856 = jsonReader.nextString();
            } else if (strNextName.equals("expiresIn")) {
                abstractC5998M9342.mo9332(m9334(jsonReader.nextString()));
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        C5991.C5993 c5993 = (C5991.C5993) abstractC5998M9342;
        c5993.f16858 = AbstractC5997.EnumC5999.OK;
        return c5993.mo9333();
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final void m9341(HttpURLConnection httpURLConnection) throws IOException {
        OutputStream outputStream = httpURLConnection.getOutputStream();
        if (outputStream == null) {
            throw new IOException("Cannot send GenerateAuthToken request to FIS. No OutputStream available.");
        }
        GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream);
        try {
            try {
                gZIPOutputStream.write(m9335().toString().getBytes("UTF-8"));
            } catch (JSONException e) {
                throw new IllegalStateException(e);
            }
        } finally {
            gZIPOutputStream.close();
        }
    }
}
