Skip to content

Commit

Permalink
Added option to delay audioservice foreground stop after pause
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Jun 16, 2018
1 parent 247bb6c commit b9222de
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
40 changes: 30 additions & 10 deletions app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.support.v4.media.session.PlaybackStateCompat.*
import android.text.format.DateUtils.MINUTE_IN_MILLIS
import android.text.format.DateUtils.YEAR_IN_MILLIS
import android.text.format.DateUtils.*
import android.util.Log
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.ExoPlayerFactory
Expand Down Expand Up @@ -54,7 +53,8 @@ const val AUDIOSERVICE_ACTION_PAUSE = "eu.zderadicka.audioserve.ACTION_PAUSE"
const val AUDIOSERVICE_FORCE_RELOAD = "eu.zderadicka.audioserve.FORCE_RELOAD"
const val AUDIOSERVICE_DONT_PRELOAD_LATEST = "eu.zderadicka.audioserve.NO_PRELOAD_LATEST"
private const val AUDIOSERVICE_ACTION_SELF_START = "eu.zderadicka.audioserve.SELF_START"
private const val PAUSE_DELAYED_TASKS = "pause_tasks"
private const val PAUSE_DELAYED_TASK_SAVE_POSITION = "pause_task_save_position"
private const val PAUSE_DELAYED_TASK_STOP_FOREGROUND = "pause_task_stop_fg"


private class ResultWrapper(val result: MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>>) {
Expand Down Expand Up @@ -99,6 +99,7 @@ class AudioService : MediaBrowserServiceCompat() {
private var isOffline: Boolean = false
private val scheduler = Handler()
private var enableAutoRewind = false
private var delayedFgStop = 0

private val playerController = object : DefaultPlaybackController(REWIND_MS, FF_MS, MediaSessionConnector.DEFAULT_REPEAT_TOGGLE_MODES) {

Expand Down Expand Up @@ -405,7 +406,11 @@ class AudioService : MediaBrowserServiceCompat() {
}

if (state.state != PlaybackStateCompat.STATE_PAUSED) {
scheduler.removeCallbacksAndMessages(PAUSE_DELAYED_TASKS)
scheduler.removeCallbacksAndMessages(PAUSE_DELAYED_TASK_SAVE_POSITION)
}

if (state.state == PlaybackStateCompat.STATE_PLAYING) {
scheduler.removeCallbacksAndMessages(PAUSE_DELAYED_TASK_STOP_FOREGROUND)
}
}

Expand Down Expand Up @@ -485,6 +490,9 @@ class AudioService : MediaBrowserServiceCompat() {
"pref_autorewind" -> {
enableAutoRewind = sharedPreferences.getBoolean("pref_autorewind", true)
}
"pref_delayed_fg_stop" -> {
delayedFgStop = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_delayed_fg_stop", "0").toInt()
}
}
}

Expand All @@ -511,6 +519,7 @@ class AudioService : MediaBrowserServiceCompat() {
preloadFiles = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_preload","2").toInt()
isOffline = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_offline",false)
enableAutoRewind = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_autorewind", true)
delayedFgStop = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_delayed_fg_stop", "0").toInt()
session = MediaSessionCompat(this, LOG_TAG)
session.controller.registerCallback(sessionCallback)
player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
Expand Down Expand Up @@ -579,18 +588,29 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);

}

fun stopFg() {
Log.d(LOG_TAG, "Stop foreground")
stopForeground(false)
isStartedInForeground = false
}

fun pauseMe() {
cancelSleepTimer(this)
scheduler.postAtTime({
saveCurrentlyListened()
},
PAUSE_DELAYED_TASKS,
PAUSE_DELAYED_TASK_SAVE_POSITION,
SystemClock.uptimeMillis()+ 10_000)
Log.d(LOG_TAG, "Pausing service - stopForeground")
//TODO - consider if we really want to stop foreground - as playback service might get recycled
stopForeground(false)
isStartedInForeground = false

Log.d(LOG_TAG, "Pausing service")
if (delayedFgStop == 0) {
stopFg()
} else {
scheduler.postAtTime(
{stopFg()},
PAUSE_DELAYED_TASK_STOP_FOREGROUND,
SystemClock.uptimeMillis()+ HOUR_IN_MILLIS * delayedFgStop
)
}
}


Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen
}
}

findPreference("pref_delayed_fg_stop").setOnPreferenceChangeListener{_, newValue ->
try {
val n = (newValue as String).toInt();
n>=0 && n< 365 * 24
} catch (e: Exception) {
false
}
}

findPreference("pref_clear_cache").setOnPreferenceClickListener {

Thread({
Expand Down Expand Up @@ -235,6 +244,17 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen
}
}

"pref_delayed_fg_stop" -> {
if (pref !is EditTextPreference) return
val delay = sps.getString("pref_delayed_fg_stop", null)
val isOff = delay.isNullOrBlank() || delay.toInt() == 0
if (isOff) {
pref.summary = getString(R.string.pref_delayed_fg_stop_summary_off)
} else {
pref.summary = getString(R.string.pref_delayed_fg_stop_summary_on, delay)
}
}


}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ControllerFragment : MediaFragment() {
enablePlay = false
}
PlaybackStateCompat.STATE_PAUSED,
PlaybackStateCompat.STATE_BUFFERING,
PlaybackStateCompat.STATE_STOPPED-> enablePlay = true
PlaybackStateCompat.STATE_ERROR -> {
val msg = state.errorMessage?:"Playback error"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@
<string name="pref_web_search_prefix_title">Web search prefix</string>
<string name="pref_web_search_prefix_summary_empty">No prefix. You can set one to limit web search for folder details</string>
<string name="pref_web_search_prefix_summary">Prefix %1$s is used to search web for folder details</string>
<string name="pref_delayed_fg_stop_title">Keep running in foreground</string>
<string name="pref_delayed_fg_stop_summary_off">After pause player service is not running in foreground. This can save some batteries, but system can recycle it soon. You can set number of hours service will be kept in foreground after pause.</string>
<string name="pref_delayed_fg_stop_summary_on">After pause player service will remain in foreground for %1$s hours. This will assure quicker continuation, but can consume some very small amount of power. By setting to 0 you can turn this off.</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
android:title="@string/pref_autorewind_title"
android:defaultValue="true"
/>
<EditTextPreference
android:key="pref_delayed_fg_stop"
android:title="@string/pref_delayed_fg_stop_title"
android:defaultValue="0"
android:numeric="integer"
/>

</PreferenceCategory>

Expand Down

0 comments on commit b9222de

Please sign in to comment.