Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disconnection of the service when the activity dies #1272

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/com/zionhuang/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import com.zionhuang.music.constants.PauseSearchHistoryKey
import com.zionhuang.music.constants.PureBlackKey
import com.zionhuang.music.constants.SearchSource
import com.zionhuang.music.constants.SearchSourceKey
import com.zionhuang.music.constants.StopMusicOnTaskClearKey
import com.zionhuang.music.db.MusicDatabase
import com.zionhuang.music.db.entities.SearchHistory
import com.zionhuang.music.extensions.toEnum
Expand Down Expand Up @@ -184,6 +185,15 @@ class MainActivity : ComponentActivity() {
super.onStop()
}

override fun onDestroy() {
super.onDestroy()
if (dataStore.get(StopMusicOnTaskClearKey, false) && playerConnection?.isPlaying?.value == true) {
stopService(Intent(this, MusicService::class.java))
unbindService(serviceConnection)
playerConnection = null
}
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum class AudioQuality {
val PersistentQueueKey = booleanPreferencesKey("persistentQueue")
val SkipSilenceKey = booleanPreferencesKey("skipSilence")
val AudioNormalizationKey = booleanPreferencesKey("audioNormalization")
val StopMusicOnTaskClearKey = booleanPreferencesKey("stopMusicOnTaskClear")

val MaxImageCacheSizeKey = intPreferencesKey("maxImageCacheSize")
val MaxSongCacheSizeKey = intPreferencesKey("maxSongCacheSize")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.zionhuang.music.constants.AudioQuality
import com.zionhuang.music.constants.AudioQualityKey
import com.zionhuang.music.constants.PersistentQueueKey
import com.zionhuang.music.constants.SkipSilenceKey
import com.zionhuang.music.constants.StopMusicOnTaskClearKey
import com.zionhuang.music.ui.component.EnumListPreference
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.SwitchPreference
Expand All @@ -38,6 +39,7 @@ fun PlayerSettings(
val (persistentQueue, onPersistentQueueChange) = rememberPreference(key = PersistentQueueKey, defaultValue = true)
val (skipSilence, onSkipSilenceChange) = rememberPreference(key = SkipSilenceKey, defaultValue = false)
val (audioNormalization, onAudioNormalizationChange) = rememberPreference(key = AudioNormalizationKey, defaultValue = true)
val (stopMusicOnTaskClear, onStopMusicOnTaskClearChange) = rememberPreference(key = StopMusicOnTaskClearKey, defaultValue = false)

Column(
Modifier
Expand Down Expand Up @@ -75,6 +77,12 @@ fun PlayerSettings(
checked = audioNormalization,
onCheckedChange = onAudioNormalizationChange
)
SwitchPreference(
title = { Text(stringResource(R.string.stop_music_on_task_clear)) },
icon = { Icon(painterResource(R.drawable.clear_all), null) },
checked = stopMusicOnTaskClear,
onCheckedChange = onStopMusicOnTaskClearChange
)
}

TopAppBar(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
<string name="persistent_queue">保留播放佇列</string>
<string name="skip_silence">跳過無聲片段</string>
<string name="audio_normalization">標準化音量</string>
<string name="stop_music_on_task_clear">將音樂在清除任務時停止</string>
<string name="equalizer">等化器</string>

<string name="storage">儲存</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<string name="persistent_queue">Persistent queue</string>
<string name="skip_silence">Skip silence</string>
<string name="audio_normalization">Audio normalization</string>
<string name="stop_music_on_task_clear">Stop music on task clear</string>
<string name="equalizer">Equalizer</string>

<string name="storage">Storage</string>
Expand Down