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 com.google.android.gms.common.stats.ConnectionTracker; import com.google.android.gms.common.util.concurrent.NamedThreadFactory; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; import com.google.firebase.messaging.j1; import java.util.ArrayDeque; import java.util.Queue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ class j1 implements ServiceConnection { /* renamed from: a, reason: collision with root package name */ private final Context f5600a; /* renamed from: b, reason: collision with root package name */ private final Intent f5601b; /* renamed from: c, reason: collision with root package name */ private final ScheduledExecutorService f5602c; /* renamed from: d, reason: collision with root package name */ private final Queue f5603d; /* renamed from: e, reason: collision with root package name */ private g1 f5604e; /* renamed from: f, reason: collision with root package name */ private boolean f5605f; static class a { /* renamed from: a, reason: collision with root package name */ final Intent f5606a; /* renamed from: b, reason: collision with root package name */ private final TaskCompletionSource f5607b = new TaskCompletionSource(); a(Intent intent) { this.f5606a = intent; } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void f() { Log.w("FirebaseMessaging", "Service took too long to process intent: " + this.f5606a.getAction() + " finishing."); d(); } void c(ScheduledExecutorService scheduledExecutorService) { final ScheduledFuture schedule = scheduledExecutorService.schedule(new Runnable() { // from class: com.google.firebase.messaging.h1 @Override // java.lang.Runnable public final void run() { j1.a.this.f(); } }, 20L, TimeUnit.SECONDS); e().addOnCompleteListener(scheduledExecutorService, new OnCompleteListener() { // from class: com.google.firebase.messaging.i1 @Override // com.google.android.gms.tasks.OnCompleteListener public final void onComplete(Task task) { schedule.cancel(false); } }); } /* JADX INFO: Access modifiers changed from: package-private */ public void d() { this.f5607b.trySetResult(null); } Task e() { return this.f5607b.getTask(); } } j1(Context context, String str) { this(context, str, new ScheduledThreadPoolExecutor(0, new NamedThreadFactory("Firebase-FirebaseInstanceIdServiceConnection"))); } private void a() { while (!this.f5603d.isEmpty()) { ((a) this.f5603d.poll()).d(); } } private synchronized void b() { if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "flush queue called"); } while (!this.f5603d.isEmpty()) { if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "found intent to be delivered"); } g1 g1Var = this.f5604e; if (g1Var == null || !g1Var.isBinderAlive()) { d(); return; } if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "binder is alive, sending the intent."); } this.f5604e.c((a) this.f5603d.poll()); } } private void d() { if (Log.isLoggable("FirebaseMessaging", 3)) { StringBuilder sb = new StringBuilder(); sb.append("binder is dead. start connection? "); sb.append(!this.f5605f); Log.d("FirebaseMessaging", sb.toString()); } if (this.f5605f) { return; } this.f5605f = true; try { } catch (SecurityException e10) { Log.e("FirebaseMessaging", "Exception while binding the service", e10); } if (ConnectionTracker.getInstance().bindService(this.f5600a, this.f5601b, this, 65)) { return; } Log.e("FirebaseMessaging", "binding to the service failed"); this.f5605f = false; a(); } synchronized Task c(Intent intent) { a aVar; if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "new intent queued in the bind-strategy delivery"); } aVar = new a(intent); aVar.c(this.f5602c); this.f5603d.add(aVar); b(); return aVar.e(); } @Override // android.content.ServiceConnection public synchronized void onServiceConnected(ComponentName componentName, IBinder iBinder) { if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "onServiceConnected: " + componentName); } this.f5605f = false; if (iBinder instanceof g1) { this.f5604e = (g1) iBinder; b(); return; } Log.e("FirebaseMessaging", "Invalid service connection: " + iBinder); a(); } @Override // android.content.ServiceConnection public void onServiceDisconnected(ComponentName componentName) { if (Log.isLoggable("FirebaseMessaging", 3)) { Log.d("FirebaseMessaging", "onServiceDisconnected: " + componentName); } b(); } j1(Context context, String str, ScheduledExecutorService scheduledExecutorService) { this.f5603d = new ArrayDeque(); this.f5605f = false; Context applicationContext = context.getApplicationContext(); this.f5600a = applicationContext; this.f5601b = new Intent(str).setPackage(applicationContext.getPackageName()); this.f5602c = scheduledExecutorService; } }