Skip to content

Commit 2083b28

Browse files
committed
Support EasyBundle
1 parent 6f6859c commit 2083b28

File tree

7 files changed

+432
-1
lines changed

7 files changed

+432
-1
lines changed

app/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ dependencies {
2727
implementation 'junit:junit:4.12'
2828
implementation 'com.android.support:appcompat-v7:27.1.1'
2929

30-
implementation 'com.alibaba:fastjson:1.2.46'
30+
implementation 'com.alibaba:fastjson:1.1.68.android'
31+
implementation 'com.google.code.gson:gson:2.8.5'
32+
33+
implementation 'org.jetbrains.anko:anko-commons:0.10.5'
3134

3235
implementation "com.jakewharton:butterknife:$butterknife_version"
3336
kapt "com.jakewharton:butterknife-compiler:$butterknife_version"

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
<activity android:name=".activities.EasyResultActivity" />
2929
<activity android:name=".activities.EasyPermissionsActivity" />
3030
<activity android:name=".activities.EasyExecutorActivity" />
31+
<activity android:name=".activities.EasyBundleActivity" />
3132
</application>
3233
</manifest>

app/src/main/java/com/haoge/sample/easyandroid/DemosActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DemosActivity:ListActivity() {
2828
Item("测试EasyActivityResult", EasyResultActivity::class.java),
2929
Item("测试EasyPermissions", EasyPermissionsActivity::class.java),
3030
Item("测试EasyExecutor", EasyExecutorActivity::class.java),
31+
Item("测试EasyBundle", EasyBundleActivity::class.java),
3132
Item("测试MVPDemo", MVPDemoActivity::class.java)
3233
)
3334

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.haoge.sample.easyandroid.activities
2+
3+
import android.os.Bundle
4+
import android.os.Parcel
5+
import android.os.Parcelable
6+
import butterknife.OnClick
7+
import com.google.gson.Gson
8+
import com.haoge.easyandroid.easy.EasyBundle
9+
import com.haoge.easyandroid.easy.EasyLog
10+
import com.haoge.sample.easyandroid.BaseActivity
11+
import com.haoge.sample.easyandroid.R
12+
import java.io.Serializable
13+
14+
/**
15+
* @author haoge on 2018/6/14
16+
*/
17+
class EasyBundleActivity:BaseActivity() {
18+
override fun getLayoutId() = R.layout.activity_bundle
19+
20+
@OnClick(R.id.savePrimite)
21+
fun savePrimite() {
22+
// 基本数据类型存储测试
23+
val bundle = EasyBundle.create(null).put("byte", 1.toByte())
24+
.put("short", 2.toShort())
25+
.put("int", 3)
26+
.put("long", 4.toLong())
27+
.put("float", 5.toFloat())
28+
.put("double", 6.toDouble())
29+
.put("char", 'c')
30+
.put("boolean", true)
31+
.put("String", "String")
32+
.bundle
33+
EasyLog.DEFAULT.e("存储基本数据类型后的bundle数据为==>$bundle \n 要求长度为9 实际为${bundle.size()}")
34+
}
35+
36+
@OnClick(R.id.savePrimiteArray)
37+
fun savePrimiteArray() {
38+
// 基本数据类型数组存储测试
39+
val bundle = EasyBundle.create(null)
40+
.put("IntArray", intArrayOf(1,2,3))
41+
.put("LongArray", longArrayOf(1,2,3))
42+
.put("FloatArray", floatArrayOf(1f,2f,3f))
43+
.put("DoubleArray", doubleArrayOf(1.toDouble(),2.toDouble(),3.toDouble()))
44+
.put("CharArray", charArrayOf('a','b','c'))
45+
.put("ShortArray", shortArrayOf(1,2,3))
46+
.put("BooleanArray", booleanArrayOf(true,false,true))
47+
.put("StringArray", arrayOf("hello", "world"))
48+
.bundle
49+
50+
EasyLog.DEFAULT.e("存储基本数据类型数组后的bundle数据为==>$bundle \n 要求长度为8 实际为${bundle.size()}")
51+
}
52+
53+
@OnClick(R.id.saveSerializable)
54+
fun saveSerializable() {
55+
// 其他序列化数据存储测试
56+
val bundle = EasyBundle.create(null)
57+
.put("parcelable", ParcelableSubclass())
58+
.put("serializable", SerializableSubclass())
59+
.put("arrayParcelable", arrayOf(ParcelableSubclass(), ParcelableSubclass()))
60+
.put("bundle", Bundle())
61+
.bundle
62+
EasyLog.DEFAULT.e("存储可序列化数据后 ==> $bundle \n 要求长度为4,实际为${bundle.size()}")
63+
}
64+
65+
@OnClick(R.id.saveUnSerializable)
66+
fun saveUnSerializable() {
67+
val bundle = EasyBundle.create(null)
68+
.put("info", Info("存储的名字"))
69+
.bundle
70+
EasyLog.DEFAULT.e("存储后的info数据为 ==> ${bundle.get("info")}")
71+
}
72+
73+
@OnClick(R.id.restorePrimite)
74+
fun restorePrimite() {
75+
val easyBundle = EasyBundle.create(null)
76+
// 使用空bundle进行读取。测试读取基本数据类型时。是否正确返回默认值
77+
EasyLog.DEFAULT.e("读取空Byte数据:${easyBundle.get("key", Byte::class.java)}")
78+
EasyLog.DEFAULT.e("读取空char数据:${easyBundle.get("key", Char::class.java)}")
79+
EasyLog.DEFAULT.e("读取空Short数据:${easyBundle.get("key", Short::class.java)}")
80+
EasyLog.DEFAULT.e("读取空Int数据:${easyBundle.get("key", Int::class.java)}")
81+
EasyLog.DEFAULT.e("读取空Long数据:${easyBundle.get("key", Long::class.java)}")
82+
EasyLog.DEFAULT.e("读取空Float数据:${easyBundle.get("key", Float::class.java)}")
83+
EasyLog.DEFAULT.e("读取空Double数据:${easyBundle.get("key", Double::class.java)}")
84+
EasyLog.DEFAULT.e("读取空Boolean数据:${easyBundle.get("key", Boolean::class.java)}")
85+
}
86+
87+
@OnClick(R.id.restoreWithJSON)
88+
fun restoreWithJSON() {
89+
val json = Gson().toJson(Info("不可序列化的类"))
90+
val easyBundle = EasyBundle.create(null).put("jsonOfInfo", json)// 先将json保存进去
91+
val info = easyBundle.get<Info>("jsonOfInfo")
92+
EasyLog.DEFAULT.e("从json反序列化回来后的info数据:$info")
93+
}
94+
}
95+
96+
data class ParcelableSubclass(val name: String = "this is a subclass of Parcelable"):Parcelable {
97+
constructor(parcel: Parcel) : this(parcel.readString())
98+
99+
override fun writeToParcel(parcel: Parcel, flags: Int) {
100+
parcel.writeString(name)
101+
}
102+
103+
override fun describeContents(): Int {
104+
return 0
105+
}
106+
107+
companion object CREATOR : Parcelable.Creator<ParcelableSubclass> {
108+
override fun createFromParcel(parcel: Parcel): ParcelableSubclass {
109+
return ParcelableSubclass(parcel)
110+
}
111+
112+
override fun newArray(size: Int): Array<ParcelableSubclass?> {
113+
return arrayOfNulls(size)
114+
}
115+
}
116+
}
117+
118+
data class SerializableSubclass(val name: String = "this is a subclass of Serializable"):Serializable
119+
120+
class Info(val name:String?) {
121+
constructor():this("默认名字")// JSON反序列化时需要空构造
122+
123+
override fun toString(): String {
124+
return "Info(name=$name)"
125+
}
126+
127+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<LinearLayout
7+
android:orientation="vertical"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content">
10+
11+
<TextView
12+
android:text="请结合Logcat日志进行查看"
13+
android:padding="10dp"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content" />
16+
17+
<Button
18+
android:id="@+id/savePrimite"
19+
android:text="基本数据类型存储测试"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content" />
22+
23+
<Button
24+
android:id="@+id/savePrimiteArray"
25+
android:text="基本数据类型数组存储测试"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content" />
28+
29+
<Button
30+
android:id="@+id/saveSerializable"
31+
android:text="可序列化数据存储测试"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content" />
34+
35+
<Button
36+
android:id="@+id/saveUnSerializable"
37+
android:text="非可序列化数据存储测试(自动转为json)"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content" />
40+
41+
<Button
42+
android:id="@+id/restorePrimite"
43+
android:text="基本数据类型读取测试"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content" />
46+
47+
<Button
48+
android:id="@+id/restoreWithJSON"
49+
android:text="读取json数据并自动转换测试"
50+
android:layout_width="match_parent"
51+
android:layout_height="wrap_content" />
52+
53+
54+
55+
</LinearLayout>
56+
57+
</ScrollView>

utils/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ dependencies {
2525
testImplementation 'junit:junit:4.12'
2626

2727
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
28+
29+
compileOnly 'com.alibaba:fastjson:1.2.47'
30+
compileOnly 'com.google.code.gson:gson:2.8.5'
2831
}
2932

3033
apply from: '../javadoc.gradle'

0 commit comments

Comments
 (0)