[TUT] Tasker - Hardware Button Control | Screen Off Media Player [BETA]

Search This thread

brandall

Senior Member
May 16, 2008
2,679
5,222
Birmingham
So I was bored tonight and thought I'd try and revive this project.

What I've got is still a work in progress, but it basically involves using Buttonremapper to reassign volume_up and volume_down to fast_forward and rewind, then use Tasker's 'state: media button pressed' to forbid any other apps from responding to those two buttons and have them act out Tasker tasks only.

One button is set to play/pause media (if screen is off) and raise volume (if screen is on) and the other set to skip forward media (if screen is off) and lower volume (if screen is on). The end result is almost full media control from hardware buttons without having to look at the phone or take it out of your pocket: use the volume up and down buttons to skip and pause tracks, or press power to turn the screen on and change their function to volume up or down temporarily before reverting back to skip and pause as the screen times out. You don't need to be looking at the screen while this is happening.

The problems at the moment are:
1) While two buttons have effectively been made into four, there are five necessary functions - volume up, volume down, play/pause, next track, previous track. I figured previous track was the least important but it would be nice to have it as well.
2) Normally the volume buttons in Android are context-sensitive, changing media or system or whatever volume depending on where you are. With this they're always assigned to media volume, so you'd need an app like Audiomanager or something to change the volume of other things.
3) Other problems that I haven't realised yet.

One other thing, Tasker's 'state: media button pressed' lets you set both long presses and short presses, but they don't seem to play nice together. I think both the long press and short press tasks activate and conflict when the button is held down, but I haven't had a look at this yet.

Good work! Would be nice to get this running cleanly on all devices, but I was surprised it didn't have more interest... Plus, my latest Oxygen ROM has long press volume up for skip track up/down and short press for volume up/down, so less of a need for me!

However, would like to get this sorted.

What I couldn't discover, is how the volume buttons are able to perform different actions when the screen was on/off. My hunting got me as far as the two files in this thread, which enable the OJ to function when the screen is off without kernel/ROM changes...

Once the .lib file is applied, the function of the OJ is easily adjusted, within curcial-oj.kl to a media action, I assume...

