package zj.xuitls.view; import android.text.TextUtils; import android.view.View; import java.lang.ref.WeakReference; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import zj.xuitls.common.util.DoubleKeyValueMap; import zj.xuitls.common.util.LogUtil; import zj.xuitls.view.annotation.Event; final class EventListenerManager { private static final HashSet<String> AVOID_QUICK_EVENT_SET; private static final long QUICK_EVENT_TIME_SPAN = 300; private static final DoubleKeyValueMap<ViewInfo, Class<?>, Object> listenerCache = new DoubleKeyValueMap<>(); static { HashSet<String> hashSet = new HashSet<>(2); AVOID_QUICK_EVENT_SET = hashSet; hashSet.add("onClick"); hashSet.add("onItemClick"); } private EventListenerManager() { } public static void addEventMethod(ViewFinder viewFinder, ViewInfo viewInfo, Event event, Object obj, Method method) { boolean z; try { View findViewByInfo = viewFinder.findViewByInfo(viewInfo); if (findViewByInfo != null) { Class<?> type = event.type(); String str = event.setter(); if (TextUtils.isEmpty(str)) { str = "set" + type.getSimpleName(); } String method2 = event.method(); DoubleKeyValueMap<ViewInfo, Class<?>, Object> doubleKeyValueMap = listenerCache; Object obj2 = doubleKeyValueMap.get(viewInfo, type); if (obj2 != null) { DynamicHandler dynamicHandler = (DynamicHandler) Proxy.getInvocationHandler(obj2); z = obj.equals(dynamicHandler.getHandler()); if (z) { dynamicHandler.addMethod(method2, method); } } else { z = false; } if (!z) { DynamicHandler dynamicHandler2 = new DynamicHandler(obj); dynamicHandler2.addMethod(method2, method); obj2 = Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, dynamicHandler2); doubleKeyValueMap.put(viewInfo, type, obj2); } findViewByInfo.getClass().getMethod(str, type).invoke(findViewByInfo, obj2); } } catch (Throwable th) { LogUtil.e(th.getMessage(), th); } } public static class DynamicHandler implements InvocationHandler { private static long lastClickTime; private WeakReference<Object> handlerRef; private final HashMap<String, Method> methodMap = new HashMap<>(1); public DynamicHandler(Object obj) { this.handlerRef = new WeakReference<>(obj); } public void addMethod(String str, Method method) { this.methodMap.put(str, method); } public Object getHandler() { return this.handlerRef.get(); } @Override // java.lang.reflect.InvocationHandler public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable { Object obj2 = this.handlerRef.get(); if (obj2 != null) { String name = method.getName(); if ("toString".equals(name)) { return DynamicHandler.class.getSimpleName(); } Method method2 = this.methodMap.get(name); if (method2 == null && this.methodMap.size() == 1) { Iterator<Map.Entry<String, Method>> it = this.methodMap.entrySet().iterator(); if (it.hasNext()) { Map.Entry<String, Method> next = it.next(); if (TextUtils.isEmpty(next.getKey())) { method2 = next.getValue(); } } } if (method2 != null) { if (EventListenerManager.AVOID_QUICK_EVENT_SET.contains(name)) { long currentTimeMillis = System.currentTimeMillis() - lastClickTime; if (currentTimeMillis <= 0 || currentTimeMillis >= EventListenerManager.QUICK_EVENT_TIME_SPAN) { lastClickTime = System.currentTimeMillis(); } else { LogUtil.d("onClick cancelled: " + currentTimeMillis); return null; } } try { return method2.invoke(obj2, objArr); } catch (Throwable th) { throw new RuntimeException("invoke method error:" + obj2.getClass().getName() + "#" + method2.getName(), th); } } else { LogUtil.w("method not impl: " + name + "(" + obj2.getClass().getSimpleName() + ")"); } } return null; } } }