From 7237c35510ca9ca08d911b7c3c460872e10e6628 Mon Sep 17 00:00:00 2001 From: ShawnLin013 Date: Fri, 15 Apr 2016 14:52:02 +0800 Subject: [PATCH] v1.0.1 --- .idea/encodings.xml | 6 ++ .idea/gradle.xml | 7 ++ README.md | 80 +++++++++++++++++++ build.gradle | 2 +- gradle.properties | 30 ++++--- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 26 ++++-- library/proguard-rules.pro | 15 ++++ .../PreferencesManager.java | 31 +++++++ .../com/shawnlin/sample/MainActivity.java | 38 +++++++-- sample/src/main/res/layout/activity_main.xml | 8 -- sample/src/main/res/layout/content_main.xml | 28 ++++--- 12 files changed, 222 insertions(+), 53 deletions(-) create mode 100644 .idea/encodings.xml create mode 100644 README.md diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 0ae0be1..fc8049c 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -14,6 +14,13 @@ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..bfcc261 --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +# Preferences Manager + +The android library that is used to manage the preferences. + +[![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) + +## Usage + +Initialize the preferences manager + +```java +new PreferencesManager(this) + .setName("prefs") + .init(); +``` + +Example 1: + +```java +// put int to preferences +PreferencesManager.putInt(key, value); + +// get int from preferences +PreferencesManager.getInt(key) +``` + +Example 2: + +```java +// your object +class Person { + + public String name; + + public Person(String name) { + this.name = name; + } + +} + +// put object to preferences +PreferencesManager.putObject(key, new Person(name)); + +// get object from preferences +PreferencesManager.getObject(key, Person.class); +``` + +## Gradle + +Add the dependency in your `build.gradle` + +```groovy +buildscript { + repositories { + jcenter() + } +} + +dependencies { + compile 'com.shawnlin:PreferencesManager:1.0.1' +} +``` + +## License + +``` +Copyright (C) 2016 ShawnLin013(Shawn Lin) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4aefa6b..ec6b0e4 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' classpath 'com.novoda:bintray-release:0.3.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index 1d3591c..fe9a727 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,18 +1,16 @@ -# Project-wide Gradle settings. +VERSION_CODE=2 +VERSION_NAME=1.0.1 -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. +GROUP=com.shawnlin +ARTIFACT_ID=PreferencesManager -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html - -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. -# Default value: -Xmx10248m -XX:MaxPermSize=256m -# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 - -# When configured, Gradle will run in incubating parallel mode. -# This option should only be used with decoupled projects. More details, visit -# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +POM_DESCRIPTION=The android library that is used to manage the preferences. +POM_URL=https://github.com/ShawnLin013/PreferencesManager +POM_SCM_URL=git@github.com:ShawnLin013/PreferencesManager.git +POM_LICENSE_NAME=The Apache Software License, Version 2.0 +POM_LICENSE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt +POM_LICENSE_DIST=repo +POM_DEVELOPER_ID=shawnlin013 +POM_DEVELOPER_NAME=shawnlin013 +POM_DEVELOPER_EMAIL=hn62257@gmail.com +ISSUE_URL=https://github.com/ShawnLin013/PreferencesManager/issues \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f23df6e..b9d2f8b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 21 11:34:03 PDT 2015 +#Thu Apr 14 15:31:39 CST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/library/build.gradle b/library/build.gradle index c91adcc..b221b8d 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 23 - versionCode 1 - versionName "1.0.0" + versionCode VERSION_CODE as int + versionName VERSION_NAME } buildTypes { release { @@ -18,11 +18,21 @@ android { } } } + +dependencies { + compile 'com.google.code.gson:gson:2.6.2' +} + publish { - userOrg = 'shawnlin013' - groupId = 'com.shawnlin' - artifactId = rootProject.name - publishVersion = '1.0.0' - desc = 'The utility that is used to manage the preferences.' - website = 'https://github.com/ShawnLin013/${rootProject.name}' + userOrg = POM_DEVELOPER_ID + groupId = GROUP + artifactId = ARTIFACT_ID + publishVersion = VERSION_NAME + licences = ['Apache-2.0'] + desc = POM_DESCRIPTION + website = POM_URL + issueTracker = ISSUE_URL + repository = POM_SCM_URL + autoPublish = false + dryRun = false } \ No newline at end of file diff --git a/library/proguard-rules.pro b/library/proguard-rules.pro index 93fc6a7..db196cd 100644 --- a/library/proguard-rules.pro +++ b/library/proguard-rules.pro @@ -15,3 +15,18 @@ #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} + +# Gson +# Gson uses generic type information stored in a class file when working with fields. Proguard +# removes such information by default, so configure it to keep all of it. +-keepattributes Signature + +# For using GSON @Expose annotation +-keepattributes *Annotation* + +# Gson specific classes +-keep class sun.misc.Unsafe { *; } +#-keep class com.google.gson.stream.** { *; } + +# Application classes that will be serialized/deserialized over Gson +-keep class com.google.gson.examples.android.model.** { *; } \ No newline at end of file diff --git a/library/src/main/java/com/shawnlin/preferencesmanager/PreferencesManager.java b/library/src/main/java/com/shawnlin/preferencesmanager/PreferencesManager.java index ec79851..60b1c63 100644 --- a/library/src/main/java/com/shawnlin/preferencesmanager/PreferencesManager.java +++ b/library/src/main/java/com/shawnlin/preferencesmanager/PreferencesManager.java @@ -5,12 +5,16 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import com.google.gson.Gson; + /** * The utility that is used to manage the preferences */ public class PreferencesManager { private static SharedPreferences mSharedPreferences; + private static Gson mGson; + private Context mContext; private String mName; @@ -20,6 +24,7 @@ public class PreferencesManager { */ public PreferencesManager(Context context) { mContext = context; + mGson = new Gson(); } /** @@ -181,4 +186,30 @@ public static boolean getBoolean(String key) { return mSharedPreferences.getBoolean(key, false); } + /** + * Put a object in the preferences editor. + * @param key The name of the preference to modify. + * @param value The new value for the preference. + */ + public static void putObject(String key, Object value) { + if (mGson == null || value == null) { + return; + } + + putString(key, mGson.toJson(value)); + } + + /** + * Retrieval a object from the preferences. + * @param key The name of the preference to retrieve. + * @param type The class of the preference to retrieve. + * @return Returns the preference values if they exist, or defValues. + */ + public static T getObject(String key, Class type) { + if (mSharedPreferences == null || mGson == null) { + return null; + } + return mGson.fromJson(getString(key), type); + } + } diff --git a/sample/src/main/java/com/shawnlin/sample/MainActivity.java b/sample/src/main/java/com/shawnlin/sample/MainActivity.java index 96dc5ff..e2f75a4 100644 --- a/sample/src/main/java/com/shawnlin/sample/MainActivity.java +++ b/sample/src/main/java/com/shawnlin/sample/MainActivity.java @@ -1,17 +1,19 @@ package com.shawnlin.sample; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; +import android.widget.Button; import android.widget.Toast; import com.shawnlin.preferencesmanager.PreferencesManager; public class MainActivity extends AppCompatActivity { + private static final String KEY_INT = "int"; + private static final String KEY_OBJECT = "object"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); @@ -22,14 +24,36 @@ public class MainActivity extends AppCompatActivity { .setName("prefs") .init(); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { + + Button intButton = (Button) findViewById(R.id.int_button); + intButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - PreferencesManager.putInt("test", (int) (Math.random() * 10 + 10)); - Toast.makeText(MainActivity.this, String.valueOf(PreferencesManager.getInt("test")), - Toast.LENGTH_SHORT).show(); + PreferencesManager.putInt(KEY_INT, (int) (Math.random() * 10 + 10)); + makeToast(String.valueOf(PreferencesManager.getInt(KEY_INT))); } }); + + Button objectButton = (Button) findViewById(R.id.object_button); + objectButton.setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View view) { + PreferencesManager.putObject(KEY_OBJECT, new Person(getString(R.string.app_name))); + makeToast(PreferencesManager.getObject(KEY_OBJECT, Person.class).name); + } + }); + } + + private void makeToast(String str) { + Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); + } + + class Person { + + public String name; + + public Person(String name) { + this.name = name; + } + } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index b2966c7..addf49b 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -23,12 +23,4 @@ - - diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index 0656871..14593a7 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -1,19 +1,25 @@ - + android:paddingTop="@dimen/activity_vertical_margin"> - - + android:text="putInt()" /> + +