Skip to content

Commit

Permalink
chore(Android): add error logs, improve array parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tompsota committed Nov 5, 2024
1 parent 5f45b08 commit 38342e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/android/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

private inline fun <reified T> JSONArray.toPrimitiveArray(): Array<T> {
val output = mutableListOf<T>()

internal fun JSONArray.toArray(): Array<String> {
val output = mutableListOf<String>()
for (i in 0 until this.length()) {
this.getString(i)?.let(output::add)
val element: T = when (T::class) {
String::class -> this.getString(i) as T
Int::class -> this.getInt(i) as T
Double::class -> this.getDouble(i) as T
Long::class -> this.getLong(i) as T
Boolean::class -> this.getBoolean(i) as T
else -> throw JSONException("Cannot parse JSON array - unsupported type")
}
output.add(element)
}
return output.toTypedArray()
}

internal fun JSONObject.getArraySafe(key: String): Array<String> {
if (this.has(key)) {
val inputArray = this.getJSONArray(key)
return inputArray.toArray()
return inputArray.toPrimitiveArray()
}
return arrayOf()
}
Expand All @@ -34,7 +43,7 @@ internal fun JSONObject.getNestedArraySafe(key: String): Array<Array<String>> {
if (this.has(key)) {
val inputArray = this.getJSONArray(key)
for (i in 0 until inputArray.length()) {
outArray.add(inputArray.getJSONArray(i).toArray())
outArray.add(inputArray.getJSONArray(i).toPrimitiveArray())
}
}
return outArray.toTypedArray()
Expand Down
3 changes: 3 additions & 0 deletions src/android/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.util.Base64
import android.util.Log
import java.io.ByteArrayOutputStream

internal object Utils {
Expand Down Expand Up @@ -53,6 +54,7 @@ internal object Utils {
}
return null
} catch (e: Exception) {
Log.e("Talsec", "Could not retrieve app icon for ${packageName}: ${e.message}")
return null
}
}
Expand All @@ -72,6 +74,7 @@ internal object Utils {
context.packageManager.getInstallerPackageName(packageName)
}
} catch (e: Exception) {
Log.e("Talsec", "Could retrieve app installation source for ${packageName}: ${e.message}")
null
}
}
Expand Down

0 comments on commit 38342e7

Please sign in to comment.