FROM gradle:9.6.1-jdk17@sha256:7364ce528f33bb6038672bcef990d524f1ad8fbc292935819c235db886d0fae7 AS android-base

USER root
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ARG ANDROID_COMMAND_LINE_TOOLS_VERSION=15859902
ARG ANDROID_COMMAND_LINE_TOOLS_SHA1=040d3996a65543d22ec4bf73e4c37aa37a8d4af4

ENV ANDROID_HOME=/opt/android-sdk
ENV ANDROID_SDK_ROOT=/opt/android-sdk
ENV PATH="${PATH}:${ANDROID_HOME}/cmdline-tools/latest/bin:${ANDROID_HOME}/platform-tools"

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl=8.18.0-1ubuntu2.3 \
        unzip=6.0-29ubuntu1 \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p "${ANDROID_HOME}/cmdline-tools" \
    && curl -fsSL \
        "https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_COMMAND_LINE_TOOLS_VERSION}_latest.zip" \
        -o /tmp/android-command-line-tools.zip \
    && echo "${ANDROID_COMMAND_LINE_TOOLS_SHA1}  /tmp/android-command-line-tools.zip" \
        | sha1sum -c - \
    && unzip -q /tmp/android-command-line-tools.zip -d /tmp/android-command-line-tools \
    && mv /tmp/android-command-line-tools/cmdline-tools "${ANDROID_HOME}/cmdline-tools/latest" \
    && rm -rf /tmp/android-command-line-tools /tmp/android-command-line-tools.zip

RUN android --no-metrics --sdk="${ANDROID_HOME}" sdk install \
    "build-tools/37.0.0" \
    "platforms/android-37.0" \
    && chown -R gradle:gradle "${ANDROID_HOME}"

USER gradle

FROM android-base AS android-sdk

USER gradle
WORKDIR /workspace/android

COPY --chown=gradle:gradle . .

ARG WNH_DEBUG_BASE_URL
ARG WNH_TRACKING_MIN_TIME_MS
ARG WNH_TRACKING_HTTP_TIMEOUT_MS

RUN --mount=type=cache,target=/home/gradle/.gradle,uid=1000,gid=1000 \
    --mount=type=cache,target=/home/gradle/.android,uid=1000,gid=1000 \
    gradle --no-daemon \
        "-PWNH_DEBUG_BASE_URL=${WNH_DEBUG_BASE_URL}" \
        "-PWNH_TRACKING_MIN_TIME_MS=${WNH_TRACKING_MIN_TIME_MS}" \
        "-PWNH_TRACKING_HTTP_TIMEOUT_MS=${WNH_TRACKING_HTTP_TIMEOUT_MS}" \
        testDebugUnitTest lintDebug assembleDebug assembleDebugAndroidTest

FROM android-base AS emulator

USER root
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ARG ANDROID_EMULATOR_SYSTEM_IMAGE=system-images/android-37.0/google_apis_ps16k/x86_64

RUN android --no-metrics --sdk="${ANDROID_HOME}" sdk install \
    "emulator" \
    "${ANDROID_EMULATOR_SYSTEM_IMAGE}" \
    && chown -R gradle:gradle "${ANDROID_HOME}"

