Skip to content

Commit 76c42cb

Browse files
initial version
1 parent 2adce9f commit 76c42cb

10 files changed

Lines changed: 403 additions & 0 deletions

File tree

Library/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Library/build.gradle

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
apply plugin: 'com.android.library'
2+
3+
apply plugin: 'com.github.dcendents.android-maven'
4+
apply plugin: 'com.jfrog.bintray'
5+
version = "1.0.5"
6+
7+
android {
8+
compileSdkVersion 24
9+
buildToolsVersion "24.0.2"
10+
11+
defaultConfig {
12+
minSdkVersion 9
13+
targetSdkVersion 24
14+
versionCode 6
15+
versionName "1.0.5"
16+
17+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18+
19+
}
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
}
27+
28+
dependencies {
29+
compile fileTree(dir: 'libs', include: ['*.jar'])
30+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
31+
exclude group: 'com.android.support', module: 'support-annotations'
32+
})
33+
testCompile 'junit:junit:4.12'
34+
}
35+
36+
37+
def siteUrl = 'https://github.com/ThirtyDegreesRay/DataAutoAccess' // 项目的主页
38+
def gitUrl = 'https://github.com/ThirtyDegreesRay/DataAutoAccess.git' // Git仓库的url
39+
group = "com.thirtydegreesray.dataautoaccess" // Maven Group ID for the artifact,
40+
install {
41+
repositories.mavenInstaller {
42+
// This generates POM.xml with proper parameters
43+
pom {
44+
project {
45+
packaging 'aar'
46+
// Add your description here
47+
name 'Android bundle data auto access' //项目的描述 你可以多写一点
48+
url siteUrl
49+
// Set your license
50+
licenses {
51+
license {
52+
name 'The Apache Software License, Version 2.0'
53+
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
54+
}
55+
}
56+
developers {
57+
developer {
58+
id 'ThirtyDegreesRay' //填写的一些基本信息
59+
name 'ThirtyDegreesRay'
60+
email '550906320@qq.com'
61+
}
62+
}
63+
scm {
64+
connection gitUrl
65+
developerConnection gitUrl
66+
url siteUrl
67+
}
68+
}
69+
}
70+
}
71+
}
72+
task sourcesJar(type: Jar) {
73+
from android.sourceSets.main.java.srcDirs
74+
classifier = 'sources'
75+
}
76+
task javadoc(type: Javadoc) {
77+
source = android.sourceSets.main.java.srcDirs
78+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
79+
}
80+
task javadocJar(type: Jar, dependsOn: javadoc) {
81+
classifier = 'javadoc'
82+
from javadoc.destinationDir
83+
}
84+
artifacts {
85+
archives javadocJar
86+
archives sourcesJar
87+
}
88+
Properties properties = new Properties()
89+
//读取properties的配置信息,当然直接把信息写到代码里也是可以的
90+
properties.load(project.rootProject.file('local.properties').newDataInputStream())
91+
bintray {
92+
user = properties.getProperty("bintray.user")
93+
key = properties.getProperty("bintray.apikey")
94+
configurations = ['archives']
95+
pkg {
96+
repo = "maven" //这个应该是传到maven的仓库的
97+
name = "data-auto-access" //发布的项目名字
98+
websiteUrl = siteUrl
99+
vcsUrl = gitUrl
100+
licenses = ["Apache-2.0"]
101+
publish = true
102+
}
103+
}

