Skip to content

Commit daa1156

Browse files
committed
Implemented SystemServices with CodeModel
1 parent e10432f commit daa1156

2 files changed

Lines changed: 85 additions & 52 deletions

File tree

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/model/AndroidSystemServices.java‎

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,53 @@
2020

2121
import javax.lang.model.type.TypeMirror;
2222

23+
import com.sun.codemodel.JCodeModel;
24+
import com.sun.codemodel.JFieldRef;
25+
2326
public class AndroidSystemServices {
2427

25-
private Map<String, String> registeredServices = new HashMap<String, String>();
26-
27-
public AndroidSystemServices() {
28-
registeredServices.put("android.view.WindowManager", "android.content.Context.WINDOW_SERVICE");
29-
registeredServices.put("android.view.LayoutInflater", "android.content.Context.LAYOUT_INFLATER_SERVICE");
30-
registeredServices.put("android.app.ActivityManager", "android.content.Context.ACTIVITY_SERVICE");
31-
registeredServices.put("android.os.PowerManager", "android.content.Context.POWER_SERVICE");
32-
registeredServices.put("android.app.AlarmManager", "android.content.Context.ALARM_SERVICE");
33-
registeredServices.put("android.app.NotificationManager", "android.content.Context.NOTIFICATION_SERVICE");
34-
registeredServices.put("android.app.KeyguardManager", "android.content.Context.KEYGUARD_SERVICE");
35-
registeredServices.put("android.location.LocationManager", "android.content.Context.LOCATION_SERVICE");
36-
registeredServices.put("android.app.SearchManager", "android.content.Context.SEARCH_SERVICE");
37-
registeredServices.put("android.os.Vibrator", "android.content.Context.VIBRATOR_SERVICE");
38-
registeredServices.put("android.net.ConnectivityManager", "android.content.Context.CONNECTIVITY_SERVICE");
39-
registeredServices.put("android.net.wifi.WifiManager", "android.content.Context.WIFI_SERVICE");
40-
registeredServices.put("android.view.inputMethod.InputMethodManager", "android.content.Context.INPUT_METHOD_SERVICE");
41-
registeredServices.put("android.app.UiModeManager", "android.content.Context.UI_MODE_SERVICE");
42-
registeredServices.put("android.app.DownloadManager", "android.content.Context.DOWNLOAD_SERVICE");
43-
}
44-
45-
public boolean contains(TypeMirror serviceType) {
46-
return registeredServices.containsKey(serviceType.toString());
47-
}
48-
49-
public String getServiceConstant(TypeMirror serviceType) {
50-
return registeredServices.get(serviceType.toString());
51-
}
28+
private Map<String, String> registeredServices = new HashMap<String, String>();
29+
30+
public AndroidSystemServices() {
31+
registeredServices.put("android.view.WindowManager", "android.content.Context.WINDOW_SERVICE");
32+
registeredServices.put("android.view.LayoutInflater", "android.content.Context.LAYOUT_INFLATER_SERVICE");
33+
registeredServices.put("android.app.ActivityManager", "android.content.Context.ACTIVITY_SERVICE");
34+
registeredServices.put("android.os.PowerManager", "android.content.Context.POWER_SERVICE");
35+
registeredServices.put("android.app.AlarmManager", "android.content.Context.ALARM_SERVICE");
36+
registeredServices.put("android.app.NotificationManager", "android.content.Context.NOTIFICATION_SERVICE");
37+
registeredServices.put("android.app.KeyguardManager", "android.content.Context.KEYGUARD_SERVICE");
38+
registeredServices.put("android.location.LocationManager", "android.content.Context.LOCATION_SERVICE");
39+
registeredServices.put("android.app.SearchManager", "android.content.Context.SEARCH_SERVICE");
40+
registeredServices.put("android.os.Vibrator", "android.content.Context.VIBRATOR_SERVICE");
41+
registeredServices.put("android.net.ConnectivityManager", "android.content.Context.CONNECTIVITY_SERVICE");
42+
registeredServices.put("android.net.wifi.WifiManager", "android.content.Context.WIFI_SERVICE");
43+
registeredServices.put("android.view.inputMethod.InputMethodManager", "android.content.Context.INPUT_METHOD_SERVICE");
44+
registeredServices.put("android.app.UiModeManager", "android.content.Context.UI_MODE_SERVICE");
45+
registeredServices.put("android.app.DownloadManager", "android.content.Context.DOWNLOAD_SERVICE");
46+
}
47+
48+
public boolean contains(TypeMirror serviceType) {
49+
return registeredServices.containsKey(serviceType.toString());
50+
}
51+
52+
public String getServiceConstant(TypeMirror serviceType) {
53+
return registeredServices.get(serviceType.toString());
54+
}
55+
56+
public JFieldRef getServiceConstant(TypeMirror serviceType, JCodeModel codeModel) {
57+
return extractIdStaticRef(codeModel, registeredServices.get(serviceType.toString()));
58+
}
59+
60+
private JFieldRef extractIdStaticRef(JCodeModel codeModel, String staticFieldQualifiedName) {
61+
if (staticFieldQualifiedName != null) {
62+
int fieldSuffix = staticFieldQualifiedName.lastIndexOf('.');
63+
String fieldName = staticFieldQualifiedName.substring(fieldSuffix + 1);
64+
String className = staticFieldQualifiedName.substring(0, fieldSuffix);
65+
66+
return codeModel.ref(className).staticRef(fieldName);
67+
} else {
68+
return null;
69+
}
70+
}
5271

5372
}

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/processing/SystemServiceProcessor.java‎

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,57 @@
2727
import com.googlecode.androidannotations.model.Instruction;
2828
import com.googlecode.androidannotations.model.MetaActivity;
2929
import com.googlecode.androidannotations.model.MetaModel;
30+
import com.sun.codemodel.JBlock;
3031
import com.sun.codemodel.JCodeModel;
32+
import com.sun.codemodel.JExpr;
33+
import com.sun.codemodel.JFieldRef;
3134

