From 1bbbc011be7a7c19299b260936c03ef4b0926112 Mon Sep 17 00:00:00 2001 From: Chatur27 Date: Fri, 11 Mar 2022 14:52:02 +0000 Subject: [PATCH] universal7885: parts: Use audio hw parameter for dolby effect --- .../apps/SamsungParts/res/values/arrays.xml | 7 +- .../samsungextras/dolby/DolbyCore.kt | 65 ++++++++++--------- .../samsungextras/dolby/DolbyFragment.kt | 34 +++++----- .../samsungextras/dolby/DolbyTile.kt | 22 +++---- 4 files changed, 59 insertions(+), 69 deletions(-) diff --git a/universal7885-common/apps/SamsungParts/res/values/arrays.xml b/universal7885-common/apps/SamsungParts/res/values/arrays.xml index c9d2ac8..bd6594e 100644 --- a/universal7885-common/apps/SamsungParts/res/values/arrays.xml +++ b/universal7885-common/apps/SamsungParts/res/values/arrays.xml @@ -10,13 +10,8 @@ @string/dolby_profile_auto - @string/dolby_profile_game - @string/dolby_profile_game_1 - @string/dolby_profile_game_2 @string/dolby_profile_movie @string/dolby_profile_music - @string/dolby_profile_off - @string/dolby_profile_special_audio @string/dolby_profile_voice - \ No newline at end of file + diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyCore.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyCore.kt index 404e63a..3eaf8f5 100644 --- a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyCore.kt +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyCore.kt @@ -2,46 +2,51 @@ package com.eurekateam.samsungextras.dolby import android.media.audiofx.AudioEffect import java.util.* +import android.content.Context +import android.media.AudioManager +import android.os.IBinder +import android.content.Intent +import android.app.Service -class DolbyCore { - private var currentProfile : Int = -1 - private val audioEffect = AudioEffect( - EFFECT_TYPE_DAP, AudioEffect.EFFECT_TYPE_NULL, 0, 0 - ) - fun startDolbyEffect(PROFILE: Int){ - audioEffect.setParameter(EFFECT_PARAM_EFF_ENAB, 1) - audioEffect.setParameter(EFFECT_PARAM_PROFILE, PROFILE) - currentProfile = PROFILE - audioEffect.enabled = true +class DolbyCore : Service() { + override fun onBind(intent: Intent?): IBinder? { + return null } - fun stopDolbyEffect(){ - audioEffect.enabled = false - } - fun isRunning (): Boolean { - return audioEffect.enabled - } - fun justStartOnly(){ - audioEffect.enabled = true - } - - fun currentState(): Int{ - return currentProfile + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent != null){ + if (intent.getBooleanExtra(DAP_ENABLED, false)){ + val PROFILE = intent.getIntExtra(DAP_PROFILE, PROFILE_AUTO) + mAudioEffect.setParameter(EFFECT_PARAM_EFF_ENAB, 1) + mAudioEffect.setParameter(EFFECT_PARAM_PROFILE, PROFILE) + mCurrentProfile = PROFILE + mAudioEffect.enabled = true + (getSystemService(Context.AUDIO_SERVICE) as AudioManager). + setParameters("${HW_DOLBY_ENABLE}1;${HW_DOLBY_PROFILE}${PROFILE}") + } else { + mAudioEffect.setParameter(EFFECT_PARAM_EFF_ENAB, 0) + mAudioEffect.enabled = false + (getSystemService(Context.AUDIO_SERVICE) as AudioManager). + setParameters("${HW_DOLBY_ENABLE}0;${HW_DOLBY_PROFILE}" + + mCurrentProfile.toString()) + } + } + return START_NOT_STICKY } + companion object { - private val EFFECT_TYPE_DAP = UUID.fromString("46d279d9-9be7-453d-9d7c-ef937f675587") private const val EFFECT_PARAM_PROFILE = 0 private const val EFFECT_PARAM_EFF_ENAB = 19 - + const val DAP_ENABLED = "dap_enabled" + const val DAP_PROFILE = "dap_profile" const val PROFILE_AUTO = 0 const val PROFILE_MOVIE = 1 const val PROFILE_MUSIC = 2 const val PROFILE_VOICE = 3 - const val PROFILE_GAME = 4 - const val PROFILE_OFF = 5 - const val PROFILE_GAME_1 = 6 - const val PROFILE_GAME_2 = 7 - const val PROFILE_SPACIAL_AUDIO = 8 + private const val HW_DOLBY_ENABLE = "g_effect_dolby_enable=" + private const val HW_DOLBY_PROFILE = "g_effect_dolby_profile=" + val mAudioEffect = AudioEffect(EFFECT_TYPE_DAP, AudioEffect.EFFECT_TYPE_NULL, 0, 0) + var mCurrentProfile = PROFILE_AUTO } -} \ No newline at end of file +} diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyFragment.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyFragment.kt index 99bce5c..657f698 100644 --- a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyFragment.kt +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyFragment.kt @@ -21,48 +21,44 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import com.eurekateam.samsungextras.R +import android.content.Intent class DolbyFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener { private var DolbyModesPreference: ListPreference? = null private var DolbyEnablePreference: SwitchPreference? = null - private var dolbyCore = DolbyCore() + private lateinit var mIntent : Intent override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.dolby_settings) DolbyModesPreference = findPreference(DOLBY_MODES) DolbyEnablePreference = findPreference(PREF_DOLBY) - DolbyEnablePreference!!.isChecked = dolbyCore.isRunning() + DolbyEnablePreference!!.isChecked = DolbyCore.mAudioEffect.enabled DolbyEnablePreference!!.onPreferenceChangeListener = this val items = arrayOf( - "1", "2", "3", "4", "5", "6", "7", "8", "9" + "0", "1", "2", "3" ) + mIntent = Intent(requireContext(), DolbyCore::class.java) DolbyModesPreference!!.entryValues = items DolbyModesPreference!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> when ((newValue as String).toInt()) { - 1 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_AUTO) - 2 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_GAME) - 3 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_GAME_1) - 4 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_GAME_2) - 5 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_MOVIE) - 6 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_MUSIC) - 7 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_OFF) - 8 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_VOICE) - 9 -> dolbyCore.startDolbyEffect(DolbyCore.PROFILE_SPACIAL_AUDIO) + 0 -> mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.PROFILE_AUTO) + 1 -> mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.PROFILE_MOVIE) + 2 -> mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.PROFILE_MUSIC) + 3 -> mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.PROFILE_VOICE) else -> { } } + mIntent.putExtra(DolbyCore.DAP_ENABLED, true) + requireContext().startService(mIntent) true } } override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { - if (preference === DolbyEnablePreference) { + if (preference == DolbyEnablePreference) { val value = newValue as Boolean - if (value) { - dolbyCore.justStartOnly() - } else { - dolbyCore.stopDolbyEffect() - } + mIntent.putExtra(DolbyCore.DAP_ENABLED, value) + requireContext().startService(mIntent) return true } return false @@ -72,4 +68,4 @@ class DolbyFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeL private const val PREF_DOLBY = "dolby_pref" private const val DOLBY_MODES = "dolby_modes" } -} \ No newline at end of file +} diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyTile.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyTile.kt index af21c72..23439f7 100644 --- a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyTile.kt +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyTile.kt @@ -2,30 +2,24 @@ package com.eurekateam.samsungextras.dolby import android.service.quicksettings.Tile import android.service.quicksettings.TileService +import android.content.Intent class DolbyTile : TileService() { - private var isRunning = false - private var dolbyCore = DolbyCore() - override fun onStartListening() { - super.onStartListening() - isRunning = dolbyCore.isRunning() - updateTile() - } - override fun onClick() { - if (isRunning) { - dolbyCore.stopDolbyEffect() + val mIntent = Intent(this, DolbyCore::class.java) + if (DolbyCore.mAudioEffect.enabled) { + mIntent.putExtra(DolbyCore.DAP_ENABLED, false) } else { - dolbyCore.justStartOnly() + mIntent.putExtra(DolbyCore.DAP_ENABLED, true) + mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.mCurrentProfile) } - isRunning = !isRunning updateTile() } private fun updateTile() { val tile = qsTile tile.state = - if (isRunning) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE + if (DolbyCore.mAudioEffect.enabled) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE tile.updateTile() } -} \ No newline at end of file +}