package com.google.firebase.iid; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageItemInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.Looper; import android.util.Log; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.common.util.concurrent.NamedThreadFactory; import com.google.android.gms.measurement.AppMeasurement; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.SuccessContinuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.firebase.DataCollectionDefaultChange; import com.google.firebase.FirebaseApp; import com.google.firebase.events.Event; import com.google.firebase.events.EventHandler; import com.google.firebase.events.Subscriber; import com.google.firebase.heartbeatinfo.HeartBeatInfo; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.installations.FirebaseInstallationsApi; import com.google.firebase.platforminfo.UserAgentPublisher; import java.io.IOException; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; /* loaded from: classes.dex */ public class FirebaseInstanceId { private static zzaz zzb; private static ScheduledExecutorService zzd; private final Executor zze; private final FirebaseApp zzf; private final zzao zzg; private final zzt zzh; private final zzat zzi; private final FirebaseInstallationsApi zzj; private boolean zzk; private final zza zzl; private static final long zza = TimeUnit.HOURS.toSeconds(8); private static final Pattern zzc = Pattern.compile("\\AA[\\w-]{38}\\z"); public static FirebaseInstanceId getInstance() { return getInstance(FirebaseApp.getInstance()); } public static FirebaseInstanceId getInstance(FirebaseApp firebaseApp) { zza(firebaseApp); return (FirebaseInstanceId) firebaseApp.get(FirebaseInstanceId.class); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public final class zza { final FirebaseInstanceId zza; private boolean zzb; private final Subscriber zzc; private boolean zzd; private EventHandler zze; private Boolean zzf; zza(FirebaseInstanceId firebaseInstanceId, Subscriber subscriber) { this.zza = firebaseInstanceId; this.zzc = subscriber; } private final void zzb() { synchronized (this) { if (this.zzd) { return; } this.zzb = zzd(); Boolean zzc = zzc(); this.zzf = zzc; if (zzc == null && this.zzb) { EventHandler eventHandler = new EventHandler(this) { // from class: com.google.firebase.iid.zzq private final FirebaseInstanceId.zza zza; @Override // com.google.firebase.events.EventHandler public final void handle(Event event) { FirebaseInstanceId.zza zzaVar = this.zza; synchronized (zzaVar) { if (zzaVar.zza()) { zzaVar.zza.zzj(); } } } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = this; } }; this.zze = eventHandler; this.zzc.subscribe(DataCollectionDefaultChange.class, eventHandler); } this.zzd = true; } } /* JADX INFO: Access modifiers changed from: package-private */ public final boolean zza() { synchronized (this) { zzb(); Boolean bool = this.zzf; if (bool != null) { return bool.booleanValue(); } return this.zzb && this.zza.zzf.isDataCollectionDefaultEnabled(); } } private final Boolean zzc() { ApplicationInfo applicationInfo; Context applicationContext = this.zza.zzf.getApplicationContext(); SharedPreferences sharedPreferences = applicationContext.getSharedPreferences("com.google.firebase.messaging", 0); if (sharedPreferences.contains("auto_init")) { return Boolean.valueOf(sharedPreferences.getBoolean("auto_init", false)); } try { PackageManager packageManager = applicationContext.getPackageManager(); if (packageManager == null || (applicationInfo = packageManager.getApplicationInfo(applicationContext.getPackageName(), 128)) == null || PackageItemInfo.class.getField("metaData").get(applicationInfo) == null || !((Bundle) PackageItemInfo.class.getField("metaData").get(applicationInfo)).containsKey("firebase_messaging_auto_init_enabled")) { return null; } return Boolean.valueOf(((Bundle) PackageItemInfo.class.getField("metaData").get(applicationInfo)).getBoolean("firebase_messaging_auto_init_enabled")); } catch (PackageManager.NameNotFoundException unused) { return null; } } private final boolean zzd() { try { Class.forName("com.google.firebase.messaging.FirebaseMessaging"); return true; } catch (ClassNotFoundException unused) { Context applicationContext = this.zza.zzf.getApplicationContext(); Intent intent = new Intent("com.google.firebase.MESSAGING_EVENT"); intent.setPackage(applicationContext.getPackageName()); ResolveInfo resolveService = applicationContext.getPackageManager().resolveService(intent, 0); return (resolveService == null || resolveService.serviceInfo == null) ? false : true; } } } /* JADX INFO: Access modifiers changed from: package-private */ public FirebaseInstanceId(FirebaseApp firebaseApp, Subscriber subscriber, UserAgentPublisher userAgentPublisher, HeartBeatInfo heartBeatInfo, FirebaseInstallationsApi firebaseInstallationsApi) { this(firebaseApp, new zzao(firebaseApp.getApplicationContext()), zzh.zzb(), zzh.zzb(), subscriber, userAgentPublisher, heartBeatInfo, firebaseInstallationsApi); } private FirebaseInstanceId(FirebaseApp firebaseApp, zzao zzaoVar, Executor executor, Executor executor2, Subscriber subscriber, UserAgentPublisher userAgentPublisher, HeartBeatInfo heartBeatInfo, FirebaseInstallationsApi firebaseInstallationsApi) { this.zzk = false; if (zzao.zza(firebaseApp) == null) { throw new IllegalStateException("FirebaseInstanceId failed to initialize, FirebaseApp is missing project ID"); } synchronized (FirebaseInstanceId.class) { if (zzb == null) { zzb = new zzaz(firebaseApp.getApplicationContext()); } } this.zzf = firebaseApp; this.zzg = zzaoVar; this.zzh = new zzt(firebaseApp, zzaoVar, executor, userAgentPublisher, heartBeatInfo, firebaseInstallationsApi); this.zze = executor2; this.zzl = new zza(this, subscriber); this.zzi = new zzat(executor); this.zzj = firebaseInstallationsApi; executor2.execute(new Runnable(this) { // from class: com.google.firebase.iid.zzl private final FirebaseInstanceId zza; @Override // java.lang.Runnable public final void run() { this.zza.zzi(); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = this; } }); } /* JADX INFO: Access modifiers changed from: private */ public final void zzj() { if (zza(zzb())) { zzk(); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void zza(boolean z) { synchronized (this) { this.zzk = z; } } private final void zzk() { synchronized (this) { if (!this.zzk) { zza(0L); } } } /* JADX INFO: Access modifiers changed from: package-private */ public final void zza(long j) { synchronized (this) { zza(new zzbb(this, Math.min(Math.max(30L, j << 1), zza)), j); this.zzk = true; } } /* JADX INFO: Access modifiers changed from: package-private */ public static void zza(Runnable runnable, long j) { synchronized (FirebaseInstanceId.class) { if (zzd == null) { zzd = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("FirebaseInstanceId")); } zzd.schedule(runnable, j, TimeUnit.SECONDS); } } public String getId() { zza(this.zzf); zzj(); return zzl(); } private static void zza(FirebaseApp firebaseApp) { Preconditions.checkNotEmpty(firebaseApp.getOptions().getProjectId(), "Please set your project ID. A valid Firebase project ID is required to communicate with Firebase server APIs: It identifies your project with Google."); Preconditions.checkNotEmpty(firebaseApp.getOptions().getApplicationId(), "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase."); Preconditions.checkNotEmpty(firebaseApp.getOptions().getApiKey(), "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google."); Preconditions.checkArgument(firebaseApp.getOptions().getApplicationId().contains(":"), "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options."); Preconditions.checkArgument(zzc.matcher(firebaseApp.getOptions().getApiKey()).matches(), "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options."); } private final String zzl() { try { zzb.zzb(this.zzf.getPersistenceKey()); Task id = this.zzj.getId(); Preconditions.checkNotNull(id, "Task must not be null"); final CountDownLatch countDownLatch = new CountDownLatch(1); id.addOnCompleteListener(zzn.zza, new OnCompleteListener(countDownLatch) { // from class: com.google.firebase.iid.zzm private final CountDownLatch zza; @Override // com.google.android.gms.tasks.OnCompleteListener public final void onComplete(Task task) { this.zza.countDown(); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = countDownLatch; } }); countDownLatch.await(30000L, TimeUnit.MILLISECONDS); if (id.isSuccessful()) { return id.getResult(); } if (id.isCanceled()) { throw new CancellationException("Task is already canceled"); } if (id.isComplete()) { throw new IllegalStateException(id.getException()); } throw new IllegalThreadStateException("Firebase Installations getId Task has timed out."); } catch (InterruptedException e) { throw new IllegalStateException(e); } } public Task getInstanceId() { zza(this.zzf); return zza(zzao.zza(this.zzf), "*"); } private final Task zza(final String str, String str2) { final String zza2 = zza(str2); return Tasks.forResult(null).continueWithTask(this.zze, new Continuation(this, str, zza2) { // from class: com.google.firebase.iid.zzk private final FirebaseInstanceId zza; private final String zzb; private final String zzc; @Override // com.google.android.gms.tasks.Continuation public final Object then(Task task) { return this.zza.zza(this.zzb, this.zzc, task); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = this; this.zzb = str; this.zzc = zza2; } }); } public String getToken(String str, String str2) throws IOException { zza(this.zzf); if (Looper.getMainLooper() == Looper.myLooper()) { throw new IOException("MAIN_THREAD"); } return ((InstanceIdResult) zza(zza(str, str2))).getToken(); } /* JADX INFO: Access modifiers changed from: package-private */ public final zzay zzb() { return zzb(zzao.zza(this.zzf), "*"); } private final zzay zzb(String str, String str2) { return zzb.zza(zzm(), str, str2); } /* JADX INFO: Access modifiers changed from: package-private */ public final String zzc() throws IOException { return getToken(zzao.zza(this.zzf), "*"); } private final T zza(Task task) throws IOException { try { return (T) Tasks.await(task, 30000L, TimeUnit.MILLISECONDS); } catch (InterruptedException | TimeoutException unused) { throw new IOException("SERVICE_NOT_AVAILABLE"); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) { if ("INSTANCE_ID_RESET".equals(cause.getMessage())) { zze(); } throw ((IOException) cause); } if (cause instanceof RuntimeException) { throw ((RuntimeException) cause); } throw new IOException(e); } } /* JADX INFO: Access modifiers changed from: package-private */ public static boolean zzd() { return Log.isLoggable("FirebaseInstanceId", 3); } /* JADX INFO: Access modifiers changed from: package-private */ public final void zze() { synchronized (this) { zzb.zza(); if (this.zzl.zza()) { zzk(); } } } public final boolean zzf() { return this.zzg.zza(); } /* JADX INFO: Access modifiers changed from: package-private */ public final void zzg() { zzb.zzc(zzm()); zzk(); } public final boolean zzh() { return this.zzl.zza(); } private static String zza(String str) { return (str.isEmpty() || str.equalsIgnoreCase(AppMeasurement.FCM_ORIGIN) || str.equalsIgnoreCase("gcm")) ? "*" : str; } private final String zzm() { return "[DEFAULT]".equals(this.zzf.getName()) ? "" : this.zzf.getPersistenceKey(); } /* JADX INFO: Access modifiers changed from: package-private */ public final boolean zza(zzay zzayVar) { return zzayVar == null || zzayVar.zzb(this.zzg.zzc()); } /* JADX INFO: Access modifiers changed from: package-private */ public final /* synthetic */ Task zza(final String str, final String str2, Task task) throws Exception { final String zzl = zzl(); zzay zzb2 = zzb(str, str2); if (!zza(zzb2)) { return Tasks.forResult(new zzaa(zzl, zzb2.zza)); } return this.zzi.zza(str, str2, new zzav(this, zzl, str, str2) { // from class: com.google.firebase.iid.zzp private final FirebaseInstanceId zza; private final String zzb; private final String zzc; private final String zzd; @Override // com.google.firebase.iid.zzav public final Task zza() { return this.zza.zza(this.zzb, this.zzc, this.zzd); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = this; this.zzb = zzl; this.zzc = str; this.zzd = str2; } }); } /* JADX INFO: Access modifiers changed from: package-private */ public final /* synthetic */ Task zza(final String str, final String str2, final String str3) { return this.zzh.zza(str, str2, str3).onSuccessTask(this.zze, new SuccessContinuation(this, str2, str3, str) { // from class: com.google.firebase.iid.zzo private final FirebaseInstanceId zza; private final String zzb; private final String zzc; private final String zzd; @Override // com.google.android.gms.tasks.SuccessContinuation public final Task then(Object obj) { return this.zza.zza(this.zzb, this.zzc, this.zzd, (String) obj); } /* JADX INFO: Access modifiers changed from: package-private */ { this.zza = this; this.zzb = str2; this.zzc = str3; this.zzd = str; } }); } /* JADX INFO: Access modifiers changed from: package-private */ public final /* synthetic */ Task zza(String str, String str2, String str3, String str4) throws Exception { zzb.zza(zzm(), str, str2, str4, this.zzg.zzc()); return Tasks.forResult(new zzaa(str3, str4)); } /* JADX INFO: Access modifiers changed from: package-private */ public final /* synthetic */ void zzi() { if (this.zzl.zza()) { zzj(); } } /* JADX INFO: Access modifiers changed from: package-private */ public final FirebaseApp zza() { return this.zzf; } }