[Android] Content indexing: trigger by message bus

Geoffrey Métais git at videolan.org
Tue Mar 3 11:03:16 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Mar  2 13:16:45 2020 +0100| [3ad39c971fe3000e182f7e1eddb84c43040d6fdc] | committer: Geoffrey Métais

Content indexing: trigger by message bus

This remove the use of AppContextProvider

> https://code.videolan.org/videolan/vlc-android/commit/3ad39c971fe3000e182f7e1eddb84c43040d6fdc
---

 .../java/org/videolan/mobile/app/AppSetupDelegate.kt   | 18 ++++++++++++++----
 .../java/org/videolan/resources/AppContextProvider.kt  |  2 --
 .../src/main/java/org/videolan/resources/Constants.kt  |  1 +
 .../main/java/org/videolan/tools/KotlinExtensions.kt   |  7 +++++--
 .../src/org/videolan/vlc/MediaParsingService.kt        |  9 ++-------
 5 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
index 18fb2ab03..cb482f608 100644
--- a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
+++ b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
@@ -20,8 +20,7 @@
 package org.videolan.mobile.app
 
 import android.annotation.TargetApi
-import android.content.ComponentName
-import android.content.Context
+import android.content.*
 import android.content.pm.PackageManager
 import android.os.Build
 import kotlinx.coroutines.Dispatchers
@@ -34,16 +33,18 @@ import org.videolan.libvlc.interfaces.ILibVLCFactory
 import org.videolan.libvlc.interfaces.IMediaFactory
 import org.videolan.moviepedia.MediaScraper
 import org.videolan.moviepedia.provider.MediaScrapingTvshowProvider
+import org.videolan.resources.ACTION_CONTENT_INDEXING
 import org.videolan.resources.AppContextProvider
+import org.videolan.resources.VLCInstance
 import org.videolan.tools.AppScope
 import org.videolan.tools.Settings
+import org.videolan.tools.localBroadcastManager
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.gui.SendCrashActivity
 import org.videolan.vlc.gui.helpers.AudioUtil
 import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.util.DialogDelegate
 import org.videolan.vlc.util.SettingsMigration
-import org.videolan.resources.VLCInstance
 
 interface AppDelegate {
     val appContextProvider : AppContextProvider
@@ -70,7 +71,7 @@ class AppSetupDelegate : AppDelegate {
         }
 
         // Setup Moviepedia indexing after Medialibrary scan
-        (AppContextProvider.indexingListeners as MutableList).add(MediaScraper.indexListener)
+        localBroadcastManager.registerReceiver(indexingReceiver, IntentFilter(ACTION_CONTENT_INDEXING))
 
         //Initiate Kotlinx Dispatchers in a thread to prevent ANR
         backgroundInit()
@@ -93,4 +94,13 @@ class AppSetupDelegate : AppDelegate {
             SettingsMigration.migrateSettings(this)
         }).start()
     }
+
+    private val indexingReceiver = object : BroadcastReceiver() {
+        override fun onReceive(context: Context?, intent: Intent?) {
+            AppScope.launch {
+                MediaScraper.indexListener.onIndexingDone()
+            }
+        }
+
+    }
 }
\ No newline at end of file
diff --git a/application/resources/src/main/java/org/videolan/resources/AppContextProvider.kt b/application/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
index 2acb27256..64c042e94 100644
--- a/application/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
+++ b/application/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
@@ -30,14 +30,12 @@ import android.content.ContextWrapper
 import android.content.res.Resources
 import androidx.collection.SimpleArrayMap
 import org.videolan.resources.interfaces.IMediaContentResolver
-import org.videolan.resources.interfaces.IndexingListener
 import org.videolan.tools.wrap
 import java.lang.reflect.InvocationTargetException
 
 object AppContextProvider {
 
     //Store dependency inversion related instances to avoid GC
-    val indexingListeners : List<IndexingListener> = mutableListOf()
     val mediaContentResolvers = SimpleArrayMap<String, IMediaContentResolver>()
 
     private lateinit var context: Context
diff --git a/application/resources/src/main/java/org/videolan/resources/Constants.kt b/application/resources/src/main/java/org/videolan/resources/Constants.kt
index 2e97d4cf7..78cdf9997 100644
--- a/application/resources/src/main/java/org/videolan/resources/Constants.kt
+++ b/application/resources/src/main/java/org/videolan/resources/Constants.kt
@@ -71,6 +71,7 @@ const val EXTRA_PATH = "extra_path"
 const val EXTRA_UUID = "extra_uuid"
 const val ACTION_RESUME_SCAN = "action_resume_scan"
 const val ACTION_PAUSE_SCAN = "action_pause_scan"
+const val ACTION_CONTENT_INDEXING = "action_content_indexing"
 
 // VideoPlayerActivity
 @JvmField val PLAY_FROM_VIDEOGRID = "gui.video.PLAY_FROM_VIDEOGRID".buildPkgString()
diff --git a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
index 141586968..19eb29130 100644
--- a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
+++ b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -13,10 +13,10 @@ import android.util.Patterns
 import android.util.TypedValue
 import android.view.View
 import androidx.annotation.AttrRes
-import androidx.core.content.ContextCompat.getSystemService
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.ProcessLifecycleOwner
+import androidx.localbroadcastmanager.content.LocalBroadcastManager
 import kotlinx.coroutines.CancellationException
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.channels.Channel
@@ -121,4 +121,7 @@ fun <E> SendChannel<E>.safeOffer(value: E) = !isClosedForSend && try {
 @SuppressLint("MissingPermission")
 fun Context.isConnected(): Boolean {
     return (getSystemService(CONNECTIVITY_SERVICE) as? ConnectivityManager?)?.activeNetworkInfo?.isConnected == true
-}
\ No newline at end of file
+}
+
+val Context.localBroadcastManager: LocalBroadcastManager
+    get() = LocalBroadcastManager.getInstance(this)
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index c817bf775..07a1c6175 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -37,6 +37,7 @@ import android.text.TextUtils
 import android.util.Log
 import androidx.core.content.ContextCompat
 import androidx.lifecycle.*
+import androidx.localbroadcastmanager.content.LocalBroadcastManager
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.ActorScope
 import kotlinx.coroutines.channels.Channel
@@ -48,7 +49,6 @@ import org.videolan.medialibrary.interfaces.DevicesDiscoveryCb
 import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.medialibrary.stubs.StubMedialibrary
 import org.videolan.resources.*
-import org.videolan.resources.interfaces.IndexingListener
 import org.videolan.resources.util.dbExists
 import org.videolan.tools.*
 import org.videolan.vlc.gui.SendCrashActivity
@@ -85,7 +85,6 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
     @Volatile
     private var discoverTriggered = false
     internal val sb = StringBuilder()
-    val indexingListeners : List<IndexingListener> = AppContextProvider.indexingListeners
     private lateinit var actions : SendChannel<MLAction>
     private lateinit var notificationActor : SendChannel<Notification>
 
@@ -394,12 +393,8 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
         if (!medialibrary.isWorking && !serviceLock && !discoverTriggered) {
             lastNotificationTime = 0L
             if (wakeLock.isHeld) wakeLock.release()
+            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(ACTION_CONTENT_INDEXING))
             //todo reenable entry point when ready
-            if (BuildConfig.DEBUG) try {
-                indexingListeners.forEach { it.onIndexingDone() }
-            } catch (e: Exception) {
-                if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "${e.cause}")
-            }
             if (::notificationActor.isInitialized) notificationActor.safeOffer(Hide)
             //Delay service stop to ensure service goes foreground.
             // Otherwise, we get some RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()



More information about the Android mailing list