PhoneProfilesPlus icon indicating copy to clipboard operation
PhoneProfilesPlus copied to clipboard

Play Media or Shell command option

Open jtothe4n opened this issue 7 years ago • 1 comments

Hey,

what I do every morning when I get to work and in the evening back home, is turning my BT headset on and listen to music.

Currently it is automated that way, that PhoneProfiles opens my music player when my BT headset connects. What would be very convenient now would be if there is a way to start the music player. That could be done via a new Event that checks the current foreground app and then starts the "Play Media" Profile..probably needs a little delay after the app is in foreground, of you play music from the cloud, the app needs sometimes a little to load the music.

Currently there is no "Play media" Option. For non rooted users there are two ways to do that:

via a sendOrderedBroadcast or via notifcation service access. The one via notification service access works for the most of the media players...just know of one far where it doesn't work: PowerAMP. From my experience for non rooted users the one with notification service access works the best and for nearly very media player. The ordered broadcast one works for some, but a lot did not register to that broadcast.

If you are rooted the 100% working method is to just send a keyvent via su shell. Code example from my app:

` public static void sendMediaKeyEvent(Context context, int code){

    if(Helper.hasNotificationAccess(context) && !getSharedPreferences(context).getBoolean(FragmentMain.PREF_ALTERNATIVE_METHOD, false)) {

        if(mManager == null)
            mManager = (MediaSessionManager)context.getSystemService(Context.MEDIA_SESSION_SERVICE);

        boolean executed = false;

        for (MediaController controller : mManager.getActiveSessions(new ComponentName(context, NotificationListener.class))) {
            PlaybackState playbackState = controller.getPlaybackState();
            int state = playbackState.getState();
            if (state == PlaybackState.STATE_PLAYING || state == PlaybackState.STATE_PAUSED)
            {

                if(code == KeyEvent.KEYCODE_MEDIA_NEXT)
                    controller.getTransportControls().skipToNext();
                else if(code == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
                    controller.getTransportControls().skipToPrevious();
                else if(code == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)
                {
                    if(state == PlaybackState.STATE_PLAYING)
                        controller.getTransportControls().pause();
                    else
                        controller.getTransportControls().play();
                }

                executed = true;
                break;
            }
        }

        if(!executed)
            sendMediaOrderedBroadcast(context, code);
    }
    else
        sendMediaOrderedBroadcast(context, code);


}

private static void sendMediaOrderedBroadcast(Context context, int code) {

    if(Helper.isRootEnabled(context))
    {
        ShellInterface.runSU("input keyevent " + code);
        return;
    }

    long uptimeMillis = SystemClock.uptimeMillis();

    Intent intent = new Intent("android.intent.action.MEDIA_BUTTON", null);
    intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(uptimeMillis, uptimeMillis, KeyEvent.ACTION_DOWN, code, 0));
    context.sendOrderedBroadcast(intent, null);

    intent = new Intent("android.intent.action.MEDIA_BUTTON", null);
    intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(uptimeMillis, uptimeMillis, KeyEvent.ACTION_UP, code, 0));
    context.sendOrderedBroadcast(intent, null);
}`

jtothe4n avatar Feb 12 '18 05:02 jtothe4n

..oh and a shell command option for rooted users would be cool too..that way people can execute own commands like they wish.

jtothe4n avatar Feb 12 '18 05:02 jtothe4n