soul-browser/scripts/compile-pdf-page-theme.sh
KaKi87 abb005843e Invert PDF pages when the UI theme is dark.
Map Display → UI theme onto PdfRenderer bitmaps so dark UI gets inverted pages instead of always-white renders.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:18:00 +02:00

77 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC="${ROOT_DIR}/sources/java/com/mycompany/app/compress/PdfPageTheme.java"
OUT_SMALI="${ROOT_DIR}/app/smali_classes3/com/mycompany/app/compress"
TOOLS="${ROOT_DIR}/tools/pdf-page-theme"
ANDROID_HOME="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-${HOME}/android-sdk}}"
ANDROID_JAR="$(ls -1 "${ANDROID_HOME}/platforms"/android-*/android.jar 2>/dev/null | sort -V | tail -1)"
D8="$(find "${ANDROID_HOME}/build-tools" -name d8 -type f 2>/dev/null | sort -V | tail -1)"
APK="$(ls -1 "${ROOT_DIR}/dist"/soul-browser-*-aligned.apk 2>/dev/null | sort | tail -1)"
if [[ -z "${APK}" || ! -f "${APK}" ]]; then
APK="$(ls -1 "${ROOT_DIR}/dist"/soul-browser-*-merged-unsigned.apk 2>/dev/null | sort | tail -1)"
fi
DEX2JAR_ZIP="${ROOT_DIR}/tools/dex2jar-2.4.zip"
mkdir -p "${TOOLS}" "${OUT_SMALI}"
if [[ ! -f "${ANDROID_JAR}" ]]; then
echo "error: android.jar not found" >&2
exit 1
fi
if [[ -z "${D8}" ]]; then
echo "error: d8 not found" >&2
exit 1
fi
if [[ ! -f "${APK}" ]]; then
echo "error: merged unsigned APK not found; run ./scripts/build.sh first" >&2
exit 1
fi
if [[ ! -f "${DEX2JAR_ZIP}" ]]; then
echo "Downloading dex2jar 2.4..."
curl -fsSL -o "${DEX2JAR_ZIP}" \
"https://github.com/pxb1988/dex2jar/releases/download/v2.4/dex-tools-v2.4.zip"
unzip -qo "${DEX2JAR_ZIP}" -d "${ROOT_DIR}/tools/dex2jar-2.4"
fi
D2J="$(find "${ROOT_DIR}/tools" -name d2j-dex2jar.sh 2>/dev/null | head -1)"
D2S="$(find "${ROOT_DIR}/tools" -name d2j-dex2smali.sh 2>/dev/null | head -1)"
if [[ -z "${D2J}" || ! -f "${D2J}" ]]; then
echo "error: d2j-dex2jar.sh not found under ${ROOT_DIR}/tools" >&2
exit 1
fi
if [[ -z "${D2S}" || ! -f "${D2S}" ]]; then
echo "error: d2j-dex2smali.sh not found under ${ROOT_DIR}/tools" >&2
exit 1
fi
STAGE="$(mktemp -d)"
cleanup() { rm -rf "${STAGE}"; }
trap cleanup EXIT
echo "Building classpath from APK dex files..."
CP="${ANDROID_JAR}"
for dex in classes.dex classes2.dex classes3.dex classes4.dex; do
unzip -p "${APK}" "${dex}" > "${STAGE}/${dex}" 2>/dev/null || true
if [[ -s "${STAGE}/${dex}" ]]; then
jar="${STAGE}/${dex%.dex}.jar"
bash "${D2J}" --force "${STAGE}/${dex}" -o "${jar}" >/dev/null
CP="${CP}:${jar}"
fi
done
echo "Compiling PdfPageTheme..."
javac --release 11 -classpath "${CP}" -d "${STAGE}/classes" "${SRC}"
echo "Dexing compiled classes..."
mkdir -p "${STAGE}/out"
mapfile -t CLASS_FILES < <(find "${STAGE}/classes" -name '*.class' -type f)
("${D8}" --release --lib "${ANDROID_JAR}" --output "${STAGE}/out" "${CLASS_FILES[@]}")
echo "Disassembling to smali..."
bash "${D2S}" "${STAGE}/out/classes.dex" -o "${STAGE}/smali"
cp -a "${STAGE}/smali/com/mycompany/app/compress/PdfPageTheme"* "${OUT_SMALI}/"
echo "Wrote smali under ${OUT_SMALI}"