package androidx.lifecycle;

import androidx.lifecycle.g;
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 */
@Deprecated
final class a {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    static a f2538c = new a();

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

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

    /* JADX INFO: renamed from: androidx.lifecycle.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ClassesInfoCache.java */
    @Deprecated
    static class C0033a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final Map<g.b, List<b>> f2541a = new HashMap();

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

        C0033a(Map<b, g.b> map) {
            this.f2542b = map;
            for (Map.Entry<b, g.b> entry : map.entrySet()) {
                g.b value = entry.getValue();
                List<b> arrayList = this.f2541a.get(value);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    this.f2541a.put(value, arrayList);
                }
                arrayList.add(entry.getKey());
            }
        }

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

        void a(k kVar, g.b bVar, Object obj) {
            b(this.f2541a.get(bVar), kVar, bVar, obj);
            b(this.f2541a.get(g.b.ON_ANY), kVar, bVar, obj);
        }
    }

    /* JADX INFO: compiled from: ClassesInfoCache.java */
    @Deprecated
    static final class b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final int f2543a;

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

        b(int i3, Method method) {
            this.f2543a = i3;
            this.f2544b = method;
            method.setAccessible(true);
        }

        void a(k kVar, g.b bVar, Object obj) {
            try {
                int i3 = this.f2543a;
                if (i3 == 0) {
                    this.f2544b.invoke(obj, new Object[0]);
                } else if (i3 == 1) {
                    this.f2544b.invoke(obj, kVar);
                } else {
                    if (i3 != 2) {
                        return;
                    }
                    this.f2544b.invoke(obj, kVar, bVar);
                }
            } catch (IllegalAccessException e3) {
                throw new RuntimeException(e3);
            } catch (InvocationTargetException e4) {
                throw new RuntimeException("Failed to call observer method", e4.getCause());
            }
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof b)) {
                return false;
            }
            b bVar = (b) obj;
            return this.f2543a == bVar.f2543a && this.f2544b.getName().equals(bVar.f2544b.getName());
        }

        public int hashCode() {
            return (this.f2543a * 31) + this.f2544b.getName().hashCode();
        }
    }

    a() {
    }

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

    private Method[] b(Class<?> cls) {
        try {
            return cls.getDeclaredMethods();
        } catch (NoClassDefFoundError e3) {
            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.", e3);
        }
    }

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

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

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