[Android] Download subtitles by name if uri cannot be converted in file name

Nicolas Pomepuy git at videolan.org
Wed Jan 20 09:54:26 UTC 2021


vlc-android | branch: 3.3.x | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Dec 16 10:10:23 2020 +0100| [df2840afce899493c80224768cff82517ca2b5d4] | committer: Nicolas Pomepuy

Download subtitles by name if uri cannot be converted in file name

Fixes #1583

(cherry picked from commit 810e96d5139b30ef878e157227349324ea716ae1)

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

 .../vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt  | 16 ++++++++++------
 .../vlc-android/src/org/videolan/vlc/media/MediaUtils.kt |  6 +++---
 .../src/org/videolan/vlc/viewmodels/SubtitlesModel.kt    | 11 +++++++----
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
index 80d4fd88c..9499e9daf 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
@@ -28,7 +28,7 @@ import org.videolan.vlc.util.VLCDownloadManager
 import org.videolan.vlc.viewmodels.SubtitlesModel
 
 private const val MEDIA_PATHS = "MEDIA_PATHS"
-const val MEDIA_PATH = "MEDIA_PATH"
+private const val MEDIA_NAMES = "MEDIA_NAMES"
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
@@ -44,6 +44,7 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
     private lateinit var historyAdapter: SubtitlesAdapter
     private lateinit var binding: SubtitleDownloaderDialogBinding
     private lateinit var uris: List<Uri>
+    private lateinit var names: List<String>
     private lateinit var viewModel: SubtitlesModel
     private lateinit var toast: Toast
 
@@ -83,8 +84,10 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
 
         uris = savedInstanceState?.getParcelableArrayList<Uri>(MEDIA_PATHS)?.toList()
                 ?: arguments?.getParcelableArrayList<Uri>(MEDIA_PATHS)?.toList() ?: listOf()
+        names = savedInstanceState?.getStringArrayList(MEDIA_NAMES)?.toList()
+                ?: arguments?.getStringArrayList(MEDIA_NAMES)?.toList() ?: listOf()
 
-        viewModel = ViewModelProvider(requireActivity(), SubtitlesModel.Factory(requireContext(), uris[0])).get(uris[0].path!!, SubtitlesModel::class.java)
+        viewModel = ViewModelProvider(requireActivity(), SubtitlesModel.Factory(requireContext(), uris[0], names[0])).get(uris[0].path!!, SubtitlesModel::class.java)
         if (uris.isEmpty()) dismiss()
     }
 
@@ -99,11 +102,11 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
 
         binding.subDownloadNext.setOnClickListener {
             if (uris.size > 1)
-                MediaUtils.showSubtitleDownloaderDialogFragment(requireActivity(), uris.takeLast(uris.size - 1))
+                MediaUtils.showSubtitleDownloaderDialogFragment(requireActivity(), uris.takeLast(uris.size - 1), names.takeLast(names.size - 1))
             dismiss()
         }
 
-        binding.movieName.text = uris[0].lastPathSegment
+        binding.movieName.text = names.firstOrNull() ?: uris[0].lastPathSegment
         state = SubDownloadDialogState.Download
 
         downloadAdapter = SubtitlesAdapter(listEventActor)
