Skip to content

Commit dae02a3

Browse files
committed
Merge pull request androidannotations#1101 from dodgex/ReceiverDataSchemes
New @receiver attribute: scheme androidannotations#1096
2 parents 20204df + 61cee35 commit dae02a3

9 files changed

Lines changed: 183 additions & 24 deletions

File tree

‎AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/Receiver.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181

8282
String[] actions();
8383

84+
String[] dataSchemes() default {};
85+
8486
RegisterAt registerAt() default RegisterAt.OnCreateOnDestroy;
8587

8688
boolean local() default false;

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ReceiverHandler.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ public void process(Element element, HasReceiverRegistration holder) throws Exce
6363

6464
Receiver annotation = element.getAnnotation(Receiver.class);
6565
String[] actions = annotation.actions();
66+
String[] dataSchemes = annotation.dataSchemes();
6667
Receiver.RegisterAt registerAt = annotation.registerAt();
6768
boolean local = annotation.local();
6869

69-
JFieldVar intentFilterField = holder.getIntentFilterField(actions);
70+
JFieldVar intentFilterField = holder.getIntentFilterField(actions, dataSchemes);
7071
JFieldVar receiverField = createReceiverField(holder, receiverName, methodName, hasIntentParam);
7172
registerAndUnregisterReceiver(holder, registerAt, intentFilterField, receiverField, local);
7273
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ public JBlock getOnDetachBeforeSuperBlock() {
723723
}
724724

