I have checked the query content provider and can't find any method to list all available alarms. Don't know if there is any way to do it without root. I have root, I can query directly using SQLite to the /data/user_de/0/com.google.android.deskclock/databases/alarms.db (LOS based ROM clock db is stored at /user_de). Non root user have to use other method, just as you have done.
So you have solved the next alarm problem. But you need to convert from "Mon 17:00" to a proper date. It can be parsed by pattern, but it lose its week, month and year; since it is converted to 0 based (1st Jan 1970, thursday). We have to added back the latest week counted from the 0 based.
It takes probably around 1 hours spanned accross several hours to get this right. Didn't realize it is quite complicated. I tested several function and some date pattern doesn't work when used in getDate(), such as the "c". After 50-60 trials, finally get it working. But I think the script is still ugly. I am open to any better idea than mine below
Assume your next_alarm_formatted is stored at variable {alarm}
Code: Select all
alarm = "Mon 17:00";
talarm = getDate(alarm, "EEE HH:mm");
toweek = getDate() / (7*86400000) * (7*86400000) ;
nexta = toweek + talarm;
if(nexta < getDate())
nexta = addDays(nexta, 7);
reminder = addHours(nexta, -1);
{talarm} get the date from the alarm, but in 0 based. So it will the first monday after 1st Jan 1970
{toweek} get the latest week anchor. It is using the common method to round off to the nearest week. divide by 7 days (7* 86400000 miliseconds) and multiply again by the same value. Tested on today : Tuesday, 12 Dec 2018, 12:00, It will truncate the decimal and get you Thursday, 6 Dec 2018, 00:00 (will be different hours based on your timezone).
{nexta} is next alarm, add the talarm and toweek. We will get Monday, 10 Dec 2018, 17:00. But it is impossible to have alarm in the past, so we check it again to current date. If it is less than today, then add 7 days to it. Finally we get nexta = 17 Dec 2018, 17:00.
Since you want reminder to be 1 hour before, minus this 1 hour. {reminder} should contain the time, 1 hour before your next alarm.
Put the script into script and add condition debug dialog to check it. You should check {reminder} whether it is correct already. Try to change the alarm to "Tue 17:00" or "Wed 20:00", or any other value to see if it is working as expected.