package android.arch.lifecycle;

import android.arch.lifecycle.c;
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 */
class a {

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

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

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

    /* JADX INFO: renamed from: android.arch.lifecycle.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ClassesInfoCache.java */
    static class C0016a {

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

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

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

        private static void b(List<b> list, e eVar, c.a aVar, Object obj) {
            if (list != null) {
                for (int size = list.size() - 1; size >= 0; size--) {
                    list.get(size).a(eVar, aVar, obj);
                }
            }
        }

        void a(e eVar, c.a aVar, Object obj) {
            b(this.f358a.get(aVar), eVar, aVar, obj);
            b(this.f358a.get(c.a.ON_ANY), eVar, aVar, obj);
        }
    }

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

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

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

        b(int i, Method method) {
            this.f360a = i;
            this.f361b = method;
            method.setAccessible(true);
        }

        void a(e eVar, c.a aVar, Object obj) {
            try {
                int i = this.f360a;
                if (i == 0) {
                    this.f361b.invoke(obj, new Object[0]);
                } else if (i == 1) {
                    this.f361b.invoke(obj, eVar);
                } else {
                    if (i != 2) {
                        return;
                    }
                    this.f361b.invoke(obj, eVar, aVar);
                }
            } 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 == null || b.class != obj.getClass()) {
                return false;
            }
            b bVar = (b) obj;
            return this.f360a == bVar.f360a && this.f361b.getName().equals(bVar.f361b.getName());
        }

        public int hashCode() {
            return (this.f360a * 31) + this.f361b.getName().hashCode();
        }
    }

    a() {
    }

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

    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<b, c.a> map, b bVar, c.a aVar, Class cls) {
        c.a aVar2 = map.get(bVar);
        if (aVar2 == null || aVar == aVar2) {
            if (aVar2 == null) {
                map.put(bVar, aVar);
                return;
            }
            return;
        }
        throw new IllegalArgumentException("Method " + bVar.f361b.getName() + " in " + cls.getName() + " already declared with different @OnLifecycleEvent value: previous value " + aVar2 + ", new value " + aVar);
    }

    C0016a c(Class cls) {
        C0016a c0016a = this.f356a.get(cls);
        return c0016a != null ? c0016a : a(cls, null);
    }

    boolean d(Class cls) {
        if (this.f357b.containsKey(cls)) {
            return this.f357b.get(cls).booleanValue();
        }
        Method[] methodArrB = b(cls);
        for (Method method : methodArrB) {
            if (((l) method.getAnnotation(l.class)) != null) {
                a(cls, methodArrB);
                return true;
            }
        }
        this.f357b.put(cls, Boolean.FALSE);
        return false;
    }
}
