endian11
2020-01-02 2268d28bb43c2b3d7b4dfd96d35c0a844390c5bd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package safeluck.drive.evaluation.cEventCenter;
 
import android.text.TextUtils;
import android.util.Log;
 
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
 
/**
 * MyApplication2
 * Created by lzw on 2020/1/2. 14:03:04
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class CEventCenter {
 
    private static final String TAG = CEventCenter.class.getSimpleName();
 
    /**
     * 监听器列表,支持一对多存储
     */
    private static final HashMap<String , Object> LISTENER_MAP = new HashMap<>();
 
    /**
     * 监听器列表锁
     */
    private static final Object LISTENER_LOCK = new Object();
 
    /**
     * 事件对象池
     */
    private static final CEventObjPool POOL = new CEventObjPool(5);
 
    /**
     * 注册/注销监听器
     * @param toBind true注册  false注销
     * @param listener 监听器
     * @param topic 单个主题
     */
    public static void onBindEvent(boolean toBind,ICEventListener listener,String topic){
        onBindEvent(toBind,listener,new String[]{topic});
    }
 
    /**
     *
     * @param toBind
     * @param listener
     * @param topics 多个主题
     */
    public static void onBindEvent(boolean toBind,ICEventListener listener,String[] topics){
        if (toBind){
            registerEventListener(listener, topics);
        }else{
            unregisterEventListener(listener, topics);
        }
    }
 
    /**
     * 注册监听器
     * @param listener
     * @param topic 单个主题
     */
    public static void registerEventListener(ICEventListener listener, String topic){
        registerEventListener(listener,new String[]{topic});
    }
 
    /**
     * 注销监听器
     * @param listener
     * @param topic  单个个主题
     */
    private static void unregisterEventListener(ICEventListener listener, String topic) {
        unregisterEventListener(listener,new String[]{topic});
    }
 
    /**
     * 注销监听器
     * @param listener
     * @param topics 多个主题
     */
    private static void unregisterEventListener(ICEventListener listener, String[] topics) {
        if (null == listener || null == topics){
            return;
        }
        synchronized (LISTENER_LOCK){
            for (String topic :
                    topics) {
                if (TextUtils.isEmpty(topic)) {
                    continue;
                }
                Object obj = LISTENER_MAP.get(topic);
                if (null == obj){
                    continue;
                }else if (obj instanceof ICEventListener){
                    //有一个监听器
                    if (obj == listener){
                        LISTENER_MAP.remove(topic);
                    }
                }else if (obj instanceof List){
                    //有多个监听器
                    LinkedList<ICEventListener> listeners = (LinkedList<ICEventListener>)obj;
                    listeners.remove(listener);
                }
            }
        }
    }
    /**
     * 注册监听器
     * @param listener
     * @param topics 多个主题
     */
    private static void registerEventListener(ICEventListener listener, String[] topics) {
        if (null == listener || null == topics){
            return;
        }
        synchronized (LISTENER_LOCK){
            for (String topic :
                    topics) {
                if (TextUtils.isEmpty(topic)){
                    continue;
                }
                Object obj = LISTENER_MAP.get(topic);
                if (null == obj){
                    //还没有监听器,直接放到Map集合
                    LISTENER_MAP.put(topic,listener);
                }else if (obj instanceof  ICEventListener){
                    //有一个监听器
                    ICEventListener oldListener = (ICEventListener)obj;
                    if (listener == oldListener){
                        //去重
                        continue;
                    }
 
                    LinkedList<ICEventListener> list = new LinkedList<>();
                    list.add(oldListener);
                    list.add(listener);
                    LISTENER_MAP.put(topic,list);
                }else if (obj instanceof List){
                    //有多个监听器
                    LinkedList<ICEventListener> listeners = (LinkedList<ICEventListener>)obj;
                    if (listeners.indexOf(listener) >=0){
                        //去重
                        continue;
                    }
                    listeners.add(listener);
                }
            }
        }
    }
 
    public static void dispatchEvent(String topic, int msgCode, int resultCode, Object object){
        if (!TextUtils.isEmpty(topic)){
            CEvent event = POOL.get();
            event.setMsgCode(msgCode);
            event.setTopic(topic);
            event.setResultCode(resultCode);
            event.setObj(object);
 
            dispatchEvent(event);
        }
    }
 
    private static void dispatchEvent(CEvent event){
        if (LISTENER_MAP.size() == 0){
            return;
        }
        if (null != event && !TextUtils.isEmpty(event.getTopic())){
            String topic = event.getTopic();
 
            //通知事件监听器处理事件
            ICEventListener listener = null;
            LinkedList<ICEventListener> listeners = null;
            synchronized (LISTENER_LOCK){
                Log.d(TAG, "dispatchEvent | topic="+topic+"\tmsgCode="+event.getMsgCode()+"\tresultCode="+event.getResultCode()
                +"\tobj="+event.getObj());
                Object obj = LISTENER_MAP.get(topic);
                if (obj == null){
                    return;
                }
                if (obj instanceof ICEventListener){
                    listener = (ICEventListener)obj;
                }else if (obj instanceof List){
                    listeners = (LinkedList<ICEventListener>)((LinkedList)obj).clone();
                }
            }
 
            if (null != listener){
                listener.onCEvent(topic,event.getMsgCode(),event.getResultCode(),event.getObj());
            }else if (null != listeners && listeners.size()>0){
                for (ICEventListener
                     l:
                     listeners) {
                    l.onCEvent(topic, event.getMsgCode(), event.getResultCode(), event.getObj());
                }
            }
            //把对象放回池里
            POOL.returnObj(event);
        }
    }
 
}