package com.google.android.gms.cloudmessaging; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.util.Log; import android.util.SparseArray; import androidx.annotation.MainThread; import androidx.annotation.Nullable; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.common.stats.ConnectionTracker; import csdk.gluads.Consts; import java.util.ArrayDeque; import java.util.Iterator; import java.util.Queue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ final class zzp implements ServiceConnection { zzq zzc; final /* synthetic */ zzv zzf; int zza = 0; final Messenger zzb = new Messenger(new com.google.android.gms.internal.cloudmessaging.zzf(Looper.getMainLooper(), new Handler.Callback() { // from class: com.google.android.gms.cloudmessaging.zzm @Override // android.os.Handler.Callback public final boolean handleMessage(Message message) { int i = message.arg1; if (Log.isLoggable("MessengerIpcClient", 3)) { StringBuilder sb = new StringBuilder(); sb.append("Received response to request: "); sb.append(i); } zzp zzpVar = zzp.this; synchronized (zzpVar) { try { zzs zzsVar = (zzs) zzpVar.zze.get(i); if (zzsVar == null) { Log.w("MessengerIpcClient", "Received response for unknown request: " + i); return true; } zzpVar.zze.remove(i); zzpVar.zzf(); Bundle data = message.getData(); if (data.getBoolean(Consts.SDK_PRIVACY_STAGE_UNSUPPORTED, false)) { zzsVar.zzc(new zzt(4, "Not supported by GmsCore", null)); return true; } zzsVar.zza(data); return true; } finally { } } } })); final Queue zzd = new ArrayDeque(); final SparseArray zze = new SparseArray(); public /* synthetic */ zzp(zzv zzvVar, zzo zzoVar) { this.zzf = zzvVar; } @Override // android.content.ServiceConnection @MainThread public final void onServiceConnected(ComponentName componentName, final IBinder iBinder) { ScheduledExecutorService scheduledExecutorService; Log.isLoggable("MessengerIpcClient", 2); scheduledExecutorService = this.zzf.zzc; scheduledExecutorService.execute(new Runnable() { // from class: com.google.android.gms.cloudmessaging.zzi @Override // java.lang.Runnable public final void run() { zzp zzpVar = zzp.this; IBinder iBinder2 = iBinder; synchronized (zzpVar) { if (iBinder2 == null) { zzpVar.zza(0, "Null service connection"); return; } try { zzpVar.zzc = new zzq(iBinder2); zzpVar.zza = 2; zzpVar.zzc(); } catch (RemoteException e) { zzpVar.zza(0, e.getMessage()); } } } }); } @Override // android.content.ServiceConnection @MainThread public final void onServiceDisconnected(ComponentName componentName) { ScheduledExecutorService scheduledExecutorService; Log.isLoggable("MessengerIpcClient", 2); scheduledExecutorService = this.zzf.zzc; scheduledExecutorService.execute(new Runnable() { // from class: com.google.android.gms.cloudmessaging.zzl @Override // java.lang.Runnable public final void run() { zzp.this.zza(2, "Service disconnected"); } }); } public final synchronized void zza(int i, @Nullable String str) { zzb(i, str, null); } public final synchronized void zzb(int i, @Nullable String str, @Nullable Throwable th) { Context context; Log.isLoggable("MessengerIpcClient", 3); int i2 = this.zza; if (i2 == 0) { throw new IllegalStateException(); } if (i2 != 1 && i2 != 2) { if (i2 != 3) { return; } this.zza = 4; return; } Log.isLoggable("MessengerIpcClient", 2); this.zza = 4; zzv zzvVar = this.zzf; ConnectionTracker connectionTracker = ConnectionTracker.getInstance(); context = zzvVar.zzb; connectionTracker.unbindService(context, this); zzt zztVar = new zzt(i, str, th); Iterator it = this.zzd.iterator(); while (it.hasNext()) { ((zzs) it.next()).zzc(zztVar); } this.zzd.clear(); for (int i3 = 0; i3 < this.zze.size(); i3++) { ((zzs) this.zze.valueAt(i3)).zzc(zztVar); } this.zze.clear(); } public final void zzc() { ScheduledExecutorService scheduledExecutorService; scheduledExecutorService = this.zzf.zzc; scheduledExecutorService.execute(new Runnable() { // from class: com.google.android.gms.cloudmessaging.zzj @Override // java.lang.Runnable public final void run() { final zzs zzsVar; ScheduledExecutorService scheduledExecutorService2; Context context; while (true) { final zzp zzpVar = zzp.this; synchronized (zzpVar) { try { if (zzpVar.zza != 2) { return; } if (zzpVar.zzd.isEmpty()) { zzpVar.zzf(); return; } zzsVar = (zzs) zzpVar.zzd.poll(); zzpVar.zze.put(zzsVar.zza, zzsVar); scheduledExecutorService2 = zzpVar.zzf.zzc; scheduledExecutorService2.schedule(new Runnable() { // from class: com.google.android.gms.cloudmessaging.zzn @Override // java.lang.Runnable public final void run() { zzp.this.zze(zzsVar.zza); } }, 30L, TimeUnit.SECONDS); } catch (Throwable th) { throw th; } } Log.isLoggable("MessengerIpcClient", 3); zzv zzvVar = zzpVar.zzf; Messenger messenger = zzpVar.zzb; int i = zzsVar.zzc; context = zzvVar.zzb; Message obtain = Message.obtain(); obtain.what = i; obtain.arg1 = zzsVar.zza; obtain.replyTo = messenger; Bundle bundle = new Bundle(); bundle.putBoolean("oneWay", zzsVar.zzb()); bundle.putString("pkg", context.getPackageName()); bundle.putBundle("data", zzsVar.zzd); obtain.setData(bundle); try { zzpVar.zzc.zza(obtain); } catch (RemoteException e) { zzpVar.zza(2, e.getMessage()); } } } }); } public final synchronized void zzd() { if (this.zza == 1) { zza(1, "Timed out while binding"); } } public final synchronized void zze(int i) { zzs zzsVar = (zzs) this.zze.get(i); if (zzsVar != null) { Log.w("MessengerIpcClient", "Timing out request: " + i); this.zze.remove(i); zzsVar.zzc(new zzt(3, "Timed out waiting for response", null)); zzf(); } } public final synchronized void zzf() { Context context; if (this.zza == 2 && this.zzd.isEmpty() && this.zze.size() == 0) { Log.isLoggable("MessengerIpcClient", 2); this.zza = 3; zzv zzvVar = this.zzf; ConnectionTracker connectionTracker = ConnectionTracker.getInstance(); context = zzvVar.zzb; connectionTracker.unbindService(context, this); } } public final synchronized boolean zzg(zzs zzsVar) { Context context; ScheduledExecutorService scheduledExecutorService; int i = this.zza; if (i != 0) { if (i == 1) { this.zzd.add(zzsVar); return true; } if (i != 2) { return false; } this.zzd.add(zzsVar); zzc(); return true; } this.zzd.add(zzsVar); Preconditions.checkState(this.zza == 0); Log.isLoggable("MessengerIpcClient", 2); this.zza = 1; Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"); intent.setPackage("com.google.android.gms"); try { ConnectionTracker connectionTracker = ConnectionTracker.getInstance(); context = this.zzf.zzb; if (connectionTracker.bindService(context, intent, this, 1)) { scheduledExecutorService = this.zzf.zzc; scheduledExecutorService.schedule(new Runnable() { // from class: com.google.android.gms.cloudmessaging.zzk @Override // java.lang.Runnable public final void run() { zzp.this.zzd(); } }, 30L, TimeUnit.SECONDS); } else { zza(0, "Unable to bind to service"); } } catch (SecurityException e) { zzb(0, "Unable to bind to service", e); } return true; } }