Skip to content

Commit 7237c35

Browse files
committed
v1.0.1
1 parent 5ea87fb commit 7237c35

12 files changed

Lines changed: 222 additions & 53 deletions

File tree

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Preferences Manager
2+
3+
The android library that is used to manage the preferences.
4+
5+
[![Platform](http://img.shields.io/badge/platform-android-brightgreen.svg?style=flat)](http://developer.android.com/index.html) [![Language](http://img.shields.io/badge/language-java-orange.svg?style=flat)](http://www.oracle.com/technetwork/java/javase/downloads/index.html) [![License](http://img.shields.io/badge/license-apache2.0-lightgrey.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
6+
7+
## Usage
8+
9+
Initialize the preferences manager
10+
11+
```java
12+
new PreferencesManager(this)
13+
.setName("prefs")
14+
.init();
15+
```
16+
17+
Example 1:
18+
19+
```java
20+
// put int to preferences
21+
PreferencesManager.putInt(key, value);
22+
23+
// get int from preferences
24+
PreferencesManager.getInt(key)
25+
```
26+
27+
Example 2:
28+
29+
```java
30+
// your object
31+
class Person {
32+
33+
public String name;
34+
35+
public Person(String name) {
36+
this.name = name;
37+
}
38+
39+
}
40+
41+
// put object to preferences
42+
PreferencesManager.putObject(key, new Person(name));
43+
44+
// get object from preferences
45+
PreferencesManager.getObject(key, Person.class);
46+
```
47+
48+
## Gradle
49+
50+
Add the dependency in your `build.gradle`
51+
52+
```groovy
53+
buildscript {
54+
repositories {
55+
jcenter()
56+
}
57+
}
58+
59+
dependencies {
60+
compile 'com.shawnlin:PreferencesManager:1.0.1'
61+
}
62+
```
63+
64+
## License
65+
66+
```
67+
Copyright (C) 2016 ShawnLin013(Shawn Lin)
68+
69+
Licensed under the Apache License, Version 2.0 (the "License");
70+
you may not use this file except in compliance with the License.
71+
You may obtain a copy of the License at
72+
73+
http://www.apache.org/licenses/LICENSE-2.0
74+
75+
Unless required by applicable law or agreed to in writing, software
76+
distributed under the License is distributed on an "AS IS" BASIS,
77+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78+
See the License for the specific language governing permissions and
79+
limitations under the License.
80+
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
jcenter()
55
}
66
dependencies {
7-
classpath 'com.android.tools.build:gradle:1.5.0'
7+
classpath 'com.android.tools.build:gradle:2.0.0'
88
classpath 'com.novoda:bintray-release:0.3.4'
99
// NOTE: Do not place your application dependencies here; they belong
1010
// in the individual module build.gradle files

gradle.properties

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# Project-wide Gradle settings.
1+
VERSION_CODE=2
2+
VERSION_NAME=1.0.1
23

3-
# IDE (e.g. Android Studio) users:
4-
# Gradle settings configured through the IDE *will override*
5-
# any settings specified in this file.
4+
GROUP=com.shawnlin
5+
ARTIFACT_ID=PreferencesManager
66

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
7+
POM_DESCRIPTION=The android library that is used to manage the preferences.
8+
POM_URL=https://github.com/ShawnLin013/PreferencesManager
9+
POM_SCM_URL=git@github.com:ShawnLin013/PreferencesManager.git
10+
POM_LICENSE_NAME=The Apache Software License, Version 2.0
11+
POM_LICENSE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
12+
POM_LICENSE_DIST=repo
13+
POM_DEVELOPER_ID=shawnlin013
14+
POM_DEVELOPER_NAME=shawnlin013
15+
POM_DEVELOPER_EMAIL=hn62257@gmail.com
16+
ISSUE_URL=https://github.com/ShawnLin013/PreferencesManager/issues
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Oct 21 11:34:03 PDT 2015
1+
#Thu Apr 14 15:31:39 CST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

library/build.gradle

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 15
1010
targetSdkVersion 23
11-
versionCode 1
12-
versionName "1.0.0"
11+
versionCode VERSION_CODE as int
12+
versionName VERSION_NAME
1313
}
1414
buildTypes {
1515
release {
@@ -18,11 +18,21 @@ android {
1818
}
1919
}
2020
}
21+
22+
dependencies {
23+
compile 'com.google.code.gson:gson:2.6.2'
24+
}
25+
2126
publish {
22-
userOrg = 'shawnlin013'
23-
groupId = 'com.shawnlin'
24-
artifactId = rootProject.name
25-
publishVersion = '1.0.0'
26-
desc = 'The utility that is used to manage the preferences.'
27-
website = 'https://github.com/ShawnLin013/${rootProject.name}'
27+
userOrg = POM_DEVELOPER_ID
28+
groupId = GROUP
29+
artifactId = ARTIFACT_ID
30+
publishVersion = VERSION_NAME
31+
licences = ['Apache-2.0']
32+
desc = POM_DESCRIPTION
33+
website = POM_URL
34+
issueTracker = ISSUE_URL
35+
repository = POM_SCM_URL
36+
autoPublish = false
37+
dryRun = false
2838
}

library/proguard-rules.pro

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@
1515
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
1616
# public *;
1717
#}
18+
19+
# Gson
20+
# Gson uses generic type information stored in a class file when working with fields. Proguard
21+
# removes such information by default, so configure it to keep all of it.
22+
-keepattributes Signature
23+
24+
# For using GSON @Expose annotation
25+
-keepattributes *Annotation*
26+
27+
# Gson specific classes
28+
-keep class sun.misc.Unsafe { *; }
29+
#-keep class com.google.gson.stream.** { *; }
30+
31+
# Application classes that will be serialized/deserialized over Gson
32+
-keep class com.google.gson.examples.android.model.** { *; }

library/src/main/java/com/shawnlin/preferencesmanager/PreferencesManager.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
import android.content.SharedPreferences;
66
import android.content.SharedPreferences.Editor;
77

8+
import com.google.gson.Gson;
9+
810
/**
911
* The utility that is used to manage the preferences
1012
*/
1113
public class PreferencesManager {
1214

1315
private static SharedPreferences mSharedPreferences;
16+
private static Gson mGson;
17+
1418
private Context mContext;
1519
private String mName;
1620

@@ -20,6 +24,7 @@ public class PreferencesManager {
2024
*/
2125
public PreferencesManager(Context context) {
2226
mContext = context;
27+
mGson = new Gson();
2328
}
2429

2530
/**
@@ -181,4 +186,30 @@ public static boolean getBoolean(String key) {
181186
return mSharedPreferences.getBoolean(key, false);
182187
}
183188

189+
/**
190+
* Put a object in the preferences editor.
191+
* @param key The name of the preference to modify.
192+
* @param value The new value for the preference.
193+
*/
194+
public static void putObject(String key, Object value) {
195+
if (mGson == null || value == null) {
196+
return;
197+
}
198+
199+
putString(key, mGson.toJson(value));
200+
}
201+
202+
/**
203+
* Retrieval a object from the preferences.
204+
* @param key The name of the preference to retrieve.
205+
* @param type The class of the preference to retrieve.
206+
* @return Returns the preference values if they exist, or defValues.
207+
*/
208+
public static <T> T getObject(String key, Class<T> type) {
209+
if (mSharedPreferences == null || mGson == null) {
210+
return null;
211+
}
212+
return mGson.fromJson(getString(key), type);
213+
}
214+
184215
}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.shawnlin.sample;
22

33
import android.os.Bundle;
4-
import android.support.design.widget.FloatingActionButton;
5-
import android.support.design.widget.Snackbar;
64
import android.support.v7.app.AppCompatActivity;
75
import android.support.v7.widget.Toolbar;
86
import android.view.View;
7+
import android.widget.Button;
98
import android.widget.Toast;
109

1110
import com.shawnlin.preferencesmanager.PreferencesManager;
1211

1312
public class MainActivity extends AppCompatActivity {
1413

14+
private static final String KEY_INT = "int";
15+
private static final String KEY_OBJECT = "object";
16+
1517
@Override protected void onCreate(Bundle savedInstanceState) {
1618
super.onCreate(savedInstanceState);
1719
setContentView(R.layout.activity_main);
@@ -22,14 +24,36 @@ public class MainActivity extends AppCompatActivity {
2224
.setName("prefs")
2325
.init();
2426

25-
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
26-
fab.setOnClickListener(new View.OnClickListener() {
27+
28+
Button intButton = (Button) findViewById(R.id.int_button);
29+
intButton.setOnClickListener(new View.OnClickListener() {
2730
@Override public void onClick(View view) {
28-
PreferencesManager.putInt("test", (int) (Math.random() * 10 + 10));
29-
Toast.makeText(MainActivity.this, String.valueOf(PreferencesManager.getInt("test")),
30-
Toast.LENGTH_SHORT).show();
31+
PreferencesManager.putInt(KEY_INT, (int) (Math.random() * 10 + 10));
32+
makeToast(String.valueOf(PreferencesManager.getInt(KEY_INT)));
3133
}
3234
});
35+
36+
Button objectButton = (Button) findViewById(R.id.object_button);
37+
objectButton.setOnClickListener(new View.OnClickListener() {
38+
@Override public void onClick(View view) {
39+
PreferencesManager.putObject(KEY_OBJECT, new Person(getString(R.string.app_name)));
40+
makeToast(PreferencesManager.getObject(KEY_OBJECT, Person.class).name);
41+
}
42+
});
43+
}
44+
45+
private void makeToast(String str) {
46+
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
47+
}
48+
49+
class Person {
50+
51+
public String name;
52+
53+
public Person(String name) {
54+
this.name = name;
55+
}
56+
3357
}
3458

3559
}

0 commit comments

Comments
 (0)