Skip to content

Commit 180d676

Browse files
committed
eventbus v1.0.2
1 parent 357923f commit 180d676

File tree

9 files changed

+258
-27
lines changed

9 files changed

+258
-27
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ dependencies {
150150
## 感谢
151151
在此非常感谢网友“淡蓝色的星期三”提出的bug以及反馈,也希望更多的朋友能够加入到Android EventBus的开发中来。
152152

153+
## 发布历史
154+
### V1.0.2 ( 2015.2.28 )
155+
1. 修复订阅方法的参数是基本类型( int, boolean等 )不能接收事件的问题。
156+
157+
### 1.0.1 ( 2015.2.13 )
158+
1. 修复订阅方法是基类,而发布事件时传递的是子类型导致订阅方法无法接收到事件的问题。
159+
160+
161+
### v1.0 ( 2015.2.9 )
162+
1. 事件总线框架发布,使用@Subcriber注解标识订阅方法;
163+
2. 订阅方法支持tag标识,使得事件投递更加精准。
164+
153165

154166
## License
155167
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.test;
26+
27+
import android.test.AndroidTestCase;
28+
29+
public class PrimitiveEventTest extends AndroidTestCase {
30+
31+
public void testPrimitiveType() {
32+
paramInt(2);
33+
}
34+
35+
public void paramInt(Object aParam) {
36+
assertEquals(Integer.class, aParam.getClass());
37+
}
38+
39+
// public void paramInt(int aParam) {
40+
//
41+
// }
42+
//
43+
// public void paramInt(Integer aParam) {
44+
//
45+
// }
46+
47+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.test;
26+
27+
import android.test.AndroidTestCase;
28+
29+
import org.simple.eventbus.EventBus;
30+
import org.simple.eventbus.EventType;
31+
import org.simple.eventbus.SubsciberMethodHunter;
32+
import org.simple.eventbus.Subscription;
33+
import org.simple.eventbus.test.mock.PrimitiveParamObject;
34+
35+
import java.util.List;
36+
37+
public class SubscriberMethodHunterTest extends AndroidTestCase {
38+
39+
SubsciberMethodHunter mHunter = new SubsciberMethodHunter(EventBus.getDefault()
40+
.getSubscriberMap());
41+
42+
PrimitiveParamObject object = new PrimitiveParamObject();
43+
44+
protected void setUp() throws Exception {
45+
super.setUp();
46+
EventBus.getDefault().register(object);
47+
}
48+
49+
protected void tearDown() throws Exception {
50+
super.tearDown();
51+
EventBus.getDefault().unregister(object);
52+
}
53+
54+
public void testFindPrimitiveParamMethod() {
55+
mHunter.findSubcribeMethods(object);
56+
List<Subscription> subscriptions = EventBus.getDefault().getSubscriberMap()
57+
.get(new EventType(Integer.class));
58+
assertEquals(1, subscriptions.size());
59+
assertEquals(object, subscriptions.get(0).subscriber);
60+
}
61+
62+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.test.mock;
26+
27+
import org.simple.eventbus.Subcriber;
28+
29+
/**
30+
* @author mrsimple
31+
*/
32+
public class PrimitiveParamObject {
33+
@Subcriber
34+
public void intParam(int aInt) {
35+
36+
}
37+
38+
@Subcriber
39+
public void booleanParam(boolean ab) {
40+
41+
}
42+
43+
@Subcriber
44+
public void floatParam(float aFloat) {
45+
46+
}
47+
48+
@Subcriber
49+
public void doubleParam(double aDouble) {
50+
51+
}
52+
53+
@Subcriber
54+
public void intArrayParam(int[] ints) {
55+
56+
}
57+
}

Simple_eventbus_demo/res/layout/menu_fragment.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
android:layout_height="wrap_content"
4242
android:text="@string/post_to_thread" />
4343

44+
<Button
45+
android:id="@+id/post_primitive_btn"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:text="@string/post_primitive" />
49+
4450
<TextView
4551
android:id="@+id/click_tv"
4652
android:layout_width="match_parent"

Simple_eventbus_demo/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<string name="post_list">post list data</string>
99
<string name="user_click_hint">no one click</string>
1010
<string name="post_to_thread">post to thread</string>
11+
<string name="post_primitive">post primitive type</string>
1112
<string name="execute_async">this execute async, thread = </string>
1213

1314
</resources>

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/ConstactFragment.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
7272
});
7373
}
7474

75+
@Subcriber
76+
private void primitiveParam(int aInt) {
77+
Toast.makeText(getActivity(), "int = " + aInt, Toast.LENGTH_SHORT).show();
78+
}
79+
80+
@Subcriber
81+
private void primitiveArrayParam(int[] aInt) {
82+
Toast.makeText(getActivity(), "int array = " + aInt[0] + ", " + aInt[1], Toast.LENGTH_SHORT)
83+
.show();
84+
}
85+
86+
@Subcriber
87+
private void primitiveParam(boolean ab) {
88+
Toast.makeText(getActivity(), "boolean = " + ab, Toast.LENGTH_SHORT).show();
89+
}
90+
7591
@Override
7692
public void onDestroy() {
7793
EventBus.getDefault().unregister(this);

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/MenuFragment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ public void onClick(View v) {
138138
}
139139
});
140140

