services:
  apk-downloader:
    image: curlimages/curl:8.5.0
    environment:
      - APP_URL=${APP_URL:-}
    command:
      - sh
      - -c
      - >
        if [ -n "$APP_URL" ]; then
          curl -L "$APP_URL" -o /apk/wishlist.apk;
        else
          cp /apk-source/app.apk /apk/wishlist.apk;
        fi
    volumes:
      - ./apk:/apk
      - ./wishlist-349317-5fd795.apk:/apk-source/app.apk:ro
  android-emulator:
    image: budtmo/docker-android:emulator_13.0
    privileged: true
    depends_on:
      apk-downloader:
        condition: service_completed_successfully
    ports:
      - "4723:4723"
      - "6080:6080"
      - "5554:5554"
      - "5555:5555"
    environment:
      - DEVICE=Pixel_5
      - APPIUM=true
      - WEB_VNC=true
      - AUTO_GRANT_PERMISSIONS=true
      - ENABLE_VNC=true
      - EMULATOR_PARAMS=-no-window -no-audio -gpu swiftshader_indirect -no-snapshot -no-boot-anim -accel off
    shm_size: 2gb
    volumes:
      - ./apk:/apk:ro
  apk-installer:
    image: budtmo/docker-android:emulator_13.0
    depends_on:
      android-emulator:
        condition: service_started
    entrypoint:
      - /bin/bash
      - -lc
    command:
      - >
        for i in $(seq 1 60); do
          adb connect android-emulator:5555 || true;
          if adb devices | tr -s ' ' | grep -q "android-emulator:5555"; then
            break;
          fi;
          sleep 2;
        done;
        adb -s android-emulator:5555 wait-for-device;
        for i in $(seq 1 120); do
          if adb -s android-emulator:5555 shell getprop sys.boot_completed | tr -d '\r' | grep -q "1"; then
            break;
          fi;
          sleep 2;
        done;
        adb -s android-emulator:5555 install -r /apk/wishlist.apk &&
        adb -s android-emulator:5555 shell monkey -p ru.otus.wishlist -c android.intent.category.LAUNCHER 1
    volumes:
      - ./apk:/apk:ro