# The device suite stores only the two APKs and short-lived test data. A 1 GiB
# userdata partition passed the complete API 30/34/37 matrix on 2026-07-20;
# 4 GiB made API 34 exceed Docker's writable-layer space before boot.
ARG ANDROID_EMULATOR_DATA_PARTITION_SIZE=1G

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libdbus-1-3=1.16.2-2ubuntu4 \
        libfontconfig1=2.17.1-3ubuntu1 \
        libgl1=1.7.0-3 \
        libnss3=2:3.120-1ubuntu2.1 \
        libpulse0=1:17.0+dfsg1-2ubuntu4 \
        libx11-6=2:1.8.13-1 \
        libxcomposite1=1:0.4.6-1build1 \
        libxcursor1=1:1.2.3-1build1 \
        libxi6=2:1.8.2-2 \
        libxkbfile1=1:1.1.0-1build5 \
        libxrandr2=2:1.5.4-1build1 \
        libxrender1=1:0.9.12-1build1 \
        libxtst6=2:1.2.5-1build1 \
    && rm -rf /var/lib/apt/lists/*

# The container is operated from the host through `docker exec ... adb`.
# Keep platform-tools in the emulator target without invalidating the large
# system-image layer when application sources change.
RUN android --no-metrics --sdk="${ANDROID_HOME}" sdk install "platform-tools" \
    && chown -R gradle:gradle "${ANDROID_HOME}"

USER gradle

RUN echo no | avdmanager create avd \
    --force \
    --name who_need_help_test \
    --package "$(printf '%s' "${ANDROID_EMULATOR_SYSTEM_IMAGE}" | tr '/' ';')" \
    && sed -i \
        "s/^disk.dataPartition.size=.*/disk.dataPartition.size=${ANDROID_EMULATOR_DATA_PARTITION_SIZE}/" \
        /home/gradle/.android/avd/who_need_help_test.avd/config.ini

COPY --from=android-sdk --chown=gradle:gradle \
    /workspace/android/app/build/outputs/apk/debug/app-debug.apk \
    /opt/who-need-help/who-need-help-debug.apk
COPY --from=android-sdk --chown=gradle:gradle \
    /workspace/android/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk \
    /opt/who-need-help/who-need-help-debug-androidTest.apk

CMD ["/opt/android-sdk/emulator/emulator", \
    "-avd", "who_need_help_test", \
    "-no-metrics", \
    "-no-window", \
    "-no-audio", \
    "-no-boot-anim", \
    "-no-snapshot", \
    "-gpu", "software", \
    "-feature", "-Vulkan", \
    "-camera-back", "none", \
    "-camera-front", "none", \
    "-netdelay", "none", \
    "-netspeed", "full"]

FROM scratch AS artifact

USER 65532:65532

COPY --from=android-sdk \
    /workspace/android/app/build/outputs/apk/debug/app-debug.apk \
    /who-need-help-debug.apk
COPY --from=android-sdk \
    /workspace/android/app/build/reports/lint-results-debug.html \
    /lint-results-debug.html

FROM android-base AS android-staging-sdk

USER gradle
WORKDIR /workspace/android

COPY --chown=gradle:gradle . .

ARG WNH_BASE_URL
ARG WNH_TRACKING_MIN_TIME_MS
ARG WNH_TRACKING_HTTP_TIMEOUT_MS

RUN --mount=type=cache,target=/home/gradle/.gradle,uid=1000,gid=1000 \
    --mount=type=cache,target=/home/gradle/.android,uid=1000,gid=1000 \
    gradle --no-daemon \
        "-PWNH_BASE_URL=${WNH_BASE_URL}" \
        "-PWNH_DEBUG_BASE_URL=${WNH_BASE_URL}" \
        "-PWNH_TRACKING_MIN_TIME_MS=${WNH_TRACKING_MIN_TIME_MS}" \
        "-PWNH_TRACKING_HTTP_TIMEOUT_MS=${WNH_TRACKING_HTTP_TIMEOUT_MS}" \
        "-PWNH_TEST_BUILD_TYPE=staging" \
        testStagingUnitTest lintStaging assembleStaging assembleStagingAndroidTest

FROM scratch AS staging-artifact

USER 65532:65532

COPY --from=android-staging-sdk \
    /workspace/android/app/build/outputs/apk/staging/app-staging.apk \
    /who-need-help-staging.apk
COPY --from=android-staging-sdk \
    /workspace/android/app/build/outputs/apk/androidTest/staging/app-staging-androidTest.apk \
    /who-need-help-staging-androidTest.apk
COPY --from=android-staging-sdk \
    /workspace/android/app/build/reports/lint-results-staging.html \
    /lint-results-staging.html
