191 lines
7.3 KiB
Java
191 lines
7.3 KiB
Java
package androidx.lifecycle;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
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;
|
|
|
|
/* loaded from: classes.dex */
|
|
class ClassesInfoCache {
|
|
static ClassesInfoCache c = new ClassesInfoCache();
|
|
private final Map<Class, CallbackInfo> a = new HashMap();
|
|
private final Map<Class, Boolean> b = new HashMap();
|
|
|
|
static class MethodReference {
|
|
final int a;
|
|
final Method b;
|
|
|
|
MethodReference(int i, Method method) {
|
|
this.a = i;
|
|
this.b = method;
|
|
this.b.setAccessible(true);
|
|
}
|
|
|
|
void a(LifecycleOwner lifecycleOwner, Lifecycle.Event event, Object obj) {
|
|
try {
|
|
int i = this.a;
|
|
if (i == 0) {
|
|
this.b.invoke(obj, new Object[0]);
|
|
} else if (i == 1) {
|
|
this.b.invoke(obj, lifecycleOwner);
|
|
} else {
|
|
if (i != 2) {
|
|
return;
|
|
}
|
|
this.b.invoke(obj, lifecycleOwner, event);
|
|
}
|
|
} catch (IllegalAccessException e) {
|
|
throw new RuntimeException(e);
|
|
} catch (InvocationTargetException e2) {
|
|
throw new RuntimeException("Failed to call observer method", e2.getCause());
|
|
}
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (obj == null || MethodReference.class != obj.getClass()) {
|
|
return false;
|
|
}
|
|
MethodReference methodReference = (MethodReference) obj;
|
|
return this.a == methodReference.a && this.b.getName().equals(methodReference.b.getName());
|
|
}
|
|
|
|
public int hashCode() {
|
|
return (this.a * 31) + this.b.getName().hashCode();
|
|
}
|
|
}
|
|
|
|
ClassesInfoCache() {
|
|
}
|
|
|
|
private Method[] c(Class cls) {
|
|
try {
|
|
return cls.getDeclaredMethods();
|
|
} catch (NoClassDefFoundError e) {
|
|
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.", e);
|
|
}
|
|
}
|
|
|
|
CallbackInfo a(Class cls) {
|
|
CallbackInfo callbackInfo = this.a.get(cls);
|
|
return callbackInfo != null ? callbackInfo : a(cls, null);
|
|
}
|
|
|
|
boolean b(Class cls) {
|
|
if (this.b.containsKey(cls)) {
|
|
return this.b.get(cls).booleanValue();
|
|
}
|
|
Method[] c2 = c(cls);
|
|
for (Method method : c2) {
|
|
if (((OnLifecycleEvent) method.getAnnotation(OnLifecycleEvent.class)) != null) {
|
|
a(cls, c2);
|
|
return true;
|
|
}
|
|
}
|
|
this.b.put(cls, false);
|
|
return false;
|
|
}
|
|
|
|
static class CallbackInfo {
|
|
final Map<Lifecycle.Event, List<MethodReference>> a = new HashMap();
|
|
final Map<MethodReference, Lifecycle.Event> b;
|
|
|
|
CallbackInfo(Map<MethodReference, Lifecycle.Event> map) {
|
|
this.b = map;
|
|
for (Map.Entry<MethodReference, Lifecycle.Event> entry : map.entrySet()) {
|
|
Lifecycle.Event value = entry.getValue();
|
|
List<MethodReference> list = this.a.get(value);
|
|
if (list == null) {
|
|
list = new ArrayList<>();
|
|
this.a.put(value, list);
|
|
}
|
|
list.add(entry.getKey());
|
|
}
|
|
}
|
|
|
|
void a(LifecycleOwner lifecycleOwner, Lifecycle.Event event, Object obj) {
|
|
a(this.a.get(event), lifecycleOwner, event, obj);
|
|
a(this.a.get(Lifecycle.Event.ON_ANY), lifecycleOwner, event, obj);
|
|
}
|
|
|
|
private static void a(List<MethodReference> list, LifecycleOwner lifecycleOwner, Lifecycle.Event event, Object obj) {
|
|
if (list != null) {
|
|
for (int size = list.size() - 1; size >= 0; size--) {
|
|
list.get(size).a(lifecycleOwner, event, obj);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void a(Map<MethodReference, Lifecycle.Event> map, MethodReference methodReference, Lifecycle.Event event, Class cls) {
|
|
Lifecycle.Event event2 = map.get(methodReference);
|
|
if (event2 == null || event == event2) {
|
|
if (event2 == null) {
|
|
map.put(methodReference, event);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("Method " + methodReference.b.getName() + " in " + cls.getName() + " already declared with different @OnLifecycleEvent value: previous value " + event2 + ", new value " + event);
|
|
}
|
|
|
|
private CallbackInfo a(Class cls, Method[] methodArr) {
|
|
int i;
|
|
CallbackInfo a;
|
|
Class superclass = cls.getSuperclass();
|
|
HashMap hashMap = new HashMap();
|
|
if (superclass != null && (a = a(superclass)) != null) {
|
|
hashMap.putAll(a.b);
|
|
}
|
|
for (Class<?> cls2 : cls.getInterfaces()) {
|
|
for (Map.Entry<MethodReference, Lifecycle.Event> entry : a(cls2).b.entrySet()) {
|
|
a(hashMap, entry.getKey(), entry.getValue(), cls);
|
|
}
|
|
}
|
|
if (methodArr == null) {
|
|
methodArr = c(cls);
|
|
}
|
|
boolean z = false;
|
|
for (Method method : methodArr) {
|
|
OnLifecycleEvent onLifecycleEvent = (OnLifecycleEvent) method.getAnnotation(OnLifecycleEvent.class);
|
|
if (onLifecycleEvent != null) {
|
|
Class<?>[] parameterTypes = method.getParameterTypes();
|
|
if (parameterTypes.length <= 0) {
|
|
i = 0;
|
|
} else {
|
|
if (!parameterTypes[0].isAssignableFrom(LifecycleOwner.class)) {
|
|
throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
|
|
}
|
|
i = 1;
|
|
}
|
|
Lifecycle.Event value = onLifecycleEvent.value();
|
|
if (parameterTypes.length > 1) {
|
|
if (parameterTypes[1].isAssignableFrom(Lifecycle.Event.class)) {
|
|
if (value != Lifecycle.Event.ON_ANY) {
|
|
throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
|
|
}
|
|
i = 2;
|
|
} else {
|
|
throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
|
|
}
|
|
}
|
|
if (parameterTypes.length <= 2) {
|
|
a(hashMap, new MethodReference(i, method), value, cls);
|
|
z = true;
|
|
} else {
|
|
throw new IllegalArgumentException("cannot have more than 2 params");
|
|
}
|
|
}
|
|
}
|
|
CallbackInfo callbackInfo = new CallbackInfo(hashMap);
|
|
this.a.put(cls, callbackInfo);
|
|
this.b.put(cls, Boolean.valueOf(z));
|
|
return callbackInfo;
|
|
}
|
|
}
|