[Android] MediaParsingService: clean code

Geoffrey Métais git at videolan.org
Fri Sep 14 17:35:20 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Sep 14 17:35:07 2018 +0200| [5556a0d181c3e01fc18753102bf2b5aa0c2a819c] | committer: Geoffrey Métais

MediaParsingService: clean code

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

 .../src/org/videolan/vlc/MediaParsingService.kt    | 30 ++++++++++++----------
 .../videolan/vlc/gui/wizard/MLWizardActivity.kt    |  5 +++-
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index bdfc4d32e..7f6050cd6 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -22,6 +22,7 @@
  */
 package org.videolan.vlc
 
+import android.annotation.SuppressLint
 import android.app.Service
 import android.arch.lifecycle.MutableLiveData
 import android.content.BroadcastReceiver
@@ -39,7 +40,7 @@ import android.support.v7.preference.PreferenceManager
 import android.text.TextUtils
 import android.util.Log
 import kotlinx.coroutines.experimental.*
-import kotlinx.coroutines.experimental.android.UI
+import kotlinx.coroutines.experimental.android.Main
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
 import org.videolan.libvlc.util.AndroidUtil
@@ -55,7 +56,8 @@ import java.util.*
 private const val TAG = "VLC/MediaParsingService"
 private const val NOTIFICATION_DELAY = 1000L
 
-class MediaParsingService : Service(), DevicesDiscoveryCb {
+class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
+    override val coroutineContext = Dispatchers.Main.immediate
     private lateinit var wakeLock: PowerManager.WakeLock
     private lateinit var localBroadcastManager: LocalBroadcastManager
 
@@ -78,7 +80,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
     internal val sb = StringBuilder()
 
     private val notificationActor by lazy {
-        actor<Notification>(UI, capacity = Channel.UNLIMITED, start = CoroutineStart.UNDISPATCHED) {
+        actor<Notification>(capacity = Channel.UNLIMITED, start = CoroutineStart.UNDISPATCHED) {
             for (update in channel) when (update) {
                 Show -> showNotification()
                 Hide -> hideNotification()
@@ -86,6 +88,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
         }
     }
 
+    @SuppressLint("WakelockTimeout")
     override fun onCreate() {
         super.onCreate()
         localBroadcastManager = LocalBroadcastManager.getInstance(this)
@@ -205,12 +208,12 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
     private suspend fun addDevices(context: Context, addExternal: Boolean) {
         val devices = ArrayList<String>()
         Collections.addAll(devices, *DirectoryRepository.getInstance(context).getMediaDirectories())
-        val sharedPreferences = withContext(IO) { PreferenceManager.getDefaultSharedPreferences(context) }
+        val sharedPreferences = Settings.getInstance(context)
         for (device in devices) {
             val isMainStorage = TextUtils.equals(device, AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
             val uuid = FileUtils.getFileNameFromPath(device)
             if (TextUtils.isEmpty(device) || TextUtils.isEmpty(uuid)) continue
-            val isNew = (isMainStorage || (addExternal && withContext(IO) { File(device).canRead() } ))
+            val isNew = (isMainStorage || (addExternal && withContext(Dispatchers.IO) { File(device).canRead() } ))
                     && medialibrary.addDevice(if (isMainStorage) "main-storage" else uuid, device, !isMainStorage)
             val isIgnored = sharedPreferences.getBoolean("ignore_$uuid", false)
             if (!isMainStorage && isNew && !isIgnored) showStorageNotification(device)
@@ -243,8 +246,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
     private suspend fun updateStorages() {
         serviceLock = true
         val ctx = applicationContext
-        val (sharedPreferences, devices, knownDevices) = withContext(IO) {
-            val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx)
+        val (sharedPreferences, devices, knownDevices) = withContext(Dispatchers.IO) {
+            val sharedPreferences = Settings.getInstance(ctx)
             val devices = AndroidDevices.getExternalStorageDirectories()
             Triple(sharedPreferences, devices, medialibrary.devices)
         }
@@ -257,11 +260,11 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
                 missingDevices.remove("file://$device")
                 continue
             }
-            val isNew = withContext(IO) { medialibrary.addDevice(uuid, device, true) }
+            val isNew = withContext(Dispatchers.IO) { medialibrary.addDevice(uuid, device, true) }
             val isIgnored = sharedPreferences.getBoolean("ignore_$uuid", false)
             if (!isIgnored && isNew) showStorageNotification(device)
         }
-        withContext(IO) { for (device in missingDevices) medialibrary.removeDevice(FileUtils.getFileNameFromPath(device)) }
+        withContext(Dispatchers.IO) { for (device in missingDevices) medialibrary.removeDevice(FileUtils.getFileNameFromPath(device)) }
         serviceLock = false
         exitCommand()
     }
@@ -328,7 +331,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
         if (TextUtils.isEmpty(entryPoint)) --reload
     }
 
-    private fun exitCommand() = launch(UI.immediate) {
+    private fun exitCommand() = launch {
         if (!medialibrary.isWorking && !serviceLock) stopSelf()
     }
 
@@ -378,6 +381,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb {
     }
 
     private val receiver = object : BroadcastReceiver() {
+        @SuppressLint("WakelockTimeout")
         override fun onReceive(context: Context, intent: Intent) {
             when (intent.action) {
                 ACTION_PAUSE_SCAN -> {
@@ -414,9 +418,9 @@ 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) = launch(UI.immediate) {
+fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = false, parse: Boolean = true) = GlobalScope.launch(Dispatchers.Main.immediate) {
     if (Medialibrary.getInstance().isStarted || !Permissions.canReadStorage(this at startMedialibrary)) return at launch
-    val prefs = withContext(IO) { Settings.getInstance(this at startMedialibrary) }
+    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)
     if (parse && scanOpt == -1) {
         if (dbExists(this at startMedialibrary)) prefs.edit().putInt(KEY_MEDIALIBRARY_SCAN, ML_SCAN_ON).apply()
@@ -434,7 +438,7 @@ fun Context.startMedialibrary(firstRun: Boolean = false, upgrade: Boolean = fals
             .putExtra(EXTRA_PARSE, parse && scanOpt == ML_SCAN_ON))
 }
 
-private suspend fun dbExists(context: Context) = withContext(IO) {
+private suspend fun dbExists(context: Context) = withContext(Dispatchers.IO) {
     File(context.getDir("db", Context.MODE_PRIVATE).toString() + Medialibrary.VLC_MEDIA_DB_NAME).exists()
 }
 
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 0f492ec2f..cbd05d1c3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/wizard/MLWizardActivity.kt
@@ -29,7 +29,10 @@ import android.databinding.DataBindingUtil
 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.android.UI
 import kotlinx.coroutines.experimental.launch
 import org.videolan.vlc.R
@@ -61,4 +64,4 @@ class MLWizardActivity : AppCompatActivity() {
     }
 }
 
-fun Context.startMLWizard() = launch(UI) { startActivity(Intent(applicationContext, MLWizardActivity::class.java)) }
\ No newline at end of file
+fun Context.startMLWizard() = GlobalScope.launch(Dispatchers.Main.immediate) { startActivity(Intent(applicationContext, MLWizardActivity::class.java)) }
\ No newline at end of file



More information about the Android mailing list