[Android] Use AppScope when relevant

Geoffrey Métais git at videolan.org
Wed Oct 10 11:48:11 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct 10 11:25:28 2018 +0200| [e2056cdda85d60663ea25b8aadf844c73e27203c] | committer: Geoffrey Métais

Use AppScope when relevant

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

 vlc-android/src/org/videolan/vlc/MediaParsingService.kt        |  3 +--
 vlc-android/src/org/videolan/vlc/RendererDelegate.kt           |  9 +++++----
 vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt    | 10 ++++++----
 .../org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt    | 10 ++++------
 vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt              |  7 ++++---
 .../src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt        | 10 ++--------
 6 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index cd8edf287..84d343931 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -39,7 +39,6 @@ import android.support.v4.content.LocalBroadcastManager
 import android.text.TextUtils
 import android.util.Log
 import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.Main
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
 import org.videolan.libvlc.util.AndroidUtil
@@ -421,7 +420,7 @@ fun reload(ctx: Context) {
     ContextCompat.startForegroundService(ctx, Intent(ACTION_RELOAD, null, ctx, MediaParsingService::class.java))
 }
 
-fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = false, parse: Boolean = true) = GlobalScope.launch(Dispatchers.Main.immediate) {
+fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = false, parse: Boolean = true) = AppScope.launch {
     if (Medialibrary.getInstance().isStarted || !Permissions.canReadStorage(this at startMedialibrary)) return at launch
     val prefs = withContext(Dispatchers.IO) { Settings.getInstance(this at startMedialibrary) }
     val scanOpt = if (AndroidDevices.showTvUi(this at startMedialibrary)) ML_SCAN_ON else prefs.getInt(KEY_MEDIALIBRARY_SCAN, -1)
diff --git a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
index 709c0267f..805fa4ef7 100644
--- a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
@@ -21,11 +21,12 @@ package org.videolan.vlc
 
 import android.arch.lifecycle.LiveData
 import android.arch.lifecycle.MutableLiveData
-import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.Main
-import kotlinx.coroutines.experimental.android.UI
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.withContext
 import org.videolan.libvlc.RendererDiscoverer
 import org.videolan.libvlc.RendererItem
+import org.videolan.vlc.util.AppScope
 import org.videolan.vlc.util.LiveDataset
 import org.videolan.vlc.util.VLCInstance
 import org.videolan.vlc.util.retry
@@ -41,7 +42,7 @@ object RendererDelegate : RendererDiscoverer.EventListener {
     val selectedRenderer: LiveData<RendererItem> = MutableLiveData()
 
     init {
-        ExternalMonitor.connected.observeForever { GlobalScope.launch(Dispatchers.Main.immediate) { if (it == true) start() else stop() } }
+        ExternalMonitor.connected.observeForever { AppScope.launch { if (it == true) start() else stop() } }
     }
 
     suspend fun start() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
index 674a72e5c..9a4d86b40 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
@@ -13,12 +13,14 @@ import android.support.v4.view.ViewCompat
 import android.view.View
 import android.widget.ImageView
 import android.widget.TextView
-import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.Main
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.withContext
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.BR
 import org.videolan.vlc.VLCApplication
+import org.videolan.vlc.util.AppScope
 import org.videolan.vlc.util.HttpImageLoader
 import org.videolan.vlc.util.ThumbnailsProvider
 
@@ -38,12 +40,12 @@ fun loadImage(v: View, item: MediaLibraryItem?) {
     val cacheKey = if (isGroup) "group: ${item.title}" else ThumbnailsProvider.getMediaCacheKey(isMedia, item)
     val bitmap = if (cacheKey !== null) sBitmapCache.getBitmapFromMemCache(cacheKey) else null
     if (bitmap !== null) updateImageView(bitmap, v, binding)
-    else GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { getImage(v, findInLibrary(item, isMedia, isGroup), binding) }
+    else AppScope.launch { getImage(v, findInLibrary(item, isMedia, isGroup), binding) }
 }
 
 @BindingAdapter("imageUri")
 fun downloadIcon(v: View, imageUri: Uri?) {
-    if (imageUri != null && imageUri.scheme == "http") GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) {
+    if (imageUri != null && imageUri.scheme == "http") AppScope.launch {
         val image = withContext(Dispatchers.IO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
         updateImageView(image, v, DataBindingUtil.findBinding(v))
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
index 5386a1eca..fc1b7916d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
@@ -27,15 +27,13 @@ import android.content.Context
 import android.os.Bundle
 import android.support.v4.app.Fragment
 import android.support.v4.app.FragmentActivity
-import kotlinx.coroutines.experimental.Dispatchers
-import kotlinx.coroutines.experimental.GlobalScope
-import kotlinx.coroutines.experimental.android.Main
-import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.SendChannel
 import kotlinx.coroutines.experimental.launch
+import org.videolan.vlc.util.AppScope
 
-open class BaseHeadlessFragment : Fragment() {
+open class
+BaseHeadlessFragment : Fragment() {
     protected var mActivity: FragmentActivity? = null
     var channel: SendChannel<Unit>? = null
 
@@ -66,7 +64,7 @@ open class BaseHeadlessFragment : Fragment() {
     companion object {
 
         internal fun waitForIt(channel: Channel<Unit>, cb: Runnable) {
-            GlobalScope.launch(Dispatchers.Main.immediate) {
+            AppScope.launch {
                 channel.receive()
                 channel.close()
                 cb.run()
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt b/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
index c16479681..ddc317420 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/TvUtil.kt
@@ -36,8 +36,9 @@ import android.support.v17.leanback.widget.Row
 import android.support.v4.content.ContextCompat
 import android.text.TextUtils
 import android.view.View
-import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.Main
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.withContext
 import org.videolan.medialibrary.media.DummyItem
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
@@ -182,7 +183,7 @@ object TvUtil {
     @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
     fun updateBackground(bm: BackgroundManager?, item: Any?) {
         if (bm === null || item === null) return
-        if (item is MediaLibraryItem) GlobalScope.launch(Dispatchers.Main.immediate){
+        if (item is MediaLibraryItem) AppScope.launch {
             val crop = item.itemType != MediaLibraryItem.TYPE_MEDIA || (item as MediaWrapper).type == MediaWrapper.TYPE_AUDIO
             val artworkMrl = item.artworkMrl
             if (!TextUtils.isEmpty(artworkMrl)) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt b/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt
index e9d32d057..4489cb1e7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt
@@ -30,18 +30,12 @@ import android.os.Bundle
 import android.support.v7.app.AppCompatActivity
 import android.view.View
 import kotlinx.coroutines.experimental.Dispatchers
-import kotlinx.coroutines.experimental.GlobalScope
-import kotlinx.coroutines.experimental.IO
-import kotlinx.coroutines.experimental.android.Main
 import kotlinx.coroutines.experimental.launch
 import org.videolan.tools.coroutineScope
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.MlWizardActivityBinding
 import org.videolan.vlc.startMedialibrary
-import org.videolan.vlc.util.KEY_MEDIALIBRARY_SCAN
-import org.videolan.vlc.util.ML_SCAN_OFF
-import org.videolan.vlc.util.ML_SCAN_ON
-import org.videolan.vlc.util.Settings
+import org.videolan.vlc.util.*
 
 class MLWizardActivity : AppCompatActivity() {
 
@@ -64,4 +58,4 @@ class MLWizardActivity : AppCompatActivity() {
     }
 }
 
-fun Context.startMLWizard() = GlobalScope.launch(Dispatchers.Main.immediate) { startActivity(Intent(applicationContext, MLWizardActivity::class.java)) }
\ No newline at end of file
+fun Context.startMLWizard() = AppScope.launch { startActivity(Intent(applicationContext, MLWizardActivity::class.java)) }
\ No newline at end of file



More information about the Android mailing list