Anyway, all of that was heading away from a true Tasker project.... I'll check out your download over the weekend. I did previously use a double-click volume press that used current volume (prior to first press) /= original volume (on first press) +/- 1 skipped track and reverted the volume back to the original level! <- clumsy though...
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    WARNING! THIS PROFILE IS IN BETA. DO NOT INSTALL UNTIL YOU HAVE READ BELOW!

    Beta

    When creating this profile I experienced bootloops and splash-screen freezes. If you are not comfortable with how to restore your device from these states, then please wait until it has been thoroughly tested before applying the profiles and tasks.

    Testers

    If there are any of you left after reading the above :eek: I was unable to totally identify what caused the crashes. I hope that through my own testing I have removed the probable causes, but I cannot promise. Read on if you’re still game and post below to let me know you’re in!

    New to Tasker? Download your free trial from here

    Theory

    Have full media control using your device’s hardware buttons when the screen is off. Normal functionality restored when the screen is on.

    Current Shortfalls/Work-in-progress

    Fast reboot required to allow for system changes (15 second reboot).
    - To my knowledge, system changes require this to take effect. Please tell me if you know different? As a consequence, the profiles take about 25 seconds from start to finish to activate and deactivate.
    Hardware keys control media at all times, not just when the screen is off.
    - A revert profile currently takes care of this, so it’s still more than usable.
    Unable to configure hardware keys to only perform actions on long-hold
    - I’m working on this, but I think it may be down to Gingerbread/kernel restrictions?
    Unable to get trackpad click to perform an action
    - Again, Gingerbread/kernel/ROM restrictions?
    Ability to skip music folder commands unknown
    - I’m working on this
    Pop up menu tasks are ugly
    - I’ll sort out a suitable background image and work out how to change the icons.

    Preparation

    This may look like another mammoth, ball-ache of a tutorial, but it’s pretty simple really – make yourself a coffee and by the time you’ve finished it, you’ll be done.

    You must have full root read/write access.

    1) Ensure you are running the latest version of busybox free from the Market. Even if your current ROM comes with it preinstalled, the Market version allows for this and will update it according. This is essential, as a probable cause of the bootloops I experienced. Reboot after updating it.
    2) Install the Locale Execute Plugin free from the Market
    3) Download Button Saviour Root free from the Market. We will be messing with the hardware keys, so if you lose control of them, this application will save you by displaying them on the screen. Have a play with this application and make sure you are fully versed with using it just in case.
    4) Nandroid backup after you’ve done the above so you don’t have to reinstall everything again should things go wrong {We will be mounting the system for full write access}.

    Installing and Customisation

    Basically, we will be creating a file with the media controls in and asking Tasker to overwrite the existing file when we want to use it – simple.

    Using a file explorer, create a folder within the sdcard/Tasker folder called ‘mediacon’. Within this folder, create another called ‘orig’. Navigate to

    Code:
    system/usr/keylayout/

    Take a copy of the file bravo-keypad.kl (bravo will be replaced with your device name (unless you have a bravo!)). Paste it in the ‘mediacon’ folder you created. Paste another copy in the ‘orig’ folder. Open the file in the mediacon folder with a text editor on your phone (or alternatively copy/push it to your pc and view it there).

    From the .zip file below, view the bravo-keypad.kl file on your pc using an application such as Notepad ++ (download free from here) or a simple text editor (although the layout may be a little confusing).

    You’ll need to compare the two and see what the differences are. The most important additions to the bravo-keypad.kl to enable the media actions on the hardware keys are:

    Code:
    key 139   MENU                 WAKE_DROPPED
    key 158   BACK
    key 217   SEARCH

    To:

    Code:
    key 139   MEDIA_PLAY_PAUSE        WAKE
    key 158   MEDIA_PREVIOUS            WAKE
    key 217   MEDIA_NEXT                  WAKE

    The hardware key numbers and the remaining content of the file will no doubt be different from device to device, I assume it will also be different betweens ROMs and user settings too. You will need to preserve the remainder of the file.

    Please can I ask you to post the contents of this file in ‘code’ tags if you can’t see that someone has done this for your device already. Could you also detail your ROM and Kernel. It will help others to quickly edit the file with the correct settings without scratching their heads too much – community spirit and all of that.

    As you can see all we are doing is changing the three hardware buttons (menu, back and search) to perform media controls instead. You’ll need to edit the file in the ‘mediacon’ folder (either on your device or drag/push to your pc and then back again) to reflect this. Make sure you do not change the file in the ‘orig’ folder! Remember, your device may have different key numbers for the hardware buttons, this won’t matter, just edit the ones listed as menu, back and search and not the ones that have a # before them.

    So, now you have your edited file done and your original file stored safely. We’re nearly there!

    Install the profiles and tasks and make sure they are unchecked until you have edited them for your needs. The Tasker Media Control V1.zip folder contains the following two profiles:

    MediaBoot
    MediaControl


    It contains the following 8 tasks:

    ResetMedVar
    MediaMenu
    MediaContBoot
    MediaContOff
    MediaContOn
    FastReboot
    FastBootConfirm
    ContState


    The triggers for the tasks are:

    Device Boot
    Headset Plugged


    If you currently have any other profiles that load music apps or perform other tasks when the headset is connected, please disable these.

    Firstly, open the task MediaContOn. Click on action 4 (execute action) and select edit. You’ll see two mentions of the bravo-keypal.kl. Replace ‘bravo’ with your device name, or alternatively with whatever the original file was named in system/usr/keylayout/ (I assume it is your device name?). You’ll need to press the back button once you’re done.

    Secondly, do exactly the same in the task MediaContOff.

    Finally, open the task MediaContBoot and alter action number 2 to the music application you use (if you don’t use the stock android player).

    That’s it! You’re done!

    Terminal Command Information

    There will be a number of terminal commands running, to copy and overwrite files which will mount the /system as read/write. This of course always has its risks, so ensure you have that nandroid backup just in case….

    In the task FastReboot, there is the terminal command

    Code:
    Killall system_server

    This command is not without its risks too, but it is the only way to avoid a full reboot in order to apply the system changes.

    Running the task for the first time.

    In the profiles and tasks, two variables are created:

    %EARP – This is set to 2 when the headset is connected and 1 when it is disconnected. By removing the headset (%EARP =1), you will trigger the exit task which will begin to revert the media button changes.

    %MEDIACONT – It was necessary to create this variable to avoid Tasker reacting to the headset plugged trigger after the reboot and wanting to start copying the files all over again. If %MEDIACONT = 2, Tasker will therefore know you are already controlling the hardware buttons and it will prevent the headset profile running again. The variable is changed to a value of 1, once the button controls are returned to normal. Simple hey…

    When testing, sometimes these variables were showing the wrong value, due to the options to ‘cancel’ at various stages of the tasks. Before you connect your headset and run the profiles for the very first time only, you will need to open the task ResetMedVar and click on ‘test’. This will set both values to 1. At any point, if your variables go out of sync, you can repeat this exercise so the headset connected/disconnected will trigger the profile/exit task accordingly.

    So, open ResetMedVar and click test to set the variables to 1. Tick the MediaBoot and MediaControl profiles to activate them and apply to exit Tasker. Insert your headset!

    You’ll be prompted with the first pop-up asking if you want to active the media controls. Click to activate (it will disappear in ten seconds and cancel otherwise). Shortly after, a confirmation pop-up will confirm the files have been copied correctly. Shortly after that, another pop up will confirm you wish to proceed with the fast reboot. Click to activate and you’re device will reload the system. It should take around 15-20 seconds.

    When the device boots back up, the Button Saviour application will be loaded, to ensure you have button control if things go wrong. Pressing the little arrow on the right hand side of the screen will enable a back button that you can press to back out of the screen. You media player should then load. Choose a happy tune (from a folder/album/artist that has more than one song) (not forgetting you can’t use the back button other than on the screen!) and turn the screen off.

    Hopefully, search will skip forward a track, back will skip back a track and menu will play/pause the playback. WHOOP!

    When you’ve finished testing, switch the screen back on, unlock it if necessary and then remove the headset. Tasker will then perform the above steps again, replace the files back to the original ones before confirming the fast reboot.

    15-20 seconds later, hopefully all of your hardware buttons will then perform as normal!

    Job done.

    Extras

    I couldn’t get the trackpad/optical joystick to perform an action on my device. There is an entry at the very end of MediaContOn and MediaContOff after the STOP. These entries copy and reversely delete a curcial-oj.kl file to /system/usr/keylayout/ that is supposed to enable the trackpad to work. Drag these to position 5 if you’d like to test them on your device to see if they work. Let me know!

    Credits
    localh0st for the curcial-oj.kl file here
    Decad3nce for his key remapping guide here
    The mass of other threads I read through on XDA to gain the knowledge to do this and the devs of the free apps mentioned above - I'm sure they'd appreciate a donate.
    Oh and of course the dev of Tasker! What an application.... (I'm not on commission I promise!)
    1
    FAQ

    Q) I have many other Tasker questions, can I post them here??
    A) To avoid clutter, probably best to use this official Google group

    Q) Where can I learn more about Tasker?
    A) The manual and Profiles and step-throughs on the Tasker wiki and you’ll be up to speed in no time.

    Change Log

    Confirming pop up tasks may be removed in V2.

    Bugs

    Let me know...

    Future Changes/Requests

    Find another way to apply system changes without the need of fast reboot.
    Hardware keys to only control media when the screen is off.
    Configure hardware keys to only perform actions on long-hold to avoid 'pocket-presses'.
    Get trackpad/optical joystick click to perform an action
    Enable the ability to skip music folders and/or directories.
    Pop up menu tasks need sprucing up.
    1
    Installation Instructions

    In menu options/profile data 'Import One Task':

    ResetMedVar
    MediaMenu
    MediaContBoot
    MediaContOff
    MediaContOn
    FastReboot
    FastBootConfirm
    ContState


    In menu options/profile data 'Import One Profile':

    MediaBoot
    MediaControl


    The thanks meter lets me know I'm appreciated! :eek:
    1
    Heh, I'd love to be beta testing your ideas!

    Looks like you might be alone there!!
    1
    Just got a reply from the developer: "in its data directory in the subdirectory files: /data/data/com.beansoft.buttonremapper/files."
    So here you go. :)