Skip to content

Commit 2a38288

Browse files
committed
Merge pull request androidannotations#1718 from WonderCsabo/1717_receiverIntentAsExtra
Interpret @extra annotated params as extras with @receiver{Action}
2 parents 081eac0 + 50a27c5 commit 2a38288

8 files changed

Lines changed: 92 additions & 18 deletions

File tree

AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/AwaitingResultActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ public class AwaitingResultActivity extends Activity {
3131
static final int SECOND_REQUEST = 22;
3232
static final int THIRD_REQUEST = 33;
3333
static final int FORTH_REQUEST = 44;
34+
static final int FIFTH_REQUEST = 55;
3435
boolean onResultCalled = false;
3536
boolean onResultWithDataCalled = false;
3637
boolean onActivityResultWithResultCodeAndDataCalled = false;
3738
boolean onActivityResultWithDataAndResultCodeCalled = false;
3839
boolean onResultWithIntResultCodeCalled = false;
3940
boolean onResultWithIntegerResultCodeCalled = false;
4041
boolean onResultWithResultExtraCodeCalled = false;
42+
Intent originalIntent;
43+
Intent extraIntent;
4144

4245
@OnActivityResult(FIRST_REQUEST)
4346
void onResult() {
@@ -77,4 +80,10 @@ void onResultWithResultExtra(int resultCode, @OnActivityResult.Extra("value") in
7780
}
7881

7982
// CHECKSTYLE:ON
83+
84+
@OnActivityResult(FIFTH_REQUEST)
85+
void onResultWithIntentExtras(Intent originalIntent, @OnActivityResult.Extra Intent extraIntent) {
86+
this.originalIntent = originalIntent;
87+
this.extraIntent = extraIntent;
88+
}
8089
}

AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ereceiver/ReceiverWithActions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.androidannotations.annotations.ReceiverAction;
2020
import org.androidannotations.api.support.content.AbstractBroadcastReceiver;
2121

22+
import android.content.Intent;
23+
2224
@EReceiver
2325
public class ReceiverWithActions extends AbstractBroadcastReceiver {
2426

@@ -28,6 +30,7 @@ public class ReceiverWithActions extends AbstractBroadcastReceiver {
2830
public static final String ACTION_MULTIPLE_TEST_1 = "ACTION_MULTIPLE_TEST_1";
2931
public static final String ACTION_MULTIPLE_TEST_2 = "ACTION_MULTIPLE_TEST_2";
3032
public static final String ACTION_EXTRA_PARAMETER_TEST = "ACTION_EXTRA_PARAMETER_TEST";
33+
public static final String ACTION_EXTRA_INTENT_PARAMETERS_TEST = "ACTION_EXTRA_INTENT_PARAMETERS_TEST";
3134
public static final String EXTRA_ARG_NAME1 = "thisExtraHasAnotherName";
3235
public static final String EXTRA_ARG_NAME2 = "thisIsMyParameter";
3336
public static final String DATA_SCHEME = "http";
@@ -43,6 +46,9 @@ public class ReceiverWithActions extends AbstractBroadcastReceiver {
4346

4447
public int multipleActionCall = 0;
4548

49+
public Intent originalIntent;
50+
public Intent extraIntent;
51+
4652
@ReceiverAction(actions = ACTION_SIMPLE_TEST)
4753
public void onSimpleAction() {
4854
simpleActionReceived = true;
@@ -69,4 +75,10 @@ public void onExtraParameterAction(@ReceiverAction.Extra(EXTRA_ARG_NAME1) String
6975
public void onMultipleActions() {
7076
multipleActionCall++;
7177
}
78+
79+
@ReceiverAction(actions = ACTION_EXTRA_INTENT_PARAMETERS_TEST)
80+
public void onIntentParametersAction(Intent originalIntent, @ReceiverAction.Extra Intent extraIntent) {
81+
this.originalIntent = originalIntent;
82+
this.extraIntent = extraIntent;
83+
}
7284
}

AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/receiver/ActivityWithReceiver.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
@EActivity
2626
public class ActivityWithReceiver extends Activity {
2727

28+
public static final String ACTION_1 = "org.androidannotations.ACTION_1";
29+
public static final String ACTION_2 = "org.androidannotations.ACTION_2";
30+
public static final String CUSTOM_HTTP_ACTION = "CUSTOM_HTTP_ACTION";
31+
2832
public boolean localWifiChangeIntentReceived = false;
2933
public boolean dataSchemeHttpIntentReceived = false;
3034
public boolean wifiChangeIntentReceived = false;
@@ -33,6 +37,9 @@ public class ActivityWithReceiver extends Activity {
3337

3438
public String wifiSsid = null;
3539

40+
Intent originalIntent;
41+
Intent extraIntent;
42+
3643
@Receiver(actions = WifiManager.NETWORK_STATE_CHANGED_ACTION, registerAt = Receiver.RegisterAt.OnResumeOnPause)
3744
protected void onWifiStateChanged(Intent intent, @Receiver.Extra(WifiManager.EXTRA_BSSID) String ssid) {
3845
wifiChangeIntentReceived = true;
@@ -44,18 +51,24 @@ protected void onLocalWifiStateChanged() {
4451
localWifiChangeIntentReceived = true;
4552
}
4653

47-
@Receiver(actions = "CUSTOM_HTTP_ACTION", dataSchemes = "http", registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
54+
@Receiver(actions = CUSTOM_HTTP_ACTION, dataSchemes = "http", registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
4855
protected void onDataSchemeHttp(Intent intent) {
4956
dataSchemeHttpIntentReceived = true;
5057
}
5158

52-
@Receiver(actions = { "org.androidannotations.ACTION_1", "org.androidannotations.ACTION_2" }, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
59+
@Receiver(actions = { ACTION_1, ACTION_2 }, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
5360
protected void onBroadcastWithTwoActions(Intent intent) {
5461
String action = intent.getAction();
55-
if (action.equals("org.androidannotations.ACTION_1")) {
62+
if (action.equals(ACTION_1)) {
5663
action1Fired = true;
57-
} else if (action.equals("org.androidannotations.ACTION_2")) {
64+
} else if (action.equals(ACTION_2)) {
5865
action2Fired = true;
5966
}
6067
}
68+
69+
@Receiver(actions = ACTION_1)
70+
protected void onBroadcastWithExtras(Intent originalIntent, @Receiver.Extra Intent extraIntent) {
71+
this.originalIntent = originalIntent;
72+
this.extraIntent = extraIntent;
73+
}
6174
}

AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/AwaitingResultActivityTest.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@
1717

1818
import static org.fest.assertions.api.Assertions.assertThat;
1919

20+
import org.junit.Before;
2021
import org.junit.Test;
2122
import org.junit.runner.RunWith;
23+
import org.robolectric.Robolectric;
2224
import org.robolectric.RobolectricTestRunner;
2325

26+
import android.content.Intent;
27+
2428
@RunWith(RobolectricTestRunner.class)
2529
public class AwaitingResultActivityTest {
2630

31+
private AwaitingResultActivity_ activity;
32+
33+
@Before
34+
public void setUp() {
35+
activity = Robolectric.setupActivity(AwaitingResultActivity_.class);
36+
}
37+
2738
@Test
2839
public void onlyFirstRequestAnnotatedMethodAreCalled() {
29-
AwaitingResultActivity_ activity = new AwaitingResultActivity_();
30-
3140
activity.onActivityResult(AwaitingResultActivity.FIRST_REQUEST, 0, null);
3241

3342
assertThat(activity.onResultCalled).isTrue();
@@ -43,8 +52,6 @@ public void onlyFirstRequestAnnotatedMethodAreCalled() {
4352

4453
@Test
4554
public void onlySecondRequestAnnotatedMethodAreCalled() {
46-
AwaitingResultActivity_ activity = new AwaitingResultActivity_();
47-
4855
activity.onActivityResult(AwaitingResultActivity.SECOND_REQUEST, 0,
4956
null);
5057

@@ -61,8 +68,6 @@ public void onlySecondRequestAnnotatedMethodAreCalled() {
6168

6269
@Test
6370
public void onlyThirdRequestAnnotatedMethodAreCalled() {
64-
AwaitingResultActivity_ activity = new AwaitingResultActivity_();
65-
6671
activity.onActivityResult(AwaitingResultActivity.THIRD_REQUEST, 0, null);
6772

6873
assertThat(activity.onResultCalled).isFalse();
@@ -78,8 +83,6 @@ public void onlyThirdRequestAnnotatedMethodAreCalled() {
7883

7984
@Test
8085
public void onlyForthRequestAnnotatedMethodAreCalled() {
81-
AwaitingResultActivity_ activity = new AwaitingResultActivity_();
82-
8386
activity.onActivityResult(AwaitingResultActivity.FORTH_REQUEST, 0, null);
8487

8588
assertThat(activity.onResultCalled).isFalse();
@@ -92,4 +95,16 @@ public void onlyForthRequestAnnotatedMethodAreCalled() {
9295
assertThat(activity.onResultWithIntegerResultCodeCalled).isFalse();
9396
assertThat(activity.onResultWithResultExtraCodeCalled).isTrue();
9497
}
98+
99+
@Test
100+
public void onResultWithIntentExtrasPassed() {
101+
Intent intent = new Intent();
102+
Intent extraIntent = new Intent("someAction");
103+
intent.putExtra("extraIntent", extraIntent);
104+
105+
activity.onActivityResult(AwaitingResultActivity.FIFTH_REQUEST, 0, intent);
106+
107+
assertThat(activity.originalIntent).isEqualTo(intent);
108+
assertThat(activity.extraIntent).isEqualTo(extraIntent);
109+
}
95110
}

AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ereceiver/ReceiverWithActionsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,16 @@ public void onMultipleActionsTest() {
8585
receiver.onReceive(Robolectric.application, intent);
8686
assertEquals(2, receiver.multipleActionCall);
8787
}
88+
89+
@Test
90+
public void onIntentParametersActionTest() {
91+
Intent intent = new Intent(ReceiverWithActions.ACTION_EXTRA_INTENT_PARAMETERS_TEST);
92+
Intent extraIntent = new Intent("someAction");
93+
intent.putExtra("extraIntent", extraIntent);
94+
95+
receiver.onReceive(Robolectric.application, intent);
96+
97+
assertEquals(intent, receiver.originalIntent);
98+
assertEquals(extraIntent, receiver.extraIntent);
99+
}
88100
}

AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/receiver/ActivityWithReceiverTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.androidannotations.test.receiver;
1717

18+
import static org.junit.Assert.assertEquals;
1819
import static org.junit.Assert.assertFalse;
1920
import static org.junit.Assert.assertTrue;
2021

@@ -61,13 +62,13 @@ public void onLocalWifiStateChangedTest() {
6162

6263
@Test
6364
public void onDataShemeHttpTest() {
64-
Intent intentFtp = new Intent("CUSTOM_HTTP_ACTION");
65+
Intent intentFtp = new Intent(ActivityWithReceiver.CUSTOM_HTTP_ACTION);
6566
intentFtp.setData(Uri.parse("ftp://androidannotations.org"));
6667
activity.sendBroadcast(intentFtp);
6768

6869
assertFalse(activity.dataSchemeHttpIntentReceived);
6970

70-
Intent intentHttp = new Intent("CUSTOM_HTTP_ACTION");
71+
Intent intentHttp = new Intent(ActivityWithReceiver.CUSTOM_HTTP_ACTION);
7172
intentHttp.setData(Uri.parse("http://androidannotations.org"));
7273
activity.sendBroadcast(intentHttp);
7374

@@ -76,8 +77,8 @@ public void onDataShemeHttpTest() {
7677

7778
@Test
7879
public void onBroadcastWithTwoActionsTest() {
79-
Intent intent1 = new Intent("org.androidannotations.ACTION_1");
80-
Intent intent2 = new Intent("org.androidannotations.ACTION_2");
80+
Intent intent1 = new Intent(ActivityWithReceiver.ACTION_1);
81+
Intent intent2 = new Intent(ActivityWithReceiver.ACTION_2);
8182

8283
assertFalse(activity.action1Fired);
8384
assertFalse(activity.action2Fired);
@@ -91,4 +92,16 @@ public void onBroadcastWithTwoActionsTest() {
9192
assertTrue(activity.action2Fired);
9293
}
9394

95+
@Test
96+
public void onBroadcastWithExtrasTest() {
97+
Intent intent = new Intent(ActivityWithReceiver.ACTION_1);
98+
Intent extraIntent = new Intent("someAction");
99+
intent.putExtra("extraIntent", extraIntent);
100+
101+
activity.sendBroadcast(intent);
102+
103+
assertEquals(intent, activity.originalIntent);
104+
assertEquals(extraIntent, activity.extraIntent);
105+
}
106+
94107
}

AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ReceiverActionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement exec
131131

132132
if (extraParamClass.equals(getClasses().CONTEXT)) {
133133
callActionInvocation.arg(holder.getOnReceiveContext());
134-
} else if (extraParamClass.equals(getClasses().INTENT)) {
134+
} else if (extraParamClass.equals(getClasses().INTENT) && param.getAnnotation(ReceiverAction.Extra.class) == null) {
135135
callActionInvocation.arg(intent);
136136
} else if (param.getAnnotation(ReceiverAction.Extra.class) != null) {
137137
if (extras == null) {

AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ReceiverHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private JFieldVar createReceiverField(HasReceiverRegistration holder, String rec
119119

120120
if (extraParamClass.equals(getClasses().CONTEXT)) {
121121
methodCall.arg(contextVar);
122-
} else if (extraParamClass.equals(getClasses().INTENT)) {
122+
} else if (extraParamClass.equals(getClasses().INTENT) && param.getAnnotation(Receiver.Extra.class) == null) {
123123
methodCall.arg(intentVar);
124124
} else if (param.getAnnotation(Receiver.Extra.class) != null) {
125125
if (extras == null) {

0 commit comments

Comments
 (0)