Skip to content

Commit 72e618b

Browse files
authored
Merge pull request androidannotations#1830 from simopete/1656_EView_EViewGroup_Receiver
@receiver support for @eview and @eviewgroup
2 parents c5244f1 + f892b64 commit 72e618b

17 files changed

Lines changed: 497 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ enum RegisterAt {
143143
OnResumeOnPause, //
144144
/**
145145
* Register in the onAttach method, unregister in the onDetach method.
146+
* Except for View which will register in the onAttachedToWindow and
147+
* unregister in the onDetachedFromWindow.
146148
*/
147149
OnAttachOnDetach
148150
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<activity android:name=".innerclasses.ActivityWithInnerEnhancedClasses_" />
6262
<activity android:name=".MultiFindViewActivity_" />
6363
<activity android:name=".receiver.ActivityWithReceiver_" />
64+
<activity android:name=".receiver.EViewAndEViewGroupWithReceiverActivity_" />
6465
<activity android:name=".menu.OptionsMenuActivity_" />
6566
<activity android:name=".menu.OptionsMenuSubActivity_" />
6667
<activity android:name=".menu.InjectMenuActivity_" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.receiver;
17+
18+
import org.androidannotations.annotations.EActivity;
19+
import org.androidannotations.annotations.ViewById;
20+
import org.androidannotations.test.R;
21+
22+
import android.app.Activity;
23+
24+
@EActivity(R.layout.activity_eview_eviewgroup_receiver)
25+
public class EViewAndEViewGroupWithReceiverActivity extends Activity {
26+
27+
@ViewById
28+
ViewWithReceiver viewWithReceiver;
29+
30+
@ViewById
31+
ViewGroupWithReceiver viewGroupWithReceiver;
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.receiver;
17+
18+
public abstract class ReceiverActions {
19+
20+
public static final String ACTION_0 = "org.androidannotations.ACTION_0";
21+
public static final String ACTION_1 = "org.androidannotations.ACTION_1";
22+
public static final String ACTION_2 = "org.androidannotations.ACTION_2";
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.receiver;
17+
18+
import org.androidannotations.annotations.EViewGroup;
19+
import org.androidannotations.annotations.Receiver;
20+
21+
import android.content.Context;
22+
import android.content.Intent;
23+
import android.util.AttributeSet;
24+
import android.widget.LinearLayout;
25+
26+
@EViewGroup
27+
public class ViewGroupWithReceiver extends LinearLayout {
28+
29+
public boolean action0Received = false;
30+
public boolean action1Received = false;
31+
public String action1Extra;
32+
public boolean action2Received = false;
33+
public Intent action2Extra;
34+
35+
public ViewGroupWithReceiver(Context context) {
36+
super(context);
37+
}
38+
39+
public ViewGroupWithReceiver(Context context, AttributeSet attrs) {
40+
super(context, attrs);
41+
}
42+
43+
@Receiver(actions = ReceiverActions.ACTION_0, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
44+
void receive0() {
45+
action0Received = true;
46+
}
47+
48+
@Receiver(actions = ReceiverActions.ACTION_1, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
49+
void receive1(@Receiver.Extra("extra") String extra) {
50+
action1Received = true;
51+
action1Extra = extra;
52+
}
53+
54+
@Receiver(actions = ReceiverActions.ACTION_2, local = true, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
55+
protected void receive2(@Receiver.Extra Intent extra) {
56+
action2Received = true;
57+
action2Extra = extra;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.receiver;
17+
18+
import org.androidannotations.annotations.EView;
19+
import org.androidannotations.annotations.Receiver;
20+
21+
import android.content.Context;
22+
import android.content.Intent;
23+
import android.util.AttributeSet;
24+
import android.view.View;
25+
26+
@EView
27+
public class ViewWithReceiver extends View {
28+
29+
public boolean action0Received = false;
30+
public boolean action1Received = false;
31+
public String action1Extra;
32+
public boolean action2Received = false;
33+
public Intent action2Extra;
34+
35+
public ViewWithReceiver(Context context) {
36+
super(context);
37+
}
38+
39+
public ViewWithReceiver(Context context, AttributeSet attrs) {
40+
super(context, attrs);
41+
}
42+
43+
@Receiver(actions = ReceiverActions.ACTION_0, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
44+
void receive0() {
45+
action0Received = true;
46+
}
47+
48+
@Receiver(actions = ReceiverActions.ACTION_1, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
49+
void receive1(@Receiver.Extra("extra") String extra) {
50+
action1Received = true;
51+
action1Extra = extra;
52+
}
53+
54+
@Receiver(actions = ReceiverActions.ACTION_2, local = true, registerAt = Receiver.RegisterAt.OnAttachOnDetach)
55+
protected void receive2(@Receiver.Extra Intent extra) {
56+
action2Received = true;
57+
action2Extra = extra;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2016 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="fill_parent"
21+
android:layout_height="fill_parent"
22+
android:orientation="vertical">
23+
24+
<org.androidannotations.test.receiver.ViewWithReceiver_
25+
android:id="@+id/viewWithReceiver"
26+
android:layout_width="fill_parent"
27+
android:layout_height="wrap_content"/>
28+
29+
<org.androidannotations.test.receiver.ViewGroupWithReceiver_
30+
android:id="@+id/viewGroupWithReceiver"
31+
android:layout_width="fill_parent"
32+
android:layout_height="wrap_content"/>
33+
34+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.receiver;
17+
18+
import static org.junit.Assert.assertTrue;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.robolectric.Robolectric;
24+
import org.robolectric.RobolectricTestRunner;
25+
26+
import android.content.Intent;
27+
import android.support.v4.content.LocalBroadcastManager;
28+
29+
@RunWith(RobolectricTestRunner.class)
30+
public class EViewAndEViewGroupWithReceiverTest {
31+
32+
private EViewAndEViewGroupWithReceiverActivity_ activity;
33+
34+
@Before
35+
public void setUp() {
36+
activity = Robolectric.setupActivity(EViewAndEViewGroupWithReceiverActivity_.class);
37+
}
38+
39+
@Test
40+
public void eViewAndEViewGroupReceivedAction0Test() {
41+
Intent intent = new Intent(ReceiverActions.ACTION_0);
42+
activity.sendBroadcast(intent);
43+
44+
assertTrue(activity.viewWithReceiver.action0Received);
45+
assertTrue(activity.viewGroupWithReceiver.action0Received);
46+
}
47+
48+
@Test
49+
public void eViewAndEViewGroupReceivedAction1WithExtraTest() {
50+
String extraContent = "extraContent";
51+
Intent intent = new Intent(ReceiverActions.ACTION_1);
52+
intent.putExtra("extra", extraContent);
53+
activity.sendBroadcast(intent);
54+
55+
assertTrue(activity.viewWithReceiver.action1Received);
56+
assertTrue(activity.viewWithReceiver.action1Extra.equals(extraContent));
57+
assertTrue(activity.viewGroupWithReceiver.action1Received);
58+
assertTrue(activity.viewGroupWithReceiver.action1Extra.equals(extraContent));
59+
}
60+
61+
@Test
62+
public void eViewAndEViewGroupReceivedAction2WithExtraTest() {
63+
Intent intent = new Intent(ReceiverActions.ACTION_2);
64+
Intent extraIntent = new Intent("someAction");
65+
intent.putExtra("extra", extraIntent);
66+
67+
LocalBroadcastManager.getInstance(Robolectric.application).sendBroadcast(intent);
68+
69+
assertTrue(activity.viewWithReceiver.action2Received);
70+
assertTrue(activity.viewWithReceiver.action2Extra.equals(extraIntent));
71+
assertTrue(activity.viewGroupWithReceiver.action2Received);
72+
assertTrue(activity.viewGroupWithReceiver.action2Extra.equals(extraIntent));
73+
}
74+
75+
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.androidannotations.annotations.EIntentService;
5757
import org.androidannotations.annotations.EReceiver;
5858
import org.androidannotations.annotations.EService;
59+
import org.androidannotations.annotations.EView;
60+
import org.androidannotations.annotations.EViewGroup;
5961
import org.androidannotations.annotations.Trace;
6062
import org.androidannotations.annotations.ViewById;
6163
import org.androidannotations.internal.core.model.AndroidSystemServices;
@@ -168,8 +170,8 @@ public void enclosingElementHasEActivityOrEFragment(Element element, ElementVali
168170
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
169171
}
170172

171-
public void enclosingElementHasEActivityOrEFragmentOrEServiceOrEIntentService(Element element, ElementValidation valid) {
172-
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EService.class, EIntentService.class);
173+
public void enclosingElementHasEActivityOrEFragmentOrEServiceOrEIntentServiceOrEViewOrEViewGroup(Element element, ElementValidation valid) {
174+
List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EService.class, EIntentService.class, EView.class, EViewGroup.class);
173175
enclosingElementHasOneOfAnnotations(element, validAnnotations, valid);
174176
}
175177

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public JBlock getOnStopBeforeSuperBlock() {
125125

126126
@Override
127127
public JBlock getOnResumeAfterSuperBlock() {
128-
return receiverRegistrationDelegate.getOnAttachAfterSuperBlock();
128+
return receiverRegistrationDelegate.getOnResumeAfterSuperBlock();
129129
}
130130

131131
@Override

0 commit comments

Comments
 (0)