├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── api
├── .gitignore
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ └── me
│ └── yokeyword
│ └── api
│ ├── Spf.java
│ └── spf
│ ├── BaseEditorField.java
│ ├── BaseSpfField.java
│ ├── EditorHelper.java
│ ├── SpfHelper.java
│ └── field
│ ├── BooleanEditorField.java
│ ├── BooleanSpfField.java
│ ├── FloatEditorField.java
│ ├── FloatSpfField.java
│ ├── IntEditorField.java
│ ├── IntSpfField.java
│ ├── LongEditorField.java
│ ├── LongSpfField.java
│ ├── StringEditorField.java
│ └── StringSpfField.java
├── build.gradle
├── compiler
├── .gitignore
├── build.gradle
└── src
│ └── main
│ └── java
│ └── me
│ └── yokeyword
│ └── spfgenerator
│ ├── CodeGenerator.java
│ └── SpfProcessor.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── sample
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── me
│ │ └── yokeyword
│ │ └── sample
│ │ ├── MainActivity.java
│ │ └── spf
│ │ └── User.java
│ └── res
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | SmartSharedPreferences
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
26 |
27 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 | Abstraction issuesJava
36 |
37 |
38 | Android
39 |
40 |
41 | Android Lint
42 |
43 |
44 | Assignment issuesJava
45 |
46 |
47 | Bitwise operation issuesJava
48 |
49 |
50 | C/C++
51 |
52 |
53 | Class metricsJava
54 |
55 |
56 | Class structureJava
57 |
58 |
59 | Cloning issuesJava
60 |
61 |
62 | Code maturity issuesJava
63 |
64 |
65 | Code style issuesJava
66 |
67 |
68 | Compiler issuesJava
69 |
70 |
71 | Control FlowGroovy
72 |
73 |
74 | Control flow issuesJava
75 |
76 |
77 | Data flow issuesJava
78 |
79 |
80 | Dependency issuesJava
81 |
82 |
83 | Encapsulation issuesJava
84 |
85 |
86 | Error handlingJava
87 |
88 |
89 | Finalization issuesJava
90 |
91 |
92 | General
93 |
94 |
95 | GeneralC/C++
96 |
97 |
98 | GeneralJava
99 |
100 |
101 | Google Cloud Endpoints
102 |
103 |
104 | Gradle
105 |
106 |
107 | Groovy
108 |
109 |
110 | HTML
111 |
112 |
113 | ImportsJava
114 |
115 |
116 | Inheritance issuesJava
117 |
118 |
119 | Initialization issuesJava
120 |
121 |
122 | Internationalization issues
123 |
124 |
125 | Internationalization issuesJava
126 |
127 |
128 | J2ME issuesJava
129 |
130 |
131 | JUnit issuesJava
132 |
133 |
134 | Java
135 |
136 |
137 | Java language level issuesJava
138 |
139 |
140 | Java language level migration aidsJava
141 |
142 |
143 | JavaBeans issuesJava
144 |
145 |
146 | JavaFX
147 |
148 |
149 | Javadoc issuesJava
150 |
151 |
152 | Logging issuesJava
153 |
154 |
155 | Manifest
156 |
157 |
158 | Maven
159 |
160 |
161 | Memory issuesJava
162 |
163 |
164 | Method MetricsGroovy
165 |
166 |
167 | Method metricsJava
168 |
169 |
170 | Modularization issuesJava
171 |
172 |
173 | Naming ConventionsGroovy
174 |
175 |
176 | Numeric issuesJava
177 |
178 |
179 | Packaging issuesJava
180 |
181 |
182 | Pattern Validation
183 |
184 |
185 | Performance issuesJava
186 |
187 |
188 | Portability issuesJava
189 |
190 |
191 | Potentially confusing code constructsGroovy
192 |
193 |
194 | Probable bugsGradle
195 |
196 |
197 | Probable bugsGroovy
198 |
199 |
200 | Probable bugsJava
201 |
202 |
203 | Properties Files
204 |
205 |
206 | Properties FilesJava
207 |
208 |
209 | Security issuesJava
210 |
211 |
212 | Serialization issuesJava
213 |
214 |
215 | StyleGroovy
216 |
217 |
218 | TestNG
219 |
220 |
221 | Threading issuesGroovy
222 |
223 |
224 | Threading issuesJava
225 |
226 |
227 | Visibility issuesJava
228 |
229 |
230 | toString() issuesJava
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 | 1.8
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SmartSharedPreferences
2 | SmartSharedPreferences可以让你创建实体类一样创建一个SharedPreferences对象,使用链式方法操作数据。
3 |
4 | 如果你使用RxJava,这里是RxJava版本的[RxSmartSharedPreferences](https://github.com/YoKeyword/RxSmartSharedPreferences)
5 | # 特性
6 | 1、可能是使用最简单的SharedPreferences操作库,只需一个@Spf注解,基于编译时注解
7 |
8 | 2、链式操作
9 |
10 | #使用方法
11 | #### 1、引用
12 | project的gradle.build里添加:
13 | ``` groovy
14 | buildscript {
15 | dependencies {
16 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
17 | }
18 | }
19 | ```
20 | app的gradle.build里添加:
21 | ``` groovy
22 | apply plugin: 'com.neenbedankt.android-apt'
23 |
24 | dependencies {
25 | compile 'me.yokeyword.smartsharedpreferences:api:1.0.0'
26 | apt 'me.yokeyword.smartsharedpreferences:compiler:1.0.0'
27 | }
28 |
29 | ```
30 | #### 2、定义SharedPreferences对象
31 | ``` java
32 | @Spf
33 | public class User {
34 | long token;
35 |
36 | String name;
37 |
38 | String mobile;
39 |
40 | Boolean first;
41 | }
42 | ```
43 | #### 3、编译项目,生成Spf_开头的对应文件(如User对应Spf_User)
44 |
45 | #### 4、实例化使用
46 | ``` java
47 | Spf_User mSpfUser = Spf_User.create(this);
48 |
49 | // 单数据 edit
50 | mSpfUser.name().put("name");
51 | String name = mSpfUser.name().get();
52 | String mobile = mSpfUser.name().get("defaultValue");
53 |
54 | // 清理Preferences
55 | mSpfUser.clear();
56 |
57 | // name 是否存在
58 | boolean exists = mSpfUser.name().exists();
59 |
60 | // 多数据 edit
61 | mSpfUser.edit()
62 | .id()
63 | .put(124)
64 | .name()
65 | .put("name")
66 | .mobile()
67 | .remove()
68 | .apply();
69 | // 也可以使用commit()提交,返回boolean类型
70 | ```
71 |
72 | get()方法的默认值:
73 |
74 | | 返回类型 | 默认值|
75 | | ------------ | ----- |
76 | | int | 0 |
77 | | long | 0l |
78 | | float | 0f |
79 | | boolean | false |
80 | | String | "" |
81 |
82 |
83 |
84 | ## 注:
85 |
86 | **如果你像存取基本类型之外的对象实体,可以转换成Json(String类型)存入,取出时再将Json转换成对应对象。**
87 |
88 | # 致谢
89 | [AndroidAnnotations](https://github.com/excilys/androidannotations)
90 |
91 | # License
92 | ``` text
93 | Copyright 2015 YoKeyword
94 |
95 | Licensed under the Apache License, Version 2.0 (the "License");
96 | you may not use this file except in compliance with the License.
97 | You may obtain a copy of the License at
98 |
99 | http://www.apache.org/licenses/LICENSE-2.0
100 |
101 | Unless required by applicable law or agreed to in writing, software
102 | distributed under the License is distributed on an "AS IS" BASIS,
103 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
104 | See the License for the specific language governing permissions and
105 | limitations under the License.
106 | ```
107 |
--------------------------------------------------------------------------------
/api/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/api/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 | apply plugin: 'idea'
3 |
4 | sourceCompatibility = JavaVersion.VERSION_1_7
5 | targetCompatibility = JavaVersion.VERSION_1_7
6 |
7 | configurations {
8 | provided
9 | }
10 |
11 | sourceSets {
12 | main.compileClasspath += configurations.provided
13 | }
14 |
15 | idea {
16 | module{
17 | scopes.PROVIDED.plus += [configurations.provided]
18 | }
19 | }
20 |
21 | dependencies {
22 | provided 'com.google.android:android:2.3.1'
23 | }
--------------------------------------------------------------------------------
/api/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/Spf.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.CLASS)
9 | @Target(ElementType.TYPE)
10 | public @interface Spf {
11 | }
12 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/BaseEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf;
2 |
3 | /**
4 | * Created by YoKeyword on 16/1/8.
5 | */
6 | public abstract class BaseEditorField {
7 | protected final E _editorHelper;
8 | protected final String _key;
9 |
10 |
11 | public BaseEditorField(E editorHelper, String key) {
12 | this._editorHelper = editorHelper;
13 | this._key = key;
14 | }
15 |
16 |
17 | public final E remove() {
18 | _editorHelper.getEditor().remove(_key);
19 | return _editorHelper;
20 | }
21 |
22 | public abstract E put(T value);
23 | }
24 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/BaseSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public abstract class BaseSpfField {
9 | protected final SharedPreferences _sharedPreferences;
10 | protected final String _key;
11 |
12 | public BaseSpfField(SharedPreferences sharedPreferences, String key) {
13 | this._sharedPreferences = sharedPreferences;
14 | this._key = key;
15 | }
16 |
17 | public final boolean exists() {
18 | return _sharedPreferences.contains(_key);
19 | }
20 |
21 | public final void remove() {
22 | apply(edit().remove(_key));
23 | }
24 |
25 | public final String key() {
26 | return this._key;
27 | }
28 |
29 | public final T get() {
30 | return get(null);
31 | }
32 |
33 | public abstract T get(T defaultValue);
34 |
35 | public abstract void put(T value);
36 |
37 | protected SharedPreferences.Editor edit() {
38 | return _sharedPreferences.edit();
39 | }
40 |
41 | protected final void apply(SharedPreferences.Editor editor) {
42 | editor.apply();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/EditorHelper.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public class EditorHelper {
9 | protected final SharedPreferences.Editor editor;
10 |
11 | public EditorHelper(SharedPreferences.Editor editor) {
12 | this.editor = editor;
13 | }
14 |
15 | public final void clear() {
16 | editor.clear().apply();
17 | }
18 |
19 | public final void apply() {
20 | editor.apply();
21 | }
22 |
23 | public final boolean commit() {
24 | return editor.commit();
25 | }
26 |
27 | public SharedPreferences.Editor getEditor() {
28 | return editor;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/SpfHelper.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | /**
7 | * Created by YoKeyword on 16/1/8.
8 | */
9 | public abstract class SpfHelper {
10 |
11 | protected final SharedPreferences sharedPreferences;
12 |
13 | public SpfHelper(Context context, String suffixName) {
14 | this.sharedPreferences = context.getSharedPreferences(getSpfName() + suffixName, 0);
15 | }
16 |
17 | private String getSpfName() {
18 | return "MySpf";
19 | }
20 |
21 | public final SharedPreferences.Editor getEditor() {
22 | return sharedPreferences.edit();
23 | }
24 |
25 | public final void clear() {
26 | sharedPreferences.edit().clear().apply();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/BooleanEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import me.yokeyword.api.spf.BaseEditorField;
4 | import me.yokeyword.api.spf.EditorHelper;
5 |
6 | /**
7 | * Created by YoKeyword on 16/1/8.
8 | */
9 | public class BooleanEditorField extends BaseEditorField {
10 |
11 | public BooleanEditorField(E editorHelper, String key) {
12 | super(editorHelper, key);
13 | }
14 |
15 | @Override
16 | public E put(Boolean value) {
17 | _editorHelper.getEditor().putBoolean(_key, value);
18 | return _editorHelper;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/BooleanSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | import me.yokeyword.api.spf.BaseSpfField;
6 |
7 | /**
8 | * Created by YoKeyword on 16/1/8.
9 | */
10 | public class BooleanSpfField extends BaseSpfField {
11 |
12 | public BooleanSpfField(SharedPreferences sharedPreferences, String key) {
13 | super(sharedPreferences, key);
14 | }
15 |
16 | @Override
17 | public Boolean get(Boolean defaultValue) {
18 | if (defaultValue == null) {
19 | defaultValue = false;
20 | }
21 | return _sharedPreferences.getBoolean(_key, defaultValue);
22 | }
23 |
24 | @Override
25 | public void put(Boolean value) {
26 | apply(edit().putBoolean(_key, value));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/FloatEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import me.yokeyword.api.spf.*;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public class FloatEditorField extends BaseEditorField {
9 |
10 | public FloatEditorField(E editorHelper, String key) {
11 | super(editorHelper, key);
12 | }
13 |
14 | @Override
15 | public E put(Float value) {
16 | _editorHelper.getEditor().putFloat(_key, value);
17 | return _editorHelper;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/FloatSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | import me.yokeyword.api.spf.BaseSpfField;
6 |
7 | /**
8 | * Created by YoKeyword on 16/1/8.
9 | */
10 | public class FloatSpfField extends BaseSpfField {
11 |
12 | public FloatSpfField(SharedPreferences sharedPreferences, String key) {
13 | super(sharedPreferences, key);
14 | }
15 |
16 | @Override
17 | public Float get(Float defaultValue) {
18 | if (defaultValue == null) {
19 | defaultValue = 0F;
20 | }
21 | return _sharedPreferences.getFloat(_key, defaultValue);
22 | }
23 |
24 | @Override
25 | public void put(Float value) {
26 | apply(edit().putFloat(_key, value));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/IntEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import me.yokeyword.api.spf.*;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public class IntEditorField extends BaseEditorField {
9 |
10 | public IntEditorField(E editorHelper, String key) {
11 | super(editorHelper, key);
12 | }
13 |
14 | @Override
15 | public E put(Integer value) {
16 | _editorHelper.getEditor().putInt(_key, value);
17 | return _editorHelper;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/IntSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | import me.yokeyword.api.spf.BaseSpfField;
6 |
7 | /**
8 | * Created by YoKeyword on 16/1/8.
9 | */
10 | public class IntSpfField extends BaseSpfField {
11 |
12 | public IntSpfField(SharedPreferences sharedPreferences, String key) {
13 | super(sharedPreferences, key);
14 | }
15 |
16 | @Override
17 | public Integer get(Integer defaultValue) {
18 | if (defaultValue == null) {
19 | defaultValue = 0;
20 | }
21 | return _sharedPreferences.getInt(_key, defaultValue);
22 | }
23 |
24 | @Override
25 | public void put(Integer value) {
26 | apply(edit().putInt(_key, value));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/LongEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import me.yokeyword.api.spf.*;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public class LongEditorField extends BaseEditorField {
9 |
10 | public LongEditorField(E editorHelper, String key) {
11 | super(editorHelper, key);
12 | }
13 |
14 | @Override
15 | public E put(Long value) {
16 | _editorHelper.getEditor().putLong(_key, value);
17 | return _editorHelper;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/LongSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | import me.yokeyword.api.spf.BaseSpfField;
6 |
7 | /**
8 | * Created by YoKeyword on 16/1/8.
9 | */
10 | public class LongSpfField extends BaseSpfField {
11 |
12 | public LongSpfField(SharedPreferences sharedPreferences, String key) {
13 | super(sharedPreferences, key);
14 | }
15 |
16 | @Override
17 | public Long get(Long defaultValue) {
18 | if (defaultValue == null) {
19 | defaultValue = 0L;
20 | }
21 | return _sharedPreferences.getLong(_key, defaultValue);
22 | }
23 |
24 | @Override
25 | public void put(Long value) {
26 | apply(edit().putLong(_key, value));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/StringEditorField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import me.yokeyword.api.spf.*;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/8.
7 | */
8 | public class StringEditorField extends BaseEditorField {
9 |
10 | public StringEditorField(E editorHelper, String key) {
11 | super(editorHelper, key);
12 | }
13 |
14 | @Override
15 | public E put(String value) {
16 | _editorHelper.getEditor().putString(_key, value);
17 | return _editorHelper;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/api/src/main/java/me/yokeyword/api/spf/field/StringSpfField.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.api.spf.field;
2 |
3 | import android.content.SharedPreferences;
4 |
5 | import me.yokeyword.api.spf.BaseSpfField;
6 |
7 | /**
8 | * Created by YoKeyword on 16/1/8.
9 | */
10 | public class StringSpfField extends BaseSpfField {
11 |
12 | public StringSpfField(SharedPreferences sharedPreferences, String key) {
13 | super(sharedPreferences, key);
14 | }
15 |
16 | @Override
17 | public String get(String defaultValue) {
18 | if (defaultValue == null) {
19 | defaultValue = "";
20 | }
21 | return _sharedPreferences.getString(_key, defaultValue);
22 | }
23 |
24 | @Override
25 | public void put(String value) {
26 | apply(edit().putString(_key, value));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
9 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
10 | }
11 | }
12 |
13 | allprojects {
14 | repositories {
15 | jcenter()
16 | }
17 | }
18 |
19 | task clean(type: Delete) {
20 | delete rootProject.buildDir
21 | }
22 |
--------------------------------------------------------------------------------
/compiler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/compiler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | dependencies {
4 | compile project(':api')
5 |
6 | compile 'com.google.auto.service:auto-service:1.0-rc2'
7 | compile 'com.squareup:javapoet:1.4.0'
8 | }
--------------------------------------------------------------------------------
/compiler/src/main/java/me/yokeyword/spfgenerator/CodeGenerator.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.spfgenerator;
2 |
3 |
4 | import com.squareup.javapoet.ClassName;
5 | import com.squareup.javapoet.MethodSpec;
6 | import com.squareup.javapoet.ParameterizedTypeName;
7 | import com.squareup.javapoet.TypeSpec;
8 |
9 | import java.util.ArrayList;
10 | import java.util.HashMap;
11 | import java.util.List;
12 |
13 | import javax.lang.model.element.Modifier;
14 |
15 | import me.yokeyword.api.spf.EditorHelper;
16 | import me.yokeyword.api.spf.SpfHelper;
17 | import me.yokeyword.api.spf.field.BooleanEditorField;
18 | import me.yokeyword.api.spf.field.BooleanSpfField;
19 | import me.yokeyword.api.spf.field.FloatEditorField;
20 | import me.yokeyword.api.spf.field.FloatSpfField;
21 | import me.yokeyword.api.spf.field.IntEditorField;
22 | import me.yokeyword.api.spf.field.IntSpfField;
23 | import me.yokeyword.api.spf.field.LongEditorField;
24 | import me.yokeyword.api.spf.field.LongSpfField;
25 | import me.yokeyword.api.spf.field.StringEditorField;
26 | import me.yokeyword.api.spf.field.StringSpfField;
27 |
28 |
29 | /**
30 | * 代码生成
31 | * Created by YoKeyword on 16/1/7.
32 | */
33 | public class CodeGenerator {
34 | private static final String CLASS_PREFIX = "Spf_";
35 | private static final String CLASS_EDITOR_PREFIX = "Editor_";
36 |
37 | private static final String METHOD_CREATE = "create";
38 | private static final String METHOD_EDIT = "edit";
39 |
40 | private final String className;
41 | private final String generatedClassName;
42 | private final ArrayList> methodsList;
43 |
44 | private ClassName contextClass, spfClass, editorClass;
45 |
46 | public CodeGenerator(String packageName, String className, ArrayList> methodsMap) {
47 | this.className = className;
48 | this.generatedClassName = CLASS_PREFIX + className;
49 | this.methodsList = methodsMap;
50 |
51 | contextClass = ClassName.get("android.content", "Context");
52 | editorClass = ClassName.get(packageName + "." + generatedClassName, CLASS_EDITOR_PREFIX + className);
53 | spfClass = ClassName.get(packageName, generatedClassName);
54 | }
55 |
56 |
57 | public TypeSpec generateClass() {
58 | return TypeSpec.classBuilder(generatedClassName)
59 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
60 | .superclass(SpfHelper.class)
61 | .addMethod(construcotr())
62 | .addMethod(create())
63 | .addMethods(fieldMethods())
64 | .addType(classEditor())
65 | .addMethod(edit())
66 | .build();
67 | }
68 |
69 | /**
70 | * 构造函数
71 | */
72 | private MethodSpec construcotr() {
73 | return MethodSpec.constructorBuilder()
74 | .addModifiers(Modifier.PRIVATE)
75 | .addParameter(contextClass, "context")
76 | .addCode("super(context, $S);\n", "_" + className)
77 | .build();
78 | }
79 |
80 | /**
81 | * 构造者
82 | */
83 | private MethodSpec create() {
84 | return MethodSpec.methodBuilder(METHOD_CREATE)
85 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
86 | .returns(spfClass)
87 | .addParameter(contextClass, "context")
88 | .addCode("return new $T(context);\n", spfClass)
89 | .build();
90 | }
91 |
92 | /**
93 | * edit()
94 | */
95 | private MethodSpec edit() {
96 | return MethodSpec.methodBuilder(METHOD_EDIT)
97 | .addModifiers(Modifier.PUBLIC)
98 | .returns(editorClass)
99 | .addCode("return new $T(getEditor());\n", editorClass)
100 | .build();
101 | }
102 |
103 | /**
104 | * 属性方法
105 | */
106 | private Iterable fieldMethods() {
107 | List methodSpecs = new ArrayList<>();
108 | for (HashMap map : methodsList) {
109 |
110 | Class clazz = map.keySet().iterator().next();
111 | String fieldName = map.get(clazz);
112 | Class typeClass = null;
113 | if (clazz == String.class) {
114 | typeClass = StringSpfField.class;
115 | } else if (clazz == Integer.class) {
116 | typeClass = IntSpfField.class;
117 | } else if (clazz == Long.class) {
118 | typeClass = LongSpfField.class;
119 | } else if (clazz == Float.class) {
120 | typeClass = FloatSpfField.class;
121 | } else if (clazz == Boolean.class) {
122 | typeClass = BooleanSpfField.class;
123 | }
124 | if (typeClass != null) {
125 | MethodSpec methodSpec = getSpfFieldMethodSpec(fieldName, typeClass);
126 | methodSpecs.add(methodSpec);
127 | }
128 | }
129 | return methodSpecs;
130 | }
131 |
132 | private MethodSpec getSpfFieldMethodSpec(String fieldName, Class clazz) {
133 | return MethodSpec.methodBuilder(fieldName)
134 | .addModifiers(Modifier.PUBLIC)
135 | .returns(clazz)
136 | .addCode("return new $T(sharedPreferences,$S);\n", clazz, fieldName)
137 | .build();
138 | }
139 |
140 | /**
141 | * Editor类
142 | */
143 | private TypeSpec classEditor() {
144 | return TypeSpec.classBuilder(CLASS_EDITOR_PREFIX + className)
145 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
146 | .superclass(ClassName.get(EditorHelper.class))
147 | .addMethod(editorConstrucotr())
148 | .addMethods(editorFieldMethods())
149 | .build();
150 | }
151 |
152 | /**
153 | * Editor类 构造函数
154 | *
155 | * @return
156 | */
157 | private MethodSpec editorConstrucotr() {
158 | return MethodSpec.constructorBuilder()
159 | .addModifiers(Modifier.PUBLIC)
160 | .addParameter(ClassName.get("android.content.SharedPreferences", "Editor"), "editor")
161 | .addCode("super(editor);\n")
162 | .build();
163 | }
164 |
165 | /**
166 | * Editor类 属性阿福
167 | *
168 | * @return
169 | */
170 | private Iterable editorFieldMethods() {
171 | List methodSpecs = new ArrayList<>();
172 |
173 | for (HashMap map : methodsList) {
174 |
175 | Class clazz = map.keySet().iterator().next();
176 | String fieldName = map.get(clazz);
177 |
178 | Class typeClass = null;
179 | if (clazz == String.class) {
180 | typeClass = StringEditorField.class;
181 | } else if (clazz == Integer.class) {
182 | typeClass = IntEditorField.class;
183 | } else if (clazz == Long.class) {
184 | typeClass = LongEditorField.class;
185 | } else if (clazz == Float.class) {
186 | typeClass = FloatEditorField.class;
187 | } else if (clazz == Boolean.class) {
188 | typeClass = BooleanEditorField.class;
189 | }
190 | if (typeClass != null) {
191 | MethodSpec methodSpec = getEditorFieldsMethodSpec(fieldName, typeClass);
192 | methodSpecs.add(methodSpec);
193 | }
194 | }
195 | return methodSpecs;
196 | }
197 |
198 | private MethodSpec getEditorFieldsMethodSpec(String fieldName, Class clazz) {
199 | MethodSpec methodSpec;
200 | methodSpec = MethodSpec.methodBuilder(fieldName)
201 | .addModifiers(Modifier.PUBLIC)
202 | .returns(ParameterizedTypeName.get(ClassName.get(clazz), editorClass))
203 | .addCode("return new $T(this,$S);\n", clazz, fieldName)
204 | .build();
205 | return methodSpec;
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/compiler/src/main/java/me/yokeyword/spfgenerator/SpfProcessor.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.spfgenerator;
2 |
3 | import com.google.auto.service.AutoService;
4 | import com.squareup.javapoet.JavaFile;
5 | import com.squareup.javapoet.TypeSpec;
6 |
7 | import java.io.IOException;
8 | import java.util.ArrayList;
9 | import java.util.Collections;
10 | import java.util.HashMap;
11 | import java.util.Set;
12 |
13 | import javax.annotation.processing.AbstractProcessor;
14 | import javax.annotation.processing.Filer;
15 | import javax.annotation.processing.ProcessingEnvironment;
16 | import javax.annotation.processing.Processor;
17 | import javax.annotation.processing.RoundEnvironment;
18 | import javax.lang.model.SourceVersion;
19 | import javax.lang.model.element.Element;
20 | import javax.lang.model.element.ElementKind;
21 | import javax.lang.model.element.TypeElement;
22 | import javax.lang.model.type.TypeKind;
23 | import javax.lang.model.util.Elements;
24 | import javax.tools.Diagnostic;
25 |
26 | import me.yokeyword.api.Spf;
27 |
28 | @AutoService(Processor.class)
29 | public class SpfProcessor extends AbstractProcessor {
30 | private Filer filer;
31 | private Elements elementUtils;
32 |
33 | @Override
34 | public synchronized void init(ProcessingEnvironment processingEnv) {
35 | super.init(processingEnv);
36 |
37 | filer = processingEnv.getFiler();
38 | elementUtils = processingEnv.getElementUtils();
39 | }
40 |
41 | @Override
42 | public Set getSupportedAnnotationTypes() {
43 | return Collections.singleton(Spf.class.getCanonicalName());
44 | }
45 |
46 | @Override
47 | public SourceVersion getSupportedSourceVersion() {
48 | return SourceVersion.latestSupported();
49 | }
50 |
51 | @Override
52 | public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
53 | for (Element element : roundEnv.getElementsAnnotatedWith(Spf.class)) {
54 | TypeElement annotatedClass = (TypeElement) element;
55 | generateCode(annotatedClass);
56 | }
57 | return true;
58 | }
59 |
60 | private void generateCode(TypeElement typeElement) {
61 | String packageName = getPackageName(typeElement);
62 |
63 | ArrayList> methodsList = new ArrayList<>();
64 | for (Element element : typeElement.getEnclosedElements()) {
65 | HashMap hashMap = new HashMap<>();
66 |
67 | if (element.getKind() == ElementKind.FIELD) {
68 |
69 | TypeKind typeKind = element.asType().getKind();
70 |
71 | if (typeKind == TypeKind.INT) {
72 | hashMap.put(Integer.class, element.toString());
73 | } else if (typeKind == TypeKind.LONG) {
74 | hashMap.put(Long.class, element.toString());
75 | } else if (typeKind == TypeKind.FLOAT) {
76 | hashMap.put(Float.class, element.toString());
77 | } else if (typeKind == TypeKind.BOOLEAN) {
78 | hashMap.put(Boolean.class, element.toString());
79 | } else if (typeKind == TypeKind.DECLARED) {
80 | String type = element.asType().toString();
81 | if (type.equals(String.class.getName())) {
82 | hashMap.put(String.class, element.toString());
83 | } else if (type.equals(Integer.class.getName())) {
84 | hashMap.put(Integer.class, element.toString());
85 | } else if (type.equals(Long.class.getName())) {
86 | hashMap.put(Long.class, element.toString());
87 | } else if (type.equals(Float.class.getName())) {
88 | hashMap.put(Float.class, element.toString());
89 | } else if (type.equals(Boolean.class.getName())) {
90 | hashMap.put(Boolean.class, element.toString());
91 | } else {
92 | errorField(typeElement);
93 | }
94 | } else {
95 | errorField(typeElement);
96 | }
97 | }
98 | if (!hashMap.isEmpty()) {
99 | methodsList.add(hashMap);
100 | }
101 | }
102 | CodeGenerator codeGenerator = new CodeGenerator(
103 | packageName, typeElement.getSimpleName().toString(), methodsList);
104 | TypeSpec generatedClass = codeGenerator.generateClass();
105 | JavaFile javaFile = JavaFile.builder(packageName, generatedClass).build();
106 | try {
107 | javaFile.writeTo(filer);
108 | } catch (IOException e) {
109 | e.printStackTrace();
110 | }
111 | }
112 |
113 | private void errorField(TypeElement typeElement) {
114 | error(typeElement, "Fields of classes annotated with %s must be String/int/long/float/boolean.", "@Spf");
115 | }
116 |
117 | private void error(Element element, String message, Object... args) {
118 | if (args.length > 0) {
119 | message = String.format(message, args);
120 | }
121 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
122 | }
123 |
124 | private String getPackageName(TypeElement typeElement) {
125 | return elementUtils.getPackageOf(typeElement).getQualifiedName().toString();
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 21 11:34:03 PDT 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'com.neenbedankt.android-apt'
3 |
4 | android {
5 | compileSdkVersion 23
6 | buildToolsVersion "23.0.2"
7 |
8 | defaultConfig {
9 | applicationId "me.yokeyword.sample"
10 | minSdkVersion 9
11 | targetSdkVersion 23
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile 'com.android.support:appcompat-v7:23.1.1'
25 |
26 | compile 'me.yokeyword.smartsharedpreferences:api:1.0.0'
27 | apt 'me.yokeyword.smartsharedpreferences:compiler:1.0.0'
28 | }
29 |
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/YoKeyword/Documents/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/sample/src/main/java/me/yokeyword/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.sample;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.EditText;
8 | import android.widget.TextView;
9 | import android.widget.Toast;
10 |
11 | import me.yokeyword.sample.spf.Spf_User;
12 |
13 |
14 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
15 | private TextView mTvShow;
16 | private EditText mEtName, mEtMobile;
17 | private Button mBtnSave, mBtnRemoveName, mBtnRemoveAll, mBtnExistsName, mBtnGet;
18 |
19 | private Spf_User mUserSpf;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 |
26 | mUserSpf = Spf_User.create(this);
27 | initView();
28 | initListener();
29 | }
30 |
31 | @Override
32 | public void onClick(View v) {
33 | switch (v.getId()) {
34 | case R.id.btn_save:
35 | // 单数据提交
36 | // mUserSpf.name().put(mEtName.getText().toString());
37 | // 事务提交
38 | mUserSpf.edit()
39 | .name()
40 | .put(mEtName.getText().toString())
41 | .mobile()
42 | .put(mEtMobile.getText().toString())
43 | .apply();
44 | break;
45 | case R.id.btn_remove_name:
46 | mUserSpf.name().remove();
47 | Toast.makeText(getApplicationContext(), "移除name成功!", Toast.LENGTH_SHORT).show();
48 | break;
49 | case R.id.btn_exsits_name:
50 | Toast.makeText(getApplicationContext(), mUserSpf.name().exists() + "", Toast.LENGTH_SHORT).show();
51 | break;
52 | case R.id.btn_remove_all:
53 | mUserSpf.edit().clear();
54 | Toast.makeText(getApplicationContext(), "全部移除成功!", Toast.LENGTH_SHORT).show();
55 | break;
56 | case R.id.btn_get:
57 | String name = mUserSpf.name().get();
58 | String mobile = mUserSpf.mobile().get();
59 | mTvShow.setText("name:" + name + "\n" + "mobile:" + mobile);
60 | break;
61 | }
62 | }
63 |
64 | private void initListener() {
65 | mBtnGet.setOnClickListener(this);
66 | mBtnSave.setOnClickListener(this);
67 | mBtnRemoveName.setOnClickListener(this);
68 | mBtnExistsName.setOnClickListener(this);
69 | mBtnRemoveAll.setOnClickListener(this);
70 | }
71 |
72 | private void initView() {
73 | mTvShow = (TextView) findViewById(R.id.tv_show);
74 | mEtName = (EditText) findViewById(R.id.et_name);
75 | mEtMobile = (EditText) findViewById(R.id.et_mobile);
76 | mBtnSave = (Button) findViewById(R.id.btn_save);
77 | mBtnRemoveName = (Button) findViewById(R.id.btn_remove_name);
78 | mBtnExistsName = (Button) findViewById(R.id.btn_exsits_name);
79 | mBtnRemoveAll = (Button) findViewById(R.id.btn_remove_all);
80 | mBtnGet = (Button) findViewById(R.id.btn_get);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/sample/src/main/java/me/yokeyword/sample/spf/User.java:
--------------------------------------------------------------------------------
1 | package me.yokeyword.sample.spf;
2 |
3 | import me.yokeyword.api.Spf;
4 |
5 | /**
6 | * Created by YoKeyword on 16/1/7.
7 | */
8 | @Spf
9 | public class User {
10 | long token;
11 |
12 | private String name;
13 |
14 | String mobile;
15 |
16 | Boolean first;
17 | }
18 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
19 |
20 |
25 |
26 |
31 |
32 |
33 |
37 |
38 |
43 |
44 |
49 |
50 |
51 |
56 |
57 |
61 |
62 |
67 |
68 |
73 |
74 |
79 |
80 |
81 |
88 |
89 |
97 |
98 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoKeyword/SmartSharedPreferences/3e3d9c4f78e35c0679f43e66ad758559367f17c8/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SmartSharedPreferencesDemo
3 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':compiler', ':api'
2 |
--------------------------------------------------------------------------------