package com.google.firebase.components; import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.pm.PackageItemInfo; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.os.Bundle; import com.google.firebase.inject.Provider; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; /* loaded from: classes.dex */ public final class ComponentDiscovery { private final T context; private final RegistrarNameRetriever retriever; /* loaded from: classes.dex */ interface RegistrarNameRetriever { List retrieve(T t); } public static ComponentDiscovery forContext(Context context, Class cls) { return new ComponentDiscovery<>(context, new MetadataRegistrarNameRetriever(cls)); } ComponentDiscovery(T t, RegistrarNameRetriever registrarNameRetriever) { this.context = t; this.retriever = registrarNameRetriever; } public final List> discoverLazy() { ArrayList arrayList = new ArrayList(); for (final String str : this.retriever.retrieve(this.context)) { arrayList.add(new Provider(str) { // from class: com.google.firebase.components.ComponentDiscovery$$ExternalSyntheticLambda0 public final String f$0; @Override // com.google.firebase.inject.Provider public final Object get() { ComponentRegistrar instantiate; instantiate = ComponentDiscovery.instantiate(this.f$0); return instantiate; } { this.f$0 = str; } }); } return arrayList; } /* JADX INFO: Access modifiers changed from: private */ public static ComponentRegistrar instantiate(String str) { try { Class cls = Class.forName(str); if (!ComponentRegistrar.class.isAssignableFrom(cls)) { throw new InvalidRegistrarException(String.format("Class %s is not an instance of %s", str, "com.google.firebase.components.ComponentRegistrar")); } return (ComponentRegistrar) cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]); } catch (ClassNotFoundException unused) { new Object[]{str}; return null; } catch (IllegalAccessException e) { throw new InvalidRegistrarException(String.format("Could not instantiate %s.", str), e); } catch (InstantiationException e2) { throw new InvalidRegistrarException(String.format("Could not instantiate %s.", str), e2); } catch (NoSuchMethodException e3) { throw new InvalidRegistrarException(String.format("Could not instantiate %s", str), e3); } catch (InvocationTargetException e4) { throw new InvalidRegistrarException(String.format("Could not instantiate %s", str), e4); } } /* loaded from: classes.dex */ static class MetadataRegistrarNameRetriever implements RegistrarNameRetriever { private final Class discoveryService; private MetadataRegistrarNameRetriever(Class cls) { this.discoveryService = cls; } @Override // com.google.firebase.components.ComponentDiscovery.RegistrarNameRetriever public List retrieve(Context context) { Bundle metadata = getMetadata(context); if (metadata == null) { return Collections.emptyList(); } ArrayList arrayList = new ArrayList(); for (String str : metadata.keySet()) { if ("com.google.firebase.components.ComponentRegistrar".equals(metadata.get(str)) && str.startsWith("com.google.firebase.components:")) { arrayList.add(str.substring(31)); } } return arrayList; } private Bundle getMetadata(Context context) { ServiceInfo serviceInfo; try { PackageManager packageManager = context.getPackageManager(); if (packageManager == null || (serviceInfo = packageManager.getServiceInfo(new ComponentName(context, this.discoveryService), 128)) == null) { return null; } return (Bundle) PackageItemInfo.class.getField("metaData").get(serviceInfo); } catch (PackageManager.NameNotFoundException unused) { return null; } } } }