com.cleveradssolutions.gradleplugin.ManifestUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugin Show documentation
Show all versions of gradle-plugin Show documentation
CAS Gradle Plugin provides an easy way to integrate and configure CAS.AI Mediation in your android project.
package com.cleveradssolutions.gradleplugin
internal object ManifestUtils {
@Throws(Exception::class)
fun addMetaData(
manifest: String,
name: String,
value: String
): String {
var appEndPos = manifest.indexOf("")
if (appEndPos > -1) {
val insertionPos = manifest.getNodeLineStart(appEndPos)
return manifest.insertAt(insertionPos, createMetaData(name, value))
}
val appStartPos = manifest.indexOf(" -1) {
appEndPos = manifest.indexOf("/>", appStartPos)
if (appEndPos == -1) {
throw Exception("AndroidManifest.xml is invalid. tag have not closed.")
}
appEndPos = manifest.getNodeLineStart(appEndPos)
val appEnd = ">\n${createMetaData(name, value)} "
return manifest.replaceRange(appEndPos, appEndPos + 2, appEnd)
}
val manifestEnd = manifest.indexOf("")
if (manifestEnd < 0) {
throw Exception("AndroidManifest.xml is invalid. tag not found.")
}
var i = manifestEnd - 1
while (manifest[i].isWhitespace()) {
i--
}
val insertionData = "\n\n " + createMetaData(name, value) +
" \n"
return manifest.replaceRange(i + 1, manifestEnd, insertionData)
}
fun updateMetaData(
manifest: String,
value: String,
metaDataNamePos: Int
): String {
val metaDataEnd = manifest.indexOf('>', metaDataNamePos)
val valuePos = manifest.indexOf("android:value")
if (valuePos < metaDataEnd) {
val valueStart = manifest.indexOf('"', valuePos + 13) + 1
val valueEnd = manifest.indexOf('"', valueStart)
return manifest.replaceRange(valueStart, valueEnd, value)
} else {
val pos = if (manifest[metaDataEnd - 1] == '/') metaDataEnd - 1 else metaDataEnd
return manifest.insertAt(pos, "\n android:value=\"$value\" ")
}
}
@Throws(Exception::class)
fun addPermission(
manifest: String,
permission: String
): String {
var nextTagStartPos = manifest.indexOf("")
if (nextTagStartPos < 0) {
throw Exception("AndroidManifest.xml is invalid. tag not found.")
}
}
val data = " \n\n"
val insertionPos = manifest.getNodeLineStart(nextTagStartPos)
return manifest.insertAt(insertionPos, data)
}
fun removePermission(manifest: String, pos: Int): String {
var start = manifest.lastIndexOf('<', pos)
while (manifest[start - 1] == ' ') start--
val end = manifest.indexOf('>', pos)
return manifest.removeRange(pos..end)
}
private fun createMetaData(name: String, value: String): String {
return "\n \n"
}
fun String.insertAt(pos: Int, text: String): String {
return substring(0, pos) + text + substring(pos)
}
fun String.getNodeLineStart(startNodeIndex: Int): Int {
var i = startNodeIndex
var char: Char
while (i > 0) {
i--
char = this[i]
if (!char.isWhitespace() || char == '\n') {
return i + 1
}
}
return 0
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy