- Clear morning-alarm.log before logging execution timestamp. - Set a flag 'alarm_dismissed' for loop control. - Modify the check_notification function to continuously check the alarm state and sleep for better efficiency. - Start play.sh script in the background. - Replace 'killall' with '/system/bin/pkill' for better compatibility. - Introduce sleep intervals for smoother execution. - Adjust the volume increment loop and add sleep before checking the notification. - Properly exit the script when the alarm is dismissed, killing play.sh and mpv. - Overall improvements for better readability and functionality in the morning-alarm.sh script.
68 lines
1.6 KiB
Bash
Executable File
68 lines
1.6 KiB
Bash
Executable File
#!/data/data/com.termux/files/usr/bin/zsh
|
|
#
|
|
# Alarm Control Script
|
|
#
|
|
# This script controls the alarm on the Android device.
|
|
# It turns off Bluetooth, increases the volume, and checks if the alarm has been dismissed.
|
|
# After the alarm is dismissed, it plays the alarm sound.
|
|
|
|
# clear logs
|
|
rm -rf /data/scripts/morning-alarm.log
|
|
echo "Script executed at: $(date)"
|
|
|
|
# Set a flag for the loop
|
|
alarm_dismissed=false
|
|
|
|
# Function to turn off Bluetooth
|
|
turn_off_bluetooth() {
|
|
echo "Turning off Bluetooth."
|
|
svc bluetooth disable || { echo "Error turning off Bluetooth." ; exit 1; }
|
|
}
|
|
|
|
# Function to increase the volume
|
|
increase_volume() {
|
|
echo "Increasing volume."
|
|
cmd media_session volume --set 15 --stream 9 || { echo "Error increasing volume." ; exit 1; }
|
|
}
|
|
|
|
# Function to check the alarm notification
|
|
check_notification() {
|
|
# Run the dumpsys command within a timeout
|
|
output=$(dumpsys activity processes | grep "com.urbandroid.sleep")
|
|
|
|
# Check if the result is not empty and contains the alarm
|
|
if [ -n "$output" ] && echo "$output" | grep -q "AlarmKlaxon"; then
|
|
date && echo "Alarm not dismissed yet."
|
|
sleep 5
|
|
return 1
|
|
else
|
|
date && echo "Alarm dismissed."
|
|
alarm_dismissed=true
|
|
/system/bin/pkill play.sh
|
|
/system/bin/pkill mpv
|
|
fi
|
|
}
|
|
|
|
# Main program
|
|
|
|
# Turn off Bluetooth
|
|
turn_off_bluetooth
|
|
|
|
# start play.sh script
|
|
/data/scripts/play.sh &
|
|
|
|
# Repeat the check_notification function
|
|
while true; do
|
|
increase_volume
|
|
sleep 2
|
|
check_notification
|
|
|
|
# Check if the alarm dismissed flag is set
|
|
if [ "$alarm_dismissed" = true ]; then
|
|
/system/bin/pkill play.sh
|
|
/system/bin/pkill mpv
|
|
exit 0 # Exit the script here
|
|
fi
|
|
done
|
|
|