From ad27f7c8f6d19600deac9951c732d7ae14b43ed3 Mon Sep 17 00:00:00 2001 From: roynatech2544 Date: Tue, 22 Mar 2022 21:59:09 +0900 Subject: [PATCH] universal7885: Merge LineageOS SamsungDAP Change-Id: Ic8faa1b8b2c23ca1cd1add256c2229efef36cfd1 Signed-off-by: roynatech2544 --- .../apps/SamsungParts/AndroidManifest.xml | 49 ++++---- .../apps/SamsungParts/res/values/strings.xml | 26 +++-- .../SamsungParts/res/xml/dolby_settings.xml | 58 +++++++--- .../samsungextras/dolby/DolbyActivity.kt | 34 ++---- .../samsungextras/dolby/DolbyCore.kt | 109 ++++++++++-------- .../samsungextras/dolby/DolbyFragment.kt | 89 +++++++------- .../dolby/DolbySearchIndexablesProvider.kt | 74 ++++++++++++ .../samsungextras/dolby/DolbyTile.kt | 51 +++++--- 8 files changed, 323 insertions(+), 167 deletions(-) create mode 100644 universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbySearchIndexablesProvider.kt diff --git a/universal7885-common/apps/SamsungParts/AndroidManifest.xml b/universal7885-common/apps/SamsungParts/AndroidManifest.xml index 5f6421f..b495006 100644 --- a/universal7885-common/apps/SamsungParts/AndroidManifest.xml +++ b/universal7885-common/apps/SamsungParts/AndroidManifest.xml @@ -17,7 +17,7 @@ android:theme="@style/Theme.SubSettingsBase" android:fullBackupOnly="true"> @@ -45,7 +45,7 @@ android:resource="@drawable/ic_device" /> @@ -54,7 +54,7 @@ @@ -63,7 +63,7 @@ @@ -71,8 +71,32 @@ + + + + + + + + + + + @@ -81,25 +105,10 @@ - - - - - - - Run this feature once or twice if you find that your speaker is lightly blocked by dust. Set media volume to maximum.\n\nIf the speaker is blocked heavily, run this feature 2–5 times while shaking your device with the speaker facing downwards. - Dolby Atmos Enable the Dolby Atmos audio effect - Enable Dolby audio, just like in OneUI. - Available Dolby Modes - Choose one of these + + + Dolby Atmos + + + Enable Dolby Atmos + + + Experience breakthrough audio for media playback that flows above and around you + + Auto Profile - Movie Profile - Music Profile - Voice Profile Game Profile - No Profile Game 1 Profile Game 2 Profile - Special Audio Profile + Movie Profile + Music Profile + No Profile + Spacial Audio Profile + Voice Profile + Eureka Kernel Features diff --git a/universal7885-common/apps/SamsungParts/res/xml/dolby_settings.xml b/universal7885-common/apps/SamsungParts/res/xml/dolby_settings.xml index 6e604be..a494eb6 100644 --- a/universal7885-common/apps/SamsungParts/res/xml/dolby_settings.xml +++ b/universal7885-common/apps/SamsungParts/res/xml/dolby_settings.xml @@ -3,22 +3,50 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:title="@string/dolby_title"> - + - + - + + + + + + + + + + + + + + + + + diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyActivity.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyActivity.kt index 1d3d3a3..28ac258 100644 --- a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyActivity.kt +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbyActivity.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Paranoid Android + * Copyright (C) 2022 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. @@ -13,32 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.eurekateam.samsungextras.dolby import android.os.Bundle -import android.view.MenuItem -import androidx.fragment.app.FragmentActivity -import com.android.internal.R.id.content -import com.android.internal.R.id.home -class DolbyActivity : FragmentActivity() { - override fun onCreate(savedInstanceState: Bundle?) { +import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity +import com.android.settingslib.collapsingtoolbar.R + +class DolbyActivity : CollapsingToolbarBaseActivity() { + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val fragment = this.supportFragmentManager.findFragmentById(content) - val dolbyFragment: DolbyFragment - if (fragment == null) { - dolbyFragment = DolbyFragment() - this.supportFragmentManager.beginTransaction() - .add(content, dolbyFragment) - .commit() - } - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (item.itemId == home) { - finish() - return true - } - return super.onOptionsItemSelected(item) + fragmentManager.beginTransaction().replace( + R.id.content_frame, + DolbyFragment() + ).commit() } } 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 3eaf8f5..45f93bd 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 @@ -1,52 +1,71 @@ +/* + * Copyright (C) 2022 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + 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 +import android.media.audiofx.AudioEffect -class DolbyCore : Service() { - override fun onBind(intent: Intent?): IBinder? { - return null - } - 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") +import com.eurekateam.samsungextras.dolby.DolbyFragment.Companion.PREF_DOLBY_MODES - 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 - 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 +import java.util.UUID + +object DolbyCore { + private const val EFFECT_PARAM_PROFILE = 0 + private const val EFFECT_PARAM_EFF_ENAB = 19 + + private val EFFECT_TYPE_DAP = UUID.fromString("46d279d9-9be7-453d-9d7c-ef937f675587") + + 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 val audioEffect = runCatching { + AudioEffect(EFFECT_TYPE_DAP, AudioEffect.EFFECT_TYPE_NULL, 0, 0) + }.getOrNull() + + fun getProfile(): Int { + val out = intArrayOf(PROFILE_AUTO) + audioEffect?.getParameter(EFFECT_PARAM_PROFILE, out) + return out.first() } + + fun getProfileName(context: Context): String { + val profile = getProfile() + val resourceName = PREF_DOLBY_MODES.filter { it.value == profile }.keys.first() + + return context.resources.getString(context.resources.getIdentifier( + resourceName, "string", context.packageName + )) + } + + fun setProfile(profile: Int) { + audioEffect?.setParameter(EFFECT_PARAM_EFF_ENAB, 1) + audioEffect?.setParameter(EFFECT_PARAM_PROFILE, profile) + } + + fun setEnabled(enabled: Boolean) { + audioEffect?.enabled = enabled + } + + fun isEnabled() = audioEffect?.enabled ?: false } 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 657f698..261cb66 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 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Paranoid Android + * Copyright (C) 2022 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. @@ -13,59 +13,66 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.eurekateam.samsungextras.dolby import android.os.Bundle -import androidx.preference.ListPreference -import androidx.preference.Preference -import androidx.preference.PreferenceFragmentCompat -import androidx.preference.SwitchPreference -import com.eurekateam.samsungextras.R -import android.content.Intent +import android.widget.Switch + +import androidx.preference.PreferenceFragment + +import com.android.settingslib.widget.MainSwitchPreference +import com.android.settingslib.widget.OnMainSwitchChangeListener +import com.android.settingslib.widget.RadioButtonPreference + +import com.eurekateam.samsungextras.dolby.R + +class DolbyFragment : PreferenceFragment(), OnMainSwitchChangeListener { + + private lateinit var switchBar: MainSwitchPreference -class DolbyFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener { - private var DolbyModesPreference: ListPreference? = null - private var DolbyEnablePreference: SwitchPreference? = null - 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.mAudioEffect.enabled - DolbyEnablePreference!!.onPreferenceChangeListener = this - val items = arrayOf( - "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()) { - 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) + + switchBar = findPreference(PREF_DOLBY_ENABLE)!! + switchBar.addOnSwitchChangeListener(this) + switchBar.isChecked = DolbyCore.isEnabled() + + for ((key, value) in PREF_DOLBY_MODES) { + val preference = findPreference(key)!! + preference.setOnPreferenceClickListener { + setProfile(value) true } + } } - override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { - if (preference == DolbyEnablePreference) { - val value = newValue as Boolean - mIntent.putExtra(DolbyCore.DAP_ENABLED, value) - requireContext().startService(mIntent) - return true + override fun onSwitchChanged(switchView: Switch, isChecked: Boolean) { + DolbyCore.setEnabled(isChecked) + } + + private fun setProfile(profile: Int) { + DolbyCore.setProfile(profile) + + for ((key, value) in PREF_DOLBY_MODES) { + val preference = findPreference(key)!! + preference.isChecked = value == profile } - return false } companion object { - private const val PREF_DOLBY = "dolby_pref" - private const val DOLBY_MODES = "dolby_modes" + const val PREF_DOLBY_ENABLE = "dolby_enable" + + val PREF_DOLBY_MODES = mapOf( + "dolby_profile_auto" to DolbyCore.PROFILE_AUTO, + "dolby_profile_movie" to DolbyCore.PROFILE_MOVIE, + "dolby_profile_music" to DolbyCore.PROFILE_MUSIC, + "dolby_profile_voice" to DolbyCore.PROFILE_VOICE, + "dolby_profile_game" to DolbyCore.PROFILE_GAME, + "dolby_profile_off" to DolbyCore.PROFILE_OFF, + "dolby_profile_game_1" to DolbyCore.PROFILE_GAME_1, + "dolby_profile_game_2" to DolbyCore.PROFILE_GAME_2, + "dolby_profile_spacial_audio" to DolbyCore.PROFILE_SPACIAL_AUDIO, + ) } } diff --git a/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbySearchIndexablesProvider.kt b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbySearchIndexablesProvider.kt new file mode 100644 index 0000000..b21b402 --- /dev/null +++ b/universal7885-common/apps/SamsungParts/src/com/eurekateam/samsungextras/dolby/DolbySearchIndexablesProvider.kt @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2022 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.eurekateam.samsungextras.dolby + +import android.database.Cursor +import android.database.MatrixCursor +import android.provider.SearchIndexableResource +import android.provider.SearchIndexablesProvider +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_CLASS_NAME +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_ICON_RESID +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_ACTION +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RANK +import android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RESID +import android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS +import android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS +import android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS + +class DolbySearchIndexablesProvider : SearchIndexablesProvider() { + override fun onCreate(): Boolean = true + + override fun queryXmlResources(projection: Array?): Cursor { + val cursor = MatrixCursor(INDEXABLES_XML_RES_COLUMNS) + INDEXABLE_RES.forEach { + cursor.addRow(generateResourceRef(it)) + } + return cursor + } + + override fun queryRawData(projection: Array?): Cursor { + return MatrixCursor(INDEXABLES_RAW_COLUMNS) + } + + override fun queryNonIndexableKeys(projection: Array?): Cursor { + return MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS) + } + + private fun generateResourceRef(sir: SearchIndexableResource): Array { + val ref = arrayOfNulls(7) + ref[COLUMN_INDEX_XML_RES_RANK] = sir.rank + ref[COLUMN_INDEX_XML_RES_RESID] = sir.xmlResId + ref[COLUMN_INDEX_XML_RES_CLASS_NAME] = null + ref[COLUMN_INDEX_XML_RES_ICON_RESID] = sir.iconResId + ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = "com.android.settings.action.EXTRA_SETTINGS" + ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = "com.eurekateam.samsungextras.dolby" + ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS] = sir.className + return ref + } + + companion object { + private const val TAG = "DolbySearchIndexablesProvider" + + private val INDEXABLE_RES = arrayOf( + SearchIndexableResource( + 1, R.xml.dolby_settings, DolbyActivity::class.java.name, 0 + ) + ) + } +} 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 23439f7..1e6afdf 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 @@ -1,25 +1,48 @@ +/* + * Copyright (C) 2022 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.eurekateam.samsungextras.dolby import android.service.quicksettings.Tile import android.service.quicksettings.TileService -import android.content.Intent + +import androidx.preference.PreferenceManager + +import com.eurekateam.samsungextras.dolby.DolbyFragment.Companion.PREF_DOLBY_ENABLE class DolbyTile : TileService() { - override fun onClick() { - val mIntent = Intent(this, DolbyCore::class.java) - if (DolbyCore.mAudioEffect.enabled) { - mIntent.putExtra(DolbyCore.DAP_ENABLED, false) - } else { - mIntent.putExtra(DolbyCore.DAP_ENABLED, true) - mIntent.putExtra(DolbyCore.DAP_PROFILE, DolbyCore.mCurrentProfile) + private var isEnabled = false + set(value) { + field = value + qsTile.state = if (value) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE + qsTile.subtitle = DolbyCore.getProfileName(this) + qsTile.updateTile() } - updateTile() + + override fun onStartListening() { + super.onStartListening() + isEnabled = DolbyCore.isEnabled() } - private fun updateTile() { - val tile = qsTile - tile.state = - if (DolbyCore.mAudioEffect.enabled) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE - tile.updateTile() + override fun onClick() { + isEnabled = !isEnabled + DolbyCore.setEnabled(isEnabled) + PreferenceManager.getDefaultSharedPreferences(this) + .edit() + .putBoolean(PREF_DOLBY_ENABLE, isEnabled) + .commit() } }