package androidx.lifecycle;

import androidx.lifecycle.f;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: compiled from: ClassesInfoCache.java */
/* JADX INFO: loaded from: classes.dex */
final class b {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    static b f935c = new b();
    private final Map<Class<?>, a> a = new HashMap();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Map<Class<?>, Boolean> f936b = new HashMap();

    /* JADX INFO: compiled from: ClassesInfoCache.java */
    static class a {
        final Map<f.b, List<C0013b>> a = new HashMap();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final Map<C0013b, f.b> f937b;

        a(Map<C0013b, f.b> map) {
            this.f937b = map;
            for (Map.Entry<C0013b, f.b> entry : map.entrySet()) {
                f.b value = entry.getValue();
                List<C0013b> arrayList = this.a.get(value);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    this.a.put(value, arrayList);
                }
                arrayList.add(entry.getKey());
            }
        }

        private static void b(List<C0013b> list, l lVar, f.b bVar, Object obj) {
            if (list != null) {
                for (int size = list.size() - 1; size >= 0; size--) {
                    list.get(size).a(lVar, bVar, obj);
                }
            }
        }

        void a(l lVar, f.b bVar, Object obj) {
            b(this.a.get(bVar), lVar, bVar, obj);
            b(this.a.get(f.b.ON_ANY), lVar, bVar, obj);
        }
    }

    /* JADX INFO: renamed from: androidx.lifecycle.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ClassesInfoCache.java */
    static final class C0013b {
        final int a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final Method f938b;

        C0013b(int i2, Method method) {
            this.a = i2;
            this.f938b = method;
            method.setAccessible(true);
        }

        void a(l lVar, f.b bVar, Object obj) {
            try {
                int i2 = this.a;
                if (i2 == 0) {
                    this.f938b.invoke(obj, new Object[0]);
                } else if (i2 == 1) {
                    this.f938b.invoke(obj, lVar);
                } else {
                    if (i2 != 2) {
                        return;
                    }
                    this.f938b.invoke(obj, lVar, bVar);
                }
            } catch (IllegalAccessException e2) {
                throw new RuntimeException(e2);
            } catch (InvocationTargetException e3) {
                throw new RuntimeException("Failed to call observer method", e3.getCause());
            }
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof C0013b)) {
                return false;
            }
            C0013b c0013b = (C0013b) obj;
            return this.a == c0013b.a && this.f938b.getName().equals(c0013b.f938b.getName());
        }

        public int hashCode() {
            return (this.a * 31) + this.f938b.getName().hashCode();
        }
    }

    b() {
    }

    private a a(Class<?> cls, Method[] methodArr) {
        int i2;
        a aVarC;
        Class<? super Object> superclass = cls.getSuperclass();
        HashMap map = new HashMap();
        if (superclass != null && (aVarC = c(superclass)) != null) {
            map.putAll(aVarC.f937b);
        }
        for (Class<?> cls2 : cls.getInterfaces()) {
            for (Map.Entry<C0013b, f.b> entry : c(cls2).f937b.entrySet()) {
                e(map, entry.getKey(), entry.getValue(), cls);
            }
        }
        if (methodArr == null) {
            methodArr = b(cls);
        }
        boolean z = false;
        for (Method method : methodArr) {
            s sVar = (s) method.getAnnotation(s.class);
            if (sVar != null) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i2 = 0;
                } else {
                    if (!parameterTypes[0].isAssignableFrom(l.class)) {
                        throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                    }
                    i2 = 1;
                }
                f.b bVarValue = sVar.value();
                if (parameterTypes.length > 1) {
                    if (!parameterTypes[1].isAssignableFrom(f.b.class)) {
                        throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
                    }
                    if (bVarValue != f.b.ON_ANY) {
                        throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
                    }
                    i2 = 2;
                }
                if (parameterTypes.length > 2) {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
                e(map, new C0013b(i2, method), bVarValue, cls);
                z = true;
            }
        }
        a aVar = new a(map);
        this.a.put(cls, aVar);
        this.f936b.put(cls, Boolean.valueOf(z));
        return aVar;
    }

    private Method[] b(Class<?> cls) {
        try {
            return cls.getDeclaredMethods();
        } catch (NoClassDefFoundError e2) {
            throw new IllegalArgumentException("The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.", e2);
        }
    }

    private void e(Map<C0013b, f.b> map, C0013b c0013b, f.b bVar, Class<?> cls) {
        f.b bVar2 = map.get(c0013b);
        if (bVar2 == null || bVar == bVar2) {
            if (bVar2 == null) {
                map.put(c0013b, bVar);
                return;
            }
            return;
        }
        throw new IllegalArgumentException("Method " + c0013b.f938b.getName() + " in " + cls.getName() + " already declared with different @OnLifecycleEvent value: previous value " + bVar2 + ", new value " + bVar);
    }

    a c(Class<?> cls) {
        a aVar = this.a.get(cls);
        return aVar != null ? aVar : a(cls, null);
    }

    boolean d(Class<?> cls) {
        Boolean bool = this.f936b.get(cls);
        if (bool != null) {
            return bool.booleanValue();
        }
        Method[] methodArrB = b(cls);
        for (Method method : methodArrB) {
            if (((s) method.getAnnotation(s.class)) != null) {
                a(cls, methodArrB);
                return true;
            }
        }
        this.f936b.put(cls, Boolean.FALSE);
        return false;
    }
}
