Skip to content

Commit dcc3fd1

Browse files
committed
Merge branch '741_forcelayoutinjection' into develop
2 parents 4068475 + d3939a1 commit dcc3fd1

7 files changed

Lines changed: 123 additions & 15 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
/**
2424
* <p>
25-
* Should be used on android.app.Fragment or
26-
* link android.support.v4.app.Fragment classes to enable usage of
25+
* Should be used on android.app.Fragment or link
26+
* android.support.v4.app.Fragment classes to enable usage of
2727
* AndroidAnnotations.
2828
* </p>
2929
* <p>
@@ -46,6 +46,11 @@
4646
* <code>onCreateView()</code>.
4747
* </p>
4848
* <p>
49+
* We only set the layout if the <code>onCreateView()</code> method returns null
50+
* from the annotated class. If you want to set the layout regardless of that
51+
* return value, use the {@link #forceLayoutInjection()} annotation parameter.
52+
* </p>
53+
* <p>
4954
* The generated class will also contain a FragmentBuilder to build fragment
5055
* with a fluent API. Arguments can be passed by using {@link FragmentArg}
5156
* annotation on every native or serializable/parcelable field.
@@ -106,4 +111,13 @@
106111
int value() default ResId.DEFAULT_VALUE;
107112

108113
String resName() default "";
114+
115+
/**
116+
* We only set the layout if the <code>onCreateView()</code> method returns
117+
* null from the annotated class. If you want to set the layout regardless
118+
* of that return value, pass <b>true</b> to this annotation parameter.
119+
*
120+
* @return <b>true</b>, if the layout must be set, <b>false</b> otherwise
121+
*/
122+
boolean forceLayoutInjection() default false;
109123
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ public void process(Element element, EFragmentHolder holder) {
7474

7575
JFieldVar contentView = holder.getContentView();
7676

77-
block._if(contentView.eq(_null())) //
78-
._then() //
79-
.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
77+
boolean forceInjection = element.getAnnotation(EFragment.class).forceLayoutInjection();
78+
79+
if (!forceInjection) {
80+
block._if(contentView.eq(_null())) //
81+
._then() //
82+
.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
83+
} else {
84+
block.assign(contentView, inflater.invoke("inflate").arg(contentViewId).arg(container).arg(FALSE));
85+
}
86+
8087
}
8188

8289
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.sun.codemodel.JMod;
2828
import com.sun.codemodel.JTypeVar;
2929
import com.sun.codemodel.JVar;
30+
import org.androidannotations.annotations.EFragment;
3031
import org.androidannotations.helper.ActionBarSherlockHelper;
3132
import org.androidannotations.helper.AnnotationHelper;
3233
import org.androidannotations.process.ProcessHolder;
@@ -247,8 +248,12 @@ private void setOnCreateView() {
247248

248249
JVar savedInstanceState = onCreateView.param(classes().BUNDLE, "savedInstanceState");
249250

251+
boolean forceInjection = getAnnotatedElement().getAnnotation(EFragment.class).forceLayoutInjection();
252+
250253
JBlock body = onCreateView.body();
251-
body.assign(contentView, _super().invoke(onCreateView).arg(inflater).arg(container).arg(savedInstanceState));
254+
255+
if (!forceInjection)
256+
body.assign(contentView, _super().invoke(onCreateView).arg(inflater).arg(container).arg(savedInstanceState));
252257

253258
setContentViewBlock = body.block();
254259

‎AndroidAnnotations/functional-test-1-5/res/layout/list_fragment.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@
3737
android:layout_height="match_parent"
3838
android:background="#FF0000"
3939
android:text="No data" />
40+
41+
<Button
42+
android:id="@+id/conventionButton"
43+
android:layout_width="match_parent"
44+
android:layout_height="wrap_content" />
4045

4146
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (C) 2010-2014 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.test15.efragment;
17+
18+
import org.androidannotations.annotations.EFragment;
19+
import org.androidannotations.test15.R;
20+
21+
import android.support.v4.app.ListFragment;
22+
23+
@EFragment(value = R.layout.list_fragment, forceLayoutInjection = true)
24+
public class ForceLayoutInjectedListFragment extends ListFragment {
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2010-2014 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.test15.efragment;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
import static org.robolectric.util.FragmentTestUtil.startFragment;
20+
21+
import org.androidannotations.test15.R;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.robolectric.RobolectricTestRunner;
25+
26+
import android.view.View;
27+
28+
@RunWith(RobolectricTestRunner.class)
29+
public class ForceLayoutInjectedListFragmentTest {
30+
31+
@Test
32+
public void testLayoutForceInjected() {
33+
ForceLayoutInjectedListFragment fragment = new ForceLayoutInjectedListFragment_();
34+
35+
startFragment(fragment);
36+
37+
View buttonInInjectedLayout = fragment.getView().findViewById(R.id.conventionButton);
38+
39+
assertThat(buttonInInjectedLayout).isNotNull();
40+
}
41+
42+
}

‎AndroidAnnotations/functional-test-1-5/src/test/java/org/androidannotations/test15/efragment/MyListFragmentTest.java‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*/
1616
package org.androidannotations.test15.efragment;
1717

18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.util.concurrent.Executor;
23+
24+
import org.androidannotations.api.BackgroundExecutor;
25+
import org.androidannotations.test15.R;
1826
import org.junit.Before;
1927
import org.junit.Test;
2028
import org.junit.runner.RunWith;
@@ -27,20 +35,13 @@
2735
import android.support.v4.app.FragmentTransaction;
2836
import android.view.View;
2937
import android.widget.ListView;
30-
import org.androidannotations.api.BackgroundExecutor;
31-
32-
import java.util.concurrent.Executor;
33-
34-
import static org.fest.assertions.api.Assertions.assertThat;
35-
import static org.junit.Assert.assertFalse;
36-
import static org.junit.Assert.assertTrue;
3738

3839
@RunWith(RobolectricTestRunner.class)
3940
public class MyListFragmentTest {
4041

4142
private static final int TESTED_CLICKED_INDEX = 4;
4243

43-
MyListFragment_ myListFragment;
44+
MyListFragment_ myListFragment;
4445
FragmentManager fragmentManager;
4546

4647
@Before
@@ -110,8 +111,16 @@ public void ignored_when_detached_works_for_ignored_method() {
110111
assertFalse(myListFragment.didExecute);
111112
}
112113

114+
@Test
115+
public void layout_not_injected_without_force() {
116+
View buttonInInjectedLayout = myListFragment.getView().findViewById(R.id.conventionButton);
117+
118+
assertThat(buttonInInjectedLayout).isNull();
119+
}
120+
113121
private void runBackgroundsOnSameThread() {
114-
//Simplify the threading by making a dummy executor that runs off the same thread
122+
// Simplify the threading by making a dummy executor that runs off the
123+
// same thread
115124
BackgroundExecutor.setExecutor(new Executor() {
116125
@Override
117126
public void execute(Runnable command) {

0 commit comments

Comments
 (0)