@@ -2,7 +2,11 @@ package io.easyprefs.impl
22
33import android.content.SharedPreferences
44import io.easyprefs.contract.Write
5+ import io.easyprefs.secure.Crypt
56import io.easyprefs.typedef.Encryption
7+ import kotlinx.coroutines.GlobalScope
8+ import kotlinx.coroutines.launch
9+ import org.json.JSONArray
610
711class WriteImpl (
812 private val edit : SharedPreferences .Editor ,
@@ -14,63 +18,129 @@ class WriteImpl(
1418 return if (encType == Encryption .NONE ) {
1519 edit.putInt(key, value).commit()
1620 } else {
21+ crypt(key, value.toString())
22+ }
23+ }
24+
25+ private fun crypt (key : String , value : String ): Boolean {
26+ return if (aesKey.isEmpty()) {
27+ edit.putString(key, Crypt .encrypt(key, value)).commit()
28+ } else {
29+ edit.putString(key, Crypt .encrypt(aesKey, value)).commit()
30+ }
31+ }
32+
33+ private fun cryptAsync (key : String , value : String ) {
34+ GlobalScope .launch {
1735 if (aesKey.isEmpty()) {
18- edit.putInt (key, value).commit ()
36+ edit.putString (key, Crypt .encrypt(key, value)). apply ()
1937 } else {
20- edit.putInt (key, value).commit ()
38+ edit.putString (key, Crypt .encrypt(aesKey, value)). apply ()
2139 }
2240 }
2341 }
2442
2543 override fun intAsync (key : String , value : Int ) {
26- edit.putInt(key, value).apply ()
44+ if (encType == Encryption .NONE ) {
45+ edit.putInt(key, value).apply ()
46+ } else {
47+ cryptAsync(key, value.toString())
48+ }
2749 }
2850
2951 override fun string (key : String , value : String ): Boolean {
30- return edit.putString(key, value).commit()
52+ return if (encType == Encryption .NONE ) {
53+ edit.putString(key, value).commit()
54+ } else {
55+ crypt(key, value)
56+ }
3157 }
3258
3359 override fun stringAsync (key : String , value : String ) {
34- edit.putString(key, value).apply ()
60+ if (encType == Encryption .NONE ) {
61+ edit.putString(key, value).apply ()
62+ } else {
63+ cryptAsync(key, value)
64+ }
3565 }
3666
3767 override fun long (key : String , value : Long ): Boolean {
38- return edit.putLong(key, value).commit()
68+ return if (encType == Encryption .NONE ) {
69+ edit.putLong(key, value).commit()
70+ } else {
71+ crypt(key, value.toString())
72+ }
3973 }
4074
4175 override fun longAsync (key : String , value : Long ) {
42- edit.putLong(key, value).apply ()
76+ if (encType == Encryption .NONE ) {
77+ edit.putLong(key, value).apply ()
78+ } else {
79+ cryptAsync(key, value.toString())
80+ }
4381 }
4482
4583 override fun float (key : String , value : Float ): Boolean {
46- return edit.putFloat(key, value).commit()
84+ return if (encType == Encryption .NONE ) {
85+ edit.putFloat(key, value).commit()
86+ } else {
87+ crypt(key, value.toString())
88+ }
4789 }
4890
4991 override fun floatAsync (key : String , value : Float ) {
50- edit.putFloat(key, value).apply ()
92+ if (encType == Encryption .NONE ) {
93+ edit.putFloat(key, value).apply ()
94+ } else {
95+ cryptAsync(key, value.toString())
96+ }
5197 }
5298
5399 override fun double (key : String , value : Double ): Boolean {
54- return string(key, value.toString())
100+ return if (encType == Encryption .NONE ) {
101+ string(key, value.toString())
102+ } else {
103+ crypt(key, value.toString())
104+ }
55105 }
56106
57107 override fun doubleAsync (key : String , value : Double ) {
58- stringAsync(key, value.toString())
108+ if (encType == Encryption .NONE ) {
109+ stringAsync(key, value.toString())
110+ } else {
111+ cryptAsync(key, value.toString())
112+ }
59113 }
60114
61115 override fun boolean (key : String , value : Boolean ): Boolean {
62- return edit.putBoolean(key, value).commit()
116+ return if (encType == Encryption .NONE ) {
117+ edit.putBoolean(key, value).commit()
118+ } else {
119+ crypt(key, value.toString())
120+ }
63121 }
64122
65123 override fun booleanAsync (key : String , value : Boolean ) {
66- edit.putBoolean(key, value).apply ()
124+ if (encType == Encryption .NONE ) {
125+ edit.putBoolean(key, value).apply ()
126+ } else {
127+ cryptAsync(key, value.toString())
128+ }
67129 }
68130
69131 override fun stringSet (key : String , value : Set <String >): Boolean {
70- return edit.putStringSet(key, value).commit()
132+ return if (encType == Encryption .NONE ) {
133+ edit.putStringSet(key, value).commit()
134+ } else {
135+ crypt(key, JSONArray (value).toString())
136+ }
71137 }
72138
73139 override fun stringSetAsync (key : String , value : Set <String >) {
74- edit.putStringSet(key, value).apply ()
140+ if (encType == Encryption .NONE ) {
141+ edit.putStringSet(key, value).apply ()
142+ } else {
143+ cryptAsync(key, JSONArray (value).toString())
144+ }
75145 }
76- }
146+ }
0 commit comments