Improved handling of volume buttons

When the actions volume up / volume down are bound to the volume up
key resp. volume down key, key presses are now completely ignored
allowing other apps to bind something to those keys
(this is not possible on stock Android, but with some custom ROMs
or root).
This commit is contained in:
Josia Pietsch 2024-11-03 16:09:48 +01:00
parent 3c1436e759
commit 5b00c4b6bb
Signed by: jrpie
GPG key ID: E70B571D66986A2D

View file

@ -13,6 +13,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.isVisible
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.actions.LauncherAction
import de.jrpie.android.launcher.databinding.HomeBinding
@ -186,8 +187,21 @@ class HomeActivity : UIObject, AppCompatActivity(),
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> LauncherAction.CHOOSE.launch(this)
KeyEvent.KEYCODE_VOLUME_UP -> Gesture.VOLUME_UP(this)
KeyEvent.KEYCODE_VOLUME_DOWN -> Gesture.VOLUME_DOWN(this)
KeyEvent.KEYCODE_VOLUME_UP -> {
if (Action.forGesture(Gesture.VOLUME_UP) == LauncherAction.VOLUME_UP) {
// Let the OS handle the key event. This works better with some custom ROMs
// and apps like Samsung Sound Assistant.
return false
}
Gesture.VOLUME_UP(this)
}
KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (Action.forGesture(Gesture.VOLUME_DOWN) == LauncherAction.VOLUME_DOWN) {
// see above
return false
}
Gesture.VOLUME_DOWN(this)
}
}
return true
}