"I've made an app which *somewhat* fixes PoGO's audio dampening"
#PokemonGO: Hi fellow travelers,motivated by this reddit post, I've pieced together a small Android app which somewhat fixes Pokemon GO repeatedly causing other audio playback to lower its volume. You can find the source on GitHub and either build it yourself or download the APK directly.Since the solution looks a bit ugly from a developer's point of view but seems to work in practice, I decided to write a bit about the creation process.Identifying the problemIn order to handle audio output from multiple sources, Android uses something called Audio focus. The basic idea is that if your app has audio focus, it's the dude with the mic while all others can only scream. However, it doesn't work like that (at least not until Android O). It's more like Android gives out microphones and tells everybody without audio focus to shut up or at least tone it down. What's the difference? Well, it means that the volume of apps without audio focus is not controlled globally by the system but rather by each app itself making it harder to crank it back up from the outside.Take Spotify as an example. Every time Pokemon GO switches into the foreground, it requests audio focus and gets it. Android then tells all other apps to lower their playback volume. Being a good audio playback citizen, Spotify lowers its own volume by about 70% (completely made-up number). From the outside, though, the system/music volume has not changed so that we can neither detect that a change in volume has happened nor raise the volume ourselves.The solutionAs an IT guy, the solution is obvious.Have you tried turning it off and on again?Indeed, pausing and unpausing the playback makes Spotify (or any other decently coded audio playback app), request audio focus so that it restores the initial volume. For the average user, this can be quite aggrevating because manually hitting pause&play interupts the playback. However, if you do it programatically, then there is only like a 1-2 millisecond break which one doesn't really notice. So we just have to automate it somehow.PermissionsOur roadmap seems clear: Have an app running in the background which notices when Pokemon GO switches to the foreground, checks if music is running at that time and if that's the case, pause/unpauses this playback. Sounds easy enough but this is where Android's permission system gets tricky:Keeping tracks of app usageIn a hacky way, this can be done by periodically looking at app usage stats. For an app to be able to acces those, it needs the permission android.permission.PACKAGE_USAGE_STATS. Looking at the Android API, we see that it is considered a dangerous permission and that the user has to use the device settings to specifically grant this permission to our app.Allows an application to collect component usage statistics Declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.Pausing/Unpausing playback and keeping track of media sessionsThis is where it gets funny/confusing. There is the permission android.permission.MEDIA_CONTENT_CONTROL which would in theory allow our app to control media playback. Again, looking at the Android API, we see that this permission can't be granted to third-party apps.Allows an application to know what content is playing and control its playback. Not for use by third-party applications due to privacy of media consumptionThere is, however, a work-around: If our app has an active NotificationListenerService, we are allowed to use all the nice functions to monitor and control media playback. Don't ask me why, I have no freaking clue. Nevertheless, of course you need the user's permission to have a NotificationListenerService running in your app. And it's again one which the user has to grant specifically by using the device's settings.Why open source?Since the solution requires two intrusive permissions to be granted by the user, I wouldn't feel quite comfortable releasing this app without allowing every user to monitor its source code. This is why I published it on GitHub and everybody who's interested can copy/use the code as they see fit.Expected issuesAs I have quite hastily thrown this app together and only tested it on one device with 2 music players, some issues may turn up:Pause/unpause will be noticed if device is laggingBattery/CPU usage may increase (I did not have time for long-time testing) because the app needs to check whether Pokemon GO is in the foreground every few seconds. How often this is done, can be changed inside the app.If your device is running low on resources (CPU/memory), Android starts killing apps and it especially loves those background services. So the app may get killed by Android while playing.Pokemon GO only requests audio focus several seconds after it was launched. If the app was already running then, it may not register the change. You may need to switch out and back in the game.TL;DRThe app monitors if Pokemon GO switches to the foreground by looking at Android's app usage statistics (this is important to note if you worry about getting (shadow-)banned: it's not interacting with the game directly!). Each time this happens, it checks for currently playing media sessions and pauses/unpauses them in such quick succession that it should not be noticeable by the user. Due to Android's permission system, the app needs two permissions: access to notifications (grants ability to alter media playback) and access to usages statistics. I did not have much time to test its long-term behavior so some issues may arise.EDITS: - fixed a silly bug which made the app crash on startup (also adjusted link to new apk) - v1.02: added option to generate/send debugging logs if exception occurs via /r/TheSilphRoad http://ift.tt/2vcaSiu
"I've made an app which *somewhat* fixes PoGO's audio dampening"
Reviewed by The Pokémonger
on
06:57
Rating:
No comments