3235
public class SystemServiceProcessor implements ElementProcessor {
3336

34-
private final AndroidSystemServices androidSystemServices;
37+
private final AndroidSystemServices androidSystemServices;
3538

36-
public SystemServiceProcessor(AndroidSystemServices androidSystemServices) {
37-
this.androidSystemServices = androidSystemServices;
38-
}
39+
public SystemServiceProcessor(AndroidSystemServices androidSystemServices) {
40+
this.androidSystemServices = androidSystemServices;
41+
}
3942

40-
@Override
41-
public Class<? extends Annotation> getTarget() {
42-
return SystemService.class;
43-
}
43+
@Override
44+
public Class<? extends Annotation> getTarget() {
45+
return SystemService.class;
46+
}
4447

45-
@Override
46-
public void process(Element element, MetaModel metaModel) {
47-
String fieldName = element.getSimpleName().toString();
48+
@Override
49+
public void process(Element element, MetaModel metaModel) {
50+
String fieldName = element.getSimpleName().toString();
4851

49-
TypeMirror serviceType = element.asType();
50-
String fieldTypeQualifiedName = serviceType.toString();
52+
TypeMirror serviceType = element.asType();
53+
String fieldTypeQualifiedName = serviceType.toString();
5154

52-
String serviceConstant = androidSystemServices.getServiceConstant(serviceType);
55+
String serviceConstant = androidSystemServices.getServiceConstant(serviceType);
5356

54-
Element enclosingElement = element.getEnclosingElement();
55-
MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement);
56-
List<Instruction> beforeCreateInstructions = metaActivity.getBeforeCreateInstructions();
57+
Element enclosingElement = element.getEnclosingElement();
58+
MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement);
59+
List<Instruction> beforeCreateInstructions = metaActivity.getBeforeCreateInstructions();
5760

58-
Instruction instruction = new SystemServiceInstruction(fieldName, fieldTypeQualifiedName, serviceConstant);
59-
beforeCreateInstructions.add(instruction);
61+
Instruction instruction = new SystemServiceInstruction(fieldName, fieldTypeQualifiedName, serviceConstant);
62+
beforeCreateInstructions.add(instruction);
6063

61-
}
64+
}
6265

63-
@Override
64-
public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) {
65-
// TODO Auto-generated method stub
66-
67-
}
66+
@Override
67+
public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) {
68+
69+
ActivityHolder holder = activitiesHolder.getActivityHolder(element);
70+
71+
String fieldName = element.getSimpleName().toString();
72+
73+
TypeMirror serviceType = element.asType();
74+
String fieldTypeQualifiedName = serviceType.toString();
75+
76+
JFieldRef serviceRef = androidSystemServices.getServiceConstant(serviceType, codeModel);
77+
78+
JBlock methodBody = holder.beforeSetContentView.body();
79+
80+
methodBody.assign(JExpr.ref(fieldName), JExpr.cast(codeModel.ref(fieldTypeQualifiedName), JExpr.invoke("getSystemService").arg(serviceRef)));
81+
}
6882

6983
}

0 commit comments

Comments
 (0)