Skip to content

Commit

Permalink
Autorewind + fix condition of search button appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 20, 2018
1 parent ef1c054 commit bed843e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "eu.zderadicka.audioserve"
minSdkVersion 21
targetSdkVersion 27
versionCode 12
versionName "0.6.5"
versionCode 13
versionName "0.6.6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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.util.Log
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.ExoPlayerFactory
Expand Down Expand Up @@ -88,13 +90,15 @@ class AudioService : MediaBrowserServiceCompat() {
private var currentMediaItem: MediaItem? = null
private var lastKnownPosition = 0L
private var lastPositionUpdateTime = 0L
private var previousPositionUpdateTime = 0L
private var playQueue: MutableList<MediaItem> = ArrayList<MediaItem>()
private lateinit var apiClient: ApiClient
private var preloadFiles: Int = 2
private var seekAfterPrepare: Long? = null
private var deletePreviousQueueItem: Int = -1 // delete previous queue Item
private var isOffline: Boolean = false
private val scheduler = Handler()
private var enableAutoRewind = false

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

Expand Down Expand Up @@ -128,6 +132,10 @@ class AudioService : MediaBrowserServiceCompat() {

override fun onPlay(player: Player) {
Log.d(LOG_TAG, "Playback started")
val autoRewind = calcAutoRewind()
if (autoRewind>100) {
super.onSeekTo(player, player.currentPosition - autoRewind)
}
if (requestAudioFocus()) {
super.onPlay(player)
}
Expand Down Expand Up @@ -170,6 +178,18 @@ class AudioService : MediaBrowserServiceCompat() {
}
}

private fun calcAutoRewind():Int {
if (!enableAutoRewind) return 0
val prevPos = if (previousPositionUpdateTime >0) previousPositionUpdateTime else lastPositionUpdateTime
previousPositionUpdateTime = 0
val updatedBefore = System.currentTimeMillis() - prevPos
Log.d(LOG_TAG,"Determine autorewind for item ${currentMediaItem}, updated before${updatedBefore}")
return if (updatedBefore < 5* MINUTE_IN_MILLIS) 2000
else if (updatedBefore < 30 * MINUTE_IN_MILLIS) 15_000
else if (updatedBefore < YEAR_IN_MILLIS) 30_000
else 0
}

private fun findIndexInFolder(mediaId: String): Int {
return currentFolder.indexOfFirst { it.mediaId == mediaId }
}
Expand Down Expand Up @@ -250,6 +270,8 @@ class AudioService : MediaBrowserServiceCompat() {
seekAfterPrepare = seekTo
}

previousPositionUpdateTime = extras?.getLong(METADATA_KEY_LAST_LISTENED_TIMESTAMP)?:0


} else {
//source = factory.createMediaSource(apiClient.uriFromMediaId(mediaId))
Expand Down Expand Up @@ -454,6 +476,9 @@ class AudioService : MediaBrowserServiceCompat() {
isOffline = sharedPreferences.getBoolean("pref_offline", false)
preparer.sourceFactory = null
}
"pref_autorewind" -> {
enableAutoRewind = sharedPreferences.getBoolean("pref_autorewind", false)
}
}
}

Expand All @@ -479,6 +504,7 @@ class AudioService : MediaBrowserServiceCompat() {
super.onCreate()
preloadFiles = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_preload","2").toInt()
isOffline = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_offline",false)
enableAutoRewind = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_autorewind", false)
session = MediaSessionCompat(this, LOG_TAG)
session.controller.registerCallback(sessionCallback)
player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
Expand Down Expand Up @@ -793,6 +819,9 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
extras.putLong(METADATA_KEY_LAST_POSITION,
lastItem.description.extras?.getLong(METADATA_KEY_LAST_POSITION)
?: 0L)
extras.putLong(METADATA_KEY_LAST_LISTENED_TIMESTAMP,
lastItem.description.extras?.getLong(METADATA_KEY_LAST_LISTENED_TIMESTAMP)
?: 0L)
preparer.onPrepareFromMediaId(lastItem.mediaId!!, extras)
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ class MainActivity : AppCompatActivity(),
val extras = Bundle()
val startAt: Long = item.description.extras?.getLong(METADATA_KEY_LAST_POSITION) ?: 0
if (startAt > 0) {
ctl.seekTo(startAt)
//ctl.seekTo(startAt)
extras.putLong(METADATA_KEY_LAST_POSITION, startAt)
}
extras.putLong(METADATA_KEY_LAST_LISTENED_TIMESTAMP,
item.description.extras?.getLong(METADATA_KEY_LAST_LISTENED_TIMESTAMP)?: 0)
ctl.prepareFromMediaId(item.mediaId, extras)
}

