From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 239 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java new file mode 100644 index 0000000..37f8b50 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java @@ -0,0 +1,239 @@ +package com.hdl.sdk.link.common.event; + +import androidx.annotation.NonNull; +import androidx.collection.ArrayMap; + + +import com.hdl.sdk.link.common.utils.LockArrayMap; +import com.hdl.sdk.link.common.utils.LockList; +import com.hdl.sdk.link.common.utils.LogUtils; +import com.hdl.sdk.link.common.utils.ThreadToolUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + + +/** + * Created by Tong on 2021/9/22. + * 浜嬩欢鍒嗗彂 + */ +public class EventDispatcher { + + private static final LockList<EventListener> ALL_TOPICS_EVENT = new LockList<EventListener>();//鎵�鏈変富棰樻秷鎭� + +// private static final ArrayMap<String, List<EventListener>> EVENT = new ArrayMap<>(); + private static final LockArrayMap<String, List<EventListener>> EVENT = new LockArrayMap<String, List<EventListener>>(); + + private static final ExecutorService ioThread = ThreadToolUtils.getInstance().newFixedThreadPool(2); + + private EventDispatcher() { + } + + private static class SingletonInstance { + private static final EventDispatcher INSTANCE = new EventDispatcher(); + } + + public static EventDispatcher getInstance() { + return SingletonInstance.INSTANCE; + } + + public synchronized void register(String tag, EventListener listener) { + try { + if (!EVENT.containsKey(tag)) { + EVENT.put(tag, new ArrayList<>()); + } + List<EventListener> events = EVENT.get(tag); + if (events != null && !events.contains(listener)) { + events.add(listener); + LogUtils.i(String.format("澧炲姞璁㈤槄涓婚:%s,褰撳墠鍥炶皟鏁伴噺:%s", tag, events.size())); + } + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + } + + public synchronized void registerIo(String tag, EventListener listener) { + try { + if (!EVENT.containsKey(tag)) { + EVENT.put(tag, new ArrayList<>()); + } + List<EventListener> events = EVENT.get(tag); + if (events != null && !events.contains(listener)) { + events.add(listener); + } + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + } + + public synchronized void remove(Object tag) { + ioThread.execute(new Runnable() { + @Override + public void run() { + try { + if (EVENT.containsKey(tag)) { +// List<EventListener> list = EVENT.get(tag); +// for (EventListener eventListener : list) { +// TYPE.remove(eventListener); +// } + EVENT.remove(tag); + } + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + + } + }); + } + + public synchronized void remove(Object tag, EventListener listener) { + ioThread.execute(new Runnable() { + @Override + public void run() { + try { + if (EVENT.containsKey(tag)) { + List<EventListener> ev = EVENT.get(tag); + if (ev != null && !ev.isEmpty()) { +// TYPE.remove(listener); + ev.remove(listener); + LogUtils.i(String.format("绉婚櫎璁㈤槄涓婚:%s,褰撳墠鍥炶皟鏁伴噺:%s",tag,ev.size())); + } + } + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + + } + }); + } + + /** + * 涓や釜涓婚鏄惁鍖归厤 + * @param desString 瀛楀吀涓殑涓婚 + * @param sourceString 鎺ユ敹鍒扮殑涓婚 + * @return + */ + boolean isMatch(String desString,String sourceString) { + String[] des = desString.split("/"); + String[] source = sourceString.split("/"); + if (des.length != source.length) { + return false; + } + for (int i = 0; i < des.length; i++) { + if (!(des[i].equals(source[i]) || des[i].equals("+"))) { + if (i != 2) { + //缃戝叧id涓嶅垽鏂紝鍙兘鏄疧id,涔熷彲鑳芥槸mac + return false; + } + } + } + return true; + } + + /** + * 浜嬩欢鍒嗗彂鍣紝鍒嗗彂鎵�鏈夊湪鎺ュ彛鍒楄〃涓殑浜嬩欢 + * @param topicTag + * @param o + */ + public synchronized void post(String topicTag, @NonNull Object o) { + try { + for (String key : EVENT.keySet()) { + if (!isMatch(key, topicTag)) { + continue; + } + List<EventListener> list = EVENT.get(key); + if (list != null && !list.isEmpty()) { + for (EventListener listener : list) { + ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { + @Override + public void run() { + try { + if (listener != null) { + listener.onMessage(o); + } + } catch (Exception e) { + LogUtils.e("post鏁版嵁寮傚父", o + " " + e.getMessage()); + } + } + }); + } + } + } + //鎵�鏈変富棰樼殑Listener閫氱煡 + if (ALL_TOPICS_EVENT == null || ALL_TOPICS_EVENT.isEmpty()) { + return; + } + //寮�鍙戝垎鍙戜簨浠� + for (EventListener listener : ALL_TOPICS_EVENT) { + ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { + @Override + public void run() { + if (listener != null) { + listener.onMessage(o); + } + } + }); + } + }catch (Exception e){ + LogUtils.e(e.getMessage()); + } + + } + + /** + * 鏂囦欢鍙戦�侀�氱煡 wxr 2022-03-08 15:38:59 + */ + public synchronized void filePost() { + //TODO + } + /** + * 娉ㄥ唽鎵�鏈変富棰樻秷鎭殑鐩戝惉 + * @param listener + */ + public synchronized void registerAllTopicsListener(EventListener listener) { + try { + if (ALL_TOPICS_EVENT != null && !ALL_TOPICS_EVENT.contains(listener)) { + ALL_TOPICS_EVENT.add(listener); + } +// TYPE.put(listener, MAIN_TYPE); + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + } + + /** + * 鍙栨秷鎵�鏈変富棰樻秷鎭殑鐩戝惉 + * @param listener + */ + public synchronized void removeAllTopicsListener(EventListener listener) { + ioThread.execute(new Runnable() { + @Override + public void run() { + try { + if (ALL_TOPICS_EVENT != null && !ALL_TOPICS_EVENT.isEmpty()) { +// TYPE.remove(listener); + ALL_TOPICS_EVENT.remove(listener); + } + } catch (Exception e) { + LogUtils.e(e.getMessage()); + } + } + }); + } + + public synchronized void clear() { + ALL_TOPICS_EVENT.clear(); + EVENT.clear(); +// TYPE.clear(); + } + + public synchronized void release() { + clear(); + ioThread.shutdownNow(); + } + + +} -- Gitblit v1.8.0