Skip to content

Commit

Permalink
POC using enum for notification play over setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mchowning authored and jamie23 committed Jul 24, 2023
1 parent f223138 commit 3fdb8f8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreference
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTrackerWrapper
import au.com.shiftyjelly.pocketcasts.preferences.PlayOverNotificationSetting
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.notification.NewEpisodeNotificationAction
import au.com.shiftyjelly.pocketcasts.repositories.notification.NotificationHelper
Expand Down Expand Up @@ -136,10 +137,18 @@ class NotificationsSettingsFragment :
true
}
playOverNotificationPreference?.setOnPreferenceChangeListener { _, newValue ->
val playOverNotificationSetting = (newValue as? String)
?.let { PlayOverNotificationSetting.fromPreferenceString(it) }
?: throw IllegalStateException("Invalid value for play over notification preference: $newValue")

analyticsTracker.track(
AnalyticsEvent.SETTINGS_NOTIFICATIONS_PLAY_OVER_NOTIFICATIONS_TOGGLED,
mapOf("enabled" to (newValue != "2"))
mapOf(
"enabled" to (playOverNotificationSetting != PlayOverNotificationSetting.NEVER),
"value" to playOverNotificationSetting.analyticsString,
),
)

true
}
}
Expand Down Expand Up @@ -458,25 +467,21 @@ class NotificationsSettingsFragment :
}

private fun setupPlayOverNotifications() {
playOverNotificationPreference?.let {
it.entries = arrayOf(
getString(LR.string.settings_notification_play_over_never),
getString(LR.string.settings_notification_play_over_duck),
getString(LR.string.settings_notification_play_over_always)
playOverNotificationPreference?.apply {
val options = listOf(
PlayOverNotificationSetting.NEVER,
PlayOverNotificationSetting.DUCK,
PlayOverNotificationSetting.ALWAYS,
)
it.entryValues = arrayOf("2", "1", "0")
it.value = settings.getPlayOverNotification().toString()
entries = options.map { getString(it.titleRes) }.toTypedArray()
entryValues = options.map { it.preferenceInt.toString() }.toTypedArray()
value = settings.getPlayOverNotification().preferenceInt.toString()
}
changePlayOverNotificationSummary()
}

private fun changePlayOverNotificationSummary() {
playOverNotificationPreference?.summary = when (settings.getPlayOverNotification()) {
2 -> getString(LR.string.settings_notification_play_over_never)
1 -> getString(LR.string.settings_notification_play_over_duck)
0 -> getString(LR.string.settings_notification_play_over_always)
else -> ""
}
playOverNotificationPreference?.summary = getString(settings.getPlayOverNotification().titleRes)
}

override fun getBackstackCount(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package au.com.shiftyjelly.pocketcasts.preferences

import androidx.annotation.StringRes
import au.com.shiftyjelly.pocketcasts.localization.R as LR

enum class PlayOverNotificationSetting(
val preferenceInt: Int,
@StringRes val titleRes: Int,
val analyticsString: String,
) {
NEVER(
titleRes = LR.string.settings_notification_play_over_never,
preferenceInt = 2,
analyticsString = "never"
),
DUCK(
titleRes = LR.string.settings_notification_play_over_duck,
preferenceInt = 1,
analyticsString = "duck"
),
ALWAYS(
titleRes = LR.string.settings_notification_play_over_always,
preferenceInt = 0,
analyticsString = "always"
);

companion object {
fun fromPreferenceString(stringValue: String): PlayOverNotificationSetting {
try {
val intValue = stringValue.toInt()
return values().first { it.preferenceInt == intValue }
} catch (e: Exception) {
throw IllegalStateException("Unknown play over notification setting: $stringValue")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ interface Settings {

fun setPopularPodcastCountryCode(code: String)

fun getPlayOverNotification(): Int
fun getPlayOverNotification(): PlayOverNotificationSetting

fun hasBlockAlreadyRun(label: String): Boolean
fun setBlockAlreadyRun(label: String, hasRun: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,12 @@ class SettingsImpl @Inject constructor(
return getBoolean(Settings.PREFERENCE_AUTO_SHOW_PLAYED, false)
}

override fun getPlayOverNotification(): Int {
override fun getPlayOverNotification(): PlayOverNotificationSetting {
val value = sharedPreferences.getString(
Settings.PREFERENCE_NOTIFICATION_AUDIO,
Settings.PREFERENCE_NOTIFICATION_AUDIO_DEFAULT
) ?: Settings.PREFERENCE_NOTIFICATION_AUDIO_DEFAULT
return Integer.parseInt(value)
return PlayOverNotificationSetting.fromPreferenceString(value)
}

override fun hasBlockAlreadyRun(label: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.media.AudioManager
import androidx.media.AudioAttributesCompat
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat
import au.com.shiftyjelly.pocketcasts.preferences.PlayOverNotificationSetting
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer
import timber.log.Timber
Expand Down Expand Up @@ -150,21 +151,10 @@ open class FocusManager(private val settings: Settings, context: Context?) : Aud
}
}

enum class PlayOverNotificationSetting {
NEVER, DUCK, ALWAYS
}

private fun canDuck(): PlayOverNotificationSetting {
if (audioFocus != AUDIO_NO_FOCUS_CAN_DUCK_TRANSIENT) return PlayOverNotificationSetting.NEVER
return playOverNotification()
return settings.getPlayOverNotification()
}

protected open fun playOverNotification() = when (settings.getPlayOverNotification()) {
0 -> PlayOverNotificationSetting.ALWAYS
1 -> PlayOverNotificationSetting.DUCK
else -> PlayOverNotificationSetting.NEVER
}

interface FocusChangeListener {
fun onFocusGain(shouldResume: Boolean)
fun onFocusLoss(playOverNotification: PlayOverNotificationSetting, transientLoss: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import au.com.shiftyjelly.pocketcasts.models.to.PodcastGrouping
import au.com.shiftyjelly.pocketcasts.models.type.EpisodePlayingStatus
import au.com.shiftyjelly.pocketcasts.models.type.EpisodeStatusEnum
import au.com.shiftyjelly.pocketcasts.models.type.UserEpisodeServerStatus
import au.com.shiftyjelly.pocketcasts.preferences.PlayOverNotificationSetting
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.R
import au.com.shiftyjelly.pocketcasts.repositories.chromecast.CastManager
Expand Down Expand Up @@ -1337,14 +1338,14 @@ open class PlaybackManager @Inject constructor(
focusWasPlaying = null
}

override fun onFocusLoss(playOverNotification: FocusManager.PlayOverNotificationSetting, transientLoss: Boolean) {
override fun onFocusLoss(playOverNotification: PlayOverNotificationSetting, transientLoss: Boolean) {
val player = player
if (player == null || player.isRemote) {
return
}
// if we are playing but can't just reduce the volume then play when focus gained
val playing = isPlaying()
if ((playOverNotification == FocusManager.PlayOverNotificationSetting.NEVER) && playing) {
if ((playOverNotification == PlayOverNotificationSetting.NEVER) && playing) {
LogBuffer.i(LogBuffer.TAG_PLAYBACK, "Focus lost while playing")
focusWasPlaying = Date()

Expand All @@ -1355,7 +1356,7 @@ open class PlaybackManager @Inject constructor(
}

// check if we need to reduce the volume
if (playOverNotification == FocusManager.PlayOverNotificationSetting.DUCK) {
if (playOverNotification == PlayOverNotificationSetting.DUCK) {
player.setVolume(VOLUME_DUCK)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class Support @Inject constructor(
output.append(eol)

output.append("Notifications").append(eol)
output.append("Play over notifications? ").append(playOverNotificationString(settings.getPlayOverNotification())).append(eol)
output.append("Play over notifications? ").append(settings.getPlayOverNotification().analyticsString).append(eol)
output.append("Hide notification on pause? ").append(if (settings.hideNotificationOnPause()) "yes" else "no").append(eol)
output.append(eol)

Expand Down Expand Up @@ -475,13 +475,6 @@ class Support @Inject constructor(
return if (value) "yes" else "no"
}

private fun playOverNotificationString(value: Int) = when (value) {
0 -> "always"
1 -> "duck"
2 -> "never"
else -> "unknown"
}

private fun getDeviceName(): String {
try {
return DeviceName.getDeviceName()
Expand Down

0 comments on commit 3fdb8f8

Please sign in to comment.