Skip to content

Commit

Permalink
Added cache to offline browser
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 6, 2018
1 parent 18f44d2 commit 94b13e8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ mediaSessionConnector.setErrorMessageProvider(messageProvider);
else -> {
result.detach()
val t = Thread({
val res = cacheManager.cacheBrowser.listFolder(parentId)
val res = cacheManager.cacheBrowser.listFolder(parentId, options.getBoolean(AUDIOSERVICE_FORCE_RELOAD))
currentFolder = res.filter{it.isPlayable}
result.sendResult(res)
prepareLatestItem(folderIdFromOfflinePath(parentId), options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen

Thread({
ApiClient.clearCache(activity)
CacheManager.clearCache(activity)
CacheManager.clearCache()
activity.runOnUiThread({
Toast.makeText(activity, "Cache cleared", Toast.LENGTH_LONG).show()
})
Expand Down
30 changes: 24 additions & 6 deletions app/src/main/java/eu/zderadicka/audioserve/net/CacheBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import android.media.MediaMetadataRetriever
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaDescriptionCompat
import android.text.format.DateUtils
import android.util.Log
import android.util.LruCache
import eu.zderadicka.audioserve.data.*
import eu.zderadicka.audioserve.utils.splitExtension
import java.io.File
Expand Down Expand Up @@ -40,8 +42,6 @@ private fun fileToItem(mediaId: String, cacheDir: File): MediaBrowserCompat.Medi
try {
val duration = metaExtractor.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
extras.putLong(METADATA_KEY_DURATION, duration!!) // in miliseconds
val bitrate = metaExtractor.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE)?.toInt()

} catch (e: NumberFormatException) {
Log.e(LOG_TAG, "Wrong duration value")
}
Expand Down Expand Up @@ -71,7 +71,10 @@ private fun fileToItem(mediaId: String, cacheDir: File): MediaBrowserCompat.Medi
return item
}

private const val MAX_CACHE_LIVE = 24 * DateUtils.HOUR_IN_MILLIS
class CacheBrowser(val ids: List<String>, val cacheDir:File) {
private val cache = LruCache<String, CacheItem>(16)

val rootFolder :List<MediaBrowserCompat.MediaItem> by lazy() {
listRootFolder()
}
Expand All @@ -80,8 +83,8 @@ class CacheBrowser(val ids: List<String>, val cacheDir:File) {
val rootFolders = HashSet<String>()
for (id in ids) {
val segments = id.split(SEP)
val name_idx = if (segments[0] == "audio") 1 else if (segments[1] == "audio") 2 else continue
rootFolders.add(segments.take(name_idx+1).joinToString(SEP))
val nameIdx = if (segments[0] == "audio") 1 else if (segments[1] == "audio") 2 else continue
rootFolders.add(segments.take(nameIdx+1).joinToString(SEP))
}

return rootFolders.toList().sorted().map{ subfolderToItem(it)}
Expand All @@ -90,7 +93,7 @@ class CacheBrowser(val ids: List<String>, val cacheDir:File) {

}
val FOLDER_RE = Regex("""^(\d+/)?folder/""")
fun listFolder(folderId: String):List<MediaBrowserCompat.MediaItem> {
fun listFolder(folderId: String, noCache:Boolean = false):List<MediaBrowserCompat.MediaItem> {
val folderMatch = FOLDER_RE.find(folderId)
@Suppress("NAME_SHADOWING")
val folderId = if (folderMatch!= null) {
Expand All @@ -99,6 +102,14 @@ class CacheBrowser(val ids: List<String>, val cacheDir:File) {
} else {
folderId
}

if (!noCache) {
val cachedEntry = cache.get(folderId)
if (cachedEntry != null && System.currentTimeMillis() - cachedEntry.timestamp < MAX_CACHE_LIVE) {
return cachedEntry.data
}
}

val folders = HashSet<String>()
val files = ArrayList<String>()
val items = ArrayList<MediaBrowserCompat.MediaItem>()
Expand All @@ -123,6 +134,13 @@ class CacheBrowser(val ids: List<String>, val cacheDir:File) {
if (item == null) continue
items.add(item)
}
cache.put(folderId, CacheItem(System.currentTimeMillis(), items))
return items
}
}

fun clearCache() {
cache.evictAll()
}
private data class CacheItem(val timestamp: Long, val data: List<MediaBrowserCompat.MediaItem>)
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.zderadicka.audioserve.net

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
Expand Down Expand Up @@ -200,7 +201,6 @@ class CacheManager private constructor(val context: Context) {
cacheDir = File(File(cacheBase), MEDIA_CACHE_DIR)
val cacheSize: Long = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_cache_size",
DEFAULT_CACHE_SIZE_MB.toString()).toLong() * 1024 * 1024
val baseUrl:String = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_server_url","")
cache = FileCache(cacheDir,cacheSize, context)
transcode = transcodingFromPrefs(context)
PreferenceManager.getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListener(prefsListener)
Expand All @@ -217,15 +217,14 @@ class CacheManager private constructor(val context: Context) {
cache.stopLoader()
}


@Suppress("UNCHECKED_CAST")
private fun reset() {
cache.stopLoader()
val cacheBase = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_cache_location",
context.cacheDir.absolutePath)!!
cacheDir = File(File(cacheBase), MEDIA_CACHE_DIR)
val cacheSize: Long = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_cache_size",
DEFAULT_CACHE_SIZE_MB.toString()).toLong() * 1024 * 1024
val baseUrl:String = PreferenceManager.getDefaultSharedPreferences(context).getString("pref_server_url","")
val oldListeners: HashSet<FileCache.Listener> = cache.listeners.clone() as HashSet<FileCache.Listener>
cache.removeAllListeners()
cache = FileCache(cacheDir, cacheSize, context)
Expand All @@ -242,6 +241,7 @@ class CacheManager private constructor(val context: Context) {

private fun clearCache() {
cache.resetIndex(true)
cacheBrowser.clearCache()
}

fun isCached(mediaId: String): Boolean {
Expand Down Expand Up @@ -298,6 +298,7 @@ class CacheManager private constructor(val context: Context) {
}

companion object {
@SuppressLint("StaticFieldLeak") // should be OK as we save only app context
@Volatile
private var instance: CacheManager? = null

Expand All @@ -310,7 +311,7 @@ class CacheManager private constructor(val context: Context) {
}

@Synchronized
fun clearCache(context: Context) {
fun clearCache() {
try {
instance?.clearCache()
} catch (e: Exception) {
Expand Down

0 comments on commit 94b13e8

Please sign in to comment.