-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2adce9f
commit 76c42cb
Showing
10 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
apply plugin: 'com.github.dcendents.android-maven' | ||
apply plugin: 'com.jfrog.bintray' | ||
version = "1.0.5" | ||
|
||
android { | ||
compileSdkVersion 24 | ||
buildToolsVersion "24.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 9 | ||
targetSdkVersion 24 | ||
versionCode 6 | ||
versionName "1.0.5" | ||
|
||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
|
||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', include: ['*.jar']) | ||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
}) | ||
testCompile 'junit:junit:4.12' | ||
} | ||
|
||
|
||
def siteUrl = 'https://github.com/ThirtyDegreesRay/DataAutoAccess' // 项目的主页 | ||
def gitUrl = 'https://github.com/ThirtyDegreesRay/DataAutoAccess.git' // Git仓库的url | ||
group = "com.thirtydegreesray.dataautoaccess" // Maven Group ID for the artifact, | ||
install { | ||
repositories.mavenInstaller { | ||
// This generates POM.xml with proper parameters | ||
pom { | ||
project { | ||
packaging 'aar' | ||
// Add your description here | ||
name 'Android bundle data auto access' //项目的描述 你可以多写一点 | ||
url siteUrl | ||
// Set your license | ||
licenses { | ||
license { | ||
name 'The Apache Software License, Version 2.0' | ||
url 'http://www.apache.org/licenses/LICENSE-2.0.txt' | ||
} | ||
} | ||
developers { | ||
developer { | ||
id 'ThirtyDegreesRay' //填写的一些基本信息 | ||
name 'ThirtyDegreesRay' | ||
email '550906320@qq.com' | ||
} | ||
} | ||
scm { | ||
connection gitUrl | ||
developerConnection gitUrl | ||
url siteUrl | ||
} | ||
} | ||
} | ||
} | ||
} | ||
task sourcesJar(type: Jar) { | ||
from android.sourceSets.main.java.srcDirs | ||
classifier = 'sources' | ||
} | ||
task javadoc(type: Javadoc) { | ||
source = android.sourceSets.main.java.srcDirs | ||
classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) | ||
} | ||
task javadocJar(type: Jar, dependsOn: javadoc) { | ||
classifier = 'javadoc' | ||
from javadoc.destinationDir | ||
} | ||
artifacts { | ||
archives javadocJar | ||
archives sourcesJar | ||
} | ||
Properties properties = new Properties() | ||
//读取properties的配置信息,当然直接把信息写到代码里也是可以的 | ||
properties.load(project.rootProject.file('local.properties').newDataInputStream()) | ||
bintray { | ||
user = properties.getProperty("bintray.user") | ||
key = properties.getProperty("bintray.apikey") | ||
configurations = ['archives'] | ||
pkg { | ||
repo = "maven" //这个应该是传到maven的仓库的 | ||
name = "data-auto-access" //发布的项目名字 | ||
websiteUrl = siteUrl | ||
vcsUrl = gitUrl | ||
licenses = ["Apache-2.0"] | ||
publish = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in E:\develop\Android\sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
26 changes: 26 additions & 0 deletions
26
...ary/src/androidTest/java/com/thirtydegreesray/dataautoaccess/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.thirtydegreesray.dataautoaccess; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumentation test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() throws Exception { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("com.thirtydegreesray.dataautoaccess.test", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.thirtydegreesray.dataautoaccess"> | ||
|
||
<application android:allowBackup="true" android:label="@string/app_name" | ||
android:supportsRtl="true"> | ||
|
||
</application> | ||
|
||
</manifest> |
29 changes: 29 additions & 0 deletions
29
Library/src/main/java/com/thirtydegreesray/dataautoaccess/DataAutoAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.thirtydegreesray.dataautoaccess; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* data auto access<br> | ||
* set this inject, the object will save automatic when onSaveInstanceState, and get data when onCreate<br> | ||
* if you want transfer data with intent, you must set dataName value<br> | ||
* field type we supported,@see com.thirtydegreesray.dataautoaccess.DataAutoAccessTool#saveData | ||
* @author ThirtyDegreesRay | ||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DataAutoAccess { | ||
/** | ||
* if you want transfer data with intent, you must set this value<br> | ||
* onSaveInstanceState don't use this value, use filed name directly | ||
* @return dataName | ||
*/ | ||
String dataName() default ""; | ||
/** | ||
* if the field is the type of ArrayList,you need declare the type of ArrayList | ||
* @return arrayListType | ||
*/ | ||
Class<?> arrayListType() default String.class; | ||
} |
165 changes: 165 additions & 0 deletions
165
Library/src/main/java/com/thirtydegreesray/dataautoaccess/DataAutoAccessTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.thirtydegreesray.dataautoaccess; | ||
|
||
import android.os.Bundle; | ||
import android.os.Parcelable; | ||
import android.util.Log; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* data auto access tool | ||
* @author ThirtyDegreesRay | ||
* | ||
*/ | ||
public class DataAutoAccessTool { | ||
|
||
private final static String TAG = "DataAutoAccessTool"; | ||
|
||
/** | ||
* save data | ||
* @param injectedSource the object need to save data | ||
* @param outState the bundle to save data | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static void saveData(Object injectedSource, Bundle outState){ | ||
if(injectedSource == null || outState == null) | ||
return ; | ||
|
||
Field[] fields = injectedSource.getClass().getDeclaredFields(); | ||
if(fields!=null && fields.length>0){ | ||
for(Field field : fields){ | ||
try { | ||
field.setAccessible(true); | ||
|
||
DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class); | ||
if(dataAutoAccess != null){ | ||
Class<?> type = field.getType(); | ||
Object value = field.get(injectedSource); | ||
String key = field.getName(); | ||
|
||
if(type.equals(String.class)){ | ||
outState.putString(key, (String) value); | ||
}else if(type.equals(int.class)){ | ||
outState.putInt(key, (Integer) value); | ||
}else if(type.equals(boolean.class)){ | ||
outState.putBoolean(key, (Boolean) value); | ||
}else if(type.equals(double.class)){ | ||
outState.putDouble(key, (Double) value); | ||
}else if(type.equals(float.class)){ | ||
outState.putFloat(key, (Float) value); | ||
}else if(type.equals(long.class)){ | ||
outState.putLong(key, (Long) value); | ||
}else if(type.equals(byte.class)){ | ||
outState.putByte(key, (Byte) value); | ||
}else if(type.equals(char.class)){ | ||
outState.putChar(key, (Character) value); | ||
}else if(type.equals(short.class)){ | ||
outState.putShort(key, (Short) value); | ||
}else if(type.equals(Parcelable.class)){ | ||
outState.putParcelable(key, (Parcelable) value); | ||
}else if(type.equals(Serializable.class)){ | ||
outState.putSerializable(key, (Serializable) value); | ||
}else if(type.equals(Bundle.class)){ | ||
outState.putBundle(key, (Bundle) value); | ||
} | ||
|
||
else if(type.equals(ArrayList.class)){ | ||
Class<?> arrayListType = dataAutoAccess.arrayListType(); | ||
if(arrayListType.equals(String.class)){ | ||
outState.putStringArrayList(key, (ArrayList<String>) value); | ||
}else if(arrayListType.equals(Integer.class)){ | ||
outState.putIntegerArrayList(key, (ArrayList<Integer>) value); | ||
}else if(arrayListType.equals(Parcelable.class)){ | ||
outState.putParcelableArrayList(key, (ArrayList<? extends Parcelable>) value); | ||
} | ||
} | ||
|
||
else if(type.equals(String[].class)){ | ||
outState.putStringArray(key, (String[]) value); | ||
}else if(type.equals(int[].class)){ | ||
outState.putIntArray(key, (int[]) value); | ||
}else if(type.equals(boolean[].class)){ | ||
outState.putBooleanArray(key, (boolean[]) value); | ||
}else if(type.equals(double[].class)){ | ||
outState.putDoubleArray(key, (double[]) value); | ||
}else if(type.equals(float[].class)){ | ||
outState.putFloatArray(key, (float[]) value); | ||
}else if(type.equals(long[].class)){ | ||
outState.putLongArray(key, (long[]) value); | ||
}else if(type.equals(byte[].class)){ | ||
outState.putByteArray(key, (byte[]) value); | ||
}else if(type.equals(char[].class)){ | ||
outState.putCharArray(key, (char[]) value); | ||
}else if(type.equals(short[].class)){ | ||
outState.putShortArray(key, (short[]) value); | ||
}else if(type.equals(Parcelable[].class)){ | ||
outState.putParcelableArray(key, (Parcelable[]) value); | ||
} | ||
// Log.i("Save", "save success:filed " + field.getName() ); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Log.w("Save", "save exception:filed " + field.getName() + " " + e.getMessage().toString()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* get data from bundle, and init filed value | ||
* @param injectedSource the object need init | ||
* @param data bundle data | ||
* @param isFromIntent if the data from intent, set true, otherwise set false | ||
*/ | ||
public static void getData(Object injectedSource, Bundle data, boolean isFromIntent){ | ||
if(injectedSource == null || data == null) | ||
return ; | ||
|
||
Field[] fields = injectedSource.getClass().getDeclaredFields(); | ||
if(fields!=null && fields.length>0){ | ||
for(Field field : fields){ | ||
String error = null; | ||
try { | ||
field.setAccessible(true); | ||
|
||
DataAutoAccess dataAutoAccess = field.getAnnotation(DataAutoAccess.class); | ||
if(dataAutoAccess != null){ | ||
String key ; | ||
//because the field name will changed when proguard, so we need to set ‘dataName' as key | ||
if(isFromIntent){ | ||
key = dataAutoAccess.dataName(); | ||
}else{ | ||
//when onSaveInstanceState, we save field name as key, so get use filed name too | ||
key = field.getName(); | ||
} | ||
|
||
if(key.equals("")) | ||
continue; | ||
|
||
if(data.containsKey(key)){ | ||
Object value = data.get(key); | ||
field.set(injectedSource, value); | ||
// Log.i("Save", "get success:filed " + field.getName() ); | ||
}else{ | ||
error = key + "don't exits"; | ||
} | ||
|
||
} | ||
} catch (Exception e) { | ||
error = field.getName() + " get Exception:" + e.getMessage().toString(); | ||
} | ||
|
||
if(error != null){ | ||
if(isFromIntent){ | ||
Log.w("GetIntentDataError", injectedSource.getClass().getName() + " " + error); | ||
}else{ | ||
Log.w("GetSaveInstanceError", | ||
injectedSource.getClass().getName() + " " + error); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">Library</string> | ||
</resources> |
17 changes: 17 additions & 0 deletions
17
Library/src/test/java/com/thirtydegreesray/dataautoaccess/ExampleUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.thirtydegreesray.dataautoaccess; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Example local unit test, which will execute on the development machine (host). | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
public class ExampleUnitTest { | ||
@Test | ||
public void addition_isCorrect() throws Exception { | ||
assertEquals(4, 2 + 2); | ||
} | ||
} |
Oops, something went wrong.