How to Display a Picture-in-Picture Mode Toggle while using YouTube on Android O
In our last article, we were the first to show you how you can use Android O’s new picture-in-picture (PiP) mode right now on your smartphone. To recap, the method involves sending a certain key known as KEYCODE_WINDOW which is defined by the constant 171. This key code is most readily sent by enabling the hidden navigation bar customizer in SystemUI Tuner then adding a new navigation bar item which triggers the keycode.
However, doing this method means you’ll have a permanent icon in your navigation bar to toggle picture-in-picture mode, even when it’s not applicable. PiP only works on certain applications, namely apps that can display full-screen video content, so it doesn’t make sense for there to be a button to toggle it in all apps. At the end of the article, we stated that we would show you how to display picture-in-picture mode on a per-app basis. Here’s how that’s done.
Display Picture-in-Picture Toggle on a Per-App Basis
Tasker is necessary because it is the automation app we’re using to detect what application we’re in and send commands through the SecureTask plug-in, which is what will handle changing our navigation bar. Once you’ve installed both applications, we need to set both of them up.
In order for Tasker to detect what application we’re in, we need to grant its Accessibility Service. Doing so is very quick, simply go to Settings –> Accessibility and look for “Tasker” on the list of Services. Enable the Accessibility Service.
Next, we need to grant SecureTask the ability to modify system settings on our device. In order to do so, we have to grant SecureTask a special permission known as WRITE_SECURE_SETTINGS which is normally inaccessible to regular applications, but can be granted manually by a user through the use of ADB. Thus, you’ll need to have ADB up and running on your machine to get this working. Fortunately, granting this permission is a one-time thing, and we’ll be using SecureTask in future Android O-related tutorials, so I definitely advise you doing this now. If you’ve followed my previous tutorials where I told you to install AutoTools, SecureTask is less feature-filled but it has just enough for our needs here.
Setting up ADB
The first thing you’ll need to do is download the ADB binary for your particular OS. You can do so here. Once you’ve downloaded them, you need to then make sure you have the proper driver if you’re on Windows.
Once you’ve extracted the binary to a separate folder and have installed the driver, we need to next enable USB Debugging on the smartphone. In order to do so, open up Settings and go to About Phone. Tap on Build Number 7 times until you get a dialog telling you that you’ve unlocked Developer Options. You can access Developer Options in Settings now. Apparently in Android O, you have to enter your pin/password before you can open Developer Options. Do so and look for USB Debugging, then enable it.
Now plug in your phone and open up a command prompt in the same directory where you extracted the ADB binary. (Windows users, hold shift+right-click in that folder and select “open command prompt here.”) Type
adb devices into the command prompt. You’ll see a message that the ADB server is being started, then on your phone you’ll see a prompt asking you to grant your computer ADB access. Accept it. Now when you enter
adb devices into the command prompt, you should see your device’s serial number, if so then you were successful.
Granting WRITE_SECURE_SETTINGS to SecureTask
With an ADB command prompt opened up, enter the following command to grant SecureTask the requisite permission.
adb shell pm grant com.balda.securetask android.permission.WRITE_SECURE_SETTINGS
SecureTask will now have the ability to modify system settings without root access! Now we’re ready to move on to Tasker.
Setting up the Tasker Profile
Open up Tasker and create a new Profile. Name it “Toggle Picture-in-Picture.” Select the Application Context, and look for the video app you want to enable the navigation bar key in (eg. YouTube). Select the apps that you want, then back out to next create a Task.
Tasker will ask you to attach a Task to this new Profile. Create a new Task when asked, and name it (or don’t). When you’re in the Task creation screen, add a new Action by going to Plugin –> SecureTask –> Secure Settings. Press the pencil icon to open up the SecureTask configuration.
Under Action, pick Write. Under Setting, put
secure sysui_nav_bar_right. Under the value, put
key(171:com.android.systemui/2131230944). Exit to Tasker’s main menu. Add an Exit Task to this Profile by long-pressing on the new Task we just created then selecting “add exit task.” Repeat the above Action, but this time for the value put null.
That’s it! When you enter the YouTube app (or whatever other app you chose), you’ll now see an icon pop up in the right side of your navigation bar allowing you to toggle picture-in-picture mode.
Download and Import
As with all Tasker related tutorials, we will be providing the XML file you can download and import. Download the .prf.xml file from AndroidFileHost below and save it to your internal storage. Open up Tasker and long-press on the Profiles tab up top until you see an Import button. Tap on that and look for the XML file you just saved, then select it to import it. Make sure you have Tasker’s Accessibility Service enabled and have granted the WRITE_SECURE_SETTINGS permission to SecureTask as mentioned in my article, otherwise this Profile will not do anything on your phone!Download the “Toggle Picture-in-Picture” Profile from AndroidFileHost
If you’re wondering what else we can accomplish with SecureTask and Android O, stay tuned to the XDA Portal because we have a lot to share. Expect more tutorials on how you can make your navigation bar in Android O accomplish many useful functions!
Want more posts like this delivered to your inbox? Enter your email to be subscribed to our newsletter.