#!/system/bin/sh set -eu package=kr.offtimer.app current_user=$(am get-current-user) case "$current_user" in ''|*[!0-9]*) echo 'ERROR: Cannot identify current Android user.'; exit 1;; esac app_uid=$(cmd package list packages -U --user "$current_user" "$package" | sed -n 's/^package:kr\.offtimer\.app uid://p' | tr -d '\r') case "$app_uid" in ''|*[!0-9]*) echo 'ERROR: Install OffTimer in the current profile first.'; exit 1;; esac worker_name="kr.offtimer.worker.$app_uid" worker_log="/data/local/tmp/offtimer-$app_uid-worker.log" worker_stamp="/data/local/tmp/offtimer-$app_uid-apk-path" apk=$(pm path --user "$current_user" "$package" | sed -n 's/^package://p' | head -n 1 | tr -d '\r') if [ ! -f "$apk" ]; then echo 'ERROR: Installed APK not found.'; exit 1; fi am start --user "$current_user" -n "$package/.MainActivity" >/dev/null # Reuse only the worker loaded from this exact installed APK. if pidof "$worker_name" >/dev/null && [ "$(cat "$worker_stamp" 2>/dev/null)" = "$apk" ]; then echo 'WORKER_RUNNING: Check the permission indicator in OffTimer.' exit 0 fi # Updating the APK changes its path. Shut down only our obsolete shell process. for old_pid in $(pidof "$worker_name" 2>/dev/null || true); do kill "$old_pid"; done count=0 while pidof "$worker_name" >/dev/null; do if [ "$count" -ge 10 ]; then echo 'ERROR: Previous worker did not stop.'; exit 1; fi sleep 1 count=$((count + 1)) done printf '%s' "$apk" >"$worker_stamp" CLASSPATH="$apk" nohup /system/bin/app_process /system/bin --nice-name="$worker_name" kr.offtimer.app.BridgeMain "$app_uid" >"$worker_log" 2>&1