141+
// 发布事件,事件类型为原始类型,比如int, boolean, float等
142+
rootView.findViewById(R.id.post_primitive_btn).setOnClickListener(new OnClickListener() {
143+
144+
@Override
145+
public void onClick(View v) {
146+
EventBus.getDefault().post(12345);
147+
// 整型数组
148+
EventBus.getDefault().post(new int[] {
149+
12, 24
150+
});
151+
EventBus.getDefault().post(true);
152+
}
153+
});
154+
141155
startThreads();
142156

143157
EventBus.getDefault().register(this);

src/org/simple/eventbus/SubsciberMethodHunter.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ public SubsciberMethodHunter(Map<EventType, CopyOnWriteArrayList<Subscription>>
5757
* @param subscriber
5858
* @return
5959
*/
60-
public List<Subscription> findSubcribeMethods(Object subscriber) {
60+
public void findSubcribeMethods(Object subscriber) {
6161
if (mSubcriberMap == null) {
6262
throw new NullPointerException("the mSubcriberMap is null. ");
6363
}
64-
65-
List<Subscription> resultMethods = new CopyOnWriteArrayList<Subscription>();
6664
Class<?> clazz = subscriber.getClass();
67-
// 查找类中复合要求的方法,直到Object类
65+
// 查找类中符合要求的注册方法,直到Object类
6866
while (clazz != null && !isSystemCalss(clazz.getName())) {
6967
final Method[] allMethods = clazz.getDeclaredMethods();
7068
for (int i = 0; i < allMethods.length; i++) {
@@ -76,8 +74,9 @@ public List<Subscription> findSubcribeMethods(Object subscriber) {
7674
Class<?>[] paramsTypeClass = method.getParameterTypes();
7775
// just only one param
7876
if (paramsTypeClass != null && paramsTypeClass.length == 1) {
79-
EventType eventType = new EventType(paramsTypeClass[0], annotation.tag());
80-
TargetMethod subscribeMethod = new TargetMethod(method, paramsTypeClass[0],
77+
Class<?> paramType = convertType(paramsTypeClass[0]);
78+
EventType eventType = new EventType(paramType, annotation.tag());
79+
TargetMethod subscribeMethod = new TargetMethod(method, paramType,
8180
annotation.mode());
8281
subscibe(eventType, subscribeMethod, subscriber);
8382
}
@@ -87,7 +86,28 @@ public List<Subscription> findSubcribeMethods(Object subscriber) {
8786
// 获取父类,以继续查找父类中复合要求的方法
8887
clazz = clazz.getSuperclass();
8988
}
90-
return resultMethods;
89+
}
90+
91+
/**
92+
* @param event
93+
* @param method
94+
* @param subscriber
95+
*/
96+
private void subscibe(EventType event, TargetMethod method, Object subscriber) {
97+
CopyOnWriteArrayList<Subscription> subscriptionLists = mSubcriberMap
98+
.get(event);
99+
if (subscriptionLists == null) {
100+
subscriptionLists = new CopyOnWriteArrayList<Subscription>();
101+
}
102+
103+
Subscription newSubscription = new Subscription(subscriber, method);
104+
if (subscriptionLists.contains(newSubscription)) {
105+
return;
106+
}
107+
108+
subscriptionLists.add(newSubscription);
109+
// 将事件类型key和订阅者信息存储到map中
110+
mSubcriberMap.put(event, subscriptionLists);
91111
}
92112

93113
/**
@@ -123,31 +143,27 @@ public void removeMethodsFromMap(Object subscriber) {
123143
}
124144

125145
/**
126-
* @param event
127-
* @param method
128-
* @param subscriber
146+
* if the subscriber method's type is primitive, convert it to corresponding
147+
* Object type. for example, int to Integer.
148+
*
149+
* @param eventType origin Event Type
150+
* @return
129151
*/
130-
private void subscibe(EventType event, TargetMethod method, Object subscriber) {
131-
CopyOnWriteArrayList<Subscription> subscriptionLists = mSubcriberMap
132-
.get(event);
133-
if (subscriptionLists == null) {
134-
subscriptionLists = new CopyOnWriteArrayList<Subscription>();
135-
}
136-
137-
Subscription newSubscription = new Subscription(subscriber, method);
138-
if (subscriptionLists.contains(newSubscription)) {
139-
return;
152+
private Class<?> convertType(Class<?> eventType) {
153+
Class<?> returnClass = eventType;
154+
if (eventType.equals(boolean.class)) {
155+
returnClass = Boolean.class;
156+
} else if (eventType.equals(int.class)) {
157+
returnClass = Integer.class;
158+
} else if (eventType.equals(float.class)) {
159+
returnClass = Float.class;
160+
} else if (eventType.equals(double.class)) {
161+
returnClass = Double.class;
140162
}
141163

142-
subscriptionLists.add(newSubscription);
143-
// 将事件类型key和订阅者信息存储到map中
144-
mSubcriberMap.put(event, subscriptionLists);
164+
return returnClass;
145165
}
146166

147-
// private boolean isObjectClass(Class<?> clazz) {
148-
// return clazz.getName().equals(Object.class.getName());
149-
// }
150-
151167
private boolean isSystemCalss(String name) {
152168
return name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.");
153169
}

0 commit comments

Comments
 (0)