package androidx.lifecycle;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* renamed from: androidx.lifecycle.p */
/* loaded from: classes.dex */
public class C0438p {

    /* renamed from: a */
    public static Map<Class<?>, Integer> f1649a = new HashMap();

    /* renamed from: b */
    public static Map<Class<?>, List<Constructor<? extends InterfaceC0418f>>> f1650b = new HashMap();

    /* renamed from: a */
    public static InterfaceC0418f m3676a(Constructor<? extends InterfaceC0418f> constructor, Object obj) {
        try {
            return constructor.newInstance(obj);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e2) {
            throw new RuntimeException(e2);
        } catch (InvocationTargetException e3) {
            throw new RuntimeException(e3);
        }
    }

    /* renamed from: b */
    public static Constructor<? extends InterfaceC0418f> m3675b(Class<?> cls) {
        try {
            Package r0 = cls.getPackage();
            String canonicalName = cls.getCanonicalName();
            String name = r0 != null ? r0.getName() : "";
            if (!name.isEmpty()) {
                canonicalName = canonicalName.substring(name.length() + 1);
            }
            String m3674c = m3674c(canonicalName);
            if (!name.isEmpty()) {
                m3674c = name + "." + m3674c;
            }
            Constructor declaredConstructor = Class.forName(m3674c).getDeclaredConstructor(cls);
            if (!declaredConstructor.isAccessible()) {
                declaredConstructor.setAccessible(true);
            }
            return declaredConstructor;
        } catch (ClassNotFoundException unused) {
            return null;
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    /* renamed from: c */
    public static String m3674c(String str) {
        return str.replace(".", "_") + "_LifecycleAdapter";
    }

    /* renamed from: d */
    public static int m3673d(Class<?> cls) {
        Integer num = f1649a.get(cls);
        if (num != null) {
            return num.intValue();
        }
        int m3670g = m3670g(cls);
        f1649a.put(cls, Integer.valueOf(m3670g));
        return m3670g;
    }

    /* renamed from: e */
    public static boolean m3672e(Class<?> cls) {
        return cls != null && InterfaceC0433l.class.isAssignableFrom(cls);
    }

    /* renamed from: f */
    public static InterfaceC0432k m3671f(Object obj) {
        boolean z = obj instanceof InterfaceC0432k;
        boolean z2 = obj instanceof InterfaceC0416e;
        if (z && z2) {
            return new FullLifecycleObserverAdapter((InterfaceC0416e) obj, (InterfaceC0432k) obj);
        }
        if (z2) {
            return new FullLifecycleObserverAdapter((InterfaceC0416e) obj, null);
        }
        if (z) {
            return (InterfaceC0432k) obj;
        }
        Class<?> cls = obj.getClass();
        if (m3673d(cls) == 2) {
            List<Constructor<? extends InterfaceC0418f>> list = f1650b.get(cls);
            if (list.size() == 1) {
                return new SingleGeneratedAdapterObserver(m3676a(list.get(0), obj));
            }
            InterfaceC0418f[] interfaceC0418fArr = new InterfaceC0418f[list.size()];
            for (int i = 0; i < list.size(); i++) {
                interfaceC0418fArr[i] = m3676a(list.get(i), obj);
            }
            return new CompositeGeneratedAdaptersObserver(interfaceC0418fArr);
        }
        return new ReflectiveGenericLifecycleObserver(obj);
    }

    /* renamed from: g */
    public static int m3670g(Class<?> cls) {
        ArrayList arrayList;
        Class<?>[] interfaces;
        if (cls.getCanonicalName() == null) {
            return 1;
        }
        Constructor<? extends InterfaceC0418f> m3675b = m3675b(cls);
        if (m3675b != null) {
            f1650b.put(cls, Collections.singletonList(m3675b));
            return 2;
        } else if (C0397a.f1603c.m3737d(cls)) {
            return 1;
        } else {
            Class<? super Object> superclass = cls.getSuperclass();
            if (!m3672e(superclass)) {
                arrayList = null;
            } else if (m3673d(superclass) == 1) {
                return 1;
            } else {
                arrayList = new ArrayList(f1650b.get(superclass));
            }
            for (Class<?> cls2 : cls.getInterfaces()) {
                if (m3672e(cls2)) {
                    if (m3673d(cls2) == 1) {
                        return 1;
                    }
                    if (arrayList == null) {
                        arrayList = new ArrayList();
                    }
                    arrayList.addAll(f1650b.get(cls2));
                }
            }
            if (arrayList != null) {
                f1650b.put(cls, arrayList);
                return 2;
            }
            return 1;
        }
    }
}