val collection: Int? = collectionFromFolderId(folderId)
val collection: Int? = collectionFromFolderId(folderId)?: collectionFromSearchId(folderId)
searchPrefix = if (collection == null || isOffline) null else "${AudioService.SEARCH_PREFIX}${collection}_"
this.folderDetails = folderDetails
invalidateOptionsMenu()
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen
pref.summary = getString(R.string.pref_downloads_summary, p)

}
"pref_autorewind" -> {
if (pref !is CheckBoxPreference) return
val checked = sps.getBoolean("pref_autorewind", false)
if (checked) {
pref.summary=getString(R.string.pref_autorewind_selected_title)

} else {
pref.summary=getString(R.string.pref_autorewind_unselected_title)
}

}


}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/data/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ fun collectionFromFolderId(folderId:String): Int? {
return n.toInt()
}


private val BEGIN_NUMBERS_RE = Regex("""^\d+""")
fun collectionFromSearchId(folderId:String): Int? {
if (folderId.startsWith(AudioService.SEARCH_PREFIX)) {
val m = BEGIN_NUMBERS_RE.find(folderId.substring(AudioService.SEARCH_PREFIX.length))
if (m!=null) {
return m.value.toInt()
}
}
return null
}

fun duplicateMediaItemWithExtrasAssured(item: MediaBrowserCompat.MediaItem):MediaBrowserCompat.MediaItem {
val desc = MediaDescriptionCompat.Builder()
.setMediaId(item.description.mediaId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ class ApiClient private constructor(val context: Context) {
}



fun login(cb: (ApiError?) -> Unit) {
fun afterLogin() {
synchronized(this@ApiClient) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@
<string name="free_capacity">%1$s free</string>
<string name="minutes_abr">m</string>
<string name="download_notification_errors">Download ended with %1$s errors!</string>
<string name="prefs_category_downloads">DOWNLOADS</string>
<string name="prefs_category_playback">PLAYBACK</string>
<string name="pref_autorewind_title">Auto Rewind</string>
<string name="pref_autorewind_unselected_title">No auto rewind, playback will start exactly where it was paused</string>
<string name="pref_autorewind_selected_title">Before continuing playback it\'\'ll be rewind by a small amount (2s-30s), depending how long it was paused</string>
</resources>
13 changes: 12 additions & 1 deletion app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</PreferenceCategory>
<PreferenceCategory
android:key="prefs_downloads"
android:title="DOWNLOADS"
android:title="@string/prefs_category_downloads"
>
<ListPreference
android:key="pref_downloads"
Expand All @@ -73,6 +73,17 @@
android:defaultValue="2"
/>
</PreferenceCategory>
<PreferenceCategory
android:key="prefs_playback"
android:title="@string/prefs_category_playback">

<CheckBoxPreference
android:key="pref_autorewind"
android:title="@string/pref_autorewind_title"
android:defaultValue="true"
/>

</PreferenceCategory>



Expand Down
7 changes: 7 additions & 0 deletions app/src/test/java/eu/zderadicka/audioserve/UtilsTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.zderadicka.audioserve

import eu.zderadicka.audioserve.data.collectionFromFolderId
import eu.zderadicka.audioserve.data.collectionFromSearchId
import eu.zderadicka.audioserve.data.folderIdFromFileId
import eu.zderadicka.audioserve.data.pathFromFolderId
import eu.zderadicka.audioserve.net.parseContentRange
Expand Down Expand Up @@ -69,4 +70,10 @@ class UtilsTest {
val path2 = pathFromFolderId(fid)
assertEquals("Verne, Jules", path2)
}

@Test
fun testCollectionFromSearch() {
val c = collectionFromSearchId("__AUDIOSERVE_SEARCH_5")
assertEquals(5,c)
}
}

0 comments on commit bed843e

Please sign in to comment.