-
Notifications
You must be signed in to change notification settings - Fork 117
/
MainActivityGenerator.java
334 lines (256 loc) · 12 KB
/
MainActivityGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.athomas.androidkickstartr.generator;
import static com.sun.codemodel.JExpr.TRUE;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.athomas.androidkickstartr.model.Application;
import com.athomas.androidkickstartr.model.State;
import com.athomas.androidkickstartr.util.RefHelper;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JForEach;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;
public class MainActivityGenerator implements Generator {
private JDefinedClass jClass;
private RefHelper ref;
private JCodeModel jCodeModel;
private State state;
private Logger LOGGER = LoggerFactory.getLogger(getClass());
private Application application;
public MainActivityGenerator(State state, Application application) {
this.state = state;
this.application = application;
}
public JCodeModel generate(JCodeModel jCodeModel) throws IOException {
this.jCodeModel = jCodeModel;
ref = new RefHelper(jCodeModel);
try {
jClass = jCodeModel._class(application.getActivityPackage());
// TODO Enhance that
ref.r(application.getR()); // must do it at least once
createActivity(application.getActivityLayout());
JFieldVar textViewField = createTextViewField("hello");
JBlock afterViewsBody = createAfterViewsMethod();
doTextViewViewById(afterViewsBody, "hello", textViewField);
addRestClient(textViewField);
createOnCreateOptionsMenu();
createConfigureViewPager(afterViewsBody, application.getViewPagerAdapterPackage());
createConfigureActionBar(afterViewsBody);
} catch (JClassAlreadyExistsException e1) {
LOGGER.error("Classname already exists", e1);
}
return jCodeModel;
}
private void createActivity(String layout) {
JClass parentActivity;
if (state.isViewPager()) {
parentActivity = state.isViewPager() ? ref.sFragmentActivity() : ref.fragmentActivity();
} else {
parentActivity = state.isActionBarSherlock() ? ref.sActivity() : ref.activity();
}
jClass._extends(parentActivity);
// @EActivity
if (state.isAndroidAnnotations()) {
JAnnotationUse eactivityAnnotation = jClass.annotate(ref.eactivity());
JFieldRef field = ref.r().staticRef("layout").ref(layout);
eactivityAnnotation.param("value", field);
}
}
private JFieldVar createViewField(JClass type, String name) {
int mod = state.isAndroidAnnotations() ? JMod.NONE : JMod.PRIVATE;
JFieldVar field = jClass.field(mod, type, name);
return field;
}
private JFieldVar createTextViewField(String name) {
return createViewField(ref.textView(), name);
}
private JFieldVar createViewPagerField(String name) {
return createViewField(ref.viewPager(), name);
}
private JBlock createAfterViewsMethod() {
JBlock afterViewsBody;
if (!state.isAndroidAnnotations()) {
JMethod onCreate = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onCreate");
onCreate.annotate(ref.override());
JVar params = onCreate.param(ref.bundle(), "savedInstanceState");
afterViewsBody = onCreate.body();
JExpression _super = JExpr._super();
JInvocation onCreateInvocation = _super.invoke("onCreate").arg(params);
afterViewsBody.add(onCreateInvocation);
} else {
JMethod afterViews = jClass.method(JMod.NONE, jCodeModel.VOID, "afterViews");
afterViews.annotate(ref.afterViews());
afterViewsBody = afterViews.body();
}
return afterViewsBody;
}
private void doTextViewViewById(JBlock afterViewsBody, String id, JFieldVar field) {
doViewById(afterViewsBody, id, field, ref.textView());
}
private void doViewPagerViewById(JBlock afterViewsBody, String id, JFieldVar field) {
doViewById(afterViewsBody, id, field, ref.viewPager());
}
private void doViewById(JBlock afterViewsBody, String id, JFieldVar field, JClass type) {
if (!state.isAndroidAnnotations()) {
doFindViewById(afterViewsBody, id, field, type);
} else {
field.annotate(ref.viewById());
}
}
private void doFindViewById(JBlock afterViewsBody, String id, JFieldVar field, JClass type) {
JFieldRef rIdHello = ref.r().staticRef("id").ref(id);
JInvocation findViewById = JExpr.invoke("findViewById").arg(rIdHello);
JExpression findViewByIdCasted = JExpr.cast(type, findViewById);
afterViewsBody.assign(field, findViewByIdCasted);
}
// ACTION BAR SHERLOCK
private JBlock createOnCreateOptionsMenu() {
JMethod onCreateOptionsMenu = null;
JClass menu = !state.isActionBarSherlock() ? ref.menu() : ref.sMenu();
onCreateOptionsMenu = jClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "onCreateOptionsMenu");
JVar menuVar = onCreateOptionsMenu.param(menu, "menu");
onCreateOptionsMenu.annotate(ref.override());
JBlock onCreateOptionsMenuBody = onCreateOptionsMenu.body();
String getMenuInflater = state.isActionBarSherlock() ? "getSupportMenuInflater" : "getMenuInflater";
JFieldRef rMenuMain = ref.r().staticRef("menu").ref("activity_main");
JInvocation inflate = JExpr.invoke(getMenuInflater).//
invoke("inflate"). //
arg(rMenuMain). //
arg(menuVar);
onCreateOptionsMenuBody.add(inflate);
onCreateOptionsMenuBody._return(TRUE);
return onCreateOptionsMenuBody;
}
private void createConfigureActionBar(JBlock afterViewsBody) {
if (state.isActionBarSherlock() && (state.isListNavigation() || state.isTabNavigation())) {
JMethod configureActionBar = jClass.method(JMod.PRIVATE, jCodeModel.VOID, "configureActionBar");
JBlock configureActionBarBody = configureActionBar.body();
// LIST NAVIGATION
if (state.isListNavigation()) {
addListNavigationConfiguration(configureActionBarBody);
}
// TAB NAVIGATION
if (state.isTabNavigation()) {
addTabNavigationConfiguration(configureActionBarBody);
}
afterViewsBody.invoke(configureActionBar);
}
}
private void addTabNavigationConfiguration(JBlock configureActionBarBody) {
// implements TabListener
jClass._implements(ref.sTabListener());
JClass sherlockTab = ref.sTab();
JClass fragmentTransaction = ref.fragmentTransaction();
// override TabListener methods
JMethod onTabSelected = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onTabSelected");
onTabSelected.param(sherlockTab, "tab");
onTabSelected.param(fragmentTransaction, "ft");
onTabSelected.annotate(ref.override());
JMethod onTabUnselected = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onTabUnselected");
onTabUnselected.param(sherlockTab, "tab");
onTabUnselected.param(fragmentTransaction, "ft");
onTabUnselected.annotate(ref.override());
JMethod onTabReselected = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onTabReselected");
onTabReselected.param(sherlockTab, "tab");
onTabReselected.param(fragmentTransaction, "ft");
onTabReselected.annotate(ref.override());
JInvocation getSupportActionBar = JExpr.invoke("getSupportActionBar");
JFieldRef navigationModeList = ref.sActionBar().staticRef("NAVIGATION_MODE_TABS");
JInvocation setNavigationMode = getSupportActionBar.invoke("setNavigationMode").arg(navigationModeList);
configureActionBarBody.add(setNavigationMode);
JFieldRef rArrayLocations = ref.r().staticRef("array").ref("locations");
JInvocation getResources = JExpr.invoke("getResources");
JInvocation getStringArray = getResources.invoke("getStringArray").arg(rArrayLocations);
JClass string = ref.string();
JClass stringArray = string.array();
JVar locations = configureActionBarBody.decl(stringArray, "locations", getStringArray);
JForEach forEachLocation = configureActionBarBody.forEach(string, "location", locations);
JVar location = forEachLocation.var();
JBlock forEachLocationBody = forEachLocation.body();
JInvocation newTab = getSupportActionBar.invoke("newTab");
JVar tab = forEachLocationBody.decl(sherlockTab, "tab", newTab);
JInvocation setText = tab.invoke("setText").arg(location);
JInvocation setTabListener = tab.invoke("setTabListener").arg(JExpr._this());
JInvocation addTab = getSupportActionBar.invoke("addTab").arg(tab);
forEachLocationBody.add(setText);
forEachLocationBody.add(setTabListener);
forEachLocationBody.add(addTab);
}
private void addListNavigationConfiguration(JBlock configureActionBarBody) {
jClass._implements(ref.sNavigationListener());
JMethod onNavigationItemSelected = jClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "onNavigationItemSelected");
onNavigationItemSelected.param(jCodeModel.INT, "itemPosition");
onNavigationItemSelected.param(jCodeModel.LONG, "itemId");
onNavigationItemSelected.annotate(ref.override());
JBlock body = onNavigationItemSelected.body();
body._return(TRUE);
// configure Tab navigation
JInvocation getSupportActionbar = JExpr.invoke("getSupportActionBar");
JInvocation getContext = getSupportActionbar.invoke("getThemedContext");
JVar contextVar = configureActionBarBody.decl(ref.context(), "context");
contextVar.init(getContext);
JFieldRef rArrayLocations = ref.r().staticRef("array").ref("locations");
// NEEDED! CodeModel doesn't manage the case where two
// classes have the same name
JExpression rLayoutSherlockSpinner = JExpr.direct("android.R.layout.simple_list_item_1");
JClass arrayAdapter = ref.arrayAdapter();
JInvocation createFromResource = arrayAdapter.staticInvoke("createFromResource"). //
arg(contextVar). //
arg(rArrayLocations). //
arg(rLayoutSherlockSpinner);
JClass listType = arrayAdapter.narrow(ref.charSequence());
JVar listVar = configureActionBarBody.decl(listType, "list");
listVar.init(createFromResource);
JFieldRef navigationModeList = ref.sActionBar().staticRef("NAVIGATION_MODE_LIST");
JInvocation setNavigationMode = getSupportActionbar.invoke("setNavigationMode").arg(navigationModeList);
JInvocation setListNavigationCallbacks = getSupportActionbar.invoke("setListNavigationCallbacks").//
arg(listVar).//
arg(JExpr._this());
configureActionBarBody.add(setNavigationMode);
configureActionBarBody.add(setListNavigationCallbacks);
}
private void createConfigureViewPager(JBlock afterViewsBody, String viewPagerAdapterName) {
if (state.isViewPager()) {
JFieldVar pagerField = createViewPagerField("pager");
doViewPagerViewById(afterViewsBody, "pager", pagerField);
afterViewsBody.invoke("configureViewPager");
JMethod configureViewPager = jClass.method(JMod.PRIVATE, jCodeModel.VOID, "configureViewPager");
JBlock configureViewPagerBody = configureViewPager.body();
JClass viewPagerAdapter = ref.ref(viewPagerAdapterName);
JInvocation getSupportFragmentManager = JExpr.invoke("getSupportFragmentManager");
JInvocation newViewPagerAdapter = JExpr._new(viewPagerAdapter).arg(getSupportFragmentManager);
JVar pagerAdapter = configureViewPagerBody.decl(viewPagerAdapter, "pagerAdapter", newViewPagerAdapter);
pagerField.invoke("setAdapter").arg(pagerAdapter);
}
}
private void addRestClient(JFieldVar textViewField) {
if (state.isRestTemplate() && state.isAndroidAnnotations()) {
// add annotated restClient field
JFieldVar restClient = jClass.field(JMod.NONE, ref.ref(application.getRestClientPackage()), "restClient");
restClient.annotate(ref.restService());
// add doSomethingElseOnUiThread method
JMethod doSomethingElseOnUiThread = jClass.method(JMod.NONE, jCodeModel.VOID, "doSomethingElseOnUiThread");
doSomethingElseOnUiThread.annotate(ref.uithread());
JBlock body = doSomethingElseOnUiThread.body();
body.invoke(textViewField, "setText").arg("Hi!");
// add doSomethingInBackground method
JMethod doSomethingInBackground = jClass.method(JMod.NONE, jCodeModel.VOID, "doSomethingInBackground");
doSomethingInBackground.annotate(ref.background());
JBlock doSomethingInBackgroundBody = doSomethingInBackground.body();
JInvocation restClientMain = restClient.invoke("main");
doSomethingInBackgroundBody.add(restClientMain);
doSomethingInBackgroundBody.invoke(doSomethingElseOnUiThread);
}
}
}