|
| 1 | +#!/data/data/com.termux/files/usr/bin/bash |
| 2 | +set -e -u -f |
| 3 | + |
| 4 | +SCRIPTNAME=termux-job-scheduler |
| 5 | + |
| 6 | +show_usage () { |
| 7 | + echo "Usage: termux-job-scheduler [options]" |
| 8 | + echo "Schedule a Termux script to run later, or periodically" |
| 9 | + echo " --script path to the Termux script to be called" |
| 10 | + echo " --job-id int job id (will overwrite any previous job with the same id)" |
| 11 | + echo " --pending boolean list pending jobs only (default false)" |
| 12 | + echo " --period_ms int schedule job approximately every period_ms milliseconds (default 0 means once)" |
| 13 | + echo " --network run only when this type of network available, default none (any|unmetered|cellular|not_roaming|none)" |
| 14 | + echo " --battery-not-low boolean run only when battery is not low, default true (at least Androi O)" |
| 15 | + echo " --storage-not-low boolean run only when storage is not low, default false (at least Androi O)" |
| 16 | + echo " --charging boolean run only when charging, default false" |
| 17 | + echo " --trigger-content-uri text (at least Android N)" |
| 18 | + echo " --trigger-content-flag int default 1, (at least Android N)" |
| 19 | + exit 0 |
| 20 | +} |
| 21 | + |
| 22 | +OPT_SCRIPT="" |
| 23 | +OPT_JOB_ID="" |
| 24 | +OPT_PENDING="" |
| 25 | +OPT_PERIOD_MS="" |
| 26 | +OPT_NETWORK="" |
| 27 | +OPT_BATTERY_NOT_LOW="" |
| 28 | +OPT_STORAGE_NOT_LOW="" |
| 29 | +OPT_CHARGING="" |
| 30 | +OPT_TRIGGER_CONTENT_URI="" |
| 31 | +OPT_TRIGGER_CONTENT_FLAG="" |
| 32 | + |
| 33 | +TEMP=`busybox getopt \ |
| 34 | + -n $SCRIPTNAME \ |
| 35 | + -o hs:p \ |
| 36 | + --long script:,\ |
| 37 | +job-id:,pending:,\ |
| 38 | +period-ms:,network:,\ |
| 39 | +battery-not-low:,storage-not-low:,\ |
| 40 | +charging:,help,\ |
| 41 | +trigger_content_flag:,trigger-content-uri: \ |
| 42 | + -s bash \ |
| 43 | + -- "$@"` |
| 44 | +eval set -- "$TEMP" |
| 45 | + |
| 46 | +while true; do |
| 47 | + case "$1" in |
| 48 | + -s | --script) OPT_SCRIPT="$2"; shift 2;; |
| 49 | + --job-id) OPT_JOB_ID="$2"; shift 2;; |
| 50 | + -p | --pending) OPT_PENDING="$2"; shift 2;; |
| 51 | + --period-ms) OPT_PERIOD_MS="$2"; shift 2;; |
| 52 | + --network) OPT_NETWORK="$2"; shift 2;; |
| 53 | + --battery-not-low) OPT_BATTERY_NOT_LOW="$2"; shift 2;; |
| 54 | + --storage-not-low) OPT_STORAGE_NOT_LOW="$2"; shift 2;; |
| 55 | + --charging) OPT_CHARGING="$2"; shift 2;; |
| 56 | + --trigger-content-flag) OPT_TRIGGER_CONTENT_FLAG="$2"; shift 2;; |
| 57 | + --trigger-content-uri) OPT_TRIGGER_CONTENT_URI="$2"; shift 2;; |
| 58 | + -h | --help) show_usage;; |
| 59 | + --) shift; break ;; |
| 60 | + esac |
| 61 | +done |
| 62 | + |
| 63 | +if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi |
| 64 | + |
| 65 | +set -- |
| 66 | +if [ -n "$OPT_SCRIPT" ]; then set -- "$@" --es script "$OPT_SCRIPT"; fi |
| 67 | +if [ -n "$OPT_JOB_ID" ]; then set -- "$@" --ei job_id "$OPT_JOB_ID"; fi |
| 68 | +if [ -n "$OPT_PENDING" ]; then set -- "$@" --ez pending "$OPT_PENDING"; fi |
| 69 | +if [ -n "$OPT_PERIOD_MS" ]; then set -- "$@" --ei period_ms "$OPT_PERIOD_MS"; fi |
| 70 | +if [ -n "$OPT_NETWORK" ]; then set -- "$@" --es network "$OPT_NETWORK"; fi |
| 71 | +if [ -n "$OPT_BATTERY_NOT_LOW" ]; then set -- "$@" --ez battery_not_low "$OPT_BATTERY_NOT_LOW"; fi |
| 72 | +if [ -n "$OPT_STORAGE_NOT_LOW" ]; then set -- "$@" --ez storage_not_low "$OPT_STORAGE_NOT_LOW"; fi |
| 73 | +if [ -n "$OPT_CHARGING" ]; then set -- "$@" --ez charging "$OPT_CHARGING"; fi |
| 74 | +if [ -n "$OPT_TRIGGER_CONTENT_URI" ]; then set -- "$@" --es trigger_content_uri "$OPT_TRIGGER_CONTENT_URI"; fi |
| 75 | +if [ -n "$OPT_TRIGGER_CONTENT_FLAG" ]; then set -- "$@" --ez trigger_content_flag "$OPT_TRIGGER_CONTENT_FLAG"; fi |
| 76 | + |
| 77 | +/data/data/com.termux/files/usr/libexec/termux-api JobScheduler "$@" |
0 commit comments