package com.google.android.datatransport.runtime.backends;

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 defpackage.mv9;
import defpackage.u50;
import defpackage.v50;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;

/* JADX INFO: loaded from: classes.dex */
@Singleton
class MetadataBackendRegistry implements v50 {
    public final a a;
    public final CreationContextFactory b;
    public final HashMap c;

    public static class a {
        public final Context a;
        public Map b = null;

        public a(Context context) {
            this.a = context;
        }

        /* JADX WARN: Removed duplicated region for block: B:16:0x003a  */
        /* JADX WARN: Removed duplicated region for block: B:17:0x0042  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final u50 a(String str) {
            Bundle bundle;
            Map map;
            PackageManager packageManager;
            if (this.b == null) {
                Context context = this.a;
                try {
                    packageManager = context.getPackageManager();
                } catch (PackageManager.NameNotFoundException unused) {
                    Log.w("BackendRegistry", "Application info not found.");
                }
                if (packageManager == null) {
                    Log.w("BackendRegistry", "Context has no PackageManager.");
                } else {
                    ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(context, (Class<?>) TransportBackendDiscovery.class), 128);
                    if (serviceInfo == null) {
                        Log.w("BackendRegistry", "TransportBackendDiscovery has no service info.");
                    } else {
                        bundle = serviceInfo.metaData;
                        if (bundle != null) {
                            Log.w("BackendRegistry", "Could not retrieve metadata, returning empty list of transport backends.");
                            map = Collections.EMPTY_MAP;
                        } else {
                            HashMap map2 = new HashMap();
                            for (String str2 : bundle.keySet()) {
                                Object obj = bundle.get(str2);
                                if ((obj instanceof String) && str2.startsWith("backend:")) {
                                    for (String str3 : ((String) obj).split(",", -1)) {
                                        String strTrim = str3.trim();
                                        if (!strTrim.isEmpty()) {
                                            map2.put(strTrim, str2.substring(8));
                                        }
                                    }
                                }
                            }
                            map = map2;
                        }
                        this.b = map;
                    }
                }
                bundle = null;
                if (bundle != null) {
                }
                this.b = map;
            }
            String str4 = (String) this.b.get(str);
            if (str4 == null) {
                return null;
            }
            try {
                return (u50) Class.forName(str4).asSubclass(u50.class).getDeclaredConstructor(null).newInstance(null);
            } catch (ClassNotFoundException e) {
                Log.w("BackendRegistry", "Class " + str4 + " is not found.", e);
                return null;
            } catch (IllegalAccessException e2) {
                Log.w("BackendRegistry", "Could not instantiate " + str4 + ".", e2);
                return null;
            } catch (InstantiationException e3) {
                Log.w("BackendRegistry", "Could not instantiate " + str4 + ".", e3);
                return null;
            } catch (NoSuchMethodException e4) {
                Log.w("BackendRegistry", "Could not instantiate ".concat(str4), e4);
                return null;
            } catch (InvocationTargetException e5) {
                Log.w("BackendRegistry", "Could not instantiate ".concat(str4), e5);
                return null;
            }
        }
    }

    @Inject
    public MetadataBackendRegistry(Context context, CreationContextFactory creationContextFactory) {
        a aVar = new a(context);
        this.c = new HashMap();
        this.a = aVar;
        this.b = creationContextFactory;
    }

    @Override // defpackage.v50
    public final synchronized mv9 e(String str) {
        if (this.c.containsKey(str)) {
            return (mv9) this.c.get(str);
        }
        u50 u50VarA = this.a.a(str);
        if (u50VarA == null) {
            return null;
        }
        CreationContextFactory creationContextFactory = this.b;
        mv9 mv9VarCreate = u50VarA.create(new com.google.android.datatransport.runtime.backends.a(creationContextFactory.a, creationContextFactory.b, creationContextFactory.c, str));
        this.c.put(str, mv9VarCreate);
        return mv9VarCreate;
    }
}
