[Android Q&A, Help & Troubleshooting] Fragmentation:
With the latest software update of my Sony Z5P I faced a really annoying issue, which essentially prevents me from listening to the music with the device. Symptoms are as following: during headphones connect or disconnect, or even slight movement of the jack in the audio port, volume randomly goes all way up or down and, the most annoyingly, music pauses and Voice Search is activated. Very frustrating.

I searched for the possible fixes in the Internet and found that there are dozens of posts on various web resources, regarding exactly the same issue. The issue exists on a very broad range of phones, tablets and Android versions. There are a lot of solutions too, generally "Try to install app X" or even "disable Google Search", but nothing worked for me (and probably for majority of users). And I didn't wanted to disable Voice Search, because it's a useful feature.

Looking into the problem further, I discovered that the issue is caused by the wrong detection of a headphones by Android. The system decides that it's not headphones, but a headset, that is the headphones with a mic and hardware buttons to control audio. The system even places an icon to the upper bar and there is a "Headset is plugged in" notification in the drawer.

Due to a hardware difference between connectors of headphones and a headset, every time it is slightly moved in port generates random electric signals which Android mistakenly recognizes as commands from a hardware remote control. If you are listening to the music while walking or especially running, the chances are pretty high, that connector is moving slightly. And the music become constantly interrupted by Voice Assistant or its volume is being decreased or increased. That's really, really disturbing.

One particular useful post which I found was this one. From it I learned there is a /system/usr/keylayout directory in Android filesystem, with files handling hardware buttons. Other useful insight is a link to the small Chris Boyle's app for keypresses debug (he even made a blog post about it). With this knowledge and this small useful tool I started to dig into my phone's system.

Using Key Test app mentioned above I figured out that any slight jack movement generates keypresses with scan codes 226 and 582 (HEADSETHOOK and VOICE_ASSIST) and occasionally 114 and 115 (VOLUME_DOWN and VOLUME_UP). That's what I should find in the system.

At first I should state that to make my solution work, you will need a rooted phone. All dirty hacks need root privileges. For Sony Z5 Premium rooting I assembled a dedicated guide, although it should work with most recent Sony devices. If you have a phone from some other OEM, please search this forum, there are plenty of rooting guides.

Okay, you have a topic problem, your phone is rooted and your adb works. That's how I fixed my phone.

At first you should check contents of your phone's /system/usr/keylayout directory and inspect individual files. It's easier to do on a computer, so copy this folder (maybe with some file manager like ES File Explorer) to your internal or external storage (for example to /sdcard) and then transfer by MTP to the computer (in Windows use File Explorer, the directory is in the This PC\Xperia Z5 Premium Dual folder or something like this).

Once you have keylayout directory on your computer, use some text editor tool which can search multiple text files for a particular text (I use Notepad++, but if you are on a Unix-like OS, you may use grep as well). You should search you files for this text strings:

HEADSETHOOK

and

VOICE_ASSIST

On the latest Z5 Premium firmware (Android 6.0.1, build .224) these particular strings were found in the Generic.kl and msm8994-tomtom-snd-card_Button_Jack.kl files. While the former is a generic keymapping file of all Android devices, the latter is specific to Z5P, because of it's name, which suggests that it is somehow related to MSM 8994, which is a partnumber of Qualcomm Snapdragon 810 SoC. In fact, this file is obviously handles headset keypresses, and its contents are:

key 226 HEADSETHOOK
key 115 VOLUME_UP
key 114 VOLUME_DOWN
key 582 VOICE_ASSIST
key 260 VENDOR_1

Your phone may have file with some other name but contents should be similar. What worked in my case is a changing the codes in this file to some different values which are not used elsewhere. I changed mine to this:

key 995 HEADSETHOOK
key 996 VOLUME_UP
key 997 VOLUME_DOWN
key 998 VOICE_ASSIST
key 999 VENDOR_1

And that's how I placed modified msm8994-tomtom-snd-card_Button_Jack.kl back into its original place in the phone. This is particularly tricky and you absolutely need rooted phone to do this.

First, place the file into the phone's memory with a guaranteed write access. /sdcard usually is a such place:

adb.exe push msm8994-tomtom-snd-card_Button_Jack.kl /sdcard/

Then login to the phone's shell and become superuser (root):

adb.exe shell
shell@E6883:/ $ su
root@E6883:/ #

After that make /system mounted as a read/write filesystem:

root@E6883:/ # mount -o remount,rw /system

And copy the modified file back in place. After that reboot:

root@E6883:/ # cp /sdcard/msm8994-tomtom-snd-card_Button_Jack.kl /system/usr/keylayout/
root@E6883:/ # reboot

Once the file was updated and the phone rebooted, I checked keypresses with Key Test again. These keypresses were generated as before, with the same scan codes, but this time key codes were registered as KEYCODE_UNKNOWN and did not any harm. The system cannot recognize them and doesn't react to them in any sensible way.

That's it. Be informed, that if you plan to use a real headset, with this hack its keys will not work at all.