[Android] Refresh files browser once read access is granted

Geoffrey Métais git at videolan.org
Thu Dec 6 17:49:14 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Dec  6 17:48:13 2018 +0100| [dd36d44a1ae8e761bdc6de96af07cb011523a21a] | committer: Geoffrey Métais

Refresh files browser once read access is granted

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

 .../org/videolan/vlc/gui/browser/FileBrowserFragment.java   | 13 +++++++++++++
 .../vlc/gui/helpers/hf/StoragePermissionsDelegate.kt        |  5 +++--
 .../src/org/videolan/vlc/providers/FileBrowserProvider.kt   | 13 +++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 741651e13..7ed5efff6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -64,6 +64,19 @@ public class FileBrowserFragment extends BaseBrowserFragment {
         setupBrowser();
     }
 
+    private boolean needsRefresh;
+    @Override
+    public void onStart() {
+        super.onStart();
+        if (needsRefresh) viewModel.browserRoot();
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        if (isRootDirectory() && adapter.isEmpty()) needsRefresh = true;
+    }
+
     @Override
     public void registerSwiperRefreshlayout() {
         if (!isRootDirectory()) super.registerSwiperRefreshlayout();
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.kt
index c31371476..c58566bc2 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.kt
@@ -28,8 +28,6 @@ import android.annotation.TargetApi
 import android.content.pm.PackageManager
 import android.os.Build
 import android.os.Bundle
-import androidx.fragment.app.Fragment
-import androidx.fragment.app.FragmentActivity
 import kotlinx.coroutines.channels.Channel
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.vlc.startMedialibrary
@@ -37,6 +35,7 @@ import org.videolan.vlc.util.EXTRA_FIRST_RUN
 import org.videolan.vlc.util.EXTRA_UPGRADE
 import org.videolan.vlc.util.Permissions
 import org.videolan.vlc.util.Permissions.canReadStorage
+import videolan.org.commontools.LiveEvent
 
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 class StoragePermissionsDelegate : BaseHeadlessFragment() {
@@ -85,6 +84,7 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
                 if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                     if (ctx is CustomActionController) ctx.onStorageAccessGranted()
                     else ctx.startMedialibrary(mFirstRun, mUpgrade, true)
+                    storageAccessGranted.value = true
                     exit()
                 } else if (mActivity != null) {
                     Permissions.showStoragePermissionDialog(mActivity, false)
@@ -99,6 +99,7 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
     companion object {
 
         const val TAG = "VLC/StorageHF"
+        val storageAccessGranted = LiveEvent<Boolean>()
 
         fun askStoragePermission(activity: androidx.fragment.app.FragmentActivity, write: Boolean, cb: Runnable?) {
             if (activity.isFinishing) return
diff --git a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
index e4ae1a625..ec88bc9c9 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FileBrowserProvider.kt
@@ -36,6 +36,7 @@ import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.ExternalMonitor
 import org.videolan.vlc.R
 import org.videolan.vlc.database.models.BrowserFav
+import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate
 import org.videolan.vlc.gui.helpers.hf.getDocumentFiles
 import org.videolan.vlc.repository.BrowserFavRepository
 import org.videolan.vlc.repository.DirectoryRepository
@@ -92,7 +93,10 @@ open class FileBrowserProvider(
         showFavorites = url == null && !filePicker && this !is StorageProvider
     }
 
+    private lateinit var storageObserver : Observer<Boolean>
+
     override suspend fun browseRoot() {
+        var storageAccess = false
         val internalmemoryTitle = context.getString(R.string.internal_memory)
         val browserStorage = context.getString(R.string.browser_storages)
         val storages = DirectoryRepository.getInstance(context).getMediaDirectories()
@@ -101,6 +105,7 @@ open class FileBrowserProvider(
         for (mediaDirLocation in storages) {
             val file = File(mediaDirLocation)
             if (!file.exists() || !file.canRead()) continue
+            storageAccess = true
             val directory = MediaWrapper(AndroidUtil.PathToUri(mediaDirLocation))
             directory.type = MediaWrapper.TYPE_DIR
             if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, mediaDirLocation)) {
@@ -113,6 +118,11 @@ open class FileBrowserProvider(
             }
             devices.add(directory)
         }
+        if (AndroidUtil.isMarshMallowOrLater && !storageAccess) {
+            storageObserver = Observer { if (it == true) launch { browseRoot() } }
+            StoragePermissionsDelegate.storageAccessGranted.observeForever(storageObserver)
+        }
+        if (!storageAccess) return // For first launch, storage access may not already be granted
         if (AndroidUtil.isLolliPopOrLater && !ExternalMonitor.devices.value.isEmpty()) {
             val otg = MediaWrapper(Uri.parse("otg://")).apply {
                 title = context.getString(R.string.otg_device_title)
@@ -141,6 +151,9 @@ open class FileBrowserProvider(
         if (url == null) {
             ExternalMonitor.devices.removeObserver(this)
             if (showFavorites) favorites?.removeObserver(favoritesObserver)
+            if (this::storageObserver.isInitialized) {
+                StoragePermissionsDelegate.storageAccessGranted.removeObserver(storageObserver)
+            }
         }
         super.release()
     }



More information about the Android mailing list