package p138.p176.p291.p320;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.util.Log;
import com.google.firebase.iid.zzam;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.concurrent.GuardedBy;
import p037.p057.C1729;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p176.p177.p242.p270.AbstractC5062;
import p138.p176.p177.p242.p270.C5063;
import p138.p176.p291.p320.C5947;

/* JADX INFO: renamed from: ދ.ހ.ށ.ވ.ޓ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C5926 {

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public static int f16712;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public static PendingIntent f16713;

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

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

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public Messenger f16718;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public C5947 f16719;

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    @GuardedBy("responseCallbacks")
    public final C1729<String, C5063<Bundle>> f16714 = new C1729<>();

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public Messenger f16717 = new Messenger(new HandlerC5928(this, Looper.getMainLooper()));

    public C5926(Context context, C5920 c5920) {
        this.f16715 = context;
        this.f16716 = c5920;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static synchronized String m9258() {
        int i;
        i = f16712;
        f16712 = i + 1;
        return Integer.toString(i);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static synchronized void m9259(Context context, Intent intent) {
        if (f16713 == null) {
            Intent intent2 = new Intent();
            intent2.setPackage("com.google.example.invalidpackage");
            f16713 = PendingIntent.getBroadcast(context, 0, intent2, 0);
        }
        intent.putExtra("app", f16713);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final Bundle m9260(Bundle bundle) {
        if (this.f16716.m9254() < 12000000) {
            return m9263(bundle);
        }
        C5909 c5909M9239 = C5909.m9239(this.f16715);
        try {
            return (Bundle) C4303.m7016(c5909M9239.m9241(new C5921(c5909M9239.m9240(), bundle)));
        } catch (InterruptedException | ExecutionException e) {
            if (Log.isLoggable("FirebaseInstanceId", 3)) {
                String strValueOf = String.valueOf(e);
                StringBuilder sb = new StringBuilder(strValueOf.length() + 22);
                sb.append("Error making request: ");
                sb.append(strValueOf);
                Log.d("FirebaseInstanceId", sb.toString());
            }
            if ((e.getCause() instanceof zzam) && ((zzam) e.getCause()).f2158 == 4) {
                return m9263(bundle);
            }
            return null;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9261(Message message) {
        if (message != null) {
            Object obj = message.obj;
            if (obj instanceof Intent) {
                Intent intent = (Intent) obj;
                intent.setExtrasClassLoader(new C5947.C5948());
                if (intent.hasExtra("google.messenger")) {
                    Parcelable parcelableExtra = intent.getParcelableExtra("google.messenger");
                    if (parcelableExtra instanceof C5947) {
                        this.f16719 = (C5947) parcelableExtra;
                    }
                    if (parcelableExtra instanceof Messenger) {
                        this.f16718 = (Messenger) parcelableExtra;
                    }
                }
                Intent intent2 = (Intent) message.obj;
                String action = intent2.getAction();
                if (!"com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
                    if (Log.isLoggable("FirebaseInstanceId", 3)) {
                        String strValueOf = String.valueOf(action);
                        Log.d("FirebaseInstanceId", strValueOf.length() != 0 ? "Unexpected response action: ".concat(strValueOf) : new String("Unexpected response action: "));
                        return;
                    }
                    return;
                }
                String stringExtra = intent2.getStringExtra("registration_id");
                if (stringExtra == null) {
                    stringExtra = intent2.getStringExtra("unregistered");
                }
                if (stringExtra != null) {
                    Matcher matcher = Pattern.compile("\\|ID\\|([^|]+)\\|:?+(.*)").matcher(stringExtra);
                    if (!matcher.matches()) {
                        if (Log.isLoggable("FirebaseInstanceId", 3)) {
                            Log.d("FirebaseInstanceId", stringExtra.length() != 0 ? "Unexpected response string: ".concat(stringExtra) : new String("Unexpected response string: "));
                            return;
                        }
                        return;
                    } else {
                        String strGroup = matcher.group(1);
                        String strGroup2 = matcher.group(2);
                        Bundle extras = intent2.getExtras();
                        extras.putString("registration_id", strGroup2);
                        m9262(strGroup, extras);
                        return;
                    }
                }
                String stringExtra2 = intent2.getStringExtra("error");
                if (stringExtra2 == null) {
                    String strValueOf2 = String.valueOf(intent2.getExtras());
                    StringBuilder sb = new StringBuilder(strValueOf2.length() + 49);
                    sb.append("Unexpected response, no error or registration id ");
                    sb.append(strValueOf2);
                    Log.w("FirebaseInstanceId", sb.toString());
                    return;
                }
                if (Log.isLoggable("FirebaseInstanceId", 3)) {
                    Log.d("FirebaseInstanceId", stringExtra2.length() != 0 ? "Received InstanceID error ".concat(stringExtra2) : new String("Received InstanceID error "));
                }
                if (!stringExtra2.startsWith("|")) {
                    synchronized (this.f16714) {
                        for (int i = 0; i < this.f16714.f5620; i++) {
                            m9262(this.f16714.m3390(i), intent2.getExtras());
                        }
                    }
                    return;
                }
                String[] strArrSplit = stringExtra2.split("\\|");
                if (strArrSplit.length <= 2 || !"ID".equals(strArrSplit[1])) {
                    Log.w("FirebaseInstanceId", stringExtra2.length() != 0 ? "Unexpected structured response ".concat(stringExtra2) : new String("Unexpected structured response "));
                    return;
                }
                String str = strArrSplit[2];
                String strSubstring = strArrSplit[3];
                if (strSubstring.startsWith(":")) {
                    strSubstring = strSubstring.substring(1);
                }
                m9262(str, intent2.putExtra("error", strSubstring).getExtras());
                return;
            }
        }
        Log.w("FirebaseInstanceId", "Dropping invalid message");
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9262(String str, Bundle bundle) {
        synchronized (this.f16714) {
            C5063<Bundle> c5063Remove = this.f16714.remove(str);
            if (c5063Remove != null) {
                c5063Remove.f15214.m8562(bundle);
            } else {
                String strValueOf = String.valueOf(str);
                Log.w("FirebaseInstanceId", strValueOf.length() != 0 ? "Missing callback for ".concat(strValueOf) : new String("Missing callback for "));
            }
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final Bundle m9263(Bundle bundle) throws IOException {
        Bundle bundleM9264 = m9264(bundle);
        if (bundleM9264 == null || !bundleM9264.containsKey("google.messenger")) {
            return bundleM9264;
        }
        Bundle bundleM92642 = m9264(bundle);
        if (bundleM92642 == null || !bundleM92642.containsKey("google.messenger")) {
            return bundleM92642;
        }
        return null;
    }

    /* JADX WARN: Can't wrap try/catch for region: R(10:8|(1:10)(1:11)|12|(1:14)|15|(5:(7:17|(0)(2:28|(1:30)(1:31))|65|32|33|e8|37)|65|32|33|e8)|19|68|20|(1:22)(1:23)) */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x00b7, code lost:
    
        r1 = r1;
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x00be, code lost:
    
        if (android.util.Log.isLoggable("FirebaseInstanceId", r8) != false) goto L27;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x00c0, code lost:
    
        android.util.Log.d("FirebaseInstanceId", "Messenger failed, fallback to startService");
        r1 = r1;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:30:0x00cf  */
    /* JADX WARN: Removed duplicated region for block: B:31:0x00d5  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x00e9 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r1v0, types: [java.lang.Object, ދ.ހ.֏.ހ.އ.ޅ] */
    /* JADX WARN: Type inference failed for: r1v1 */
    /* JADX WARN: Type inference failed for: r1v10 */
    /* JADX WARN: Type inference failed for: r1v11 */
    /* JADX WARN: Type inference failed for: r1v12 */
    /* JADX WARN: Type inference failed for: r1v7 */
    /* JADX WARN: Type inference failed for: r1v9 */
    /* JADX WARN: Type inference failed for: r8v10 */
    /* JADX WARN: Type inference failed for: r8v21, types: [android.os.Bundle] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:30:0x00cf -> B:65:0x00da). Please report as a decompilation issue!!! */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:31:0x00d5 -> B:65:0x00da). Please report as a decompilation issue!!! */
    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bundle m9264(Bundle bundle) throws IOException {
        C5063 c5063;
        String strM9258 = m9258();
        ?? c50632 = new C5063();
        synchronized (this.f16714) {
            this.f16714.put(strM9258, c50632);
        }
        if (this.f16716.m9250() == 0) {
            throw new IOException("MISSING_INSTANCEID_SERVICE");
        }
        Intent intent = new Intent();
        intent.setPackage("com.google.android.gms");
        if (this.f16716.m9250() == 2) {
            intent.setAction("com.google.iid.TOKEN_REQUEST");
        } else {
            intent.setAction("com.google.android.c2dm.intent.REGISTER");
        }
        intent.putExtras(bundle);
        m9259(this.f16715, intent);
        StringBuilder sb = new StringBuilder(String.valueOf(strM9258).length() + 5);
        sb.append("|ID|");
        sb.append(strM9258);
        sb.append("|");
        intent.putExtra("kid", sb.toString());
        int i = 3;
        if (Log.isLoggable("FirebaseInstanceId", 3)) {
            String strValueOf = String.valueOf(intent.getExtras());
            StringBuilder sb2 = new StringBuilder(strValueOf.length() + 8);
            sb2.append("Sending ");
            sb2.append(strValueOf);
            Log.d("FirebaseInstanceId", sb2.toString());
        }
        intent.putExtra("google.messenger", this.f16717);
        try {
            try {
                if (this.f16718 == null) {
                    ?? r1 = c50632;
                    if (this.f16719 == null) {
                        if (this.f16716.m9250() != 2) {
                            this.f16715.sendBroadcast(intent);
                            c5063 = r1;
                        } else {
                            this.f16715.startService(intent);
                            c5063 = r1;
                        }
                    }
                    AbstractC5062 abstractC5062 = c5063.f15214;
                    c50632 = 30000;
                    i = (Bundle) C4303.m7017(abstractC5062, 30000L, TimeUnit.MILLISECONDS);
                    synchronized (this.f16714) {
                        this.f16714.remove(strM9258);
                    }
                    return i;
                }
                AbstractC5062 abstractC50622 = c5063.f15214;
                c50632 = 30000;
                i = (Bundle) C4303.m7017(abstractC50622, 30000L, TimeUnit.MILLISECONDS);
                synchronized (this.f16714) {
                }
            } catch (Throwable th) {
                synchronized (this.f16714) {
                    this.f16714.remove(strM9258);
                    throw th;
                }
            }
        } catch (InterruptedException | TimeoutException unused) {
            Log.w("FirebaseInstanceId", "No response");
            throw new IOException("TIMEOUT");
        } catch (ExecutionException e) {
            throw new IOException(e);
        }
        Message messageObtain = Message.obtain();
        messageObtain.obj = intent;
        if (this.f16718 != null) {
            this.f16718.send(messageObtain);
            c5063 = c50632;
        } else {
            this.f16719.m9290(messageObtain);
            c5063 = c50632;
        }
    }
}
