apply plugin: 'com.android.application'

boolean googleServicesEnabled \
    = project.file('google-services.json').exists() && !rootProject.ext.libreBuild

// Crashlytics integration is done as part of Firebase now, so it gets
// automagically activated with google-services.json
if (googleServicesEnabled) {
    apply plugin: 'io.fabric'
}

// Use the number of seconds/10 since Jan 1 2019 as the versionCode.
// This lets us upload a new build at most every 10 seconds for the
// next ~680 years.
// https://stackoverflow.com/a/38643838
def vcode = (int)(((new Date().getTime()/1000) - 1546297200) / 10)

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId 'org.jitsi.meet'
        versionCode vcode
        versionName project.appVersion

        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion

        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
    }

    buildTypes {
        debug {
            buildConfigField "boolean", "GOOGLE_SERVICES_ENABLED", "${googleServicesEnabled}"
            buildConfigField "boolean", "LIBRE_BUILD", "${rootProject.ext.libreBuild}"
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-release.pro'
            buildConfigField "boolean", "GOOGLE_SERVICES_ENABLED", "${googleServicesEnabled}"
            buildConfigField "boolean", "LIBRE_BUILD", "${rootProject.ext.libreBuild}"
        }
    }

    sourceSets {
        main {
            java {
                if (rootProject.ext.libreBuild) {
                    srcDir "src"
                    exclude "**/GoogleServicesHelper.java"
                }
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-5'

    if (!rootProject.ext.libreBuild) {
        implementation 'com.google.android.gms:play-services-auth:16.0.1'

        // Firebase
        //  - Crashlytics
        //  - Dynamic Links
        implementation 'com.google.firebase:firebase-core:16.0.6'
        implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
        implementation 'com.google.firebase:firebase-dynamic-links:16.1.5'
    }

    implementation project(':sdk')
}

gradle.projectsEvaluated {
    // Dropbox integration
    //

    def dropboxAppKey
    if (project.file('dropbox.key').exists()) {
        dropboxAppKey = project.file('dropbox.key').text.trim() - 'db-'
    }

    if (dropboxAppKey) {
        android.defaultConfig.resValue('string', 'dropbox_app_key', "${dropboxAppKey}")

        def dropboxActivity = """
          <activity
              android:configChanges="keyboard|orientation"
              android:launchMode="singleTask"
              android:name="com.dropbox.core.android.AuthActivity">
            <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <category android:name="android.intent.category.BROWSABLE" />
              <category android:name="android.intent.category.DEFAULT" />
              <data android:scheme="db-${dropboxAppKey}" />
            </intent-filter>
          </activity>"""

        android.applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.getProcessManifestProvider().get().doLast {
                    def outputDir = manifestOutputDirectory.get().asFile
                    def manifestPath = new File(outputDir, 'AndroidManifest.xml')
                    def charset = 'UTF-8'
                    def text
                    text = manifestPath.getText(charset)
                    text = text.replace('</application>', "${dropboxActivity}</application>")
                    manifestPath.write(text, charset)
                }
            }
        }
    }

    // Run React packager
    android.applicationVariants.all { variant ->
        def targetName = variant.name.capitalize()

        def currentRunPackagerTask = tasks.create(
                name: "run${targetName}ReactPackager",
                type: Exec) {
            group = "react"
            description = "Run the React packager."

            doFirst {
                println "Starting the React packager..."

                def androidRoot = file("${projectDir}/../")

                // Set up the call to the script
                workingDir androidRoot

                // Run the packager
                commandLine("scripts/run-packager.sh")
            }

            // Set up dev mode
            def devEnabled = !targetName.toLowerCase().contains("release")

            // Only enable for dev builds
            enabled devEnabled
        }

        def packageTask = variant.packageApplicationProvider.get()

        packageTask.dependsOn(currentRunPackagerTask)
    }

}

if (googleServicesEnabled) {
   apply plugin: 'com.google.gms.google-services'
}
