Skip to content

Commit d269e47

Browse files
committed
Working on issue 47, created the API part as well as partial validation. Remaining work : validation + generation for @SharedPref + generation for injection of pref helper in activity
1 parent fcfabce commit d269e47

26 files changed

Lines changed: 912 additions & 1 deletion

‎AndroidAnnotations/src/main/java/com/googlecode/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.googlecode.androidannotations.annotations.res.StringRes;
6262
import com.googlecode.androidannotations.annotations.res.TextArrayRes;
6363
import com.googlecode.androidannotations.annotations.res.TextRes;
64+
import com.googlecode.androidannotations.annotations.sharedpreferences.SharedPref;
6465
import com.googlecode.androidannotations.generation.CodeModelGenerator;
6566
import com.googlecode.androidannotations.helper.AndroidManifest;
6667
import com.googlecode.androidannotations.helper.AndroidManifestFinder;
@@ -106,6 +107,7 @@
106107
import com.googlecode.androidannotations.validation.ResValidator;
107108
import com.googlecode.androidannotations.validation.RoboGuiceValidator;
108109
import com.googlecode.androidannotations.validation.RunnableValidator;
110+
import com.googlecode.androidannotations.validation.SharedPrefValidator;
109111
import com.googlecode.androidannotations.validation.SystemServiceValidator;
110112
import com.googlecode.androidannotations.validation.TouchValidator;
111113
import com.googlecode.androidannotations.validation.TransactionalValidator;
@@ -128,6 +130,7 @@
128130
Background.class, //
129131
Extra.class, //
130132
SystemService.class, //
133+
SharedPref.class, //
131134
StringRes.class, //
132135
ColorRes.class, //
133136
AnimationRes.class, //
@@ -246,7 +249,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
246249
modelValidator.register(new ExtraValidator(processingEnv));
247250
modelValidator.register(new SystemServiceValidator(processingEnv, androidSystemServices));
248251
modelValidator.register(new BeforeCreateValidator(processingEnv));
249-
252+
modelValidator.register(new SharedPrefValidator(processingEnv));
250253
return modelValidator;
251254
}
252255

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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+
/**
24+
* Default value for a preference field
25+
*/
26+
@Retention(RetentionPolicy.SOURCE)
27+
@Target(ElementType.METHOD)
28+
public @interface DefaultBoolean {
29+
boolean value();
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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+
/**
24+
* Default value for a preference field
25+
*/
26+
@Retention(RetentionPolicy.SOURCE)
27+
@Target(ElementType.METHOD)
28+
public @interface DefaultFloat {
29+
float value();
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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+
/**
24+
* Default value for a preference field
25+
*/
26+
@Retention(RetentionPolicy.SOURCE)
27+
@Target(ElementType.METHOD)
28+
public @interface DefaultInt {
29+
long value();
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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+
/**
24+
* Default value for a preference field
25+
*/
26+
@Retention(RetentionPolicy.SOURCE)
27+
@Target(ElementType.METHOD)
28+
public @interface DefaultLong {
29+
int value();
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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+
/**
24+
* Default value for a preference field
25+
*/
26+
@Retention(RetentionPolicy.SOURCE)
27+
@Target(ElementType.METHOD)
28+
public @interface DefaultString {
29+
String value();
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2010-2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.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 android.content.Context;
24+
25+
/**
26+
* Apply @{@link SharedPref} on an interface to create a SharedPreference helper
27+
* that will contain access methods related to the methods you define in the
28+
* interface.
29+
*/
30+
@Retention(RetentionPolicy.SOURCE)
31+
@Target(ElementType.TYPE)
32+
public @interface SharedPref {
33+
public enum Scope {
34+
APPLICATION_DEFAULT, ACTIVITY, ACTIVITY_DEFAULT, UNIQUE;
35+
}
36+
37+
Scope value() default Scope.ACTIVITY;
38+
39+
int mode() default Context.MODE_PRIVATE;
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.androidannotations.api.sharedpreferences;
17+
18+
19+
public abstract class AbstractPrefEditorField<T extends EditorHelper<T>> {
20+
21+
protected final T editorHelper;
22+
protected final String key;
23+
24+
public AbstractPrefEditorField(T editorHelper, String key) {
25+
this.editorHelper = editorHelper;
26+
this.key = key;
27+
}
28+
29+
public final T remove() {
30+
editorHelper.getEditor().remove(key);
31+
return editorHelper;
32+
}
33+
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.androidannotations.api.sharedpreferences;
17+
18+
import android.content.SharedPreferences;
19+
import android.content.SharedPreferences.Editor;
20+
21+
public abstract class AbstractPrefField {
22+
23+
protected final SharedPreferences sharedPreferences;
24+
protected final String key;
25+
26+
public AbstractPrefField(SharedPreferences sharedPreferences, String key) {
27+
this.sharedPreferences = sharedPreferences;
28+
this.key = key;
29+
}
30+
31+
public final boolean exists() {
32+
return sharedPreferences.contains(key);
33+
}
34+
35+
public final void remove() {
36+
apply(edit().remove(key));
37+
}
38+
39+
protected Editor edit() {
40+
return sharedPreferences.edit();
41+
}
42+
43+
protected final void apply(Editor editor) {
44+
SharedPreferencesCompat.apply(editor);
45+
}
46+
47+
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2011 Pierre-Yves Ricau (py.ricau at gmail.com)
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 com.googlecode.androidannotations.api.sharedpreferences;
17+
18+
public final class BooleanPrefEditorField<T extends EditorHelper<T>> extends AbstractPrefEditorField<T> {
19+
20+
BooleanPrefEditorField(T editorHelper, String key) {
21+
super(editorHelper, key);
22+
}
23+
24+
public T put(boolean value) {
25+
editorHelper.getEditor().putBoolean(key, value);
26+
return editorHelper;
27+
}
28+
}

0 commit comments

Comments
 (0)