name: Android CI
on:
  workflow_dispatch:
  push:
    branches:
      - main
      - staging
      - trying
  pull_request:
    branches:
      - main
permissions:
  actions: none
  checks: none
  contents: read
  deployments: none
  discussions: none
  id-token: none
  issues: none
  packages: none
  pages: none
  pull-requests: none
  repository-projects: none
  security-events: none
  statuses: none
env:
  JAVA_HOME: /usr/lib/jvm/java-25-openjdk-amd64
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        flavor: [Foss, Gplay]
    steps:
      - uses: actions/checkout@v7.0.1
      - uses: gradle/actions/wrapper-validation@v6.2.0
      - name: set up OpenJDK 25
        run: |
          sudo apt-get update
          sudo apt-get install -y openjdk-25-jdk-headless
          sudo update-alternatives --auto java
      - name: Build
        run: ./gradlew assemble${{ matrix.flavor }}Release
      - name: Check for non-free libraries
        run: |
          wget -q https://codeberg.org/Katastima/apkscanner/releases/download/v0.0.7/apk-scanner_v0.0.7.jar
          java -jar apk-scanner_v0.0.7.jar scan-apk "$(echo 'app/build/outputs/apk/${{ matrix.flavor }}/release/app-${{ matrix.flavor }}-release-unsigned.apk' | tr '[:upper:]' '[:lower:]')"
      - name: Check lint
        run: ./gradlew lint${{ matrix.flavor }}Release
      - name: Run unit tests
        run: timeout 5m ./gradlew test${{ matrix.flavor }}DebugUnitTest || { ./gradlew --stop && timeout 5m ./gradlew test${{ matrix.flavor }}DebugUnitTest; }
      - name: Enable KVM
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm
      - name: Run instrumented tests (API 23)
        uses: ReactiveCircus/android-emulator-runner@v2.38.0
        with:
          api-level: 23
          arch: x86_64
          script: ./gradlew connected${{ matrix.flavor }}DebugAndroidTest
      - name: Run instrumented tests (API 35)
        uses: ReactiveCircus/android-emulator-runner@v2.38.0
        with:
          api-level: 35
          arch: x86_64
          script: ./gradlew connected${{ matrix.flavor }}DebugAndroidTest
      - name: Archive test results
        if: always()
        uses: actions/upload-artifact@v7.0.1
        with:
          name: test-results-flavor${{ matrix.flavor }}
          path: app/build/reports
