Skip to content

Commit c7b471f

Browse files
committed
feat(Android): add error logs, improve array parsing
1 parent fde67fb commit c7b471f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

android/src/main/java/com/freeraspreactnative/utils/Extensions.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.freeraspreactnative.models.RNPackageInfo
1414
import com.freeraspreactnative.models.RNSuspiciousAppInfo
1515
import kotlinx.serialization.encodeToString
1616
import kotlinx.serialization.json.Json
17+
import org.json.JSONException
1718

1819

1920
internal fun ReadableMap.getMapThrowing(key: String): ReadableMap {
@@ -31,22 +32,26 @@ internal fun ReadableMap.getBooleanSafe(key: String, defaultValue: Boolean = tru
3132
return defaultValue
3233
}
3334

34-
internal fun ReadableArray.toArray(): Array<String> {
35-
val output = mutableListOf<String>()
35+
private inline fun <reified T> ReadableArray.toPrimitiveArray(): Array<T> {
36+
val output = mutableListOf<T>()
37+
3638
for (i in 0 until this.size()) {
37-
// in RN versions < 0.63, getString is nullable
38-
@Suppress("UNNECESSARY_SAFE_CALL")
39-
this.getString(i)?.let {
40-
output.add(it)
39+
val element: T? = when (T::class) {
40+
String::class -> this.getString(i) as T?
41+
Int::class -> this.getInt(i) as T?
42+
Double::class -> this.getDouble(i) as T?
43+
Boolean::class -> this.getBoolean(i) as T?
44+
else -> throw JSONException("Cannot parse JSON array - unsupported type")
4145
}
46+
element?.let(output::add)
4247
}
4348
return output.toTypedArray()
4449
}
4550

4651
internal fun ReadableMap.getArraySafe(key: String): Array<String> {
4752
if (this.hasKey(key)) {
4853
val inputArray = this.getArray(key)!!
49-
return inputArray.toArray()
54+
return inputArray.toPrimitiveArray()
5055
}
5156
return arrayOf()
5257
}
@@ -56,7 +61,7 @@ internal fun ReadableMap.getNestedArraySafe(key: String): Array<Array<String>> {
5661
if (this.hasKey(key)) {
5762
val inputArray = this.getArray(key)!!
5863
for (i in 0 until inputArray.size()) {
59-
outArray.add(inputArray.getArray(i).toArray())
64+
outArray.add(inputArray.getArray(i).toPrimitiveArray())
6065
}
6166
}
6267
return outArray.toTypedArray()

android/src/main/java/com/freeraspreactnative/utils/Utils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.graphics.Canvas
66
import android.graphics.drawable.BitmapDrawable
77
import android.os.Build
88
import android.util.Base64
9+
import android.util.Log
910
import com.facebook.react.bridge.ReactContext
1011
import java.io.ByteArrayOutputStream
1112

@@ -54,6 +55,7 @@ internal object Utils {
5455
}
5556
return null
5657
} catch (e: Exception) {
58+
Log.e("Talsec", "Could not retrieve app icon for ${packageName}: ${e.message}")
5759
return null
5860
}
5961
}
@@ -73,6 +75,7 @@ internal object Utils {
7375
context.packageManager.getInstallerPackageName(packageName)
7476
}
7577
} catch (e: Exception) {
78+
Log.e("Talsec", "Could retrieve app installation source for ${packageName}: ${e.message}")
7679
null
7780
}
7881
}

0 commit comments

Comments
 (0)