Library/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in E:\develop\Android\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+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thirtydegreesray.dataautoaccess;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.thirtydegreesray.dataautoaccess.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.thirtydegreesray.dataautoaccess">
3+
4+
<application android:allowBackup="true" android:label="@string/app_name"
5+
android:supportsRtl="true">
6+
7+
</application>
8+
9+
</manifest>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thirtydegreesray.dataautoaccess;
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+
* data auto access<br>
10+
* set this inject, the object will save automatic when onSaveInstanceState, and get data when onCreate<br>
11+
* if you want transfer data with intent, you must set dataName value<br>
12+
* field type we supported,@see com.thirtydegreesray.dataautoaccess.DataAutoAccessTool#saveData
13+
* @author ThirtyDegreesRay
14+
*/
15+
@Target(ElementType.FIELD)
16+
@Retention(RetentionPolicy.RUNTIME)
17+
public @interface DataAutoAccess {
18+
/**
19+
* if you want transfer data with intent, you must set this value<br>
20+
* onSaveInstanceState don't use this value, use filed name directly
21+
* @return dataName
22+
*/
23+
String dataName() default "";
24+
/**
25+
* if the field is the type of ArrayList,you need declare the type of ArrayList
26+
* @return arrayListType
27+
*/
28+
Class<?> arrayListType() default String.class;
29+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.thirtydegreesray.dataautoaccess;
2+
3+
import android.os.Bundle;
4+
import android.os.Parcelable;
5+
import android.util.Log;
6+
7+
import java.io.Serializable;
8+
import java.lang.reflect.Field;
9+
import java.util.ArrayList;
10+
11+
/**
12+
* data auto access tool
13+
* @author ThirtyDegreesRay
14+
*
15+
*/
16+
public class DataAutoAccessTool {
17+
18+
private final static String TAG = "DataAutoAccessTool";
19+
20+
/**
21+
* save data
22+
* @param injectedSource the object need to save data
23+
* @param outState the bundle to save data
24+
*/
25+
@SuppressWarnings("unchecked")
26+
public static void saveData(Object injectedSource, Bundle outState){
27+
if(injectedSource == null || outState == null)
28+
return ;
29+
30+
Field[] fields = injectedSource.getClass().getDeclaredFields();
31+
if(fields!=null && fields.length>0){
32+
for(Field field : fields){
33+
try {
34+
field.setAccessible(true);
35+
36+
DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class);
37+
if(dataAutoAccess != null){
38+
Class<?> type = field.getType();
39+
Object value = field.get(injectedSource);
40+
String key = field.getName();
41+
42+
if(type.equals(String.class)){
43+
outState.putString(key, (String) value);
44+
}else if(type.equals(int.class)){
45+
outState.putInt(key, (Integer) value);
46+
}else if(type.equals(boolean.class)){
47+
outState.putBoolean(key, (Boolean) value);
48+
}else if(type.equals(double.class)){
49+
outState.putDouble(key, (Double) value);
50+
}else if(type.equals(float.class)){
51+
outState.putFloat(key, (Float) value);
52+
}else if(type.equals(long.class)){
53+
outState.putLong(key, (Long) value);
54+
}else if(type.equals(byte.class)){
55+
outState.putByte(key, (Byte) value);
56+
}else if(type.equals(char.class)){
57+
outState.putChar(key, (Character) value);
58+
}else if(type.equals(short.class)){
59+
outState.putShort(key, (Short) value);
60+
}else if(type.equals(Parcelable.class)){
61+
outState.putParcelable(key, (Parcelable) value);
62+
}else if(type.equals(Serializable.class)){
63+
outState.putSerializable(key, (Serializable) value);
64+
}else if(type.equals(Bundle.class)){
65+
outState.putBundle(key, (Bundle) value);
66+
}
67+
68+
else if(type.equals(ArrayList.class)){
69+
Class<?> arrayListType = dataAutoAccess.arrayListType();
70+
if(arrayListType.equals(String.class)){
71+
outState.putStringArrayList(key, (ArrayList<String>) value);
72+
}else if(arrayListType.equals(Integer.class)){
73+
outState.putIntegerArrayList(key, (ArrayList<Integer>) value);
74+
}else if(arrayListType.equals(Parcelable.class)){
75+
outState.putParcelableArrayList(key, (ArrayList<? extends Parcelable>) value);
76+
}
77+
}
78+
79+
else if(type.equals(String[].class)){
80+
outState.putStringArray(key, (String[]) value);
81+
}else if(type.equals(int[].class)){
82+
outState.putIntArray(key, (int[]) value);
83+
}else if(type.equals(boolean[].class)){
84+
outState.putBooleanArray(key, (boolean[]) value);
85+
}else if(type.equals(double[].class)){
86+
outState.putDoubleArray(key, (double[]) value);
87+
}else if(type.equals(float[].class)){
88+
outState.putFloatArray(key, (float[]) value);
89+
}else if(type.equals(long[].class)){
90+
outState.putLongArray(key, (long[]) value);
91+
}else if(type.equals(byte[].class)){
92+
outState.putByteArray(key, (byte[]) value);
93+
}else if(type.equals(char[].class)){
94+
outState.putCharArray(key, (char[]) value);
95+
}else if(type.equals(short[].class)){
96+
outState.putShortArray(key, (short[]) value);
97+
}else if(type.equals(Parcelable[].class)){
98+
outState.putParcelableArray(key, (Parcelable[]) value);
99+
}
100+
// Log.i("Save", "save success:filed " + field.getName() );
101+
}
102+
} catch (Exception e) {
103+
e.printStackTrace();
104+
Log.w("Save", "save exception:filed " + field.getName() + " " + e.getMessage().toString());
105+
}
106+
}
107+
}
108+
}
109+
110+
/**
111+
* get data from bundle, and init filed value
112+
* @param injectedSource the object need init
113+
* @param data bundle data
114+
* @param isFromIntent if the data from intent, set true, otherwise set false
115+
*/
116+
public static void getData(Object injectedSource, Bundle data, boolean isFromIntent){
117+
if(injectedSource == null || data == null)
118+
return ;
119+
120+
Field[] fields = injectedSource.getClass().getDeclaredFields();
121+
if(fields!=null && fields.length>0){
122+
for(Field field : fields){
123+
String error = null;
124+
try {
125+
field.setAccessible(true);
126+
127+
DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class);
128+
if(dataAutoAccess != null){
129+
String key ;
130+
//because the field name will changed when proguard, so we need to set ‘dataName' as key
131+
if(isFromIntent){
132+
key = dataAutoAccess.dataName();
133+
}else{
134+
//when onSaveInstanceState, we save field name as key, so get use filed name too
135+
key = field.getName();
136+
}
137+
138+
if(key.equals(""))
139+
continue;
140+
141+
if(data.containsKey(key)){
142+
Object value = data.get(key);
143+
field.set(injectedSource, value);
144+
// Log.i("Save", "get success:filed " + field.getName() );
145+
}else{
146+
error = key + "don't exits";
147+
}
148+
149+
}
150+
} catch (Exception e) {
151+
error = field.getName() + " get Exception:" + e.getMessage().toString();
152+
}
153+
154+
if(error != null){
155+
if(isFromIntent){
156+
Log.w("GetIntentDataError", injectedSource.getClass().getName() + " " + error);
157+
}else{
158+
Log.w("GetSaveInstanceError",
159+
injectedSource.getClass().getName() + " " + error);
160+
}
161+
}
162+
}
163+
}
164+
}
165+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Library</string>
3+
</resources>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.thirtydegreesray.dataautoaccess;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}

0 commit comments

Comments
 (0)