[Android] Prevent displaying media content when the permission is not granted

Nicolas Pomepuy git at videolan.org
Mon Nov 25 11:07:02 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Oct 15 14:09:32 2024 +0200| [7c11e62985dbe79bb878910e348cff1e619ec6c8] | committer: Duncan McNamara

Prevent displaying media content when the permission is not granted

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

 .../src/org/videolan/vlc/gui/video/VideoGridFragment.kt     | 10 ++++++++++
 .../videolan/vlc/providers/medialibrary/AlbumsProvider.kt   |  1 +
 .../videolan/vlc/providers/medialibrary/ArtistsProvider.kt  |  1 +
 .../videolan/vlc/providers/medialibrary/FoldersProvider.kt  |  3 +++
 .../videolan/vlc/providers/medialibrary/GenresProvider.kt   |  1 +
 .../vlc/providers/medialibrary/MedialibraryProvider.kt      | 13 +++++++++++++
 .../videolan/vlc/providers/medialibrary/TracksProvider.kt   |  1 +
 .../vlc/providers/medialibrary/VideoGroupsProvider.kt       |  1 +
 .../videolan/vlc/providers/medialibrary/VideosProvider.kt   |  1 +
 9 files changed, 32 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 41c77dcc02..7864573063 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -25,6 +25,7 @@ import android.content.SharedPreferences
 import android.os.Bundle
 import android.util.Log
 import android.view.*
+import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.view.ActionMode
 import androidx.coordinatorlayout.widget.CoordinatorLayout
