From 2849b32d1eb111b7037f38f9509d5bacc3aa8a04 Mon Sep 17 00:00:00 2001 From: Ark74 Date: Tue, 6 Jan 2026 04:29:13 -0600 Subject: [PATCH] improve selfcheck logic and advice reorganize variables --- android/termux-setup/00_lib_common.sh | 15 +++++++ android/termux-setup/40_mod_termux_api.sh | 26 ++++++------ android/termux-setup/60_mod_ppk_checks.sh | 49 +++++++++++++++++++++++ android/termux-setup/99_main.sh | 14 +++---- 4 files changed, 81 insertions(+), 23 deletions(-) diff --git a/android/termux-setup/00_lib_common.sh b/android/termux-setup/00_lib_common.sh index 556fb7a..acf8096 100644 --- a/android/termux-setup/00_lib_common.sh +++ b/android/termux-setup/00_lib_common.sh @@ -11,3 +11,18 @@ warn_red() { printf "${RED}${BOLD}[iiab] WARNING:${RST} %s\n" "$*" >&2; } have() { command -v "$1" >/dev/null 2>&1; } need() { have "$1" || return 1; } die() { echo "[!] $*" >&2; exit 1; } + +# ------------------------- +# Global defaults (may be overridden via environment) +# ------------------------- +STATE_DIR="${STATE_DIR:-${HOME}/.iiab-android}" +ADB_STATE_DIR="${ADB_STATE_DIR:-${STATE_DIR}/adbw_pair}" +LOG_DIR="${LOG_DIR:-${STATE_DIR}/logs}" + +HOST="${HOST:-127.0.0.1}" +CONNECT_PORT="${CONNECT_PORT:-}" +TIMEOUT_SECS="${TIMEOUT_SECS:-180}" + +# Defaults used by ADB flows / logging / misc +CLEANUP_OFFLINE="${CLEANUP_OFFLINE:-1}" +DEBUG="${DEBUG:-0}" diff --git a/android/termux-setup/40_mod_termux_api.sh b/android/termux-setup/40_mod_termux_api.sh index 9e8cad6..bddeb3d 100644 --- a/android/termux-setup/40_mod_termux_api.sh +++ b/android/termux-setup/40_mod_termux_api.sh @@ -11,10 +11,8 @@ LAST_NOTIF_ID="" # Termux:API sanity check (notifications) termux_api_ready() { have termux-notification || return 1 - have termux-dialog || return 1 - # Quick probe: some Samsung setups fail until Termux:API app is installed/allowed. - # We try a harmless notification and remove it. + # Quick probe: some setups fail until Termux:API app is installed/allowed. local msg="iiab test notification" if ! termux-notification --id "$NOTIF_BASE_ID" --title "iiab" --content "$msg" --priority max --sound >/dev/null 2>&1; then return 1 @@ -47,9 +45,8 @@ cleanup_notif() { notify_ask_one() { # args: key title content local key="$1" title="$2" content="$3" - local out="$ADB_STATE_DIR/$key.txt" + local out="$ADB_STATE_DIR/$key.reply" rm -f "$out" - local action # Fresh notification each time + sound (use a new ID so Android plays sound each time) local nid @@ -61,13 +58,10 @@ notify_ask_one() { termux-notification-remove "$nid" >/dev/null 2>&1 || true fi - # termux-notification does not provide a reply input; use termux-dialog to capture text. - if ! have termux-dialog; then - warn "termux-dialog not available. Install: pkg install termux-api + Termux:API app." - return 1 - fi - - action="sh -lc 'termux-dialog -i -t \"${title}\" -h \"${content}\" | tr -d \"\\n\" | sed -n \"s/.*\\\"text\\\"[[:space:]]*:[[:space:]]*\\\"\\([^\\\"]*\\)\\\".*/\\1/p\" > \"${out}\"'" + # Direct reply: Termux:API injects the user input into $REPLY for the action. + # Write it to a known file, then the main loop reads it. + local action + action="sh -lc 'umask 077; printf \"%s\" \"\$REPLY\" > \"${out}\"'" termux-notification \ --id "$nid" \ @@ -80,16 +74,20 @@ notify_ask_one() { --button1-action "$action" \ || return 1 - local start now + local start now reply start="$(date +%s)" + while true; do if [[ -f "$out" ]]; then + reply="$(tr -d '\r\n' < "$out" 2>/dev/null || true)" + rm -f "$out" >/dev/null 2>&1 || true if have termux-notification-remove; then termux-notification-remove "$nid" >/dev/null 2>&1 || true fi - tr -d '\r\n' < "$out" + printf '%s' "$reply" return 0 fi + now="$(date +%s)" if (( now - start >= TIMEOUT_SECS )); then if have termux-notification-remove; then diff --git a/android/termux-setup/60_mod_ppk_checks.sh b/android/termux-setup/60_mod_ppk_checks.sh index e08ab03..ad7e4c1 100644 --- a/android/termux-setup/60_mod_ppk_checks.sh +++ b/android/termux-setup/60_mod_ppk_checks.sh @@ -138,3 +138,52 @@ check_readiness() { fi return 0 } + +self_check_android_flags() { + have adb || return 0 + adb start-server >/dev/null 2>&1 || true + + local serial sdk rel mon mon_fflag ds ppk_eff + serial="$(adb_pick_loopback_serial 2>/dev/null)" || { + warn "ADB: no loopback device connected. Tip: run --adb-only (pair+connect) or --check for more info." + return 0 + } + + sdk="$(adb -s "$serial" shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r' || true)" + rel="$(adb -s "$serial" shell getprop ro.build.version.release 2>/dev/null | tr -d '\r' || true)" + log " Android flags (quick): release=${rel:-?} sdk=${sdk:-?} serial=$serial" + + if [[ "$sdk" =~ ^[0-9]+$ ]] && (( sdk >= 34 )); then + mon_fflag="$(adb_get_child_restrictions_flag "$serial")" + if [[ "$mon_fflag" == "true" || "$mon_fflag" == "false" ]]; then + mon="$mon_fflag" + else + mon="$(adb -s "$serial" shell settings get global settings_enable_monitor_phantom_procs 2>/dev/null | tr -d '\r' || true)" + fi + + if [[ "$mon" == "false" ]]; then + ok " Child restrictions: OK (monitor=false)" + elif [[ "$mon" == "true" ]]; then + warn " Child restrictions: NOT OK (monitor=true) -> check Developer Options" + else + warn " Child restrictions: unknown/unreadable (monitor='${mon:-}')" + fi + fi + + if [[ "$sdk" =~ ^[0-9]+$ ]] && (( sdk >= 31 && sdk <= 33 )); then + ds="$(adb -s "$serial" shell dumpsys activity settings 2>/dev/null | tr -d '\r' || true)" + ppk_eff="$(printf '%s\n' "$ds" | awk -F= '/max_phantom_processes=/{print $2; exit}' | tr -d '[:space:]' || true)" + + if [[ "$ppk_eff" =~ ^[0-9]+$ ]]; then + if (( ppk_eff >= 256 )); then + ok " PPK: OK (max_phantom_processes=$ppk_eff)" + else + warn " PPK: low (max_phantom_processes=$ppk_eff) -> suggest: --ppk-only" + fi + else + warn " PPK: unreadable (max_phantom_processes='${ppk_eff:-}')" + fi + fi + + log " Tip: run --check for full details." +} diff --git a/android/termux-setup/99_main.sh b/android/termux-setup/99_main.sh index 650d55b..3f3352b 100644 --- a/android/termux-setup/99_main.sh +++ b/android/termux-setup/99_main.sh @@ -10,16 +10,10 @@ # ------------------------- # Defaults # ------------------------- -STATE_DIR="${HOME}/.iiab-android" -ADB_STATE_DIR="${STATE_DIR}/adbw_pair" -LOG_DIR="${STATE_DIR}/logs" -mkdir -p "$STATE_DIR" "$ADB_STATE_DIR" +# NOTE: Core defaults live in 00_lib_common.sh to guarantee availability for all modules. -HOST="127.0.0.1" -CONNECT_PORT="" -TIMEOUT_SECS=180 -CLEANUP_OFFLINE=1 -DEBUG=0 +# Ensure state directories exist (safe even if user overrides via environment). +mkdir -p "$STATE_DIR" "$ADB_STATE_DIR" "$LOG_DIR" 2>/dev/null || true RESET_DEBIAN=0 ONLY_CONNECT=0 @@ -105,6 +99,8 @@ self_check() { else warn " adb: not present" fi + # Quick Android flags check (best-effort; no prompts) + self_check_android_flags || true if have termux-wake-lock; then ok " Termux:API wakelock: available"; else warn " Termux:API wakelock: not available"; fi if have termux-notification; then ok " Termux:API notifications: command present"; else warn " Termux:API notifications: missing"; fi