[Android] Use app context for media fetching

Geoffrey Métais git at videolan.org
Thu Nov 28 11:41:13 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Nov 28 09:47:21 2019 +0100| [677e4eab0e7a180cc168f696e290d3c6b82f622b] | committer: Geoffrey Métais

Use app context for media fetching

> https://code.videolan.org/videolan/vlc-android/commit/677e4eab0e7a180cc168f696e290d3c6b82f622b
---

 .../src/org/videolan/vlc/MediaParsingService.kt    |  2 +-
 .../videolan/vlc/moviepedia/MoviepediaIndexer.kt   | 65 +++++++++++-----------
 2 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 2a22a5d92..97f84021e 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -382,7 +382,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb, LifecycleOwn
             lastNotificationTime = 0L
             //todo reenable entry point when ready
             if (BuildConfig.DEBUG) try {
-                MoviepediaIndexer.indexMedialib(this at MediaParsingService)
+                MoviepediaIndexer.indexMedialib(applicationContext)
             } catch (e: Exception) {
                 if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "${e.cause}")
             }
diff --git a/vlc-android/src/org/videolan/vlc/moviepedia/MoviepediaIndexer.kt b/vlc-android/src/org/videolan/vlc/moviepedia/MoviepediaIndexer.kt
index b448061e9..0ff034ad2 100644
--- a/vlc-android/src/org/videolan/vlc/moviepedia/MoviepediaIndexer.kt
+++ b/vlc-android/src/org/videolan/vlc/moviepedia/MoviepediaIndexer.kt
@@ -27,7 +27,10 @@ package org.videolan.vlc.moviepedia
 import android.content.Context
 import android.net.Uri
 import android.util.Log
-import kotlinx.coroutines.*
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.MainScope
+import kotlinx.coroutines.launch
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.vlc.BuildConfig
@@ -44,40 +47,38 @@ import org.videolan.vlc.util.getLocaleLanguages
 object MoviepediaIndexer : CoroutineScope by MainScope() {
 
     fun indexMedialib(context: Context) {
-        launch {
-            withContext(Dispatchers.IO) {
-                val medias = context.getFromMl { getPagedVideos(AbstractMedialibrary.SORT_DEFAULT, false, 1000, 0) }
-
-                val filesToIndex = HashMap<Long, Uri>()
-                medias.forEach {
-                    if (it.getMetaLong(AbstractMediaWrapper.META_METADATA_RETRIEVED) != 1L)
-                        filesToIndex[it.id] = it.uri
-                }
-                if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for ${filesToIndex.size} files")
-                for (filesToIndex in filesToIndex) {
-                    if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for: ${filesToIndex.value.lastPathSegment}")
-                }
-                val repo = MoviepediaApiRepository.getInstance()
-                val results = try {
-                    repo.searchMediaBatch(filesToIndex)
-                } catch (e: Exception) {
-                    return at withContext
-                }
-                medias.forEach { media ->
-                    media?.setLongMeta(AbstractMediaWrapper.META_METADATA_RETRIEVED, 1L)
-                }
-                results.forEach { result ->
-                    result.lucky?.let {
-                        val media = medias.find { it.id == result.id.toLong() }
-                        try {
-                            saveMediaMetadata(context, media, it, retrieveCast = false, removePersonOrphans = false)
-                        } catch (e: Exception) {
-                            media?.setLongMeta(251, 0L)
-                        }
+        launch(Dispatchers.IO) {
+            val medias = context.getFromMl { getPagedVideos(AbstractMedialibrary.SORT_DEFAULT, false, 1000, 0) }
+
+            val filesToIndex = HashMap<Long, Uri>()
+            medias.forEach {
+                if (it.getMetaLong(AbstractMediaWrapper.META_METADATA_RETRIEVED) != 1L)
+                    filesToIndex[it.id] = it.uri
+            }
+            if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for ${filesToIndex.size} files")
+            for (filesToIndex in filesToIndex) {
+                if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for: ${filesToIndex.value.lastPathSegment}")
+            }
+            val repo = MoviepediaApiRepository.getInstance()
+            val results = try {
+                repo.searchMediaBatch(filesToIndex)
+            } catch (e: Exception) {
+                return at launch
+            }
+            medias.forEach { media ->
+                media?.setLongMeta(AbstractMediaWrapper.META_METADATA_RETRIEVED, 1L)
+            }
+            results.forEach { result ->
+                result.lucky?.let {
+                    val media = medias.find { it.id == result.id.toLong() }
+                    try {
+                        saveMediaMetadata(context, media, it, retrieveCast = false, removePersonOrphans = false)
+                    } catch (e: Exception) {
+                        media?.setLongMeta(251, 0L)
                     }
                 }
-                removePersonOrphans(context)
             }
+            removePersonOrphans(context)
         }
     }
 



More information about the Android mailing list