com.cleveradssolutions.gradleplugin.CASSettings.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.
The newest version!
package com.cleveradssolutions.gradleplugin
import org.gradle.api.logging.Logger
import com.cleveradssolutions.gradleplugin.BuildConfig.TAG
import groovy.json.JsonSlurper
import org.gradle.api.Project
import java.io.File
import java.net.URL
internal object CASSettings {
class Data(val adMobAppId: String?)
private const val BASE_URL: String = "https://psvpromo.psvgamestudio.com/cas-settings.php"
private const val SETTINGS_FILE_DIR: String = "src/main/res/raw"
fun findPathToLinkFile(project: Project, casId: String, isAppExtension: Boolean): File {
val relativePath =
"${SETTINGS_FILE_DIR}/cas_settings${casId.length}${casId.last().lowercaseChar()}.json"
// Caused wrong gradle task order processing with task processDebugResources
// if (!isAppExtension) {
// project.rootProject.allprojects.forEach {
// if (it != project) {
// val appExtension = it.extensions.findByType(AppExtension::class.java)
// if (appExtension?.defaultConfig?.applicationId == casId) {
// return it.file(relativePath)
// }
// }
// }
// project.logger.error(
// "$TAG: No Application with ID `{}` was found in any active project.",
// casId
// )
// }
return project.file(relativePath)
}
fun getCASSettings(casSettings: File): Data? {
if (casSettings.exists()) {
val settingsText = casSettings.readText()
if (settingsText.isBlank()) {
casSettings.delete()
} else {
val slurper = JsonSlurper()
val settings = slurper.parseText(settingsText) as Map<*, *>
return Data(settings["admob_app_id"] as String?)
}
}
return null
}
fun tryRefreshCASSettings(logger: Logger, casId: String, casSettings: File): Boolean {
val casSettingNotFound = !casSettings.exists()
if (casSettingNotFound || isSettingsFileExpired(casSettings)) {
val url = getSettingsFileUrl(casId)
val content = try {
url.readBytes()
} catch (e: Exception) {
logger.error("$TAG: Server connect failed", e)
return false
}
if (content.isEmpty()) {
logger.error(
"""
$TAG app id '$casId' is not registered in CAS.AI.
If you haven't created an CAS account and registered an app yet, just ignore this message.
""".trimIndent()
)
return false
}
if (casSettingNotFound) {
val dir = casSettings.parentFile
if (!dir.exists() && !dir.mkdirs()) {
logger.error(
"$TAG: Failed create directory for App link file: {}",
dir.path
)
return false
}
}
try {
casSettings.writeBytes(content)
} catch (e: Exception) {
logger.error("$TAG: App link file write failed", e)
return false
}
logger.info(
"$TAG: App link file in resources {}: {}",
if (casSettingNotFound) "added" else "updated",
casSettings
)
} else {
logger.info(
"$TAG: App link file UP-TO-DATE: {}",
casSettings
)
}
return true
}
private fun getSettingsFileUrl(appId: String): URL {
val url = "$BASE_URL?platform=0&apply=config&bundle=$appId"
return URL(url)
}
fun isSettingsFileExpired(casSettings: File): Boolean {
return casSettings.lastModified() < System.currentTimeMillis() - 6 * 60 * 60 * 1000
}
}