[Android Head-Units] gustden:
I was actually playing around with this the other day and re-wrote how I am doing the rotation. What I have is defining a list of applications to rotate through. It is still modifying the shell script but pretty straight forward.

Basically there are 2 variables:

rotation_list="pandora spotify radio"

music_app_to_top="iheart|pandora|spotify|vlc|music|radio"

rotation_list - is a list of applications to rotate through. The applicatons must be defined as a "am start" command earlier in the script. You can add whatever number of apps you want to rotate through.

music_app_to_top - is a list of music programs that you want brought to the top if active. This is like a grep command and will match any part of a package name.

The way the logic works -

1. If music app is not on top then bring most recent the top ( defined in pattern match of music_app_to_top ).

2. If a music app already on top go to next music player defined in rotation_list.

This allows a music program to be started and put in the background, and even if it is not in the rotation list, it will be brought to the front with the first press of the src key. The next press of the source key will rotate to the next player in the list. It works pretty nice.

You should be able to replace the code for SRC (37) with the code below. It is some very "ugly" code, but the only thing you should need to change are the two variables listed above, and make sure each of the players defined in the rotation_list have an "am start" command defined.

If this is too complicated, I'm sure you can get some help with it :)

37) # Rotates through music apps defined in $rotation_list
#
# If music app is not on top then bring most recent the top ( defined in pattern match of music_app_to_top ).
# If already on top go to next music player defined on the list
#
# Put list of applications to be rotated through
# These MUST be defined with the start command (ABOVE) to work

rotation_list="pandora spotify radio"

# List of apps to check for moving app to top ( matches any part of package name )

music_app_to_top="iheart|pandora|spotify|vlc|music|radio"

cur_music_app=""

# check to see if any music player is on top
music_on_top=`dumpsys activity activities | grep "Run #" | cut -f 13 -d" " | head -1 | egrep "$music_app_to_top"`
if [ "$music_on_top" = "" ]
then
# Music player is not on top, find most recent music player
cur_music_app=`dumpsys activity activities | grep "Run #" | cut -f 13 -d" "| egrep "$music_app_to_top" | head -1`
fi

if [ "$cur_music_app" != "" ]
then
# Bring current one to the top, since it is not already on top
am start $cur_music_app
else
# Music app already on top, time to rotate to the next one
$pause # Stop current music player

# NO ARRAYS allowed here, so now it starts to get ugly.
# rotate through each app in rotation_list

app_cnt=0
cur_cnt=0

# Loop 1 - count the number of applications in list (app_cnt)
# and see if the top app one matches any of the apps in the list
for app_item in $rotation_list
do
app_cnt=$((app_cnt+1))
# Using a variable for as variable name
eval app_cmd=\$$app_item
# find matching number for current music app
if echo $music_on_top | grep `echo $app_cmd | cut -f1 -d"/" | cut -f 3 -d" "`
then
cur_cnt=$app_cnt
fi
if [ $app_cnt = 1 ]
then
app_first=$app_item
fi
done
next=$((cur_cnt+1))	# next app in the list
# If next is past the end then start over with first in the list
if [ $next -gt $app_cnt ]
then
cur_app=$app_first
else
# Loop 2 - find next in list
cur_cnt=0
for app_item in $rotation_list
do
cur_cnt=$((cur_cnt+1))
if [ $next = $cur_cnt ]
then
cur_app=$app_item
fi
done
fi

# Don't rotate too fast, if lock is set then ignore button press
# Otherwise if doing delayed key press it may go to wrong app
if [ -e /data/$key.lock ]
then
# just in case a lock was set and never gets removed
( sleep 4 && rm /data/$key.lock ) &
exit 0
fi
# Set lock waiting to input tap
> /data/$key.lock

# Execute the app variable ( using a variable as a variable name )
eval cmd=\$$cur_app
$cmd
# Execute a key press to start app playing ( if defined )
press="${cur_app}_press"
eval cmd=\$$press
sleep 2
$cmd
rm /data/$key.lock
fi
;;