Skip to content

Instantly share code, notes, and snippets.

@RareScrap
Created August 6, 2020 17:54
Show Gist options
  • Save RareScrap/9158f9a1ece92dd1aace85021b62d333 to your computer and use it in GitHub Desktop.
Save RareScrap/9158f9a1ece92dd1aace85021b62d333 to your computer and use it in GitHub Desktop.
Дамперы AttributeSet'а и TypedArray'я
/**
* Выводит в лог удобочитаемое представление AttributeSet'а
*/
fun AttributeSet.dump(ctx: Context) {
Log.i("AttributeSetDump", "For element at $positionDescription")
for (i in 0 until attributeCount) {
val attrName = this.getAttributeName(i) ?: "null"
var attrValue = this.getAttributeValue(i) ?: "null"
if (attrValue.startsWith("@")) {
attrValue = ctx.resources.getResourceEntryName(attrValue.substring(1).toInt())
}
val attrNamespace = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
this.getAttributeNamespace(i) ?: "null"
} else {
""
}
Log.i("AttributeSetDump", "\t$attrNamespace:$attrName=$attrValue")
}
Log.i("AttributeSetDump", "\tstyle=$styleAttribute")
}
/**
* Выводит в лог удобочитаемое представление TypedArray'я
*/
fun TypedArray.dump(ctx: Context) {
Log.i("TypedArrayDump", this.positionDescription)
for(i in 0..indexCount) { // TODO: Почему length() и indexCount выдает разные числа?
val resId = this.getResourceId(i, -1)
var resName: String
var resIdReadable: String
try {
resName = ctx.resources.getResourceEntryName(resId)
} catch (e: Resources.NotFoundException) {
resName = "NOT_FOUND"
}
try {
resIdReadable = ctx.resources.getResourceName(resId)
} catch (e: Resources.NotFoundException) {
resIdReadable = "NOT_FOUND"
}
Log.i("TypedArrayDump", "\t$resName=$resIdReadable($resId)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment