diff --git a/DolbyManager/AndroidManifest.xml b/DolbyManager/AndroidManifest.xml index e054299..da98061 100644 --- a/DolbyManager/AndroidManifest.xml +++ b/DolbyManager/AndroidManifest.xml @@ -26,7 +26,7 @@ @@ -50,7 +50,7 @@ diff --git a/DolbyManager/src/co/aospa/dolby/DolbyActivity.kt b/DolbyManager/src/co/aospa/dolby/DolbyActivity.kt index 38997e4..470f82f 100644 --- a/DolbyManager/src/co/aospa/dolby/DolbyActivity.kt +++ b/DolbyManager/src/co/aospa/dolby/DolbyActivity.kt @@ -10,14 +10,16 @@ import android.os.Bundle import co.aospa.dolby.preference.DolbySettingsFragment import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity -private const val TAG = "DolbyActivity" - class DolbyActivity : CollapsingToolbarBaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - fragmentManager.beginTransaction() - .replace(com.android.settingslib.collapsingtoolbar.R.id.content_frame, DolbySettingsFragment(), TAG) + supportFragmentManager + .beginTransaction() + .replace( + com.android.settingslib.collapsingtoolbar.R.id.content_frame, + DolbySettingsFragment(), + ) .commit() } } diff --git a/DolbyManager/src/co/aospa/dolby/preference/DolbySettingsFragment.kt b/DolbyManager/src/co/aospa/dolby/preference/DolbySettingsFragment.kt index e2c413f..d60585d 100644 --- a/DolbyManager/src/co/aospa/dolby/preference/DolbySettingsFragment.kt +++ b/DolbyManager/src/co/aospa/dolby/preference/DolbySettingsFragment.kt @@ -6,21 +6,20 @@ package co.aospa.dolby.preference +import android.content.Context import android.media.AudioAttributes import android.media.AudioDeviceCallback import android.media.AudioDeviceInfo import android.media.AudioManager import android.os.Bundle import android.os.Handler +import android.os.Looper +import android.view.View import android.widget.CompoundButton -import android.widget.CompoundButton.OnCheckedChangeListener import android.widget.Toast import androidx.preference.ListPreference import androidx.preference.Preference -import androidx.preference.Preference.OnPreferenceChangeListener -import androidx.preference.PreferenceFragment import androidx.preference.SwitchPreferenceCompat -import co.aospa.dolby.DolbyConstants import co.aospa.dolby.DolbyConstants.Companion.PREF_BASS import co.aospa.dolby.DolbyConstants.Companion.PREF_DIALOGUE import co.aospa.dolby.DolbyConstants.Companion.PREF_ENABLE @@ -36,47 +35,33 @@ import co.aospa.dolby.DolbyConstants.Companion.dlog import co.aospa.dolby.DolbyController import co.aospa.dolby.R import com.android.settingslib.widget.MainSwitchPreference +import com.android.settingslib.widget.SettingsBasePreferenceFragment -class DolbySettingsFragment : PreferenceFragment(), - OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener { +class DolbySettingsFragment : SettingsBasePreferenceFragment(), + Preference.OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener { - private val switchBar by lazy { - findPreference(PREF_ENABLE)!! - } - private val profilePref by lazy { - findPreference(PREF_PROFILE)!! - } - private val presetPref by lazy { - findPreference(PREF_PRESET)!! - } - private val ieqPref by lazy { - findPreference(PREF_IEQ)!! - } - private val stereoPref by lazy { - findPreference(PREF_STEREO)!! - } - private val dialoguePref by lazy { - findPreference(PREF_DIALOGUE)!! - } - private val bassPref by lazy { - findPreference(PREF_BASS)!! - } - private val hpVirtPref by lazy { - findPreference(PREF_HP_VIRTUALIZER)!! - } - private val spkVirtPref by lazy { - findPreference(PREF_SPK_VIRTUALIZER)!! - } - private val volumePref by lazy { - findPreference(PREF_VOLUME)!! - } - private val resetPref by lazy { - findPreference(PREF_RESET)!! - } + private val appContext: Context + get() = requireContext().applicationContext - private val dolbyController by lazy { DolbyController.getInstance(context) } - private val audioManager by lazy { context.getSystemService(AudioManager::class.java) } - private val handler = Handler() + private val switchBar by lazy { findPreference(PREF_ENABLE)!! } + private val profilePref by lazy { findPreference(PREF_PROFILE)!! } + private val presetPref by lazy { findPreference(PREF_PRESET)!! } + private val ieqPref by lazy { findPreference(PREF_IEQ)!! } + private val stereoPref by lazy { findPreference(PREF_STEREO)!! } + private val dialoguePref by lazy { findPreference(PREF_DIALOGUE)!! } + private val bassPref by lazy { findPreference(PREF_BASS)!! } + private val hpVirtPref by lazy { findPreference(PREF_HP_VIRTUALIZER)!! } + private val spkVirtPref by lazy { findPreference(PREF_SPK_VIRTUALIZER)!! } + private val volumePref by lazy { findPreference(PREF_VOLUME)!! } + private val resetPref by lazy { findPreference(PREF_RESET)!! } + + private val dolbyController by lazy(LazyThreadSafetyMode.NONE) { + DolbyController.getInstance(appContext) + } + private val audioManager by lazy(LazyThreadSafetyMode.NONE) { + appContext.getSystemService(AudioManager::class.java) + } + private val handler = Handler(Looper.getMainLooper()) private var isOnSpeaker = true set(value) { @@ -100,26 +85,29 @@ class DolbySettingsFragment : PreferenceFragment(), override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { dlog(TAG, "onCreatePreferences") - addPreferencesFromResource(R.xml.dolby_settings) + setPreferencesFromResource(R.xml.dolby_settings, rootKey) + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) val profile = dolbyController.profile - preferenceManager.preferenceDataStore = DolbyPreferenceStore(context).also { + preferenceManager.preferenceDataStore = DolbyPreferenceStore(appContext).also { it.profile = profile } val dsOn = dolbyController.dsOn switchBar.addOnSwitchChangeListener(this) - switchBar.setChecked(dsOn) + switchBar.isChecked = dsOn profilePref.onPreferenceChangeListener = this updateProfileIcon(profile) - profilePref.setEnabled(dsOn) + profilePref.isEnabled = dsOn profilePref.apply { if (entryValues.contains(profile.toString())) { summary = "%s" value = profile.toString() } else { - summary = context.getString(R.string.dolby_unknown) + summary = getString(R.string.dolby_unknown) } } @@ -135,21 +123,21 @@ class DolbySettingsFragment : PreferenceFragment(), dolbyController.resetProfileSpecificSettings() updateProfileSpecificPrefs() Toast.makeText( - context, - context.getString(R.string.dolby_reset_profile_toast, profilePref.summary), + appContext, + getString(R.string.dolby_reset_profile_toast, profilePref.summary), Toast.LENGTH_SHORT ).show() true } - audioManager!!.registerAudioDeviceCallback(audioDeviceCallback, handler) + audioManager?.registerAudioDeviceCallback(audioDeviceCallback, handler) updateSpeakerState() updateProfileSpecificPrefs() } override fun onDestroyView() { dlog(TAG, "onDestroyView") - audioManager!!.unregisterAudioDeviceCallback(audioDeviceCallback) + audioManager?.unregisterAudioDeviceCallback(audioDeviceCallback) super.onDestroyView() } @@ -168,35 +156,13 @@ class DolbySettingsFragment : PreferenceFragment(), updateProfileIcon(profile) updateProfileSpecificPrefs() } - - PREF_SPK_VIRTUALIZER -> { - dolbyController.setSpeakerVirtEnabled(newValue as Boolean) - } - - PREF_HP_VIRTUALIZER -> { - dolbyController.setHeadphoneVirtEnabled(newValue as Boolean) - } - - PREF_STEREO -> { - dolbyController.setStereoWideningAmount(newValue.toString().toInt()) - } - - PREF_DIALOGUE -> { - dolbyController.setDialogueEnhancerAmount(newValue.toString().toInt()) - } - - PREF_BASS -> { - dolbyController.setBassEnhancerEnabled(newValue as Boolean) - } - - PREF_VOLUME -> { - dolbyController.setVolumeLevelerEnabled(newValue as Boolean) - } - - PREF_IEQ -> { - dolbyController.setIeqPreset(newValue.toString().toInt()) - } - + PREF_SPK_VIRTUALIZER -> dolbyController.setSpeakerVirtEnabled(newValue as Boolean) + PREF_HP_VIRTUALIZER -> dolbyController.setHeadphoneVirtEnabled(newValue as Boolean) + PREF_STEREO -> dolbyController.setStereoWideningAmount(newValue.toString().toInt()) + PREF_DIALOGUE -> dolbyController.setDialogueEnhancerAmount(newValue.toString().toInt()) + PREF_BASS -> dolbyController.setBassEnhancerEnabled(newValue as Boolean) + PREF_VOLUME -> dolbyController.setVolumeLevelerEnabled(newValue as Boolean) + PREF_IEQ -> dolbyController.setIeqPreset(newValue.toString().toInt()) else -> return false } return true @@ -205,18 +171,21 @@ class DolbySettingsFragment : PreferenceFragment(), override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) { dlog(TAG, "onCheckedChanged($isChecked)") dolbyController.dsOn = isChecked - profilePref.setEnabled(isChecked) + profilePref.isEnabled = isChecked updateProfileSpecificPrefs() } private fun updateSpeakerState() { - val device = audioManager!!.getDevicesForAttributes(ATTRIBUTES_MEDIA)[0] - isOnSpeaker = (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) + val devices = audioManager + ?.getDevicesForAttributes(ATTRIBUTES_MEDIA) + .orEmpty() + val firstType = devices.firstOrNull()?.type + isOnSpeaker = (firstType == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) } private fun updateProfileSpecificPrefs() { - val unknownRes = context.getString(R.string.dolby_unknown) - val headphoneRes = context.getString(R.string.dolby_connect_headphones) + val unknownRes = getString(R.string.dolby_unknown) + val headphoneRes = getString(R.string.dolby_connect_headphones) val dsOn = dolbyController.dsOn val currentProfile = dolbyController.profile @@ -226,15 +195,15 @@ class DolbySettingsFragment : PreferenceFragment(), ) val enable = dsOn && (currentProfile != -1) - presetPref.setEnabled(enable) - spkVirtPref.setEnabled(enable) - ieqPref.setEnabled(enable) - dialoguePref.setEnabled(enable) - volumePref.setEnabled(enable) - bassPref.setEnabled(enable) - resetPref.setEnabled(enable) - hpVirtPref.setEnabled(enable && !isOnSpeaker) - stereoPref.setEnabled(enable && !isOnSpeaker) + presetPref.isEnabled = enable + spkVirtPref.isEnabled = enable + ieqPref.isEnabled = enable + dialoguePref.isEnabled = enable + volumePref.isEnabled = enable + bassPref.isEnabled = enable + resetPref.isEnabled = enable + hpVirtPref.isEnabled = enable && !isOnSpeaker + stereoPref.isEnabled = enable && !isOnSpeaker if (!enable) return @@ -260,9 +229,9 @@ class DolbySettingsFragment : PreferenceFragment(), } } - spkVirtPref.setChecked(dolbyController.getSpeakerVirtEnabled(currentProfile)) - volumePref.setChecked(dolbyController.getVolumeLevelerEnabled(currentProfile)) - bassPref.setChecked(dolbyController.getBassEnhancerEnabled(currentProfile)) + spkVirtPref.isChecked = dolbyController.getSpeakerVirtEnabled(currentProfile) + volumePref.isChecked = dolbyController.getVolumeLevelerEnabled(currentProfile) + bassPref.isChecked = dolbyController.getBassEnhancerEnabled(currentProfile) // below prefs are not enabled on loudspeaker if (isOnSpeaker) { @@ -282,7 +251,7 @@ class DolbySettingsFragment : PreferenceFragment(), } hpVirtPref.apply { - setChecked(dolbyController.getHeadphoneVirtEnabled(currentProfile)) + isChecked = dolbyController.getHeadphoneVirtEnabled(currentProfile) summary = null } }