package com.google.firebase.messaging;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import java.util.ArrayDeque;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import v1.C1074a;
import x1.ThreadFactoryC1100a;

/* JADX INFO: loaded from: classes.dex */
public final class J implements ServiceConnection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f5863a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Intent f5864b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ScheduledThreadPoolExecutor f5865c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ArrayDeque f5866d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public H f5867e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f5868f;

    public J(Context context) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactoryC1100a("Firebase-FirebaseInstanceIdServiceConnection"));
        scheduledThreadPoolExecutor.setKeepAliveTime(40L, TimeUnit.SECONDS);
        scheduledThreadPoolExecutor.allowCoreThreadTimeOut(true);
        this.f5866d = new ArrayDeque();
        this.f5868f = false;
        Context applicationContext = context.getApplicationContext();
        this.f5863a = applicationContext;
        this.f5864b = new Intent("com.google.firebase.MESSAGING_EVENT").setPackage(applicationContext.getPackageName());
        this.f5865c = scheduledThreadPoolExecutor;
    }

    public final synchronized void a() {
        try {
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                Log.d("FirebaseMessaging", "flush queue called");
            }
            while (!this.f5866d.isEmpty()) {
                if (Log.isLoggable("FirebaseMessaging", 3)) {
                    Log.d("FirebaseMessaging", "found intent to be delivered");
                }
                H h3 = this.f5867e;
                if (h3 == null || !h3.isBinderAlive()) {
                    c();
                    return;
                }
                if (Log.isLoggable("FirebaseMessaging", 3)) {
                    Log.d("FirebaseMessaging", "binder is alive, sending the intent.");
                }
                this.f5867e.a((I) this.f5866d.poll());
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized P1.o b(Intent intent) {
        I i;
        try {
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                Log.d("FirebaseMessaging", "new intent queued in the bind-strategy delivery");
            }
            i = new I(intent);
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.f5865c;
            i.f5862b.f2505a.b(scheduledThreadPoolExecutor, new N2.a(9, scheduledThreadPoolExecutor.schedule(new androidx.lifecycle.G(4, i), 20L, TimeUnit.SECONDS)));
            this.f5866d.add(i);
            a();
        } catch (Throwable th) {
            throw th;
        }
        return i.f5862b.f2505a;
    }

    public final void c() {
        if (Log.isLoggable("FirebaseMessaging", 3)) {
            StringBuilder sb = new StringBuilder("binder is dead. start connection? ");
            sb.append(!this.f5868f);
            Log.d("FirebaseMessaging", sb.toString());
        }
        if (this.f5868f) {
            return;
        }
        this.f5868f = true;
        try {
            if (C1074a.b().a(this.f5863a, this.f5864b, this, 65)) {
                return;
            } else {
                Log.e("FirebaseMessaging", "binding to the service failed");
            }
        } catch (SecurityException e2) {
            Log.e("FirebaseMessaging", "Exception while binding the service", e2);
        }
        this.f5868f = false;
        while (true) {
            ArrayDeque arrayDeque = this.f5866d;
            if (arrayDeque.isEmpty()) {
                return;
            } else {
                ((I) arrayDeque.poll()).f5862b.d(null);
            }
        }
    }

    @Override // android.content.ServiceConnection
    public final synchronized void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        try {
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                Log.d("FirebaseMessaging", "onServiceConnected: " + componentName);
            }
            this.f5868f = false;
            if (iBinder instanceof H) {
                this.f5867e = (H) iBinder;
                a();
                return;
            }
            Log.e("FirebaseMessaging", "Invalid service connection: " + iBinder);
            while (true) {
                ArrayDeque arrayDeque = this.f5866d;
                if (arrayDeque.isEmpty()) {
                    return;
                } else {
                    ((I) arrayDeque.poll()).f5862b.d(null);
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        if (Log.isLoggable("FirebaseMessaging", 3)) {
            Log.d("FirebaseMessaging", "onServiceDisconnected: " + componentName);
        }
        a();
    }
}
