Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit a17feaf

Browse files
committed
Add an annotation to load default value for preferences from Android's resources
1 parent b0ef7e8 commit a17feaf

7 files changed

Lines changed: 173 additions & 10 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (C) 2010-2013 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.annotations.sharedpreferences;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.androidannotations.annotations.ResId;
24+
25+
/**
26+
* Default value for a preference field
27+
*/
28+
@Retention(RetentionPolicy.CLASS)
29+
@Target(ElementType.METHOD)
30+
public @interface DefaultRes {
31+
32+
int value() default ResId.DEFAULT_VALUE;
33+
34+
String resName() default "";
35+
36+
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
483483
modelProcessor.register(new EViewGroupProcessor(processingEnv, rClass));
484484
modelProcessor.register(new EViewProcessor());
485485
modelProcessor.register(new EBeanProcessor());
486-
modelProcessor.register(new SharedPrefProcessor());
486+
modelProcessor.register(new SharedPrefProcessor(processingEnv, rClass));
487487
modelProcessor.register(new PrefProcessor(validatedModel));
488488
modelProcessor.register(new RoboGuiceProcessor());
489489
modelProcessor.register(new ViewByIdProcessor(processingEnv, rClass));

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/IdAnnotationHelper.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public List<JFieldRef> extractAnnotationFieldRefs(EBeanHolder holder, Element el
5555
}
5656

5757
public JFieldRef extractOneAnnotationFieldRef(EBeanHolder holder, Element element, Res res, boolean useElementName) {
58-
List<JFieldRef> jFieldRefs = extractAnnotationFieldRefs(holder, element, res, useElementName);
58+
return extractOneAnnotationFieldRef(holder, element, getTarget(), res, useElementName);
59+
}
60+
61+
public JFieldRef extractOneAnnotationFieldRef(EBeanHolder holder, Element element, String annotationName, Res res, boolean useElementName) {
62+
List<JFieldRef> jFieldRefs = extractAnnotationFieldRefs(holder, element, annotationName, rClass.get(res), useElementName);
5963

6064
if (jFieldRefs.size() == 1) {
6165
return jFieldRefs.get(0);

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java‎

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import static com.sun.codemodel.JExpr.lit;
2020
import static com.sun.codemodel.JMod.PRIVATE;
2121
import static com.sun.codemodel.JMod.STATIC;
22+
import static org.androidannotations.helper.CanonicalNameConstants.CONTEXT;
2223

2324
import java.util.ArrayList;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
2728

29+
import javax.annotation.processing.ProcessingEnvironment;
2830
import javax.lang.model.element.Element;
2931
import javax.lang.model.element.ExecutableElement;
3032
import javax.lang.model.element.TypeElement;
@@ -34,6 +36,7 @@
3436
import org.androidannotations.annotations.sharedpreferences.DefaultFloat;
3537
import org.androidannotations.annotations.sharedpreferences.DefaultInt;
3638
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
39+
import org.androidannotations.annotations.sharedpreferences.DefaultRes;
3740
import org.androidannotations.annotations.sharedpreferences.DefaultString;
3841
import org.androidannotations.annotations.sharedpreferences.SharedPref;
3942
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
@@ -53,7 +56,10 @@
5356
import org.androidannotations.api.sharedpreferences.StringPrefEditorField;
5457
import org.androidannotations.api.sharedpreferences.StringPrefField;
5558
import org.androidannotations.helper.CanonicalNameConstants;
59+
import org.androidannotations.helper.IdAnnotationHelper;
5660
import org.androidannotations.helper.ModelConstants;
61+
import org.androidannotations.rclass.IRClass;
62+
import org.androidannotations.rclass.IRClass.Res;
5763

5864
import com.sun.codemodel.ClassType;
5965
import com.sun.codemodel.JBlock;
@@ -62,6 +68,8 @@
6268
import com.sun.codemodel.JDefinedClass;
6369
import com.sun.codemodel.JExpr;
6470
import com.sun.codemodel.JExpression;
71+
import com.sun.codemodel.JFieldRef;
72+
import com.sun.codemodel.JFieldVar;
6573
import com.sun.codemodel.JMethod;
6674
import com.sun.codemodel.JMod;
6775
import com.sun.codemodel.JVar;
@@ -89,14 +97,19 @@ public EditorFieldHolder(Class<?> fieldClass, String fieldMethodName) {
8997
}
9098
};
9199

100+
private final IdAnnotationHelper annotationHelper;
101+
102+
public SharedPrefProcessor(ProcessingEnvironment processingEnv, IRClass rClass) {
103+
annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass);
104+
}
105+
92106
@Override
93107
public String getTarget() {
94108
return SharedPref.class.getName();
95109
}
96110

97111
@Override
98112
public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHolder) throws Exception {
99-
100113
generateApiClass(element, eBeansHolder);
101114

102115
TypeElement typeElement = (TypeElement) element;
@@ -105,7 +118,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
105118

106119
String helperQualifiedName = interfaceQualifiedName + ModelConstants.GENERATION_SUFFIX;
107120
JDefinedClass helperClass = codeModel._class(JMod.PUBLIC | JMod.FINAL, helperQualifiedName, ClassType.CLASS);
108-
eBeansHolder.create(typeElement, SharedPref.class, helperClass);
121+
EBeanHolder eBeanHolder = eBeansHolder.create(typeElement, SharedPref.class, helperClass);
109122

110123
helperClass._extends(SharedPreferencesHelper.class);
111124

@@ -145,10 +158,9 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
145158
Scope scope = sharedPrefAnnotation.value();
146159
int mode = sharedPrefAnnotation.mode();
147160
JMethod constructor = helperClass.constructor(JMod.PUBLIC);
161+
JVar contextParam = constructor.param(contextClass, "context");
148162
switch (scope) {
149163
case ACTIVITY_DEFAULT: {
150-
151-
JVar contextParam = constructor.param(contextClass, "context");
152164
JMethod getLocalClassName = getLocalClassName(eBeansHolder, helperClass, codeModel);
153165
constructor.body().invoke("super") //
154166
.arg(contextParam.invoke("getSharedPreferences") //
@@ -157,7 +169,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
157169
break;
158170
}
159171
case ACTIVITY: {
160-
JVar contextParam = constructor.param(contextClass, "context");
161172
JMethod getLocalClassName = getLocalClassName(eBeansHolder, helperClass, codeModel);
162173
constructor.body().invoke("super") //
163174
.arg(contextParam.invoke("getSharedPreferences") //
@@ -167,7 +178,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
167178
break;
168179
}
169180
case UNIQUE: {
170-
JVar contextParam = constructor.param(contextClass, "context");
171181
constructor.body() //
172182
.invoke("super") //
173183
.arg(contextParam.invoke("getSharedPreferences") //
@@ -177,7 +187,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
177187
}
178188
case APPLICATION_DEFAULT: {
179189
JClass preferenceManagerClass = eBeansHolder.refClass("android.preference.PreferenceManager");
180-
JVar contextParam = constructor.param(contextClass, "context");
181190
constructor.body() //
182191
.invoke("super") //
183192
.arg(preferenceManagerClass.staticInvoke("getDefaultSharedPreferences") //
@@ -186,6 +195,10 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
186195
}
187196
}
188197

198+
// Context field
199+
JFieldVar contextField = helperClass.field(JMod.PRIVATE, eBeansHolder.refClass(CONTEXT), "context");
200+
constructor.body().assign(JExpr._this().ref(contextField), contextParam);
201+
189202
// Helper edit method
190203
JMethod editMethod = helperClass.method(JMod.PUBLIC, editorClass, "edit");
191204
editMethod.body()._return(JExpr._new(editorClass).arg(JExpr.invoke("getSharedPreferences")));
@@ -199,6 +212,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
199212
DefaultBoolean defaultAnnotation = method.getAnnotation(DefaultBoolean.class);
200213
if (defaultAnnotation != null) {
201214
defaultValue = JExpr.lit(defaultAnnotation.value());
215+
} else if (method.getAnnotation(DefaultRes.class) != null) {
216+
defaultValue = extractResValue(eBeanHolder, method, contextField, Res.BOOL);
202217
} else {
203218
defaultValue = JExpr.lit(false);
204219
}
@@ -208,6 +223,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
208223
DefaultFloat defaultAnnotation = method.getAnnotation(DefaultFloat.class);
209224
if (defaultAnnotation != null) {
210225
defaultValue = JExpr.lit(defaultAnnotation.value());
226+
} else if (method.getAnnotation(DefaultRes.class) != null) {
227+
defaultValue = extractResValue(eBeanHolder, method, contextField, Res.INTEGER);
211228
} else {
212229
defaultValue = JExpr.lit(0f);
213230
}
@@ -217,6 +234,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
217234
DefaultInt defaultAnnotation = method.getAnnotation(DefaultInt.class);
218235
if (defaultAnnotation != null) {
219236
defaultValue = JExpr.lit(defaultAnnotation.value());
237+
} else if (method.getAnnotation(DefaultRes.class) != null) {
238+
defaultValue = extractResValue(eBeanHolder, method, contextField, Res.INTEGER);
220239
} else {
221240
defaultValue = JExpr.lit(0);
222241
}
@@ -226,6 +245,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
226245
DefaultLong defaultAnnotation = method.getAnnotation(DefaultLong.class);
227246
if (defaultAnnotation != null) {
228247
defaultValue = JExpr.lit(defaultAnnotation.value());
248+
} else if (method.getAnnotation(DefaultRes.class) != null) {
249+
defaultValue = extractResValue(eBeanHolder, method, contextField, Res.INTEGER);
229250
} else {
230251
defaultValue = JExpr.lit(0l);
231252
}
@@ -235,13 +256,34 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
235256
DefaultString defaultAnnotation = method.getAnnotation(DefaultString.class);
236257
if (defaultAnnotation != null) {
237258
defaultValue = JExpr.lit(defaultAnnotation.value());
259+
} else if (method.getAnnotation(DefaultRes.class) != null) {
260+
defaultValue = extractResValue(eBeanHolder, method, contextField, Res.STRING);
238261
} else {
239262
defaultValue = JExpr.lit("");
240263
}
241264
addFieldHelperMethod(helperClass, fieldName, defaultValue, StringPrefField.class, "stringField");
242265
}
243266
}
267+
}
244268

269+
private JExpression extractResValue(EBeanHolder eBeanHolder, Element method, JFieldVar contextField, Res res) {
270+
JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(eBeanHolder, method, DefaultRes.class.getCanonicalName(), res, true);
271+
272+
String resourceGetMethodName = null;
273+
switch (res) {
274+
case BOOL:
275+
resourceGetMethodName = "getBoolean";
276+
break;
277+
case INTEGER:
278+
resourceGetMethodName = "getInteger";
279+
break;
280+
case STRING:
281+
resourceGetMethodName = "getString";
282+
break;
283+
default:
284+
break;
285+
}
286+
return contextField.invoke("getResources").invoke(resourceGetMethodName).arg(idRef);
245287
}
246288

247289
private void addFieldHelperMethod(JDefinedClass helperClass, String fieldName, JExpression defaultValue, Class<?> prefFieldHelperClass, String fieldHelperMethodName) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<bool name="prefsDefaultBool">true</bool>
5+
<string name="prefDefaultString">default pref</string>
6+
<integer name="prefDefaultInt">42</integer>
7+
<integer name="prefDefaultLong">4200</integer>
8+
<integer name="prefDefaultFloat">42</integer>
9+
10+
</resources>

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/SomePrefs.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.androidannotations.test15.prefs;
1717

18+
import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
19+
import org.androidannotations.annotations.sharedpreferences.DefaultFloat;
1820
import org.androidannotations.annotations.sharedpreferences.DefaultInt;
21+
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
1922
import org.androidannotations.annotations.sharedpreferences.DefaultString;
2023
import org.androidannotations.annotations.sharedpreferences.SharedPref;
2124
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
@@ -25,9 +28,18 @@ public interface SomePrefs {
2528

2629
@DefaultString("John")
2730
String name();
28-
31+
2932
@DefaultInt(42)
3033
int age();
34+
35+
@DefaultLong(400000L)
36+
long ageLong();
37+
38+
@DefaultFloat(42f)
39+
float ageFloat();
40+
41+
@DefaultBoolean(true)
42+
boolean isAwesome();
3143

3244
long lastUpdated();
3345

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (C) 2010-2013 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.prefs;
17+
18+
import org.androidannotations.annotations.sharedpreferences.DefaultRes;
19+
import org.androidannotations.annotations.sharedpreferences.SharedPref;
20+
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
21+
import org.androidannotations.test15.R;
22+
23+
@SharedPref(Scope.UNIQUE)
24+
public interface SomeResPrefs {
25+
26+
@DefaultRes
27+
String prefDefaultString();
28+
29+
@DefaultRes(R.string.prefDefaultString)
30+
String nameResId();
31+
32+
@DefaultRes(resName = "prefDefaultString")
33+
String nameResName();
34+
35+
@DefaultRes
36+
int prefDefaultInt();
37+
38+
@DefaultRes(R.integer.prefDefaultInt)
39+
int ageResId();
40+
41+
@DefaultRes
42+
long prefDefaultLong();
43+
44+
@DefaultRes(R.integer.prefDefaultLong)
45+
long ageLongResId();
46+
47+
@DefaultRes
48+
float prefDefaultFloat();
49+
50+
@DefaultRes(R.integer.prefDefaultFloat)
51+
float ageFloatResId();
52+
53+
@DefaultRes
54+
boolean prefsDefaultBool();
55+
56+
@DefaultRes(R.bool.prefsDefaultBool)
57+
boolean isAwesomeResId();
58+
59+
}

0 commit comments

Comments
 (0)