From 90d5f028ccdaaaf64286f9d632cb335a4d0544b9 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期二, 07 一月 2025 09:40:52 +0800
Subject: [PATCH] Merge branch '1.5.1'

---
 sdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java |  298 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 298 insertions(+), 0 deletions(-)

diff --git a/sdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java b/sdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java
new file mode 100644
index 0000000..b1a2535
--- /dev/null
+++ b/sdk/src/main/java/com/hdl/sdk/link/common/event/EventDispatcher.java
@@ -0,0 +1,298 @@
+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 LockList<EventListener> ASYNC_EVENT = new LockList<>();
+
+    private static final ExecutorService ioThread = ThreadToolUtils.getInstance().newFixedThreadPool(5);
+
+    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());
+//        }
+//    }
+
+    /**
+     * 闇�瑕佸紓姝ュ洖璋冪殑鎺ュ彛
+     * @param tag
+     * @param listener
+     */
+    public synchronized void asyncRegister(String tag, EventListener listener) {
+        register(tag, listener);
+        if (!ASYNC_EVENT.contains(listener)) {
+            ASYNC_EVENT.add(listener);
+        }
+    }
+
+    public synchronized void remove(String 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);
+                            ASYNC_EVENT.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 {
+            LogUtils.e("EventDispatcher", "===15");
+            for (String key : EVENT.keySet()) {
+                if (!isMatch(key, topicTag)) {
+                    continue;
+                }
+                LogUtils.e("EventDispatcher", "===16");
+                List<EventListener> list = EVENT.get(key);
+                if (list != null && !list.isEmpty()) {
+                    for (EventListener listener : list) {
+                        if(listener==null){
+                            continue;
+                        }
+                        LogUtils.e("EventDispatcher", "===17");
+                        //闇�瑕佸紓姝ュ洖璋冪殑
+                        if(ASYNC_EVENT.contains(listener)) {
+                            LogUtils.e("EventDispatcher", "===18");
+                            runOnSubThread(listener,o);
+                        }
+                        else {
+                            LogUtils.e("EventDispatcher", "===19");
+                            runOnUIThread(listener,o);
+                        }
+                    }
+                }
+            }
+            //鎵�鏈変富棰樼殑Listener閫氱煡
+            if (ALL_TOPICS_EVENT == null || ALL_TOPICS_EVENT.isEmpty()) {
+                return;
+            }
+            LogUtils.e("EventDispatcher", "===20");
+            //寮�鍙戝垎鍙戜簨浠�
+            for (EventListener listener : ALL_TOPICS_EVENT) {
+                runOnUIThread(listener, o);
+            }
+            LogUtils.e("EventDispatcher", "===21");
+        }catch (Exception e){
+            LogUtils.e(e.getMessage());
+        }
+
+    }
+
+    /**
+     * 杩愯鍦ㄥ瓙绾跨▼
+     * @param eventListener
+     * @param o
+     */
+    private void runOnSubThread(EventListener eventListener,Object o) {
+        if (eventListener == null) {
+            return;
+        }
+        ioThread.execute(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    LogUtils.e("EventDispatcher", "===22");
+                    eventListener.onMessage(o);
+                } catch (Exception e) {
+                    LogUtils.e("runOnSubThread鏁版嵁寮傚父", o + " " + e.getMessage());
+                }
+            }
+        });
+    }
+
+    /**
+     * 杩愯鍒颁富绾跨▼
+     * @param eventListener
+     * @param o
+     */
+    private void runOnUIThread(EventListener eventListener,Object o) {
+        if (eventListener == null) {
+            return;
+        }
+        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    LogUtils.e("EventDispatcher", "===23");
+                    eventListener.onMessage(o);
+                } catch (Exception e) {
+                    LogUtils.e("runOnUIThread鏁版嵁寮傚父", o + " " + 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();
+        ASYNC_EVENT.clear();
+//        TYPE.clear();
+    }
+
+    public synchronized void release() {
+        clear();
+        ioThread.shutdownNow();
+    }
+
+
+}

--
Gitblit v1.8.0