[Android] Improve storage runtime permissions process

Geoffrey Métais git at videolan.org
Thu Jan 17 15:16:06 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 17 14:46:13 2019 +0100| [671b0b152cc7a57a6caa1832a28fbeb00fd60d09] | committer: Geoffrey Métais

Improve storage runtime permissions process

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

 .../src/org/videolan/vlc/StartActivity.java        |  3 +-
 .../vlc/gui/helpers/hf/BaseHeadlessFragment.kt     |  3 ++
 .../gui/helpers/hf/StoragePermissionsDelegate.kt   | 37 +++++++++++-----------
 3 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index dad99750e..7c8543fb5 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -125,7 +125,8 @@ public class StartActivity extends FragmentActivity {
             }
         });
         final Intent intent = new Intent(StartActivity.this, tv ? MainTvActivity.class : MainActivity.class)
-                .putExtra(Constants.EXTRA_FIRST_RUN, firstRun);
+                .putExtra(Constants.EXTRA_FIRST_RUN, firstRun)
+                .putExtra(Constants.EXTRA_UPGRADE, upgrade);
         if (tv && getIntent().hasExtra(Constants.EXTRA_PATH)) intent.putExtra(Constants.EXTRA_PATH, getIntent().getStringExtra(Constants.EXTRA_PATH));
         if (target != 0) intent.putExtra(Constants.EXTRA_TARGET, target);
         startActivity(intent);
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
index d09e6ea58..8f8f287b0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt
@@ -23,6 +23,7 @@
 
 package org.videolan.vlc.gui.helpers.hf
 
+import android.content.pm.PackageManager
 import android.os.Bundle
 import androidx.fragment.app.Fragment
 import kotlinx.coroutines.CompletableDeferred
@@ -43,3 +44,5 @@ open class BaseHeadlessFragment : Fragment() {
         }
     }
 }
+
+internal fun IntArray.isGranted() = isNotEmpty() && get(0) == PackageManager.PERMISSION_GRANTED
\ No newline at end of file
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 59a959908..a32409890 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
@@ -25,12 +25,10 @@ package org.videolan.vlc.gui.helpers.hf
 
 import android.Manifest
 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 kotlinx.coroutines.launch
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.vlc.startMedialibrary
@@ -58,11 +56,9 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
         if (intent !== null && intent.getBooleanExtra(EXTRA_UPGRADE, false)) {
             upgrade = true
             firstRun = intent.getBooleanExtra(EXTRA_FIRST_RUN, false)
-            intent.removeExtra(EXTRA_UPGRADE)
-            intent.removeExtra(EXTRA_FIRST_RUN)
         }
         write = arguments?.getBoolean("write") ?: false
-        if (AndroidUtil.isMarshMallowOrLater && !canReadStorage(activity!!)) {
+        if (AndroidUtil.isMarshMallowOrLater && !canReadStorage(requireContext())) {
             if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
                 Permissions.showStoragePermissionDialog(requireActivity(), false)
             else
@@ -85,18 +81,24 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
             Permissions.PERMISSION_STORAGE_TAG -> {
                 // If request is cancelled, the result arrays are empty.
                 val ctx = activity ?: return
-                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-                    if (ctx is CustomActionController) ctx.onStorageAccessGranted()
-                    else ctx.startMedialibrary(firstRun, upgrade, true)
+                if (grantResults.isGranted()) {
                     storageAccessGranted.value = true
+                    deferred.complete(true)
                     exit()
                 } else {
-                    Permissions.showStoragePermissionDialog(ctx, false)
-                    if (!shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
+                    if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
+                        Permissions.showStoragePermissionDialog(ctx, false)
+                        return
+                    } else {
+                        storageAccessGranted.value = false
+                        deferred.complete(false)
                         exit()
+                    }
                 }
+                storageAccessGranted.value = false
+                deferred.complete(false)
             }
-            Permissions.PERMISSION_WRITE_STORAGE_TAG -> deferred.complete(true)
+            Permissions.PERMISSION_WRITE_STORAGE_TAG -> deferred.complete(grantResults.isGranted())
         }
     }
 
@@ -106,8 +108,11 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
         val storageAccessGranted = LiveEvent<Boolean>()
 
         fun askStoragePermission(activity: FragmentActivity, write: Boolean, cb: Runnable?) {
+            val intent = activity.intent
+            val upgrade = intent?.getBooleanExtra(EXTRA_UPGRADE, false) ?: false
+            val firstRun = upgrade && intent.getBooleanExtra(EXTRA_FIRST_RUN, false)
             AppScope.launch {
-                if (getStoragePermission(activity, write)) (cb ?: getAction(activity)).run()
+                if (getStoragePermission(activity, write)) (cb ?: getAction(activity, firstRun, upgrade)).run()
             }
         }
 
@@ -123,18 +128,14 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
                 fm.beginTransaction().add(fragment, TAG).commitAllowingStateLoss()
             } else {
                 (fragment as StoragePermissionsDelegate).requestStorageAccess(write)
+                return false //Fragment is already waiting for answear
             }
             return fragment.deferred.await()
         }
 
-        private fun getAction(activity: FragmentActivity) = Runnable {
+        private fun getAction(activity: FragmentActivity, firstRun: Boolean, upgrade: Boolean) = Runnable {
             if (activity is CustomActionController) activity.onStorageAccessGranted()
             else {
-                val intent = activity.intent
-                val upgrade = intent.getBooleanExtra(EXTRA_UPGRADE, false)
-                val firstRun = upgrade && intent.getBooleanExtra(EXTRA_FIRST_RUN, false)
-                intent.removeExtra(EXTRA_UPGRADE)
-                intent.removeExtra(EXTRA_FIRST_RUN)
                 activity.startMedialibrary(firstRun, upgrade, true)
             }
         }



More information about the Android mailing list