@@ -182,6 +185,7 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
     override fun onSaveInstanceState(outState: Bundle) {
         super.onSaveInstanceState(outState)
         outState.putParcelableArrayList(MEDIA_PATHS, ArrayList(uris))
+        outState.putStringArrayList(MEDIA_NAMES, ArrayList(names))
     }
 
     private fun focusOnView(scrollView: NestedScrollView) {
@@ -189,9 +193,9 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
     }
 
     companion object {
-        fun newInstance(mediaUris: List<Uri>): SubtitleDownloaderDialogFragment {
+        fun newInstance(mediaUris: List<Uri>, mediaTitles:List<String>): SubtitleDownloaderDialogFragment {
             return SubtitleDownloaderDialogFragment().apply {
-                arguments = bundleOf(MEDIA_PATHS to ArrayList(mediaUris))
+                arguments = bundleOf(MEDIA_PATHS to ArrayList(mediaUris), MEDIA_NAMES to mediaTitles)
             }
         }
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index a832ea1d5..de0d075fe 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -56,7 +56,7 @@ private typealias MediaContentResolver = SimpleArrayMap<String, IMediaContentRes
 @ExperimentalCoroutinesApi
 object MediaUtils {
     fun getSubs(activity: FragmentActivity, mediaList: List<MediaWrapper>) {
-        if (activity is AppCompatActivity) showSubtitleDownloaderDialogFragment(activity, mediaList.map { it.uri })
+        if (activity is AppCompatActivity) showSubtitleDownloaderDialogFragment(activity, mediaList.map { it.uri }, mediaList.map { it.title })
         else {
             val intent = Intent(activity, DialogActivity::class.java).setAction(DialogActivity.KEY_SUBS_DL)
                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@@ -68,9 +68,9 @@ object MediaUtils {
 
     fun getSubs(activity: FragmentActivity, media: MediaWrapper) = getSubs(activity, listOf(media))
 
-    fun showSubtitleDownloaderDialogFragment(activity: FragmentActivity, mediaUris: List<Uri>) {
+    fun showSubtitleDownloaderDialogFragment(activity: FragmentActivity, mediaUris: List<Uri>, mediaTitles:List<String>) {
         val callBack = java.lang.Runnable {
-            SubtitleDownloaderDialogFragment.newInstance(mediaUris).show(activity.supportFragmentManager, "Subtitle_downloader")
+            SubtitleDownloaderDialogFragment.newInstance(mediaUris, mediaTitles).show(activity.supportFragmentManager, "Subtitle_downloader")
         }
         if (Permissions.canWriteStorage()) callBack.run()
         else Permissions.askWriteStoragePermission(activity, false, callBack)
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
index c7bddd1e7..0bc4bb748 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
@@ -27,12 +27,13 @@ import org.videolan.vlc.repository.ExternalSubRepository
 import org.videolan.resources.opensubtitles.OpenSubtitleRepository
 import org.videolan.tools.CoroutineContextProvider
 import org.videolan.tools.putSingle
+import org.videolan.vlc.BuildConfig
 import java.io.File
 import java.util.*
 
 private const val LAST_USED_LANGUAGES = "last_used_subtitles"
 
-class SubtitlesModel(private val context: Context, private val mediaUri: Uri, val coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : ViewModel() {
+class SubtitlesModel(private val context: Context, private val mediaUri: Uri, private val name:String, val coroutineContextProvider: CoroutineContextProvider = CoroutineContextProvider()) : ViewModel() {
     val observableSearchName = ObservableField<String>()
     val observableSearchEpisode = ObservableField<String>()
     val observableSearchSeason = ObservableField<String>()
@@ -111,6 +112,7 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, va
     }
 
     private suspend fun getSubtitleByName(name: String, episode: Int?, season: Int?, languageIds: List<String>?): List<OpenSubtitle> {
+        if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Getting subs by name with $name")
         val builder = StringBuilder(context.getString(R.string.sub_result_by_name, "<i>$name</i>"))
         season?.let { builder.append(" - ").append(context.getString(R.string.sub_result_by_name_season, "<i>$it</i>")) }
         episode?.let { builder.append(" - ").append(context.getString(R.string.sub_result_by_name_episode, "<i>$it</i>")) }
@@ -120,6 +122,7 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, va
     }
 
     private suspend fun getSubtitleByHash(movieByteSize: Long, movieHash: String?, languageIds: List<String>?): List<OpenSubtitle> {
+        if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Getting subs by hash with $movieHash")
         manualSearchEnabled.set(false)
         observableResultDescription.set(context.getString(R.string.sub_result_by_file).toSpanned())
         return OpenSubtitleRepository.getInstance().queryWithHash(movieByteSize, movieHash, languageIds)
@@ -152,7 +155,7 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, va
                             // No result for hash. Falling back to name search
                             if (hashSubs.isEmpty()) getSubtitleByName(videoFile.name, null, null, observableSearchLanguage.get()) else hashSubs
                         } else {
-                            getSubtitleByName(videoFile.name, null, null, observableSearchLanguage.get())
+                            getSubtitleByName(name, null, null, observableSearchLanguage.get())
                         }
 
                     }
@@ -190,10 +193,10 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, va
 
     fun saveLastUsedLanguage(lastUsedLanguages: List<String>) = Settings.getInstance(context).putSingle(LAST_USED_LANGUAGES, lastUsedLanguages)
 
-    class Factory(private val context: Context, private val mediaUri: Uri) : ViewModelProvider.NewInstanceFactory() {
+    class Factory(private val context: Context, private val mediaUri: Uri, private val name: String) : ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")
-            return SubtitlesModel(context.applicationContext, mediaUri) as T
+            return SubtitlesModel(context.applicationContext, mediaUri, name) as T
         }
     }
 



More information about the Android mailing list