Skip to content

Commit

Permalink
Couple of small fixes mainly in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 4, 2018
1 parent 9cc7bdb commit d763b3c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 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 8
versionName "0.6.1"
versionCode 9
versionName "0.6.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
43 changes: 35 additions & 8 deletions app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import android.support.v4.media.MediaMetadataCompat
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.ACTION_PLAY_FROM_MEDIA_ID
import android.support.v4.media.session.PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID
import android.support.v4.media.session.PlaybackStateCompat.*
import android.util.Log
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.ExoPlayerFactory
Expand Down Expand Up @@ -602,20 +601,31 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
private val SEARCH_RE = Regex("""^(\d+)_(.*)""")
override fun onLoadChildren(parentId: String, result:Result<List<MediaItem>>) {
if (parentId == RECENTLY_LISTENED_TAG) {
result.detach()
Log.d(LOG_TAG, "Requesting list of recently listened items")

val list = ArrayList<MediaItem>()
if (currentMediaItem != null) {
val mi = currentMediaItem!!
mi.description.extras?.putBoolean(METADATA_KEY_IS_BOOKMARK, true)
updateCurrentMediaItemTime()
list.add(mi)
}
var path: String? = null
if (currentMediaItem != null) {
path = File(currentMediaItem!!.mediaId).parent
}
list.addAll(getRecents(applicationContext,path ))
result.sendResult(list)

Thread({

var path: String? = null
if (currentMediaItem != null) {
path = File(currentMediaItem!!.mediaId).parent
}
list.addAll(getRecents(applicationContext,path ))
result.sendResult(list)
}).start()






} else {
if (isOffline) {
Expand Down Expand Up @@ -722,6 +732,23 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
{
result.sendResult(it.getMediaItems(cacheManager))
currentFolder = it.getPlayableItems(cacheManager)

if (session.controller.playbackState.state == PlaybackStateCompat.STATE_NONE ||
session.controller.playbackState.state == PlaybackStateCompat.STATE_STOPPED) {
// If player is stopped we can prepare latest bookmark

val lastItems = getRecents(this, onlyLatest = true)
if (lastItems.size>0) {
val lastItem = lastItems[0]
val lastFolderId = folderIdFromFileId(lastItem.mediaId!!)
if (lastFolderId == parentId) {
val idx = findIndexInFolder(lastItem.mediaId!!)
if (idx >= 0) {
Log.d(LOG_TAG, "This folder can resume last played item ${lastItem.mediaId}")
}
}
}
}
}

}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/eu/zderadicka/audioserve/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import eu.zderadicka.audioserve.net.ApiClient
import eu.zderadicka.audioserve.net.CacheManager
import eu.zderadicka.audioserve.net.MEDIA_CACHE_DIR
import java.io.File
import java.net.URL
import java.util.*

private const val LOG_TAG = "Settings"
Expand Down Expand Up @@ -127,22 +126,23 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen
private val storagesList: List<Pair<String,String>>
get (){

fun fileSize(s:Long): String {
return android.text.format.Formatter.formatShortFileSize(activity, s)
fun freeSize(s:Long): String {
val size = android.text.format.Formatter.formatShortFileSize(activity, s)
return getString(R.string.free_capacity, size)
}

val l = ArrayList<Pair<String,String>>()
val cname = getString(R.string.storage_internal_cache)
val cfile = activity.cacheDir
l.add(Pair(cname + " (${fileSize(cfile.freeSpace)})",cfile.absolutePath))
l.add(Pair(cname + " (${freeSize(cfile.freeSpace)})",cfile.absolutePath))
val sname = R.string.storage_internal
val sfile = activity.filesDir
l.add(Pair(getString(sname) + " (${fileSize(sfile.freeSpace)})",sfile.absolutePath))
l.add(Pair(getString(sname) + " (${freeSize(sfile.freeSpace)})",sfile.absolutePath))

activity.externalMediaDirs.forEachIndexed { index, file ->
if (Environment.getExternalStorageState(file) == Environment.MEDIA_MOUNTED) {
val name = getString(R.string.storage_external, index.toString())
l.add(Pair(name+ " (${fileSize(file.freeSpace)})",file.absolutePath))
l.add(Pair(name+ " (${freeSize(file.freeSpace)})",file.absolutePath))
}
}
return l
Expand Down
28 changes: 20 additions & 8 deletions app/src/main/java/eu/zderadicka/audioserve/data/Recent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object RecentContract {
const val CONTENT_AUTHORITY = "eu.zderadicka.audioserve"
object RecentEntry {
val CONTENT_URI = Uri.parse("content://$CONTENT_AUTHORITY/$RECENT_PATH")
val CONTENT_URI_LATEST = CONTENT_URI.buildUpon().appendPath("latest").build()
const val TABLE_NAME = "recent"
const val _ID = BaseColumns._ID
const val COLUMN_TIMESTAMP = "timestamp"
Expand Down Expand Up @@ -89,13 +90,20 @@ class RecentProvider: ContentProvider() {
}

override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor {
if (uri != RecentEntry.CONTENT_URI) throw UnsupportedOperationException("Invalid content URI")
if (uri == RecentEntry.CONTENT_URI || uri == RecentEntry.CONTENT_URI_LATEST) {

val limit = if (uri == RecentEntry.CONTENT_URI_LATEST) "1" else null

val db = dbHelper.readableDatabase
val c = db.query(RecentEntry.TABLE_NAME, projection
?: RecentEntry.DEFAULT_PROJECTION, selection, selectionArgs,
null, null, RecentEntry.COLUMN_TIMESTAMP + " DESC", limit)
c.setNotificationUri(context.contentResolver, uri)
return c
}

throw UnsupportedOperationException("Invalid content URI")

val db = dbHelper.readableDatabase
val c =db.query(RecentEntry.TABLE_NAME, projection?: RecentEntry.DEFAULT_PROJECTION, selection, selectionArgs,
null,null, RecentEntry.COLUMN_TIMESTAMP + " DESC")
c.setNotificationUri(context.contentResolver, uri)
return c
}

override fun onCreate(): Boolean {
Expand Down Expand Up @@ -131,15 +139,19 @@ fun saveRecent(item:MediaBrowserCompat.MediaItem, context: Context) {
context.contentResolver.insert(RecentEntry.CONTENT_URI, v)
}

fun getRecents(context: Context, exceptPath: String? = null): List<MediaBrowserCompat.MediaItem> {
fun getRecents(context: Context, exceptPath: String? = null, onlyLatest: Boolean = false): List<MediaBrowserCompat.MediaItem> {
var selection:String? = null
var selectionArgs:Array<String?>? = null
if (exceptPath != null) {
selection = "${RecentEntry.COLUMN_FOLDER_PATH} != ?"
selectionArgs = arrayOf(exceptPath)
}
val l = ArrayList<MediaBrowserCompat.MediaItem>()
val c = context.contentResolver.query(RecentEntry.CONTENT_URI, null, selection,selectionArgs,null)
val c = context.contentResolver.query(
if (onlyLatest) RecentEntry.CONTENT_URI_LATEST else RecentEntry.CONTENT_URI,
null, selection,selectionArgs,
null
)
c.use {
while (c.moveToNext()) {
val extras = Bundle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eu.zderadicka.audioserve.fragments

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaBrowserCompat.MediaItem
import android.support.v4.media.MediaMetadataCompat
Expand Down Expand Up @@ -261,6 +262,8 @@ class FolderFragment : MediaFragment() {

private lateinit var folderView: RecyclerView
private lateinit var loadingProgress: ProgressBar
private val handler = Handler()

override val mCallback = object: MediaControllerCompat.Callback() {

override fun onMetadataChanged(metadata: MediaMetadataCompat?) {
Expand Down Expand Up @@ -373,14 +376,23 @@ class FolderFragment : MediaFragment() {
folderViewState = getFolderViewState()
}

//TODO - consider additional caching here to minimize network need
private val showProgress = object: Runnable {
override fun run() {
loadingProgress.visibility = View.VISIBLE
}

}

private fun startLoading() {
mediaActivity?.mediaBrowser?.subscribe(folderId, subscribeCallback)
loadingProgress.visibility = View.VISIBLE
folderView.visibility = View.INVISIBLE
loadingProgress.visibility = View.INVISIBLE
// do not show loading immediatelly for cached and quick responces
handler.postDelayed(showProgress, 500)
folderView.visibility = View.VISIBLE
}

private fun doneLoading(folderDetails: Bundle?, error: Boolean = false, empty: Boolean = false) {
handler.removeCallbacks(showProgress)
loadingProgress.visibility = View.INVISIBLE
folderView.visibility = View.VISIBLE

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 @@ -80,4 +80,5 @@
<string name="sleep_notification_text">You can cancel timer or extend it by actions below</string>
<string name="action_cancel_sleep_timer">Cancel Sleep Timer</string>
<string name="label_sound_notifications">Sound Notifications</string>
<string name="free_capacity">%1$s free</string>
</resources>

0 comments on commit d763b3c

Please sign in to comment.