|
15 | 15 | */ |
16 | 16 | package org.androidannotations.helper; |
17 | 17 |
|
18 | | -import static com.sun.codemodel.JMod.PUBLIC; |
19 | | - |
| 18 | +import com.sun.codemodel.*; |
20 | 19 | import org.androidannotations.holder.HasIntentBuilder; |
21 | 20 |
|
22 | | -import com.sun.codemodel.JBlock; |
23 | | -import com.sun.codemodel.JClass; |
24 | | -import com.sun.codemodel.JClassAlreadyExistsException; |
25 | | -import com.sun.codemodel.JConditional; |
26 | | -import com.sun.codemodel.JExpr; |
27 | | -import com.sun.codemodel.JMethod; |
28 | | -import com.sun.codemodel.JVar; |
| 21 | +import static com.sun.codemodel.JExpr._new; |
| 22 | +import static com.sun.codemodel.JExpr._super; |
| 23 | +import static com.sun.codemodel.JMod.PRIVATE; |
| 24 | +import static com.sun.codemodel.JMod.PUBLIC; |
| 25 | +import static com.sun.codemodel.JMod.STATIC; |
29 | 26 |
|
30 | 27 | public class ActivityIntentBuilder extends IntentBuilder { |
31 | 28 |
|
| 29 | + private static final int MIN_SDK_WITH_FRAGMENT_SUPPORT = 11; |
| 30 | + |
| 31 | + private JFieldVar fragmentField; |
| 32 | + private JFieldVar fragmentSupportField; |
| 33 | + |
32 | 34 | public ActivityIntentBuilder(HasIntentBuilder holder, AndroidManifest androidManifest) { |
33 | 35 | super(holder, androidManifest); |
34 | 36 | } |
35 | 37 |
|
36 | 38 | @Override |
37 | 39 | public void build() throws JClassAlreadyExistsException { |
38 | 40 | super.build(); |
39 | | - createStart(); |
40 | | - createStartForResult(); |
| 41 | + createAdditionalConstructor(); // See issue #541 |
| 42 | + createAdditionalIntentMethods(); |
| 43 | + overrideStartForResultMethod(); |
| 44 | + } |
| 45 | + |
| 46 | + private void createAdditionalIntentMethods() { |
| 47 | + if (hasFragmentInClasspath()) { |
| 48 | + // intent() with android.app.Fragment param |
| 49 | + JMethod method = holder.getGeneratedClass().method(STATIC | PUBLIC, holder.getIntentBuilderClass(), "intent"); |
| 50 | + JVar fragmentParam = method.param(holder.classes().FRAGMENT, "fragment"); |
| 51 | + method.body()._return(_new(holder.getIntentBuilderClass()).arg(fragmentParam)); |
| 52 | + } |
| 53 | + if (hasFragmentSupportInClasspath()) { |
| 54 | + // intent() with android.support.v4.app.Fragment param |
| 55 | + JMethod method = holder.getGeneratedClass().method(STATIC | PUBLIC, holder.getIntentBuilderClass(), "intent"); |
| 56 | + JVar fragmentParam = method.param(holder.classes().SUPPORT_V4_FRAGMENT, "supportFragment"); |
| 57 | + method.body()._return(_new(holder.getIntentBuilderClass()).arg(fragmentParam)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected JClass getSuperClass() { |
| 63 | + JClass superClass = holder.refClass(org.androidannotations.api.builder.ActivityIntentBuilder.class); |
| 64 | + return superClass.narrow(builderClass); |
41 | 65 | } |
42 | 66 |
|
43 | | - private void createStart() { |
44 | | - JMethod method = holder.getIntentBuilderClass().method(PUBLIC, holder.codeModel().VOID, "start"); |
45 | | - method.body().invoke(contextField, "startActivity").arg(holder.getIntentField()); |
| 67 | + private void createAdditionalConstructor() { |
| 68 | + if (hasFragmentInClasspath()) { |
| 69 | + fragmentField = addFragmentConstructor(holder.classes().FRAGMENT, "fragment_"); |
| 70 | + } |
| 71 | + if (hasFragmentSupportInClasspath()) { |
| 72 | + fragmentSupportField = addFragmentConstructor(holder.classes().SUPPORT_V4_FRAGMENT, "fragmentSupport_"); |
| 73 | + } |
46 | 74 | } |
47 | 75 |
|
48 | | - private void createStartForResult() { |
| 76 | + private JFieldVar addFragmentConstructor(JClass fragmentClass, String fieldName) { |
| 77 | + JFieldVar fragmentField = holder.getIntentBuilderClass().field(PRIVATE, fragmentClass, fieldName); |
| 78 | + JExpression generatedClass = holder.getGeneratedClass().dotclass(); |
| 79 | + |
| 80 | + JMethod constructor = holder.getIntentBuilderClass().constructor(JMod.PUBLIC); |
| 81 | + JVar constructorFragmentParam = constructor.param(fragmentClass, "fragment"); |
| 82 | + JBlock constructorBody = constructor.body(); |
| 83 | + constructorBody.invoke("super").arg(constructorFragmentParam.invoke("getActivity")).arg(generatedClass); |
| 84 | + constructorBody.assign(fragmentField, constructorFragmentParam); |
| 85 | + |
| 86 | + return fragmentField; |
| 87 | + } |
| 88 | + |
| 89 | + private void overrideStartForResultMethod() { |
| 90 | + if (fragmentSupportField == null && fragmentField == null) { |
| 91 | + return; |
| 92 | + } |
49 | 93 | JMethod method = holder.getIntentBuilderClass().method(PUBLIC, holder.codeModel().VOID, "startForResult"); |
| 94 | + method.annotate(Override.class); |
50 | 95 | JVar requestCode = method.param(holder.codeModel().INT, "requestCode"); |
51 | | - |
52 | 96 | JBlock body = method.body(); |
53 | | - JClass activityClass = holder.classes().ACTIVITY; |
54 | | - JConditional condition = null; |
55 | | - if (fragmentSupportField != null) { |
56 | | - condition = body._if(fragmentSupportField.ne(JExpr._null())); |
57 | | - condition._then() // |
58 | | - .invoke(fragmentSupportField, "startActivityForResult").arg(holder.getIntentField()).arg(requestCode); |
59 | | - } |
60 | | - if (fragmentField != null) { |
61 | | - if (condition == null) { |
62 | | - condition = body._if(fragmentField.ne(JExpr._null())); |
63 | | - } else { |
64 | | - condition = condition._elseif(fragmentField.ne(JExpr._null())); |
65 | | - } |
66 | | - condition._then() // |
67 | | - .invoke(fragmentField, "startActivityForResult").arg(holder.getIntentField()).arg(requestCode); |
68 | | - } |
69 | | - if (condition == null) { |
70 | | - condition = body._if(contextField._instanceof(activityClass)); |
71 | | - } else { |
72 | | - condition = condition._elseif(contextField._instanceof(activityClass)); |
73 | | - } |
74 | | - condition._then() // |
75 | | - .invoke(JExpr.cast(activityClass, contextField), "startActivityForResult").arg(holder.getIntentField()).arg(requestCode); |
76 | | - condition._else() // |
77 | | - .invoke(contextField, "startActivity").arg(holder.getIntentField()); |
| 97 | + |
| 98 | + JConditional condition = null; |
| 99 | + if (fragmentSupportField != null) { |
| 100 | + condition = body._if(fragmentSupportField.ne(JExpr._null())); |
| 101 | + condition._then() // |
| 102 | + .invoke(fragmentSupportField, "startActivityForResult").arg(holder.getIntentField()).arg(requestCode); |
| 103 | + } |
| 104 | + if (fragmentField != null) { |
| 105 | + if (condition == null) { |
| 106 | + condition = body._if(fragmentField.ne(JExpr._null())); |
| 107 | + } else { |
| 108 | + condition = condition._elseif(fragmentField.ne(JExpr._null())); |
| 109 | + } |
| 110 | + condition._then() // |
| 111 | + .invoke(fragmentField, "startActivityForResult").arg(holder.getIntentField()).arg(requestCode); |
| 112 | + } |
| 113 | + condition._else().invoke(_super(), "startForResult").arg(requestCode); |
| 114 | + } |
| 115 | + |
| 116 | + protected boolean hasFragmentInClasspath() { |
| 117 | + boolean fragmentExistsInSdk = androidManifest.getMinSdkVersion() >= MIN_SDK_WITH_FRAGMENT_SUPPORT; |
| 118 | + return fragmentExistsInSdk && elementUtils.getTypeElement(CanonicalNameConstants.FRAGMENT) != null; |
| 119 | + } |
| 120 | + |
| 121 | + protected boolean hasFragmentSupportInClasspath() { |
| 122 | + return elementUtils.getTypeElement(CanonicalNameConstants.SUPPORT_V4_FRAGMENT) != null; |
78 | 123 | } |
79 | 124 | } |
0 commit comments