Shell Script for Android TV Freezes During URL Rotation with User Interaction

  Kiến thức lập trình

I am working on a shell script that runs on an Android 8.0 TV device. The script is designed to:

  1. Call a different website every 30 seconds from a list of URLs provided by an Azure Function.
  2. Reset the 30-second timer if the user interacts with the touch screen.

Here is the script I have so far:

#!/system/bin/sh

# Azure Function URL
azure_function_url="path to azure function"
# Sample result: URLs = ["https://google.com", "https://bing.com", "https://stackoverflow.com"]

# Fetch URLs from the Azure Function
json_response=$(/data/local/tmp/curl "$azure_function_url" -k -L)
echo "JSON Response: $json_response"

# Extract URLs using jq
URLs=($(echo $json_response | /data/local/tmp/jq171 -r '.[]'))

# Print the URLs array
echo "Extracted URLs:"
for url in "${URLs[@]}"; do
    echo "$url"
done

# Function to reset sleep timer
reset_timer() {
    echo 30 > /data/local/tmp/sleep_timer.txt
}

# Initial sleep timer value
echo 30 > /data/local/tmp/sleep_timer.txt

index=0
url_count=${#URLs[@]}
# Loop through each URL
while true
do
    url="${URLs[$index]}"
    am start -a android.intent.action.VIEW -d "$url"

    # Loop with sleep timer
    while [ $(cat /data/local/tmp/sleep_timer.txt) -gt 0 ]; do
        already_reset=false
        getevent -lt /dev/input/event1 | while read line
        do
            if echo "$line" | grep -q "ABS_MT_POSITION_X"; then
                if [ "$already_reset" = "false" ]; then
                    reset_timer
                    already_reset=true
                fi
            fi
        done &
        
        sleep 1
        sleep_timer=$(cat /data/local/tmp/sleep_timer.txt)
        ((sleep_timer--))
        echo $sleep_timer > /data/local/tmp/sleep_timer.txt
    done
    index=$(( (index + 1) % url_count ))

    echo 30 > /data/local/tmp/sleep_timer.txt
done

Problem:

The script works, but the entire device (currently an emulator) freezes and becomes very slow when interacting with the touch screen.

Questions:

  1. How can I optimize the script to prevent the device from freezing during user interaction?
  2. Are there any alternative approaches to achieve the same functionality without causing performance issues?

Any ideas or suggestions to improve the script or an alternative solution would be greatly appreciated. Thank you!

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT