1
1
package com.stringcare.library
2
2
3
3
import android.content.Context
4
-
5
4
import android.support.annotation.StringRes
6
5
import android.util.Log
7
- import java.lang.Exception
8
-
9
- import java.util.ArrayList
10
6
11
7
/* *
12
8
* Created by efrainespada on 02/10/2016.
@@ -20,36 +16,45 @@ class SC {
20
16
System .loadLibrary(" native-lib" )
21
17
}
22
18
23
- private val context: Context
19
+ val context: Context
24
20
get() = when (contextFun) {
25
- null -> throw Exception (" Context not defined yet." )
26
- else -> contextFun!! .invoke ()
21
+ null -> throw StringcareException (" Context not defined yet." )
22
+ else -> contextFun!! ()
27
23
}
28
24
29
25
private var contextFun: (() -> Context )? = null
30
26
31
- private val listeners = ArrayList <ContextListener >()
27
+ private val listeners = mutableListOf <ContextListener >()
32
28
29
+ /* *
30
+ * Context getter. Common implementation
31
+ */
33
32
@JvmStatic
34
33
fun init (c : Context ) {
35
34
contextFun = { c }
36
- if (listeners.isNotEmpty()) {
37
- for (listener in listeners) {
38
- listener.contextReady()
39
- }
40
- }
35
+ processPendingContextListener()
41
36
}
42
37
38
+ /* *
39
+ * Context getter. Lambda implementation
40
+ */
43
41
@JvmStatic
44
- fun init (lambda : () -> Context ) {
45
- contextFun = lambda
46
- if (listeners.isNotEmpty()) {
47
- for (listener in listeners) {
48
- listener.contextReady()
49
- }
50
- }
42
+ fun init (context : () -> Context ) {
43
+ contextFun = context
44
+ processPendingContextListener()
51
45
}
52
46
47
+ /* *
48
+ * Process pending context listeners
49
+ */
50
+ private fun processPendingContextListener () {
51
+ if (listeners.isNotEmpty())
52
+ listeners.forEach { it.contextReady() }
53
+ }
54
+
55
+ /* *
56
+ * Holds all context listeners.
57
+ */
53
58
@JvmStatic
54
59
fun onContextReady (listener : ContextListener ) {
55
60
if (contextFun != null ) {
@@ -59,6 +64,11 @@ class SC {
59
64
listeners.add(listener)
60
65
}
61
66
67
+ /* *
68
+ * Obfuscates the string value
69
+ * @param value
70
+ * @return String
71
+ */
62
72
@JvmStatic
63
73
fun obfuscate (value : String ): String {
64
74
return obfuscate(value, defaultAndroidTreatment, defaultVersion)
@@ -67,10 +77,16 @@ class SC {
67
77
/* *
68
78
* Obfuscates the given value
69
79
* @param value
80
+ * @param androidTreatment
81
+ * @param version
70
82
* @return String
71
83
*/
72
84
@JvmStatic
73
- fun obfuscate (value : String , androidTreatment : Boolean = defaultAndroidTreatment, version : Version = defaultVersion): String {
85
+ fun obfuscate (
86
+ value : String ,
87
+ androidTreatment : Boolean = defaultAndroidTreatment,
88
+ version : Version = defaultVersion
89
+ ): String {
74
90
return if (contextFun == null ) {
75
91
Log .e(tag, initializationNeeded)
76
92
value
@@ -82,24 +98,34 @@ class SC {
82
98
}
83
99
}
84
100
101
+ /* *
102
+ * Reveals the Int (@StringRes) value
103
+ * @param id
104
+ * @return String
105
+ */
85
106
@JvmStatic
86
107
fun reveal (@StringRes id : Int ): String {
87
108
return reveal(id, defaultVersion)
88
109
}
89
110
90
111
/* *
91
- * Deobfuscates the given value
112
+ * Reveals the Int (@StringRes) value
92
113
* @param id
114
+ * @param androidTreatment
93
115
* @return String
94
116
*/
95
117
@JvmStatic
96
- fun reveal (@StringRes id : Int , androidTreatment : Boolean = defaultAndroidTreatment): String {
118
+ fun reveal (
119
+ @StringRes id : Int ,
120
+ androidTreatment : Boolean = defaultAndroidTreatment
121
+ ): String {
97
122
return reveal(id, androidTreatment, defaultVersion)
98
123
}
99
124
100
125
/* *
101
- * Deobfuscates the given value
126
+ * Reveals the Int (@StringRes) value
102
127
* @param id
128
+ * @param version
103
129
* @return String
104
130
*/
105
131
@JvmStatic
@@ -108,12 +134,18 @@ class SC {
108
134
}
109
135
110
136
/* *
111
- * Deobfuscates the given value
137
+ * Reveals the Int (@StringRes) value
112
138
* @param id
139
+ * @param androidTreatment
140
+ * @param version
113
141
* @return String
114
142
*/
115
143
@JvmStatic
116
- fun reveal (@StringRes id : Int , androidTreatment : Boolean = defaultAndroidTreatment, version : Version = defaultVersion): String {
144
+ fun reveal (
145
+ @StringRes id : Int ,
146
+ androidTreatment : Boolean = defaultAndroidTreatment,
147
+ version : Version = defaultVersion
148
+ ): String {
117
149
return if (contextFun == null ) {
118
150
Log .e(tag, initializationNeeded)
119
151
" "
@@ -125,28 +157,51 @@ class SC {
125
157
}
126
158
}
127
159
160
+ /* *
161
+ * Reveals the String value
162
+ * @param value
163
+ * @return String
164
+ */
128
165
@JvmStatic
129
166
fun reveal (value : String ): String {
130
167
return reveal(value, defaultAndroidTreatment, defaultVersion)
131
168
}
132
169
170
+ /* *
171
+ * Reveals the String value
172
+ * @param value
173
+ * @param version
174
+ * @return String
175
+ */
133
176
@JvmStatic
134
177
fun reveal (value : String , version : Version = defaultVersion): String {
135
178
return reveal(value, defaultAndroidTreatment, version)
136
179
}
137
180
181
+ /* *
182
+ * Reveals the String value
183
+ * @param value
184
+ * @param androidTreatment
185
+ * @return String
186
+ */
138
187
@JvmStatic
139
188
fun reveal (value : String , androidTreatment : Boolean ): String {
140
189
return reveal(value, androidTreatment, defaultVersion)
141
190
}
142
191
143
192
/* *
144
- * Deobfuscates the given value
193
+ * Reveals the String value
145
194
* @param value
195
+ * @param androidTreatment
196
+ * @param version
146
197
* @return String
147
198
*/
148
199
@JvmStatic
149
- fun reveal (value : String , androidTreatment : Boolean = defaultAndroidTreatment, version : Version = defaultVersion): String {
200
+ fun reveal (
201
+ value : String ,
202
+ androidTreatment : Boolean = defaultAndroidTreatment,
203
+ version : Version = defaultVersion
204
+ ): String {
150
205
return if (contextFun == null ) {
151
206
Log .e(tag, initializationNeeded)
152
207
value
@@ -158,37 +213,80 @@ class SC {
158
213
}
159
214
}
160
215
216
+ /* *
217
+ * Reveals the Int (@StringRes) value with vararg
218
+ * @param id
219
+ * @param formatArgs
220
+ * @return String
221
+ */
161
222
@JvmStatic
162
223
fun reveal (@StringRes id : Int , vararg formatArgs : Any ): String {
163
224
return reveal(id, defaultAndroidTreatment, defaultVersion, formatArgs)
164
225
}
165
226
227
+ /* *
228
+ * Reveals the Int (@StringRes) value with vararg
229
+ * @param id
230
+ * @param version
231
+ * @param formatArgs
232
+ * @return String
233
+ */
166
234
@JvmStatic
167
- fun reveal (@StringRes id : Int , version : Version = defaultVersion, vararg formatArgs : Any ): String {
235
+ fun reveal (
236
+ @StringRes id : Int ,
237
+ version : Version = defaultVersion,
238
+ vararg formatArgs : Any
239
+ ): String {
168
240
return reveal(id, defaultAndroidTreatment, version, formatArgs)
169
241
}
170
242
243
+ /* *
244
+ * Reveals the Int (@StringRes) value with vararg
245
+ * @param id
246
+ * @param androidTreatment
247
+ * @param formatArgs
248
+ * @return String
249
+ */
171
250
@JvmStatic
172
- fun reveal (@StringRes id : Int , androidTreatment : Boolean , vararg formatArgs : Any ): String {
251
+ fun reveal (
252
+ @StringRes id : Int ,
253
+ androidTreatment : Boolean = defaultAndroidTreatment,
254
+ vararg formatArgs : Any
255
+ ): String {
173
256
return reveal(id, androidTreatment, defaultVersion, formatArgs)
174
257
}
175
258
176
259
/* *
177
- * Deobfuscates the given value
260
+ * Reveals the Int (@StringRes) value with vararg
178
261
* @param id
262
+ * @param androidTreatment
263
+ * @param version
179
264
* @param formatArgs
180
- * @return
265
+ * @return String
181
266
*/
182
267
@JvmStatic
183
- fun reveal (@StringRes id : Int , androidTreatment : Boolean , version : Version , vararg formatArgs : Any ): String {
184
- return if (contextFun == null ) {
185
- Log .e(tag, initializationNeeded)
186
- " "
187
- } else return when (version) {
188
- Version .V0 -> JavaLogic .getString(context, id, formatArgs[0 ] as Array <out Any >)
189
- Version .V1 -> CPlusLogic .revealV1(context, id, formatArgs[0 ] as Array <out Any >)
190
- Version .V2 -> CPlusLogic .revealV2(context, id, formatArgs[0 ] as Array <out Any >)
191
- Version .V3 -> CPlusLogic .revealV3(context, id, androidTreatment, formatArgs[0 ] as Array <out Any >)
268
+ fun reveal (
269
+ @StringRes id : Int ,
270
+ androidTreatment : Boolean = defaultAndroidTreatment,
271
+ version : Version = defaultVersion,
272
+ vararg formatArgs : Any
273
+ ): String {
274
+ return when (contextFun) {
275
+ null -> {
276
+ Log .e(tag, initializationNeeded)
277
+ " "
278
+ }
279
+ else -> return when (version) {
280
+ Version .V0 -> JavaLogic .getString(context, id, formatArgs[0 ] as Array <out Any >)
281
+ Version .V1 -> CPlusLogic .revealV1(context, id, formatArgs[0 ] as Array <out Any >)
282
+ Version .V2 -> CPlusLogic .revealV2(context, id, formatArgs[0 ] as Array <out Any >)
283
+ Version .V3 -> CPlusLogic .revealV3(
284
+ context,
285
+ id,
286
+ androidTreatment,
287
+ formatArgs[0 ] as Array <out Any >
288
+ )
289
+ }
192
290
}
193
291
}
194
292
0 commit comments