KeyHandler: Populate tri-state-key state on boot

Change-Id: I11e785b5824d581f748e038f292c8846b2a13a0f
This commit is contained in:
dianlujitao
2025-08-08 18:08:15 +08:00
committed by Bruno Martins
parent 23a4f0c429
commit 0b09eeb4b7
2 changed files with 33 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018, 2021-2022 The LineageOS Project
Copyright (C) 2018, 2021-2025 The LineageOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
package="org.lineageos.settings.device">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2023 The LineageOS Project
* Copyright (C) 2021-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
@@ -41,10 +41,18 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
private var wasMuted = false
private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1)
val state = intent.getBooleanExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, false)
if (stream == AudioSystem.STREAM_MUSIC && !state) {
wasMuted = false
when (intent.action) {
AudioManager.STREAM_MUTE_CHANGED_ACTION -> {
val stream = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1)
val state = intent.getBooleanExtra(
AudioManager.EXTRA_STREAM_VOLUME_MUTED, false
)
if (stream == AudioSystem.STREAM_MUSIC && !state) {
wasMuted = false
}
}
Intent.ACTION_BOOT_COMPLETED -> populateKeyState(false)
}
}
}
@@ -52,7 +60,10 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
init {
context.registerReceiver(
broadcastReceiver,
IntentFilter(AudioManager.STREAM_MUTE_CHANGED_ACTION)
IntentFilter().apply {
addAction(AudioManager.STREAM_MUTE_CHANGED_ACTION)
addAction(Intent.ACTION_BOOT_COMPLETED)
}
)
}
@@ -67,15 +78,19 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
return event
}
when (File("/proc/tristatekey/tri_state").readText().trim()) {
"1" -> handleMode(POSITION_TOP)
"2" -> handleMode(POSITION_MIDDLE)
"3" -> handleMode(POSITION_BOTTOM)
}
populateKeyState(true)
return null
}
private fun populateKeyState(vibrate: Boolean) {
when (File("/proc/tristatekey/tri_state").readText().trim()) {
"1" -> handleMode(POSITION_TOP, vibrate)
"2" -> handleMode(POSITION_MIDDLE, vibrate)
"3" -> handleMode(POSITION_BOTTOM, vibrate)
}
}
private fun vibrateIfNeeded(mode: Int) {
when (mode) {
AudioManager.RINGER_MODE_VIBRATE -> vibrator.vibrate(
@@ -89,7 +104,7 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
}
}
private fun handleMode(position: Int) {
private fun handleMode(position: Int, vibrate: Boolean) {
val muteMedia = sharedPreferences.getBoolean(MUTE_MEDIA_WITH_SILENT, false)
val mode = when (position) {
@@ -124,7 +139,10 @@ class KeyHandler(context: Context) : DeviceKeyHandler {
}
}
}
vibrateIfNeeded(mode)
if (vibrate) {
vibrateIfNeeded(mode)
}
}
}