Twelve: Add "Stop playback on task removed" toggle

Change-Id: I4dbd033b85eef70a1be9ea5bf3561c4de17a9e05
diff --git a/app/src/main/java/org/lineageos/twelve/ext/SharedPreferences.kt b/app/src/main/java/org/lineageos/twelve/ext/SharedPreferences.kt
index b92ca66..8b9c4bc 100644
--- a/app/src/main/java/org/lineageos/twelve/ext/SharedPreferences.kt
+++ b/app/src/main/java/org/lineageos/twelve/ext/SharedPreferences.kt
@@ -12,8 +12,17 @@
 const val ENABLE_OFFLOAD_KEY = "enable_offload"
 private const val ENABLE_OFFLOAD_DEFAULT = true
 
+private const val STOP_PLAYBACK_ON_TASK_REMOVED_KEY = "stop_playback_on_task_removed"
+private const val STOP_PLAYBACK_ON_TASK_REMOVED_DEFAULT = true
+
 var SharedPreferences.enableOffload: Boolean
     get() = getBoolean(ENABLE_OFFLOAD_KEY, ENABLE_OFFLOAD_DEFAULT)
     set(value) = edit {
         putBoolean(ENABLE_OFFLOAD_KEY, value)
     }
+
+var SharedPreferences.stopPlaybackOnTaskRemoved: Boolean
+    get() = getBoolean(STOP_PLAYBACK_ON_TASK_REMOVED_KEY, STOP_PLAYBACK_ON_TASK_REMOVED_DEFAULT)
+    set(value) = edit {
+        putBoolean(STOP_PLAYBACK_ON_TASK_REMOVED_KEY, value)
+    }
diff --git a/app/src/main/java/org/lineageos/twelve/services/PlaybackService.kt b/app/src/main/java/org/lineageos/twelve/services/PlaybackService.kt
index 99ecb37..a77d4df 100644
--- a/app/src/main/java/org/lineageos/twelve/services/PlaybackService.kt
+++ b/app/src/main/java/org/lineageos/twelve/services/PlaybackService.kt
@@ -34,6 +34,7 @@
 import org.lineageos.twelve.TwelveApplication
 import org.lineageos.twelve.ext.enableOffload
 import org.lineageos.twelve.ext.setOffloadEnabled
+import org.lineageos.twelve.ext.stopPlaybackOnTaskRemoved
 import org.lineageos.twelve.ui.widgets.NowPlayingAppWidgetProvider
 
 @OptIn(UnstableApi::class)
@@ -235,7 +236,9 @@
     }
 
     override fun onTaskRemoved(rootIntent: Intent?) {
-        pauseAllPlayersAndStopSelf()
+        if (sharedPreferences.stopPlaybackOnTaskRemoved || !isPlaybackOngoing) {
+            pauseAllPlayersAndStopSelf()
+        }
     }
 
     override fun onDestroy() {
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index ae69f05..6e0648b 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -148,6 +148,8 @@
     <!-- General preferences -->
     <string name="enable_offload">Enable offload</string>
     <string name="enable_offload_summary">Use the offload audio path for audio playback. Disabling this may increase power usage but can be useful if you experience issues with audio playback or post processing</string>
+    <string name="stop_playback_on_task_removed">Stop playback on app\'s removal from recents</string>
+    <string name="stop_playback_on_task_removed_summary">Stop playback when the app is cleared from recents</string>
 
     <!-- Now playing widget -->
     <string name="now_playing_widget_description">Now playing</string>
diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml
index 4db2c1c..c2854e9 100644
--- a/app/src/main/res/xml/root_preferences.xml
+++ b/app/src/main/res/xml/root_preferences.xml
@@ -15,6 +15,14 @@
             app:key="enable_offload"
             app:summary="@string/enable_offload_summary"
             app:title="@string/enable_offload" />
+
+        <SwitchPreference
+            app:defaultValue="true"
+            app:iconSpaceReserved="false"
+            app:key="stop_playback_on_task_removed"
+            app:summary="@string/stop_playback_on_task_removed_summary"
+            app:title="@string/stop_playback_on_task_removed" />
+
     </PreferenceCategory>
 
 </PreferenceScreen>