[Android] Code cleaning

Geoffrey Métais git at videolan.org
Thu Aug 8 13:54:21 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug  8 11:00:51 2019 +0200| [9cf087945b63ddfdd4d14e4f4e379323261e79b4] | committer: Geoffrey Métais

Code cleaning

(cherry picked from commit 57a94f84f899c6f005298370704f84a1f9818cfd)

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

 vlc-android/src/org/videolan/vlc/StartActivity.kt  | 13 +++-----
 .../videolan/vlc/gui/dialogs/SavePlaylistDialog.kt | 39 +++++++++++-----------
 2 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.kt b/vlc-android/src/org/videolan/vlc/StartActivity.kt
index 02dc64aa1..b1acaeb50 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.kt
@@ -50,7 +50,8 @@ import videolan.org.commontools.TV_CHANNEL_PATH_VIDEO
 import videolan.org.commontools.TV_CHANNEL_QUERY_VIDEO_ID
 import videolan.org.commontools.TV_CHANNEL_SCHEME
 
-const val SEND_CRASH_RESULT = 0
+private const val SEND_CRASH_RESULT = 0
+private const val TAG = "VLC/StartActivity"
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
 class StartActivity : FragmentActivity() {
@@ -94,8 +95,8 @@ class StartActivity : FragmentActivity() {
         val intent = intent
         val action = intent?.action
 
-        if ((Intent.ACTION_VIEW == action || "org.chromium.arc.intent.action.VIEW" == action) && intent.data != null
-                && TV_CHANNEL_SCHEME != intent.data!!.scheme) {
+        if ((Intent.ACTION_VIEW == action || "org.chromium.arc.intent.action.VIEW" == action)
+                && TV_CHANNEL_SCHEME != intent.data?.scheme) {
             startPlaybackFromApp(intent)
             return
         } else if (Intent.ACTION_SEND == action) {
@@ -183,7 +184,7 @@ class StartActivity : FragmentActivity() {
             if (target != 0) intent.putExtra(EXTRA_TARGET, target)
             startActivity(intent)
         } else {
-            this at StartActivity.startOnboarding()
+            startOnboarding()
         }
     }
 
@@ -199,8 +200,4 @@ class StartActivity : FragmentActivity() {
         return AndroidDevices.isAndroidTv || !AndroidDevices.isChromeBook && !AndroidDevices.hasTsp ||
                 Settings.getInstance(this).getBoolean("tv_ui", false)
     }
-
-    companion object {
-        const val TAG = "VLC/StartActivity"
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt b/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
index a22d32a7e..2fa30ffd6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
@@ -36,18 +36,20 @@ import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
 import com.google.android.material.textfield.TextInputLayout
+import kotlinx.coroutines.*
 import org.videolan.medialibrary.Tools
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.interfaces.media.AbstractPlaylist
 import org.videolan.medialibrary.media.MediaLibraryItem
+import org.videolan.tools.isStarted
 import org.videolan.vlc.R
-import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.SimpleAdapter
-import org.videolan.vlc.util.runIO
 import java.util.*
 
-class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener, TextView.OnEditorActionListener, SimpleAdapter.ClickHandler {
+class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
+        TextView.OnEditorActionListener, SimpleAdapter.ClickHandler,
+        CoroutineScope by MainScope() {
     override fun getDefaultState(): Int = STATE_EXPANDED
 
     override fun needToManageOrientation(): Boolean = false
@@ -60,7 +62,7 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     private lateinit var tracks: Array<AbstractMediaWrapper>
     private lateinit var newTrack: Array<AbstractMediaWrapper>
     private lateinit var medialibrary: AbstractMedialibrary
-    private var playlist: AbstractPlaylist? = null
+    private var currentPLaylist: AbstractPlaylist? = null
 
     override fun initialFocusedView(): View = listView
 
@@ -115,21 +117,20 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     }
 
     override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
-        if (actionId == EditorInfo.IME_ACTION_SEND)
-            savePlaylist()
+        if (actionId == EditorInfo.IME_ACTION_SEND) savePlaylist()
         return false
     }
 
-    private fun savePlaylist() {
-        runIO(Runnable {
-            val name = editText!!.text.toString().trim { it <= ' ' }
+    private fun savePlaylist() = launch{
+        withContext(Dispatchers.IO) {
+            val name = editText?.text?.toString()?.trim { it <= ' ' } ?: return at withContext
             val addTracks = !Tools.isArrayEmpty(newTrack)
-            var playlist: AbstractPlaylist? = if (playlist != null && playlist!!.title == name) medialibrary.getPlaylist(playlist!!.id) else null
-            val exists = playlist != null
+            var playlist = if (currentPLaylist?.title == name) {
+                medialibrary.getPlaylist(currentPLaylist!!.id)
+            } else {
+                medialibrary.getPlaylistByName(name) ?: medialibrary.createPlaylist(name) ?: return at withContext
+            }
             val playlistTracks: Array<AbstractMediaWrapper>?
-            if (!exists) playlist = medialibrary.getPlaylistByName(name)
-            if (playlist == null) playlist = medialibrary.createPlaylist(name)
-            if (playlist == null) return at Runnable
             playlistTracks = if (addTracks) {
                 newTrack
             } else {//Save a playlist
@@ -138,7 +139,7 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
                 }
                 tracks
             }
-            if (playlistTracks.isEmpty()) return at Runnable
+            if (playlistTracks.isEmpty()) return at withContext
             val ids = LinkedList<Long>()
             for (mw in playlistTracks) {
                 val id = mw.id
@@ -161,14 +162,14 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
             }
 
             playlist.append(ids)
-        })
-        dismiss()
+        }
+        if (activity?.isStarted() == true)dismiss()
     }
 
 
     override fun onClick(item: MediaLibraryItem) {
-        playlist = item as AbstractPlaylist
-        editText!!.setText(item.title)
+        currentPLaylist = item as AbstractPlaylist
+        editText?.setText(item.title)
     }
 
     companion object {



More information about the Android mailing list