Skip to content

Commit

Permalink
[kiosksetup] Generate provisioning QR code.
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonides committed Mar 9, 2024
1 parent 759f57e commit 2a12b90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
47 changes: 46 additions & 1 deletion kiosksetup/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import com.android.build.gradle.api.ApplicationVariant
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
import java.security.MessageDigest
import java.util.Base64

/*
* MIT License
*
Expand Down Expand Up @@ -75,6 +80,8 @@ android {
kotlin.sourceSets {
getByName(name) { kotlin.srcDir("build/generated/ksp/${name}/kotlin") }
}

registerProvisioningInfoTask()
}
}

Expand All @@ -97,4 +104,42 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)

testImplementation(libs.junit)
}
}

fun ApplicationVariant.registerProvisioningInfoTask() {
val variantName = name
val capitalizedName = name.capitalizeAsciiOnly()
if (outputs.size != 1) {
throw IllegalArgumentException("Only single APK output is supported")
}
val output = outputs.first()
tasks.register<Copy>("generateProvisioningData${capitalizedName}") {
description = "create data for provisioning QR code"
group = "Build"
dependsOn += packageApplicationProvider

from(layout.projectDirectory.file("src/main/provisioning-qrcode.txt"))
into(layout.buildDirectory.file("outputs/qrcode/$variantName/"))

doFirst {
val expandValues = mapOf(
"version" to android.defaultConfig.versionName,
"apkChecksum" to computeApkChecksum(output.outputFile)
)
expand(expandValues)
}
}

tasks.named("assemble${capitalizedName}").get()
.dependsOn += "generateProvisioningData${capitalizedName}"
}

fun computeApkChecksum(file: File): String {
val apkDigest = MessageDigest.getInstance("SHA-256")
file.forEachBlock { buffer, bytes ->
apkDigest.update(buffer, 0, bytes)
}
val digestBytes = apkDigest.digest()
return Base64.getUrlEncoder().withoutPadding()
.encodeToString(digestBytes)
}
6 changes: 6 additions & 0 deletions kiosksetup/src/main/provisioning-qrcode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME":"com.studio4plus.homerplayer2.kiosk/com.studio4plus.homerplayer2.kiosk.deviceadmin.DeviceAdmin",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION":"https://github.com/msimonides/homerplayer/releases/download/${version}/Homer.Player2.Kiosk.${version}.apk",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM":"${apkChecksum}",
"android.app.extra.PROVISIONING_SKIP_ENCRYPTION":true
}

0 comments on commit 2a12b90

Please sign in to comment.