Commit e5d32d82 authored by Geoffrey Métais's avatar Geoffrey Métais

Change deprecated dispatchers to new ones

parent 773b5a17
......@@ -44,7 +44,7 @@ import android.telephony.TelephonyManager
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
......@@ -748,7 +748,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
if (!this::mediaSession.isInitialized) initMediaSession()
val ctx = this
val length = length
val bob = withContext(CommonPool) {
val bob = withContext(Dispatchers.Default) {
val title = media.nowPlaying ?: media.title
val coverOnLockscreen = settings.getBoolean("lockscreen_cover", true)
val bob = MediaMetadataCompat.Builder().apply {
......
......@@ -30,10 +30,7 @@ import android.content.Intent
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.support.v4.content.ContextCompat
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import kotlinx.coroutines.experimental.*
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.gui.helpers.BitmapUtil
import org.videolan.vlc.gui.video.VideoPlayerActivity
......@@ -88,7 +85,7 @@ class RecommendationsService : IntentService("RecommendationService") {
private fun doRecommendations() = launch (start = CoroutineStart.UNDISPATCHED) {
mNotificationManager.cancelAll()
val videoList = withContext(IO) { VLCApplication.getMLInstance().recentVideos }
val videoList = withContext(Dispatchers.IO) { VLCApplication.getMLInstance().recentVideos }
if (Util.isArrayEmpty(videoList)) return@launch
for ((id, mediaWrapper) in videoList.withIndex()) {
buildRecommendation(mediaWrapper, id, Notification.PRIORITY_DEFAULT)
......
......@@ -21,10 +21,8 @@ package org.videolan.vlc
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
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.LiveDataset
......@@ -48,7 +46,7 @@ object RendererDelegate : RendererDiscoverer.EventListener {
suspend fun start() {
if (started) return
started = true
val libVlc = withContext(CommonPool) { VLCInstance.get() }
val libVlc = withContext(Dispatchers.IO) { VLCInstance.get() }
for (discoverer in RendererDiscoverer.list(libVlc)) {
val rd = RendererDiscoverer(libVlc, discoverer.name)
discoverers.add(rd)
......
......@@ -23,6 +23,8 @@ package org.videolan.vlc.database
import android.arch.persistence.db.SupportSQLiteDatabase
import android.arch.persistence.room.migration.Migration
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.GlobalScope
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.launch
import org.videolan.vlc.VLCApplication
......@@ -196,7 +198,7 @@ val migration_27_28 = object:Migration(27, 28) {
}
}
fun populateDB(context: Context) = launch(IO) {
fun populateDB(context: Context) = GlobalScope.launch(Dispatchers.IO) {
val favRepo = BrowserFavRepository.getInstance(context)
val uris = listOf(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI,
AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI,
......
......@@ -30,11 +30,13 @@ import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.ScrollView
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.tools.coroutineScope
import org.videolan.vlc.BuildConfig
import org.videolan.vlc.R
import org.videolan.vlc.gui.audio.AudioPagerAdapter
......@@ -74,8 +76,8 @@ class AboutFragment : Fragment() {
tabLayout = view.findViewById(R.id.sliding_tabs)
tabLayout.setupWithViewPager(viewPager)
launch(UI.immediate) {
val asset = withContext(IO) {
coroutineScope.launch {
val asset = withContext(Dispatchers.IO) {
Util.readAsset("licence.htm", "").replace("!COMMITID!", revision)
}
UiTools.fillAboutView(view)
......
......@@ -3,11 +3,9 @@ package org.videolan.vlc.gui
import android.support.annotation.MainThread
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.withContext
......
......@@ -47,15 +47,13 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.coroutineScope
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
......@@ -267,14 +265,14 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun updateBackground() {
if (settings.getBoolean("blurred_cover_background", true)) {
launch(UI, CoroutineStart.UNDISPATCHED) {
coroutineScope.launch {
val mw = service?.currentMediaWrapper
if (mw === null || TextUtils.equals(currentCoverArt, mw.artworkMrl)) return@launch
currentCoverArt = mw.artworkMrl
if (TextUtils.isEmpty(mw.artworkMrl)) {
setDefaultBackground()
} else {
val blurredCover = withContext(IO) { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), binding.contentLayout.width)) }
val blurredCover = withContext(Dispatchers.IO) { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), binding.contentLayout.width)) }
if (blurredCover !== null) {
val activity = activity as? AudioPlayerContainerActivity
if (activity === null) return@launch
......
......@@ -37,13 +37,11 @@ import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.*
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
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.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.databinding.DirectoryBrowserBinding
import org.videolan.vlc.gui.InfoActivity
......@@ -366,9 +364,9 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
}
private fun toggleFavorite() = launch(UI.immediate, CoroutineStart.UNDISPATCHED) {
private fun toggleFavorite() = coroutineScope.launch {
val mw = currentMedia ?: return@launch
withContext(IO) {
withContext(Dispatchers.IO) {
when {
browserFavRepository.browserFavExists(mw.uri) -> browserFavRepository.deleteBrowserFav(mw.uri)
mw.uri.scheme == "file" -> browserFavRepository.addLocalFavItem(mw.uri, mw.title, mw.artworkURL)
......@@ -408,7 +406,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {
if (mActionMode == null && item.itemType == MediaLibraryItem.TYPE_MEDIA) launch(UI.immediate) {
if (mActionMode == null && item.itemType == MediaLibraryItem.TYPE_MEDIA) coroutineScope.launch {
val mw = item as MediaWrapper
if (mw.uri.scheme == "content" || mw.uri.scheme == OTG_SCHEME) return@launch
var flags = if (!isRootDirectory && this@BaseBrowserFragment is FileBrowserFragment) CTX_DELETE else 0
......@@ -419,7 +417,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
val isFileBrowser = this@BaseBrowserFragment is FileBrowserFragment && item.uri.scheme == "file"
val isNetworkBrowser = this@BaseBrowserFragment is NetworkBrowserFragment
if (isFileBrowser || isNetworkBrowser) {
val favExists = withContext(IO) { browserFavRepository.browserFavExists(mw.uri) }
val favExists = withContext(Dispatchers.IO) { browserFavRepository.browserFavExists(mw.uri) }
flags = if (favExists) {
if (isNetworkBrowser) flags or CTX_FAV_EDIT or CTX_FAV_REMOVE
else flags or CTX_FAV_REMOVE
......@@ -455,7 +453,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
CTX_ADD_TO_PLAYLIST -> UiTools.addToPlaylist(requireActivity(), mw.tracks, SavePlaylistDialog.KEY_NEW_TRACKS)
CTX_DOWNLOAD_SUBTITLES -> MediaUtils.getSubs(requireActivity(), mw)
CTX_FAV_REMOVE -> launch(IO) { browserFavRepository.deleteBrowserFav(mw.uri) }
CTX_FAV_REMOVE -> coroutineScope.launch(Dispatchers.IO) { browserFavRepository.deleteBrowserFav(mw.uri) }
}
}
......
......@@ -31,6 +31,7 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.View
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.runBlocking
import org.videolan.medialibrary.media.MediaLibraryItem
......@@ -111,10 +112,8 @@ class FilePickerFragment : FileBrowserFragment() {
override fun defineIsRoot() = mrl?.run {
if (startsWith("file")) {
//TODO: remove this after upgrade to kotlinx v 0.25+
System.setProperty("kotlinx.coroutines.blocking.checker", "disable")
val path = Strings.removeFileProtocole(this@run)
val rootDirectories = runBlocking(IO) { DirectoryRepository.getInstance(requireContext()).getMediaDirectories() }
val rootDirectories = runBlocking(Dispatchers.IO) { DirectoryRepository.getInstance(requireContext()).getMediaDirectories() }
for (directory in rootDirectories) if (path.startsWith(directory)) return false
return true
} else length < 7
......
......@@ -36,11 +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.CommonPool
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
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
......@@ -185,22 +183,19 @@ 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) launch(UI, CoroutineStart.UNDISPATCHED){
if (item is MediaLibraryItem) GlobalScope.launch(Dispatchers.Main.immediate){
val crop = item.itemType != MediaLibraryItem.TYPE_MEDIA || (item as MediaWrapper).type == MediaWrapper.TYPE_AUDIO
val artworkMrl = item.artworkMrl
if (!TextUtils.isEmpty(artworkMrl)) {
val blurred = withContext(CommonPool) {
if (bm == null) return@withContext null
val blurred = withContext(Dispatchers.IO) {
var cover: Bitmap? = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 512)
?: return@withContext null
if (crop)
cover = BitmapUtil.centerCrop(cover, cover!!.width, cover.width * 10 / 16)
UiTools.blurBitmap(cover, 10f)
}
if (bm !== null) {
bm.color = 0
bm.drawable = BitmapDrawable(VLCApplication.getAppResources(), blurred)
}
bm.color = 0
bm.drawable = BitmapDrawable(VLCApplication.getAppResources(), blurred)
}
}
clearBackground(bm)
......
......@@ -34,6 +34,7 @@ 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
......@@ -54,7 +55,7 @@ class MLWizardActivity : AppCompatActivity() {
@SuppressLint("ApplySharedPref")
@Suppress("UNUSED_PARAMETER")
fun apply(v: View) = GlobalScope.launch(Dispatchers.IO) {
fun apply(v: View) = coroutineScope.launch(Dispatchers.IO) {
val parse = binding.wizardCheckScan.isChecked
val prefs = Settings.getInstance(this@MLWizardActivity)
prefs.edit().putInt(KEY_MEDIALIBRARY_SCAN, if (parse) ML_SCAN_ON else ML_SCAN_OFF).commit()
......
......@@ -10,11 +10,9 @@ import android.support.v7.preference.PreferenceManager
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.RendererItem
......@@ -30,12 +28,14 @@ import org.videolan.vlc.gui.preferences.PreferencesFragment
import org.videolan.vlc.gui.video.VideoPlayerActivity
import org.videolan.vlc.util.*
import java.util.*
import kotlin.coroutines.experimental.CoroutineContext
private const val TAG = "VLC/PlaylistManager"
private const val PREVIOUS_LIMIT_DELAY = 5000L
private const val AUDIO_REPEAT_MODE_KEY = "audio_repeat_mode"
class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventListener, Media.EventListener {
class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventListener, Media.EventListener, CoroutineScope {
override val coroutineContext = Dispatchers.Main.immediate
companion object {
val showAudioPlayer = MutableLiveData<Boolean>().apply { value = false }
......@@ -121,7 +121,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
mediaList.addEventListener(this)
stopAfter = -1
clearABRepeat()
launch(UI.immediate) {
launch {
playIndex(position)
onPlaylistLoaded()
}
......@@ -143,9 +143,9 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
loadingLastPlaylist = false
return false
}
launch(UI.immediate) {
val playList = withContext(CommonPool) {
locations.map { Uri.decode(it) }.mapTo(ArrayList(locations.size)) { MediaWrapper(Uri.parse(it)) }
launch {
val playList = withContext(Dispatchers.Default) {
locations.asSequence().map { Uri.decode(it) }.mapTo(ArrayList(locations.size)) { MediaWrapper(Uri.parse(it)) }
}
// load playlist
shuffling = settings.getBoolean(if (audio) "audio_shuffling" else "media_shuffling", false)
......@@ -168,7 +168,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
private suspend fun onPlaylistLoaded() {
service.onPlaylistLoaded()
determinePrevAndNextIndices()
launch(IO) { mediaList.updateWithMLMeta() }
launch(Dispatchers.IO) { mediaList.updateWithMLMeta() }
}
fun play() {
......@@ -192,7 +192,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
return
}
videoBackground = !player.isVideoPlaying() && player.canSwitchToVideo()
launch(UI.immediate) { playIndex(currentIndex) }
launch { playIndex(currentIndex) }
}
fun stop(systemExit: Boolean = false) {
......@@ -226,7 +226,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
player.stop()
return
}
launch(UI.immediate) { playIndex(currentIndex) }
launch { playIndex(currentIndex) }
} else player.setPosition(0F)
}
......@@ -235,7 +235,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
if (shuffling) previous.clear()
shuffling = !shuffling
savePosition()
launch(UI.immediate) { determinePrevAndNextIndices() }
launch { determinePrevAndNextIndices() }
}
@MainThread
......@@ -244,7 +244,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
if (isAudioList() && settings.getBoolean("audio_save_repeat", false))
settings.edit().putInt(AUDIO_REPEAT_MODE_KEY, repeating).apply()
savePosition()
launch(UI.immediate) { determinePrevAndNextIndices() }
launch { determinePrevAndNextIndices() }
}
fun setRenderer(item: RendererItem?) {
......@@ -302,7 +302,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
newMedia = true
determinePrevAndNextIndices()
service.onNewPlayback()
if (settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)) launch {
if (settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)) launch(Dispatchers.IO) {
var id = mw.id
if (id == 0L) {
var internalMedia = medialibrary.findMedia(mw)
......@@ -366,7 +366,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
override fun onItemAdded(index: Int, mrl: String?) {
if (BuildConfig.DEBUG) Log.i(TAG, "CustomMediaListItemAdded")
if (currentIndex >= index && !expanding) ++currentIndex
launch(UI.immediate) {
launch {
determinePrevAndNextIndices()
executeUpdate()
saveMediaList()
......@@ -378,7 +378,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
if (BuildConfig.DEBUG) Log.i(TAG, "CustomMediaListItemDeleted")
val currentRemoved = currentIndex == index
if (currentIndex >= index && !expanding) --currentIndex
launch(UI.immediate) {
launch {
determinePrevAndNextIndices()
if (currentRemoved && !expanding) {
when {
......@@ -467,7 +467,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
// If we are in random mode, we completely reset the stored previous track
// as their indices changed.
previous.clear()
launch(UI.immediate) {
launch {
determinePrevAndNextIndices()
executeUpdate()
saveMediaList()
......
package org.videolan.vlc.util
import android.arch.lifecycle.MutableLiveData
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.MediaWrapper
......@@ -20,7 +20,7 @@ open class FilterDelegate<T : MediaLibraryItem>(protected val dataset: MutableLi
protected open suspend fun filteringJob(charSequence: CharSequence?) : MutableList<T>? {
if (charSequence !== null) initSource()?.let {
return withContext(CommonPool) { mutableListOf<T>().apply {
return withContext(Dispatchers.Default) { mutableListOf<T>().apply {
val queryStrings = charSequence.trim().toString().split(" ").filter { it.length > 2 }
for (item in it) for (query in queryStrings)
if (item.title.contains(query, true)) {
......@@ -48,10 +48,10 @@ open class FilterDelegate<T : MediaLibraryItem>(protected val dataset: MutableLi
class PlaylistFilterDelegate(dataset: MutableLiveData<out List<MediaWrapper>>) : FilterDelegate<MediaWrapper>(dataset) {
override suspend fun filteringJob(charSequence: CharSequence?): MutableList<MediaWrapper>? {
if (charSequence !== null) initSource()?.let {
return withContext(CommonPool) { mutableListOf<MediaWrapper>().apply {
val queryStrings = charSequence.trim().toString().split(" ").filter { it.length > 2 }.map { it.toLowerCase() }
for (media in it) {
if (charSequence !== null) initSource()?.let { list ->
return withContext(Dispatchers.Default) { mutableListOf<MediaWrapper>().apply {
val queryStrings = charSequence.trim().toString().split(" ").asSequence().filter { it.length > 2 }.map { it.toLowerCase() }.toList()
for (media in list) {
val title = MediaUtils.getMediaTitle(media).toLowerCase()
val location = media.location.toLowerCase()
val artist = MediaUtils.getMediaArtist(VLCApplication.getAppContext(), media).toLowerCase()
......
......@@ -7,6 +7,7 @@ import android.content.SharedPreferences
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v7.preference.PreferenceManager
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.withContext
......@@ -63,7 +64,7 @@ fun Context.getAppSystemService(name: String) = applicationContext.getSystemServ
fun Long.random() = (Random().nextFloat() * this).toLong()
suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(IO) {
suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(Dispatchers.IO) {
val ml = Medialibrary.getInstance()
if (ml.isInitiated) block.invoke(ml)
else suspendCoroutine { continuation ->
......
......@@ -30,10 +30,7 @@ import android.support.media.tv.TvContractCompat
import android.support.media.tv.WatchNextProgram
import android.text.TextUtils
import android.util.Log
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import kotlinx.coroutines.experimental.*
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.*
......@@ -46,8 +43,8 @@ private const val TAG = "VLC/TvChannels"
private const val MAX_RECOMMENDATIONS = 3
@RequiresApi(Build.VERSION_CODES.O)
fun setChannel(context: Context) = launch(start = CoroutineStart.UNDISPATCHED) {
val channelId = withContext(IO) {
fun setChannel(context: Context) = GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
val channelId = withContext(Dispatchers.IO) {
val prefs = Settings.getInstance(context)
val name = context.getString(R.string.tv_my_new_videos)
createOrUpdateChannel(prefs, context, name, R.drawable.icon, BuildConfig.APPLICATION_ID)
......@@ -63,15 +60,15 @@ suspend fun updatePrograms(context: Context, channelId: Long) {
override fun onMedialibraryIdle() {}
override fun onMedialibraryReady() {
ml.removeOnMedialibraryReadyListener(this)
launch { updatePrograms(context, channelId) }
GlobalScope.launch { updatePrograms(context, channelId) }
}
})
context.startMedialibrary(false, false, false)
return
}
val programs = withContext(IO) { existingPrograms(context, channelId) }
val videoList = withContext(IO) { VLCApplication.getMLInstance().recentVideos }
val programs = withContext(Dispatchers.IO) { existingPrograms(context, channelId) }
val videoList = withContext(Dispatchers.IO) { VLCApplication.getMLInstance().recentVideos }
if (Util.isArrayEmpty(videoList)) return
val cn = ComponentName(context, PreviewVideoInputService::class.java)
for ((count, mw) in videoList.withIndex()) {
......@@ -85,13 +82,13 @@ suspend fun updatePrograms(context: Context, channelId: Long) {
mw.artUri(), mw.length.toInt(), mw.time.toInt(),
mw.width, mw.height, BuildConfig.APPLICATION_ID)
val program = buildProgram(cn, desc)
launch(IO) {
GlobalScope.launch(Dispatchers.IO) {
context.contentResolver.insert(TvContractCompat.PreviewPrograms.CONTENT_URI, program.toContentValues())
}
if (count - programs.size >= MAX_RECOMMENDATIONS) break
}
for (program in programs) {
withContext(IO) { context.contentResolver.delete(TvContractCompat.buildPreviewProgramUri(program.programId), null, null) }
withContext(Dispatchers.IO) { context.contentResolver.delete(TvContractCompat.buildPreviewProgramUri(program.programId), null, null) }
}
}
......
......@@ -63,7 +63,7 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
@Suppress("UNCHECKED_CAST")
protected val updateActor by lazy {
actor<Update>(UI, capacity = Channel.UNLIMITED) {
actor<Update>(capacity = Channel.UNLIMITED) {
for (update in channel) when (update) {
Refresh -> updateList()
is Filter -> filter.filter(update.query)
......
......@@ -25,6 +25,7 @@ import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.Fragment
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.launch
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.media.MediaWrapper
......@@ -33,7 +34,7 @@ import org.videolan.vlc.util.EmptyPBSCallback
import org.videolan.vlc.util.LiveDataset
import org.videolan.vlc.util.PlaylistFilterDelegate
class PlaylistModel(private val service: PlaybackService) : ViewModel(), PlaybackService.Callback by EmptyPBSCallback {
class PlaylistModel(private val service: PlaybackService) : ScopedModel(), PlaybackService.Callback by EmptyPBSCallback {
val dataset = LiveDataset<MediaWrapper>()
val progress by lazy(LazyThreadSafetyMode.NONE) {
......@@ -55,7 +56,7 @@ class PlaylistModel(private val service: PlaybackService) : ViewModel(), Playbac
dataset.value = service.medias.toMutableList()
}
fun filter(query: CharSequence?) = launch { filter.filter(query) }
fun filter(query: CharSequence?) = launch(Dispatchers.Default) { filter.filter(query) }
public override fun onCleared() {
service.removeCallback(this)
......
......@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -51,7 +52,7 @@ class AlbumModel(context: Context, val parent: MediaLibraryItem? = null): AudioM
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(IO) { when (parent) {
dataset.value = withContext(Dispatchers.IO) { when (parent) {
is Artist -> parent.getAlbums(sort, desc)
is Genre -> parent.getAlbums(sort, desc)
else -> medialibrary.getAlbums(sort, desc)
......
......@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -44,7 +45,7 @@ class ArtistModel(context: Context, private var showAll: Boolean = false): Audio
//VLCApplication.getSettings().getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false)
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(IO) {
dataset.value = withContext(Dispatchers.IO) {
medialibrary.getArtists(showAll, sort, desc).toMutableList() as MutableList<MediaLibraryItem>
}
}
......
......@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -50,7 +51,7 @@ class GenresModel(context: Context): AudioModel(context), Medialibrary.GenresCb
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(IO) {
dataset.value = withContext(Dispatchers.IO) {
medialibrary.getGenres(sort, desc).toMutableList() as MutableList<MediaLibraryItem>
}
}
......
......@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -46,7 +47,7 @@ class PlaylistsModel(context: Context): AudioModel(context), Medialibrary.Playli
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(IO) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
dataset.value = withContext(Dispatchers.IO) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
......
......@@ -23,6 +23,7 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -69,7 +70,7 @@ class TracksModel(context: Context, val parent: MediaLibraryItem? = null): Audio
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(IO) {
dataset.value = withContext(Dispatchers.IO) {
when (parent) {
is Artist -> parent.getTracks(sort, desc)</