plugins { id 'com.android.application' }

android {
    namespace 'io.github.switchframetap.tv'
    compileSdk 34
    defaultConfig {
        applicationId 'io.github.switchframetap.tv'
        minSdk 23
        targetSdk 34
        // Since 0.7.14 the app carries Switch Frame Tap's version; release builds
        // pass it (.github/workflows/viewer-android.yml): -PsftVersion=0.7.14 -PsftVersionCode=714009
        versionCode(project.hasProperty('sftVersionCode') ? project.property('sftVersionCode').toInteger() : 714000)
        versionName(project.hasProperty('sftVersion') ? project.property('sftVersion') : '0.7.19-dev')
    }
    // The release key comes from the environment (CI secrets); without it the
    // release build is signed with the debug key so it still installs.
    signingConfigs {
        release {
            def ks = System.getenv('SFT_ANDROID_KEYSTORE')
            if (ks) {
                storeFile file(ks)
                storePassword System.getenv('SFT_ANDROID_KEYSTORE_PASSWORD')
                keyAlias System.getenv('SFT_ANDROID_KEY_ALIAS')
                keyPassword System.getenv('SFT_ANDROID_KEY_PASSWORD')
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            signingConfig System.getenv('SFT_ANDROID_KEYSTORE') ? signingConfigs.release : signingConfigs.debug
        }
    }
    lint { abortOnError true }
}

// Pure JVM tests: no Android device or third-party test dependency needed.
tasks.register('protocolTest', JavaExec) {
    dependsOn 'compileProtocolTest'
    classpath = files(layout.buildDirectory.dir('protocol-test'))
    mainClass = 'io.github.switchframetap.tv.ProtocolTest'
    args rootProject.projectDir.parentFile.absolutePath
}
tasks.register('compileProtocolTest', JavaCompile) {
    source = files('src/main/java/io/github/switchframetap/tv/SftProtocol.java',
                   'src/main/java/io/github/switchframetap/tv/VideoQueue.java',
                   'src/test/java/io/github/switchframetap/tv/ProtocolTest.java')
    classpath = files()
    destinationDirectory = layout.buildDirectory.dir('protocol-test')
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

// Exercise the real playback loops against delayed/stalled driver doubles.
// These are host regressions, not a claim of testing an Android device.
tasks.register('compilePlaybackTest', JavaCompile) {
    source = fileTree('src/hostTest/java') { include '**/*.java' }
    source files('src/main/java/io/github/switchframetap/tv/SftProtocol.java',
                 'src/main/java/io/github/switchframetap/tv/VideoQueue.java',
                 'src/main/java/io/github/switchframetap/tv/VideoDecoder.java',
                 'src/main/java/io/github/switchframetap/tv/AudioPump.java',
                 'src/main/java/io/github/switchframetap/tv/StreamStats.java')
    classpath = files()
    destinationDirectory = layout.buildDirectory.dir('playback-test')
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}
tasks.register('playbackTest', JavaExec) {
    dependsOn 'compilePlaybackTest'
    classpath = files(layout.buildDirectory.dir('playback-test'))
    mainClass = 'io.github.switchframetap.tv.PlaybackTest'
    args rootProject.projectDir.parentFile.absolutePath
}