725725
@Override
726-
public JFieldVar getIntentFilterField(String[] actions) {
727-
return receiverRegistrationHolder.getIntentFilterField(actions);
726+
public JFieldVar getIntentFilterField(String[] actions, String[] dataSchemes) {
727+
return receiverRegistrationHolder.getIntentFilterField(actions, dataSchemes);
728728
}
729729

730730
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ public JVar getOnActivityResultResultCodeParam() {
436436
}
437437

438438
@Override
439-
public JFieldVar getIntentFilterField(String[] actions) {
440-
return receiverRegistrationHolder.getIntentFilterField(actions);
439+
public JFieldVar getIntentFilterField(String[] actions, String[] dataSchemes) {
440+
return receiverRegistrationHolder.getIntentFilterField(actions, dataSchemes);
441441
}
442442

443443
@Override

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EServiceHolder.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public JDefinedClass getIntentBuilderClass() {
8484
}
8585

8686
@Override
87-
public JFieldVar getIntentFilterField(String[] actions) {
88-
return receiverRegistrationHolder.getIntentFilterField(actions);
87+
public JFieldVar getIntentFilterField(String[] actions, String[] dataSchemes) {
88+
return receiverRegistrationHolder.getIntentFilterField(actions, dataSchemes);
8989
}
9090

9191
@Override

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/HasReceiverRegistration.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
public interface HasReceiverRegistration extends GeneratedClassHolder {
2323

2424
JExpression getContextRef();
25-
JFieldVar getIntentFilterField(String[] actions);
25+
26+
JFieldVar getIntentFilterField(String[] actions, String[] dataSchemas);
2627

2728
JBlock getOnCreateAfterSuperBlock();
2829
JBlock getOnDestroyBeforeSuperBlock();

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/ReceiverRegistrationHolder.java‎

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,35 @@
2828
public class ReceiverRegistrationHolder {
2929

3030
private EComponentHolder holder;
31-
private Map<Set<String>, JFieldVar> intentFilterFields = new HashMap<Set<String>, JFieldVar>();
31+
private Map<IntentFilterData, JFieldVar> intentFilterFields = new HashMap<IntentFilterData, JFieldVar>();
3232
private IllegalStateException illegalStateException = new IllegalStateException("This shouldn't happen unless the validation is bad");
3333

3434
public ReceiverRegistrationHolder(EComponentHolder holder) {
3535
this.holder = holder;
3636
}
3737

38-
public JFieldVar getIntentFilterField(String[] actions) {
39-
Arrays.sort(actions);
40-
Set<String> actionSet = new HashSet<String>(Arrays.asList(actions));
41-
JFieldVar intentFilterField = intentFilterFields.get(actionSet);
38+
public JFieldVar getIntentFilterField(String[] actions, String[] dataSchemes) {
39+
IntentFilterData intentFilterData = new IntentFilterData(actions, dataSchemes);
40+
JFieldVar intentFilterField = intentFilterFields.get(intentFilterData);
4241
if (intentFilterField == null) {
43-
intentFilterField = createIntentFilterField(actionSet);
44-
intentFilterFields.put(actionSet, intentFilterField);
42+
intentFilterField = createIntentFilterField(intentFilterData);
43+
intentFilterFields.put(intentFilterData, intentFilterField);
4544
}
4645
return intentFilterField;
4746
}
4847

49-
private JFieldVar createIntentFilterField(Set<String> actionSet) {
50-
String intentFilterName = "intentFilter"+(intentFilterFields.size()+1)+ ModelConstants.GENERATION_SUFFIX;
48+
private JFieldVar createIntentFilterField(IntentFilterData intentFilterData) {
49+
String intentFilterName = "intentFilter" + (intentFilterFields.size() + 1) + ModelConstants.GENERATION_SUFFIX;
5150
JExpression newIntentFilterExpr = _new(classes().INTENT_FILTER);
5251
JFieldVar intentFilterField = getGeneratedClass().field(PRIVATE | FINAL, classes().INTENT_FILTER, intentFilterName, newIntentFilterExpr);
5352

5453
JBlock initBody = holder.getInitBody();
55-
for(String action : actionSet) {
54+
for (String action : intentFilterData.getActionSet()) {
5655
initBody.invoke(intentFilterField, "addAction").arg(action);
5756
}
57+
for (String dataScheme : intentFilterData.getDataSchemeSet()) {
58+
initBody.invoke(intentFilterField, "addDataScheme").arg(dataScheme);
59+
}
5860

5961
return intentFilterField;
6062
}
@@ -86,4 +88,61 @@ public JDefinedClass getGeneratedClass() {
8688
public ProcessHolder.Classes classes() {
8789
return holder.classes();
8890
}
91+
92+
private static class IntentFilterData {
93+
94+
private final Set<String> actionSet;
95+
private final Set<String> dataSchemeSet;
96+
97+
public IntentFilterData(String[] actions, String[] dataSchemes) {
98+
actionSet = new HashSet<String>(Arrays.asList(actions));
99+
dataSchemeSet = new HashSet<String>(Arrays.asList(dataSchemes));
100+
}
101+
102+
public Set<String> getActionSet() {
103+
return actionSet;
104+
}
105+
106+
public Set<String> getDataSchemeSet() {
107+
return dataSchemeSet;
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
final int prime = 31;
113+
int result = 1;
114+
result = prime * result + (actionSet == null ? 0 : actionSet.hashCode());
115+
result = prime * result + (dataSchemeSet == null ? 0 : dataSchemeSet.hashCode());
116+
return result;
117+
}
118+
119+
@Override
120+
public boolean equals(Object obj) {
121+
if (this == obj) {
122+
return true;
123+
}
124+
if (obj == null) {
125+
return false;
126+
}
127+
if (getClass() != obj.getClass()) {
128+
return false;
129+
}
130+
IntentFilterData other = (IntentFilterData) obj;
131+
if (actionSet == null) {
132+
if (other.actionSet != null) {
133+
return false;
134+
}
135+
} else if (!actionSet.equals(other.actionSet)) {
136+
return false;
137+
}
138+
if (dataSchemeSet == null) {
139+
if (other.dataSchemeSet != null) {
140+
return false;
141+
}
142+
} else if (!dataSchemeSet.equals(other.dataSchemeSet)) {
143+
return false;
144+
}
145+
return true;
146+
}
147+
}
89148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.androidannotations.test15.receiver;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.robolectric.Robolectric;
10+
import org.robolectric.RobolectricTestRunner;
11+
12+
import android.content.Intent;
13+
import android.net.Uri;
14+
import android.net.wifi.WifiManager;
15+
import android.support.v4.content.LocalBroadcastManager;
16+
17+
@RunWith(RobolectricTestRunner.class)
18+
public class ActivityWithReceiverTest {
19+
20+
private ActivityWithReceiver_ activity;
21+
22+
@Before
23+
public void setup() {
24+
activity = Robolectric.buildActivity(ActivityWithReceiver_.class)
25+
.create().start().resume().get();
26+
}
27+
28+
@Test
29+
public void onWifiStateChangedTest() {
30+
Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
31+
activity.sendBroadcast(intent);
32+
33+
assertTrue(activity.wifiChangeIntentReceived);
34+
}
35+
36+
@Test
37+
public void onLocalWifiStateChangedTest() {
38+
Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
39+
40+
LocalBroadcastManager.getInstance(Robolectric.application)
41+
.sendBroadcast(intent);
42+
43+
assertTrue(activity.localWifiChangeIntentReceived);
44+
}
45+
46+
// @Test
47+
// due to a bug in robolectric this test does not yet work
48+
// see https://github.com/robolectric/robolectric/issues/1244
49+
public void onDataShemeHttpTest() {
50+
Intent intentFtp = new Intent("CUSTOM_HTTP_ACTION");
51+
intentFtp.setData(Uri.parse("ftp://androidannotations.org"));
52+
activity.sendBroadcast(intentFtp);
53+
54+
assertFalse(activity.dataSchemeHttpIntentReceived);
55+
56+
Intent intentHttp = new Intent("CUSTOM_HTTP_ACTION");
57+
intentHttp.setData(Uri.parse("http://androidannotations.org"));
58+
activity.sendBroadcast(intentHttp);
59+
60+
assertTrue(activity.dataSchemeHttpIntentReceived);
61+
}
62+
63+
@Test
64+
public void onBroadcastWithTwoActionsTest() {
65+
Intent intent1 = new Intent("org.androidannotations.ACTION_1");
66+
Intent intent2 = new Intent("org.androidannotations.ACTION_2");
67+
68+
assertFalse(activity.action1Fired);
69+
assertFalse(activity.action2Fired);
70+
71+
activity.sendBroadcast(intent1);
72+
assertTrue(activity.action1Fired);
73+
assertFalse(activity.action2Fired);
74+
75+
activity.sendBroadcast(intent2);
76+
assertTrue(activity.action1Fired);
77+
assertTrue(activity.action2Fired);
78+
}
79+
80+
}

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/receiver/ActivityWithReceiver.java‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,42 @@
1818
import android.app.Activity;
1919
import android.content.Intent;
2020
import android.net.wifi.WifiManager;
21+
2122
import org.androidannotations.annotations.EActivity;
2223
import org.androidannotations.annotations.Receiver;
2324

2425
@EActivity
2526
public class ActivityWithReceiver extends Activity {
2627

28+
public boolean localWifiChangeIntentReceived = false;
29+
public boolean dataSchemeHttpIntentReceived = false;
30+
public boolean wifiChangeIntentReceived = false;
31+
public boolean action1Fired = false;
32+
public boolean action2Fired = false;
33+
2734
@Receiver(actions = WifiManager.NETWORK_STATE_CHANGED_ACTION, registerAt = Receiver.RegisterAt.OnResumeOnPause)
2835
protected void onWifiStateChanged(Intent intent) {
29-
36+
wifiChangeIntentReceived = true;
3037
}
3138

3239
@Receiver(actions = WifiManager.NETWORK_STATE_CHANGED_ACTION, registerAt = Receiver.RegisterAt.OnStartOnStop, local = true)
33-
protected void onWifiStateChangedWithSameActions() {
40+
protected void onLocalWifiStateChanged() {
41+
localWifiChangeIntentReceived = true;
42+
}
3443

44+
@Receiver(actions = "CUSTOM_HTTP_ACTION", dataSchemes = "http", registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
45+
protected void onDataShemeHttp(Intent intent) {
46+
dataSchemeHttpIntentReceived = true;
3547
}
3648

37-
@Receiver(actions = {"org.androidannotations.ACTION_1", "org.androidannotations.ACTION_2"},
38-
registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
49+
@Receiver(actions = { "org.androidannotations.ACTION_1",
50+
"org.androidannotations.ACTION_2" }, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
3951
protected void onBroadcastWithTwoActions(Intent intent) {
40-
52+
String action = intent.getAction();
53+
if (action.equals("org.androidannotations.ACTION_1")) {
54+
action1Fired = true;
55+
} else if (action.equals("org.androidannotations.ACTION_2")) {
56+
action2Fired = true;
57+
}
4158
}
42-
4359
}

0 commit comments

Comments
 (0)