apply plugin: 'com.android.application'
apply plugin: 'com.bugsnag.android.gradle'

def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()

keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "eu.faircode.email"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1172
        versionName "1.1172"
        archivesBaseName = "FairEmail-v$versionName"

        // https://en.wikipedia.org/wiki/List_of_dinosaur_genera
        buildConfigField "String", "RELEASE_NAME", "\"Fruitadens\""

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        // https://developer.android.com/guide/topics/graphics/vector-drawable-resources
        vectorDrawables.useSupportLibrary = true

        //ndkVersion "20.0.5594570"
        ndkVersion "21.0.6113669"
        ndk {
            // Bugsnag
            abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }

    lintOptions {
        disable 'MissingTranslation'
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/README.md'
        exclude 'META-INF/CHANGES'
        exclude 'META-INF/jersey-module-version'
        exclude 'META-INF/COPYRIGHT.html'
        exclude 'META-INF/LICENSE.gpl.txt'
        exclude 'META-INF/LICENSE.commercial.txt'
    }

    signingConfigs {
        play {
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
        }
        github {
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            v1SigningEnabled true
            v2SigningEnabled false
            v2SigningEnabled false
        }
    }

    buildTypes {
        play {
            debuggable = false
            minifyEnabled = true
            useProguard = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.play
        }
        github {
            debuggable = false
            minifyEnabled = true
            useProguard = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.github
        }
        debug {
            applicationIdSuffix '.debug'
            debuggable = true
            minifyEnabled = false
            useProguard = false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "all"

    productFlavors {
        full {
            dimension "all"
            buildConfigField "boolean", "BETA_RELEASE", "true"
            buildConfigField "boolean", "PLAY_STORE_RELEASE", "false"
            buildConfigField "String", "INVITE_URI", "\"https://email.faircode.eu/\""
            buildConfigField "String", "PRO_FEATURES_URI", "\"https://email.faircode.eu/donate/\""
            buildConfigField "String", "CHANGELOG", "\"https://github.com/M66B/FairEmail/releases/\""
            buildConfigField "String", "GITHUB_LATEST_API", "\"https://api.github.com/repos/M66B/open-source-email/releases/latest\""
            buildConfigField "String", "OPENKEYCHAIN_URI", "\"https://f-droid.org/en/packages/org.sufficientlysecure.keychain/\""
            buildConfigField "String", "CAMERA_URI", "\"https://f-droid.org/en/packages/net.sourceforge.opencamera/\""
            buildConfigField "String", "RECORDER_URI", "\"https://f-droid.org/packages/com.github.axet.audiorecorder/\""
            buildConfigField "String", "APPS_URI", "\"https://github.com/M66B?tab=repositories/\""
        }
        play_beta {
            dimension "all"
            //minSdkVersion 23
            buildConfigField "boolean", "BETA_RELEASE", "true"
            buildConfigField "boolean", "PLAY_STORE_RELEASE", "true"
            buildConfigField "String", "INVITE_URI", "\"https://play.google.com/store/apps/details?id=eu.faircode.email\""
            buildConfigField "String", "PRO_FEATURES_URI", "\"https://email.faircode.eu/#pro\""
            buildConfigField "String", "CHANGELOG", "\"\""
            buildConfigField "String", "GITHUB_LATEST_API", "\"\""
            buildConfigField "String", "OPENKEYCHAIN_URI", "\"https://play.google.com/store/apps/details?id=org.sufficientlysecure.keychain\""
            buildConfigField "String", "CAMERA_URI", "\"https://play.google.com/store/apps/details?id=net.sourceforge.opencamera\""
            buildConfigField "String", "RECORDER_URI", "\"https://play.google.com/store/apps/details?id=com.github.axet.audiorecorder\""
            buildConfigField "String", "APPS_URI", "\"https://play.google.com/store/apps/dev?id=8420080860664580239\""
        }
        play_release {
            dimension "all"
            //minSdkVersion 23
            buildConfigField "boolean", "BETA_RELEASE", "false"
            buildConfigField "boolean", "PLAY_STORE_RELEASE", "true"
            buildConfigField "String", "INVITE_URI", "\"https://play.google.com/store/apps/details?id=eu.faircode.email\""
            buildConfigField "String", "PRO_FEATURES_URI", "\"https://email.faircode.eu/#pro\""
            buildConfigField "String", "CHANGELOG", "\"\""
            buildConfigField "String", "GITHUB_LATEST_API", "\"\""
            buildConfigField "String", "OPENKEYCHAIN_URI", "\"https://play.google.com/store/apps/details?id=org.sufficientlysecure.keychain\""
            buildConfigField "String", "CAMERA_URI", "\"https://play.google.com/store/apps/details?id=net.sourceforge.opencamera\""
            buildConfigField "String", "RECORDER_URI", "\"https://play.google.com/store/apps/details?id=com.github.axet.audiorecorder\""
            buildConfigField "String", "APPS_URI", "\"https://play.google.com/store/apps/dev?id=8420080860664580239\""
        }
    }

    variantFilter { variant ->
        def flavors = variant.flavors*.name
        // Builds: play, github, debug
        // Flavors: full, play_beta, play_release
        if (variant.buildType.name == "play" && flavors.contains("full")) {
            setIgnore(true)
        }
        if (variant.buildType.name == "github" && flavors.contains("play_beta")) {
            setIgnore(true)
        }
        if (flavors.contains("play_release")) {
            setIgnore(true)
        }
    }

    bugsnag {
        // https://docs.bugsnag.com/build-integrations/gradle/
        apiKey "9d2d57476a0614974449a3ec33f2604a"
        builderName "M66B"
        autoUpload false
        autoReportBuilds false
        sourceControl {
            repository "https://github.com/M66B/FairEmail/"
        }
    }
}

task copyMarkdown(type: Copy) {
    from "${rootDir}"
    into "src/main/assets"
    include "SETUP.md"
    include "PRIVACY.md"
    include "ATTRIBUTION.md"
}
preBuild.dependsOn copyMarkdown

task copySetup(type: Copy) {
    from "src/main/res"
    include '**/SETUP.md'
    into "src/main/assets"
    eachFile { file ->
        file.relativePath = new RelativePath(true, "SETUP-${file.file.parentFile.name}.md")
    }
    includeEmptyDirs = false
}
preBuild.dependsOn copySetup

task cleanCrowdin(type: Delete) {
    delete fileTree("src/main/res").matching {
        include "**/SETUP.md"
        include "**/PLAYSTORE.txt"
    }
}
preBuild.dependsOn cleanCrowdin
cleanCrowdin.shouldRunAfter copySetup

repositories {
    google()
    jcenter()
    maven { url "https://repo1.maven.org/maven2/" }
    maven { url "https://jitpack.io" }
    //maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    //maven { url "https://jakarta.oss.sonatype.org/content/repositories/snapshots/" }
}

configurations.all {
    // Workaround https://issuetracker.google.com/issues/138441698
    // Support @69c481c39a17d4e1e44a4eb298bb81c48f226eef
    exclude group: "androidx.room", module: "room-runtime"
    // Workaround https://issuetracker.google.com/issues/134685570
    exclude group: "androidx.lifecycle", module: "lifecycle-livedata"
    exclude group: "androidx.lifecycle", module: "lifecycle-livedata-core"

    // lifecycle-livedata: ComputableLiveData, MediatorLiveData, Transformations
    // lifecycle-livedata-core: LiveData, MutableLiveData, Observer
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    def core_version = "1.4.0-alpha01"
    def appcompat_version = "1.3.0-alpha01"
    def fragment_version = "1.3.0-alpha05"
    def recyclerview_version = "1.2.0-alpha03"
    def coordinatorlayout_version = "1.1.0"
    def constraintlayout_version = "2.0.0-beta3"
    def material_version = "1.2.0-alpha06"
    def browser_version = "1.3.0-alpha01"
    def lbm_version = "1.0.0"
    def swiperefresh_version = "1.1.0-rc01"
    def documentfile_version = "1.0.1"
    def lifecycle_version = "2.2.0" // 2.3.0-alpha03
    def sqlite_version = "2.1.0"
    def room_version = "2.2.5"
    def paging_version = "2.1.2"
    def preference_version = "1.1.1"
    def work_version = "2.4.0-beta01"
    def exif_version = "1.3.0-alpha01"
    def biometric_version = "1.0.1"
    def textclassifier_version = "1.0.0-alpha03"
    def billingclient_version = "2.2.0"
    def javamail_version = "1.6.5"
    def jsoup_version = "1.13.1"
    def css_version = "0.9.27"
    def jax_version = "2.3.0-jaxb-1.0.6"
    def dnsjava_version = "2.1.9"
    def openpgp_version = "12.0"
    def requery_version = "3.31.0"
    def badge_version = "1.1.22"
    def bugsnag_version = "5.0.0"
    def biweekly_version = "0.6.3"
    def photoview_version = "2.3.0"
    def relinker_version = "1.3.1"
    def markwon_version = "4.3.1"
    def bouncycastle_version = "1.65"
    def colorpicker_version = "0.0.15"
    def appauth_version = "0.7.1"
    def jcharset_version = "2.1"

    // https://developer.android.com/jetpack/androidx/releases/

    // https://mvnrepository.com/artifact/androidx.core/core
    implementation "androidx.core:core:$core_version"

    // https://mvnrepository.com/artifact/androidx.appcompat/appcompat
    // https://mvnrepository.com/artifact/androidx.fragment/fragment
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.fragment:fragment:$fragment_version"

    // https://mvnrepository.com/artifact/androidx.viewpager2/viewpager2
    //implementation "androidx.viewpager2:viewpager2:1.1.0-alpha01"

    // https://mvnrepository.com/artifact/androidx.recyclerview/recyclerview
    // https://mvnrepository.com/artifact/androidx.recyclerview/recyclerview-selection
    implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
    //implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"

    // https://mvnrepository.com/artifact/androidx.coordinatorlayout/coordinatorlayout
    implementation "androidx.coordinatorlayout:coordinatorlayout:$coordinatorlayout_version"

    // https://mvnrepository.com/artifact/androidx.constraintlayout/constraintlayout
    implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version"

    // https://mvnrepository.com/artifact/com.google.android.material/material
    // https://github.com/material-components/material-components-android/releases
    implementation "com.google.android.material:material:$material_version"

    // https://mvnrepository.com/artifact/androidx.browser/browser
    implementation "androidx.browser:browser:$browser_version"

    // https://mvnrepository.com/artifact/androidx.localbroadcastmanager/localbroadcastmanager
    implementation "androidx.localbroadcastmanager:localbroadcastmanager:$lbm_version"

    // https://mvnrepository.com/artifact/androidx.swiperefreshlayout/swiperefreshlayout
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swiperefresh_version"

    // https://mvnrepository.com/artifact/androidx.documentfile/documentfile
    implementation "androidx.documentfile:documentfile:$documentfile_version"

    // https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-runtime
    // https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-livedata
    // https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-livedata-core
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    // https://mvnrepository.com/artifact/androidx.room/room-runtime
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-common:$room_version" // because of exclude
    // https://mvnrepository.com/artifact/androidx.sqlite/sqlite-framework
    implementation "androidx.sqlite:sqlite-framework:$sqlite_version" // because of exclude
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // https://mvnrepository.com/artifact/androidx.paging/paging-runtime
    implementation "androidx.paging:paging-runtime:$paging_version"

    // https://mvnrepository.com/artifact/androidx.preference/preference
    implementation "androidx.preference:preference:$preference_version"

    // https://mvnrepository.com/artifact/androidx.work/work-runtime
    implementation "androidx.work:work-runtime:$work_version"

    // https://mvnrepository.com/artifact/androidx.exifinterface/exifinterface
    implementation "androidx.exifinterface:exifinterface:$exif_version"

    // https://mvnrepository.com/artifact/androidx.biometric/biometric
    // https://developer.android.com/jetpack/androidx/releases/biometric
    implementation "androidx.biometric:biometric:$biometric_version"

    // https://mvnrepository.com/artifact/androidx.textclassifier/textclassifier
    // https://developer.android.com/jetpack/androidx/releases/textclassifier
    //implementation "androidx.textclassifier:textclassifier:$textclassifier_version"

    // https://developer.android.com/google/play/billing/billing_library_releases_notes
    implementation "com.android.billingclient:billing:$billingclient_version"

    // https://javaee.github.io/javamail/
    // https://projects.eclipse.org/projects/ee4j.javamail
    // https://mvnrepository.com/artifact/com.sun.mail
    implementation "com.sun.mail:android-mail:$javamail_version"
    implementation "com.sun.mail:android-activation:$javamail_version"

    // https://jsoup.org/news/
    implementation "org.jsoup:jsoup:$jsoup_version"

    // http://cssparser.sourceforge.net/
    // https://mvnrepository.com/artifact/net.sourceforge.cssparser/cssparser
    implementation "net.sourceforge.cssparser:cssparser:$css_version"

    // https://github.com/eclipse-ee4j/jaxb-ri
    // https://mvnrepository.com/artifact/org.w3c/dom
    implementation "org.w3c:dom:$jax_version"

    // http://www.dnsjava.org/
    // https://mvnrepository.com/artifact/dnsjava/dnsjava
    implementation "dnsjava:dnsjava:$dnsjava_version"

    // https://github.com/open-keychain/openpgp-api
    // https://mvnrepository.com/artifact/org.sufficientlysecure/openpgp-api
    //implementation "org.sufficientlysecure:openpgp-api:$openpgp_version"
    implementation project(':openpgp-api')

    // https://www.sqlite.org/changes.html
    // https://github.com/requery/sqlite-android/
    // https://mvnrepository.com/artifact/io.requery/sqlite-android?repo=jcenter
    implementation "io.requery:sqlite-android:$requery_version"

    // https://github.com/leolin310148/ShortcutBadger
    // https://mvnrepository.com/artifact/me.leolin/ShortcutBadger
    implementation "me.leolin:ShortcutBadger:$badge_version"

    // https://github.com/bugsnag/bugsnag-android
    implementation("com.bugsnag:bugsnag-android:$bugsnag_version") {
        exclude group: "com.bugsnag", module: "bugsnag-plugin-android-anr"
        exclude group: "com.bugsnag", module: "bugsnag-plugin-android-ndk"
    }

    // https://github.com/mangstadt/biweekly
    implementation("net.sf.biweekly:biweekly:$biweekly_version") {
        exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
    }

    // https://github.com/chrisbanes/PhotoView
    implementation "com.github.chrisbanes:PhotoView:$photoview_version"

    // https://github.com/KeepSafe/ReLinker
    // https://mvnrepository.com/artifact/com.getkeepsafe.relinker/relinker?repo=jcenter
    implementation "com.getkeepsafe.relinker:relinker:$relinker_version"

    // https://github.com/noties/Markwon
    implementation "io.noties.markwon:core:$markwon_version"

    // // https://github.com/QuadFlask/colorpicker
    implementation "com.github.QuadFlask:colorpicker:$colorpicker_version"

    // https://www.bouncycastle.org/latest_releases.html
    // https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on
    implementation "org.bouncycastle:bcpkix-jdk15to18:$bouncycastle_version"

    // https://github.com/openid/AppAuth-Android
    // https://mvnrepository.com/artifact/net.openid/appauth?repo=bt-openid
    implementation "net.openid:appauth:$appauth_version"

    // http://www.freeutils.net/source/jcharset/
    // https://mvnrepository.com/artifact/net.freeutils/jcharset
    implementation "net.freeutils:jcharset:$jcharset_version"
}