├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── gradle.properties ├── proguard-rules.pro └── src │ ├── androidTest │ ├── assets │ │ └── config.properties │ └── java │ │ └── com │ │ └── github │ │ └── fernandodev │ │ └── androidproperties │ │ └── lib │ │ ├── AssetsPropertiesTest.java │ │ ├── Config.java │ │ └── ConfigTest.java │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── github │ └── fernandodev │ └── androidproperties │ └── lib │ ├── AssetsProperties.java │ └── Property.java ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ ├── another.properties │ └── config.properties │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── github │ │ └── fernandodev │ │ └── androidproperties │ │ └── sample │ │ ├── AnotherConfig.java │ │ ├── Config.java │ │ └── MainActivity.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ └── activity_main.xml │ ├── menu │ └── main.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | .DS_Store 5 | 6 | # files for the dex VM 7 | *.dex 8 | 9 | # Java class files 10 | *.class 11 | 12 | # generated files 13 | bin/ 14 | gen/ 15 | out/ 16 | target/ 17 | 18 | # Local configuration file (sdk path, etc) 19 | local.properties 20 | 21 | # Eclipse project files 22 | .classpath 23 | .metadata 24 | .project 25 | 26 | # Intellij project files 27 | *.iml 28 | *.ipr 29 | *.iws 30 | .idea/ 31 | 32 | # Gradle files 33 | .gradle 34 | build/ 35 | 36 | # Git 37 | *.orig 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | android: 3 | components: 4 | - build-tools-20.0.0 5 | - android-20 6 | - sys-img-armeabi-v7a-android-19 7 | - sys-img-x86-android-19 8 | # Emulator Management: Create, Start and Wait 9 | before_script: 10 | - echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a 11 | - emulator -avd test -no-skin -no-audio -no-window & 12 | - android-wait-for-emulator 13 | - adb shell input keyevent 82 & 14 | script: 15 | - ./gradlew connectedAndroidTest -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 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 2 | 3 | http://www.apache.org/licenses/LICENSE-2.0 4 | 5 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Android @Properties[![Build Status](https://travis-ci.org/fernandodev/android-properties.svg)](https://travis-ci.org/fernandodev/android-properties)[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-android--properties-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/938) 2 | == 3 | 4 | ![Imgur](http://i.imgur.com/UsSJEIB.jpg) 5 | 6 | This lib provides a simple way to read a `.properties` from assets folder. 7 | 8 | You just have to extends from AssetsProperties and use `@Property` annotation to invoke the automatic property mapping. 9 | 10 | ## Installation 11 | 12 | It's very simple with gradle ;) 13 | 14 | Add `mavenCentral` as repository source: 15 | 16 | ```gradle 17 | repositories { 18 | mavenCentral() 19 | } 20 | ``` 21 | 22 | And finnaly add this line inside `dependencies { }` section: 23 | 24 | ```gradle 25 | implementation 'com.github.fernandodev.androidproperties:androidproperties:+' 26 | ``` 27 | 28 | The `+` symbol indicates to gradle to get the latest version. 29 | 30 | * See the sample if there are any doubts. 31 | 32 | ## Using 33 | 34 | Super simple usage ;) 35 | 36 | Android Properties can parse `String`,`int`,`float`,`double` and `boolean` values from file `.properties`. 37 | 38 | Create a class that represents your file properties and extend from `AssetsProperties` class. 39 | 40 | Example: 41 | 42 | **assets/config.properties** 43 | ``` 44 | int_value=23 45 | flag=true 46 | tax=2.3f 47 | ``` 48 | 49 | **Config.java** 50 | ```java 51 | public class Config extends AssetsProperties { 52 | 53 | @Property("int_value") int intValue; 54 | @Property boolean flag; 55 | @Property float tax; 56 | 57 | public Config(Context context) { super(context); } 58 | } 59 | ``` 60 | 61 | You must use `@Property` annotation to map field as property field from file. Pass the name of property if it's different from 62 | the field name in class. 63 | 64 | ## Tips 65 | 66 | ### Read another file properties 67 | 68 | The default file for properties is called `config.properties` but if you need to read another file properties, just pass the name of file in the constructor: 69 | 70 | ```java 71 | super(context, "another_properties_file"); 72 | ``` 73 | 74 | ### Map property name 75 | 76 | The `@Property` annotation uses the name of field as name of property field in file. So if the field name is diferent of the name in file, just pass the name of property by `@Property("property_name")`. 77 | 78 | ### Useful public methods 79 | 80 | If you do not want to use `@Property` annotation or need only a reader property class you can use some public methods from `AssetsProperties`: 81 | 82 | ```java 83 | public int getInt(String key, int defaultValue); 84 | 85 | public float getFloat(String key, float defaultValue); 86 | 87 | public double getDouble(String key, double defaultValue); 88 | 89 | public boolean getBoolean(String key, boolean defaultValue); 90 | 91 | public String getString(String key, String defaultValue); 92 | ``` 93 | 94 | If the AssetsProperties can't read the key then the defaultValue will be returned. 95 | 96 | ## Testing 97 | 98 | There are a simple test for the rating dialog. If you want to contribute check the tests too. 99 | 100 | ```shell 101 | $ git clone git@github.com:fernandodev/android-properties.git 102 | $ cd android-properties 103 | $ ./gradlew connectedAndroidTest --daemon 104 | ``` 105 | 106 | You must open an emulator before. 107 | 108 | ## License 109 | 110 | Licensed under the Apache License, Version 2.0 (the "License"); 111 | you may not use this file except in compliance with the License. 112 | You may obtain a copy of the License at 113 | 114 | http://www.apache.org/licenses/LICENSE-2.0 115 | 116 | Unless required by applicable law or agreed to in writing, software 117 | distributed under the License is distributed on an "AS IS" BASIS, 118 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 119 | See the License for the specific language governing permissions and 120 | limitations under the License. 121 | -------------------------------------------------------------------------------- /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:0.12.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | def isReleaseBuild() { 16 | return version.contains("SNAPSHOT") == false 17 | } 18 | 19 | allprojects { 20 | version = VERSION_NAME 21 | group = GROUP 22 | 23 | repositories { 24 | jcenter() 25 | } 26 | } 27 | 28 | apply plugin: 'android-reporting' -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 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 19 | 20 | VERSION_NAME=1.0.0 21 | VERSION_CODE=1 22 | GROUP=com.github.fernandodev.androidproperties 23 | 24 | POM_DESCRIPTION=Android Library for reading properties file stored in assets folder 25 | POM_URL=https://github.com/fernandodev/android-properties 26 | POM_SCM_URL=https://github.com/fernandodev/android-properties 27 | POM_SCM_CONNECTION=scm:git:git@github.com:fernandodev/android-properties.git 28 | POM_SCM_DEV_CONNECTION=scm:git:git@github.com:fernandodev/android-properties.git 29 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 30 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 31 | POM_LICENCE_DIST=repo 32 | POM_DEVELOPER_ID=fernandodev 33 | POM_DEVELOPER_NAME=Fernando Martinez -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 20 5 | buildToolsVersion "20.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.github.fernandodev.androidproperties.lib" 9 | minSdkVersion 8 10 | targetSdkVersion 19 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | runProguard false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle' 26 | -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=Android Properties Library 2 | POM_ARTIFACT_ID=androidproperties 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /library/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 /Applications/Android Studio.app/sdk/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 | -------------------------------------------------------------------------------- /library/src/androidTest/assets/config.properties: -------------------------------------------------------------------------------- 1 | my_message=Hello World 2 | tax=3.15146568 3 | max_messages=10 4 | flag=true 5 | rate=0.23f -------------------------------------------------------------------------------- /library/src/androidTest/java/com/github/fernandodev/androidproperties/lib/AssetsPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.lib; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.test.ApplicationTestCase; 6 | 7 | /** 8 | * Testing Fundamentals 9 | */ 10 | public class AssetsPropertiesTest extends ApplicationTestCase { 11 | 12 | Context mContext; 13 | AssetsProperties properties; 14 | 15 | public AssetsPropertiesTest() { 16 | super(Application.class); 17 | } 18 | 19 | @Override 20 | protected void setUp() throws Exception { 21 | super.setUp(); 22 | mContext = getContext(); 23 | properties = new AssetsProperties(mContext); 24 | createApplication(); 25 | } 26 | 27 | public void testMaxMessages(){ 28 | int maxMessages = properties.getInt("max_messages", -1); 29 | assertEquals(10, maxMessages); 30 | } 31 | 32 | public void testMaxMessagesDefaultValue(){ 33 | int maxMessages = properties.getInt("wrong_key", -1); 34 | assertEquals(-1, maxMessages); 35 | } 36 | 37 | public void testTax(){ 38 | double tax = properties.getDouble("tax", -0.1); 39 | assertEquals(3.15146568, tax); 40 | } 41 | 42 | public void testTaxDefaultValue(){ 43 | double tax = properties.getDouble("wrong_key", -0.1); 44 | assertEquals(-0.1, tax); 45 | } 46 | 47 | public void testHelloWorld(){ 48 | String myMessage = properties.getString("my_message", ""); 49 | assertEquals("Hello World", myMessage); 50 | } 51 | 52 | public void testHelloWorldDefaultValue(){ 53 | String myMessage = properties.getString("wrong_key", ""); 54 | assertEquals("", myMessage); 55 | } 56 | 57 | public void testFlag(){ 58 | boolean flag = properties.getBoolean("flag", false); 59 | assertEquals(true, flag); 60 | } 61 | 62 | public void testFlagDefaultValue(){ 63 | boolean flag = properties.getBoolean("wrong_key", false); 64 | assertEquals(false, flag); 65 | } 66 | 67 | public void testRate(){ 68 | float rate = properties.getFloat("rate", -0.1f); 69 | assertEquals(0.23f, rate); 70 | } 71 | 72 | public void testRateDefaultValue(){ 73 | float rate = properties.getFloat("wrong_key", -0.1f); 74 | assertEquals(-0.1f, rate); 75 | } 76 | } -------------------------------------------------------------------------------- /library/src/androidTest/java/com/github/fernandodev/androidproperties/lib/Config.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.lib; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by fernando on 9/13/14. 7 | */ 8 | public class Config extends AssetsProperties { 9 | @Property("my_message") String myMessage; 10 | @Property double tax; 11 | @Property("max_messages") int maxMessages; 12 | @Property boolean flag; 13 | @Property float rate; 14 | 15 | public Config(Context context) { 16 | super(context); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/github/fernandodev/androidproperties/lib/ConfigTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.lib; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Created by fernando on 9/13/14. 8 | */ 9 | public class ConfigTest extends ApplicationTestCase { 10 | 11 | Config config; 12 | 13 | public ConfigTest() { 14 | super(Application.class); 15 | } 16 | 17 | @Override 18 | protected void setUp() throws Exception { 19 | super.setUp(); 20 | config = new Config(getContext()); 21 | createApplication(); 22 | } 23 | 24 | public void testMaxMessages(){ 25 | int maxMessages = config.maxMessages; 26 | assertEquals(10, maxMessages); 27 | } 28 | 29 | public void testTax(){ 30 | double tax = config.tax; 31 | assertEquals(3.15146568, tax); 32 | } 33 | 34 | public void testHelloWorld(){ 35 | String myMessage = config.myMessage; 36 | assertEquals("Hello World", myMessage); 37 | } 38 | 39 | public void testFlag(){ 40 | boolean flag = config.flag; 41 | assertEquals(true, flag); 42 | } 43 | 44 | public void testRate(){ 45 | float rate = config.rate; 46 | assertEquals(0.23f, rate); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 7 | 8 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/fernandodev/androidproperties/lib/AssetsProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.lib; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.util.Log; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.lang.reflect.Field; 10 | 11 | /** 12 | * Created by fernando on 9/10/14. 13 | */ 14 | public class AssetsProperties { 15 | 16 | private static final String TAG = "AssetsProperties"; 17 | private static final String EXTENSION = ".properties"; 18 | private static final String DEFAUT_ANNOTATION_VALUE = ""; 19 | 20 | private Context mContext; 21 | private Resources mResources; 22 | private String mPropertiesFileName = "config"; 23 | private java.util.Properties mProperties = new java.util.Properties(); 24 | 25 | public AssetsProperties(Context context) { 26 | mContext = context; 27 | mResources = context.getResources(); 28 | openProperties(mResources); 29 | } 30 | 31 | public AssetsProperties(Context context, String propertiesFileName) { 32 | mContext = context; 33 | mResources = context.getResources(); 34 | mPropertiesFileName = propertiesFileName; 35 | openProperties(mResources); 36 | } 37 | 38 | public int getInt(String key, int defaultValue) { 39 | try { 40 | return Integer.parseInt(mProperties.getProperty(key)); 41 | } catch (Exception e) { 42 | logParseError(key, "int"); 43 | return defaultValue; 44 | } 45 | } 46 | 47 | public float getFloat(String key, float defaultValue) { 48 | try { 49 | return Float.parseFloat(mProperties.getProperty(key)); 50 | } catch (Exception e) { 51 | logParseError(key, "float"); 52 | return defaultValue; 53 | } 54 | } 55 | 56 | public double getDouble(String key, double defaultValue) { 57 | try { 58 | return Double.parseDouble(mProperties.getProperty(key)); 59 | } catch (Exception e) { 60 | logParseError(key, "double"); 61 | return defaultValue; 62 | } 63 | } 64 | 65 | public boolean getBoolean(String key, boolean defaultValue) { 66 | try { 67 | return Boolean.parseBoolean(mProperties.getProperty(key)); 68 | } catch (Exception e) { 69 | logParseError(key, "boolean"); 70 | return defaultValue; 71 | } 72 | } 73 | 74 | public String getString(String key, String defaultValue) { 75 | return mProperties.getProperty(key, defaultValue); 76 | } 77 | 78 | private void openProperties(Resources resources) { 79 | try { 80 | InputStream inputStream = resources.getAssets().open(mPropertiesFileName + EXTENSION); 81 | mProperties.load(inputStream); 82 | loadPropertiesValues(); 83 | } catch (IOException e) { 84 | Log.wtf(TAG, e); 85 | } 86 | } 87 | 88 | protected void loadPropertiesValues() { 89 | Class thisClass = this.getClass(); 90 | Field[] fields = thisClass.getDeclaredFields(); 91 | for (Field field : fields) { 92 | if (field.isAnnotationPresent(Property.class)) { 93 | field.setAccessible(true); 94 | String fieldName = field.getName(); 95 | Property annotation = field.getAnnotation(Property.class); 96 | 97 | if (annotation.value().equals(DEFAUT_ANNOTATION_VALUE)) { 98 | setFieldValue(field, fieldName); 99 | } else { 100 | setFieldValue(field, annotation.value()); 101 | } 102 | } 103 | } 104 | } 105 | 106 | private void setFieldValue(Field field, String propertiesName) { 107 | Object value = getPropertyValue(field.getType(), propertiesName); 108 | try { 109 | field.set(this, value); 110 | } catch (IllegalAccessException e) { 111 | Log.e(TAG, "AssetsProperties : impossible to set value of field: " 112 | + field.getName() + " for " + propertiesName); 113 | } 114 | } 115 | 116 | private Object getPropertyValue(Class clazz, String key) { 117 | if (clazz == String.class) { 118 | return getString(key, ""); 119 | } else if (clazz == float.class || clazz == Float.class) { 120 | return getFloat(key, 0); 121 | } else if (clazz == double.class || clazz == Double.class) { 122 | return getDouble(key, 0); 123 | } else if (clazz == boolean.class || clazz == Boolean.class) { 124 | return getBoolean(key, false); 125 | } else if (clazz == int.class || clazz == Integer.class) { 126 | return getInt(key, 0); 127 | } else { 128 | return null; 129 | } 130 | } 131 | 132 | private void logParseError(String key, String target) { 133 | Log.e(TAG, "AssetsProperties can't parse property " + key + " as " + target); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /library/src/main/java/com/github/fernandodev/androidproperties/lib/Property.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.lib; 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 | /** 9 | * Created by fernando on 9/11/14. 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.FIELD) 13 | public @interface Property { 14 | String value() default ""; 15 | } 16 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 20 5 | buildToolsVersion "20.0.0" 6 | 7 | defaultConfig { 8 | minSdkVersion 8 9 | targetSdkVersion 19 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | productFlavors { 14 | sample { 15 | applicationId "com.github.fernandodev.androidproperties.sample" 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | runProguard false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | compile project(':library') 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | compile 'com.android.support:appcompat-v7:20.0.0' 30 | compile 'com.parse.bolts:bolts-android:1.1.2' 31 | } 32 | -------------------------------------------------------------------------------- /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 /Applications/Android Studio.app/sdk/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 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sample/src/main/assets/another.properties: -------------------------------------------------------------------------------- 1 | staging_url=http://www.google.com 2 | production_url=http://www.gmail.com 3 | token=5cd1212ab90 4 | max_messages_count=15 -------------------------------------------------------------------------------- /sample/src/main/assets/config.properties: -------------------------------------------------------------------------------- 1 | max=10 2 | message=hello guys 3 | rate_value=3.5f 4 | condition=true 5 | temperature=2.4125612 -------------------------------------------------------------------------------- /sample/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/sample/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /sample/src/main/java/com/github/fernandodev/androidproperties/sample/AnotherConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.sample; 2 | 3 | import android.content.Context; 4 | 5 | import com.github.fernandodev.androidproperties.lib.AssetsProperties; 6 | import com.github.fernandodev.androidproperties.lib.Property; 7 | 8 | /** 9 | * Created by fernando on 9/13/14. 10 | */ 11 | public class AnotherConfig extends AssetsProperties { 12 | 13 | @Property("staging_url") String stagingUrl; 14 | @Property("production_url") String productionUrl; 15 | @Property("token") String token; 16 | @Property("max_messages_count") String maxMessagesCount; 17 | 18 | public AnotherConfig(Context context) { 19 | super(context, "another"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/fernandodev/androidproperties/sample/Config.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.sample; 2 | 3 | import android.content.Context; 4 | 5 | import com.github.fernandodev.androidproperties.lib.AssetsProperties; 6 | import com.github.fernandodev.androidproperties.lib.Property; 7 | 8 | /** 9 | * Created by fernando on 9/11/14. 10 | */ 11 | public class Config extends AssetsProperties { 12 | 13 | @Property int max; 14 | @Property("rate_value") float rateValue; 15 | @Property double temperature; 16 | @Property String message; 17 | @Property boolean condition; 18 | 19 | public Config(Context context) { 20 | super(context); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/fernandodev/androidproperties/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.fernandodev.androidproperties.sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.widget.TextView; 6 | 7 | import com.github.fernandodev.androidproperties.lib.AssetsProperties; 8 | 9 | 10 | public class MainActivity extends ActionBarActivity { 11 | Config myConfig; 12 | AnotherConfig anotherConfig; 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | myConfig = new Config(this); 19 | anotherConfig = new AnotherConfig(this); 20 | 21 | TextView configView = (TextView) findViewById(R.id.values_for_config); 22 | TextView anotherView = (TextView) findViewById(R.id.values_for_another); 23 | configView.setText( 24 | "message : " + myConfig.message + "\n" + 25 | "max : " + myConfig.max + "\n" + 26 | "rate_value : " + myConfig.rateValue + "\n" + 27 | "temperature : " + myConfig.temperature + "\n" 28 | ); 29 | 30 | anotherView.setText( 31 | "staging_url : " + anotherConfig.stagingUrl + "\n" + 32 | "production_url : " + anotherConfig.productionUrl + "\n" + 33 | "token_url : " + anotherConfig.token + "\n" + 34 | "max_messages_count : " + anotherConfig.maxMessagesCount 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandodev/android-properties/86e9f1ade99df2811dcfe0e6f83deca5f705cc81/sample/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 18 | 19 | 27 | 28 | 35 | 36 | 44 | 45 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /sample/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 5 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 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 | 3 | 4 | AndroidProperties 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library', ':sample' 2 | --------------------------------------------------------------------------------