@@ -147,6 +148,15 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
         lifecycleScope.launch {
             waitForML()
             viewModel.provider.pagedList.observe(this at VideoGridFragment) {
+//                if (!Permissions.canReadVideos(AppContextProvider.appContext)) {
+//                    if (viewModel.provider.isEmpty())
+//                        viewModel.provider.clear()
+//                    else
+//                        lifecycleScope.launch(Dispatchers.Main) {
+//                            updateEmptyView()
+//                        }
+//                    return at observe
+//                }
                 @Suppress("UNCHECKED_CAST")
                 (it as? PagedList<MediaLibraryItem>)?.let { pagedList -> videoListAdapter.submitList(pagedList) }
                 updateEmptyView()
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/AlbumsProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/AlbumsProvider.kt
index f284e8ab35..018deaa551 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/AlbumsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/AlbumsProvider.kt
@@ -38,6 +38,7 @@ class AlbumsProvider(val parent : MediaLibraryItem?, context: Context, model: So
     override fun canSortByReleaseDate() = true
     override fun canSortByArtist() = true
     override fun canSortByInsertionDate()= true
+    override val isAudioPermDependant = true
 
     init {
         sort = Settings.getInstance(context).getInt(sortKey, if (parent is Artist) Medialibrary.SORT_RELEASEDATE else Medialibrary.SORT_DEFAULT)
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/ArtistsProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/ArtistsProvider.kt
index 5ca95459dc..5a90412149 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/ArtistsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/ArtistsProvider.kt
@@ -28,6 +28,7 @@ import org.videolan.tools.Settings
 import org.videolan.vlc.viewmodels.SortableModel
 
 class ArtistsProvider(context: Context, model: SortableModel, var showAll: Boolean) : MedialibraryProvider<Artist>(context, model) {
+    override val isAudioPermDependant = true
 
     override fun getAll() : Array<Artist> = medialibrary.getArtists(showAll, sort, desc, Settings.includeMissing, onlyFavorites)
 
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/FoldersProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/FoldersProvider.kt
index f417e84caa..a8f2a75c16 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/FoldersProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/FoldersProvider.kt
@@ -26,6 +26,9 @@ import org.videolan.tools.Settings
 import org.videolan.vlc.viewmodels.SortableModel
 
 class FoldersProvider(context: Context, model: SortableModel, val type: Int) : MedialibraryProvider<Folder>(context, model) {
+
+    override val isVideoPermDependant = true
+
     override fun getAll() : Array<Folder> = medialibrary.getFolders(type, sort, desc, Settings.includeMissing, onlyFavorites, getTotalCount(), 0)
 
     override fun getTotalCount() = if (model.filterQuery.isNullOrEmpty()) medialibrary.getFoldersCount(type) else medialibrary.getFoldersCount(model.filterQuery)
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/GenresProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/GenresProvider.kt
index a9570e6385..7f6ea3c761 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/GenresProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/GenresProvider.kt
@@ -28,6 +28,7 @@ import org.videolan.tools.Settings
 import org.videolan.vlc.viewmodels.SortableModel
 
 class GenresProvider(context: Context, model: SortableModel) : MedialibraryProvider<Genre>(context, model)  {
+    override val isAudioPermDependant = true
 
     override fun getAll() : Array<Genre> = medialibrary.getGenres(sort, desc, Settings.includeMissing, onlyFavorites)
 
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index c1b4236578..4e73a7b185 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -63,6 +63,9 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
             field = value
         }
 
+    open val isVideoPermDependant = false
+    open val isAudioPermDependant = false
+
     protected open val sortKey : String = this.javaClass.simpleName
     var sort = settings.getInt(sortKey, Medialibrary.SORT_DEFAULT)
     var desc = settings.getBoolean("${sortKey}_desc", false)
@@ -148,6 +151,11 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     fun refresh(): Boolean {
         if ((isRefreshing && medialibrary.isWorking) || !medialibrary.isStarted || !this::dataSource.isInitialized) return false
+        if (isVideoPermDependant && !Permissions.canReadVideos(context)) return false
+        if (isAudioPermDependant && !Permissions.canReadAudios(context)) {
+            loading.postValue(false)
+            return false
+        }
         privateHeaders.clear()
         if (!dataSource.isInvalid) {
             isRefreshing = true
@@ -175,6 +183,11 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
     inner class MLDataSource : PositionalDataSource<T>() {
 
         override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
+            if (isVideoPermDependant && !Permissions.canReadVideos(context)) return callback.onResult(emptyList(), 0, 0)
+            if (isAudioPermDependant && !Permissions.canReadAudios(context)) {
+                loading.postValue(false)
+                return callback.onResult(emptyList(), 0, 0)
+            }
             val page = getPage(params.requestedLoadSize, params.requestedStartPosition)
             val count = if (page.size < params.requestedLoadSize) page.size else getTotalCount()
             try {
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/TracksProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/TracksProvider.kt
index 9198240506..05a6db1ea4 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/TracksProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/TracksProvider.kt
@@ -40,6 +40,7 @@ class TracksProvider(val parent : MediaLibraryItem?, context: Context, model: So
     override fun canSortByName() = parent !is Playlist
     override fun canSortByFileNameName() = parent !is Playlist
     override fun canSortByTrackId() = parent is Album
+    override val isAudioPermDependant = true
 
     init {
         sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_DEFAULT)
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
index 73595f26a4..9b7be89ada 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
@@ -15,6 +15,7 @@ class VideoGroupsProvider(context: Context, model: SortableModel) : Medialibrary
     override fun canSortByInsertionDate() = true
     override fun canSortByLastModified() = true
     override fun canSortByMediaNumber() = true
+    override val isVideoPermDependant = true
 
     override fun getAll() : Array<VideoGroup> = medialibrary.getVideoGroups(sort, desc, Settings.includeMissing, onlyFavorites, getTotalCount(), 0)
 
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideosProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideosProvider.kt
index d46115c2c6..8a4c4f9d4d 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideosProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideosProvider.kt
@@ -36,6 +36,7 @@ class VideosProvider(val folder : Folder?, val group: VideoGroup?, context: Cont
     override fun canSortByDuration() = true
     override fun canSortByLastModified() = folder == null
     override fun canSortByInsertionDate() = group == null
+    override val isVideoPermDependant = true
 
     override fun getTotalCount() = if (model.filterQuery == null) when {
         folder !== null -> folder.mediaCount(Folder.TYPE_FOLDER_VIDEO)



More information about the Android mailing list