11package com.stringcare.library
22
33import android.content.Context
4-
54import android.support.annotation.StringRes
65import android.util.Log
7- import java.lang.Exception
8-
9- import java.util.ArrayList
106
117/* *
128 * Created by efrainespada on 02/10/2016.
@@ -20,36 +16,45 @@ class SC {
2016 System .loadLibrary(" native-lib" )
2117 }
2218
23- private val context: Context
19+ val context: Context
2420 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!! ()
2723 }
2824
2925 private var contextFun: (() -> Context )? = null
3026
31- private val listeners = ArrayList <ContextListener >()
27+ private val listeners = mutableListOf <ContextListener >()
3228
29+ /* *
30+ * Context getter. Common implementation
31+ */
3332 @JvmStatic
3433 fun init (c : Context ) {
3534 contextFun = { c }
36- if (listeners.isNotEmpty()) {
37- for (listener in listeners) {
38- listener.contextReady()
39- }
40- }
35+ processPendingContextListener()
4136 }
4237
38+ /* *
39+ * Context getter. Lambda implementation
40+ */
4341 @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()
5145 }
5246
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+ */
5358 @JvmStatic
5459 fun onContextReady (listener : ContextListener ) {
5560 if (contextFun != null ) {
@@ -59,6 +64,11 @@ class SC {
5964 listeners.add(listener)
6065 }
6166
67+ /* *
68+ * Obfuscates the string value
69+ * @param value
70+ * @return String
71+ */
6272 @JvmStatic
6373 fun obfuscate (value : String ): String {
6474 return obfuscate(value, defaultAndroidTreatment, defaultVersion)
@@ -67,10 +77,16 @@ class SC {
6777 /* *
6878 * Obfuscates the given value
6979 * @param value
80+ * @param androidTreatment
81+ * @param version
7082 * @return String
7183 */
7284 @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 {
7490 return if (contextFun == null ) {
7591 Log .e(tag, initializationNeeded)
7692 value
@@ -82,24 +98,34 @@ class SC {
8298 }
8399 }
84100
101+ /* *
102+ * Reveals the Int (@StringRes) value
103+ * @param id
104+ * @return String
105+ */
85106 @JvmStatic
86107 fun reveal (@StringRes id : Int ): String {
87108 return reveal(id, defaultVersion)
88109 }
89110
90111 /* *
91- * Deobfuscates the given value
112+ * Reveals the Int (@StringRes) value
92113 * @param id
114+ * @param androidTreatment
93115 * @return String
94116 */
95117 @JvmStatic
96- fun reveal (@StringRes id : Int , androidTreatment : Boolean = defaultAndroidTreatment): String {
118+ fun reveal (
119+ @StringRes id : Int ,
120+ androidTreatment : Boolean = defaultAndroidTreatment
121+ ): String {
97122 return reveal(id, androidTreatment, defaultVersion)
98123 }
99124
100125 /* *
101- * Deobfuscates the given value
126+ * Reveals the Int (@StringRes) value
102127 * @param id
128+ * @param version
103129 * @return String
104130 */
105131 @JvmStatic
@@ -108,12 +134,18 @@ class SC {
108134 }
109135
110136 /* *
111- * Deobfuscates the given value
137+ * Reveals the Int (@StringRes) value
112138 * @param id
139+ * @param androidTreatment
140+ * @param version
113141 * @return String
114142 */
115143 @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 {
117149 return if (contextFun == null ) {
118150 Log .e(tag, initializationNeeded)
119151 " "
@@ -125,28 +157,51 @@ class SC {
125157 }
126158 }
127159
160+ /* *
161+ * Reveals the String value
162+ * @param value
163+ * @return String
164+ */
128165 @JvmStatic
129166 fun reveal (value : String ): String {
130167 return reveal(value, defaultAndroidTreatment, defaultVersion)
131168 }
132169
170+ /* *
171+ * Reveals the String value
172+ * @param value
173+ * @param version
174+ * @return String
175+ */
133176 @JvmStatic
134177 fun reveal (value : String , version : Version = defaultVersion): String {
135178 return reveal(value, defaultAndroidTreatment, version)
136179 }
137180
181+ /* *
182+ * Reveals the String value
183+ * @param value
184+ * @param androidTreatment
185+ * @return String
186+ */
138187 @JvmStatic
139188 fun reveal (value : String , androidTreatment : Boolean ): String {
140189 return reveal(value, androidTreatment, defaultVersion)
141190 }
142191
143192 /* *
144- * Deobfuscates the given value
193+ * Reveals the String value
145194 * @param value
195+ * @param androidTreatment
196+ * @param version
146197 * @return String
147198 */
148199 @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 {
150205 return if (contextFun == null ) {
151206 Log .e(tag, initializationNeeded)
152207 value
@@ -158,37 +213,80 @@ class SC {
158213 }
159214 }
160215
216+ /* *
217+ * Reveals the Int (@StringRes) value with vararg
218+ * @param id
219+ * @param formatArgs
220+ * @return String
221+ */
161222 @JvmStatic
162223 fun reveal (@StringRes id : Int , vararg formatArgs : Any ): String {
163224 return reveal(id, defaultAndroidTreatment, defaultVersion, formatArgs)
164225 }
165226
227+ /* *
228+ * Reveals the Int (@StringRes) value with vararg
229+ * @param id
230+ * @param version
231+ * @param formatArgs
232+ * @return String
233+ */
166234 @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 {
168240 return reveal(id, defaultAndroidTreatment, version, formatArgs)
169241 }
170242
243+ /* *
244+ * Reveals the Int (@StringRes) value with vararg
245+ * @param id
246+ * @param androidTreatment
247+ * @param formatArgs
248+ * @return String
249+ */
171250 @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 {
173256 return reveal(id, androidTreatment, defaultVersion, formatArgs)
174257 }
175258
176259 /* *
177- * Deobfuscates the given value
260+ * Reveals the Int (@StringRes) value with vararg
178261 * @param id
262+ * @param androidTreatment
263+ * @param version
179264 * @param formatArgs
180- * @return
265+ * @return String
181266 */
182267 @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+ }
192290 }
193291 }
194292
0 commit comments