package com.google.firebase.components; import android.content.ComponentName; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.os.Bundle; import android.util.Log; 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: classes3.dex */ public final class ComponentDiscovery { public final Object context; public final RegistrarNameRetriever retriever; public interface RegistrarNameRetriever { List retrieve(Object obj); } public static ComponentDiscovery forContext(Context context, Class cls) { return new ComponentDiscovery(context, new MetadataRegistrarNameRetriever(cls)); } public ComponentDiscovery(Object obj, RegistrarNameRetriever registrarNameRetriever) { this.context = obj; this.retriever = registrarNameRetriever; } public List discoverLazy() { ArrayList arrayList = new ArrayList(); for (final String str : this.retriever.retrieve(this.context)) { arrayList.add(new Provider() { // from class: com.google.firebase.components.ComponentDiscovery$$ExternalSyntheticLambda0 @Override // com.google.firebase.inject.Provider public final Object get() { ComponentRegistrar instantiate; instantiate = ComponentDiscovery.instantiate(str); return instantiate; } }); } return arrayList; } 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) { Log.w("ComponentDiscovery", String.format("Class %s is not an found.", 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); } } public static class MetadataRegistrarNameRetriever implements RegistrarNameRetriever { public final Class discoveryService; public 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) { Log.w("ComponentDiscovery", "Could not retrieve metadata, returning empty list of registrars."); 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; } public final Bundle getMetadata(Context context) { try { PackageManager packageManager = context.getPackageManager(); if (packageManager == null) { Log.w("ComponentDiscovery", "Context has no PackageManager."); return null; } ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(context, (Class) this.discoveryService), 128); if (serviceInfo == null) { Log.w("ComponentDiscovery", this.discoveryService + " has no service info."); return null; } return serviceInfo.metaData; } catch (PackageManager.NameNotFoundException unused) { Log.w("ComponentDiscovery", "Application info not found."); return null; } } } }