[Android] Ask storage permission when launched from an app

Geoffrey Métais git at videolan.org
Wed Oct 11 16:20:12 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct 11 16:08:56 2017 +0200| [816b92e15b76c153de37209ff844004527a98897] | committer: Geoffrey Métais

Ask storage permission when launched from an app

If an application starts playback with VLC, we check for storage permission
User may have never launched VLC by himself

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

 vlc-android/src/org/videolan/vlc/StartActivity.java  | 20 +++++++++++++++-----
 .../gui/helpers/hf/StoragePermissionsDelegate.java   |  2 +-
 .../videolan/vlc/gui/video/VideoPlayerActivity.java  |  4 +---
 .../src/org/videolan/vlc/util/Permissions.java       | 12 +++++-------
 4 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 394734840..15116def4 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -23,17 +23,18 @@
 
 package org.videolan.vlc;
 
-import android.app.Activity;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
+import android.support.v4.app.FragmentActivity;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.gui.AudioPlayerContainerActivity;
 import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.SearchActivity;
+import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate;
 import org.videolan.vlc.gui.tv.MainTvActivity;
 import org.videolan.vlc.gui.tv.audioplayer.AudioPlayerActivity;
 import org.videolan.vlc.media.MediaUtils;
@@ -41,7 +42,7 @@ import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Permissions;
 import org.videolan.vlc.util.Util;
 
-public class StartActivity extends Activity {
+public class StartActivity extends FragmentActivity implements StoragePermissionsDelegate.CustomActionController {
 
     public final static String TAG = "VLC/StartActivity";
 
@@ -57,9 +58,8 @@ public class StartActivity extends Activity {
         final String action = intent != null ? intent.getAction(): null;
 
         if (Intent.ACTION_VIEW.equals(action) && intent.getData() != null) {
-            intent.setDataAndType(intent.getData(), intent.getType());
-            MediaUtils.openMediaNoUi(intent.getData());
-            finish();
+            if (Permissions.checkReadStoragePermission(this, true))
+                startPlaybackFromApp(intent);
             return;
         }
 
@@ -93,6 +93,11 @@ public class StartActivity extends Activity {
         finish();
     }
 
+    private void startPlaybackFromApp(Intent intent) {
+        MediaUtils.openMediaNoUi(intent.getData());
+        finish();
+    }
+
     private void startMedialibrary(final boolean firstRun, final boolean upgrade) {
         if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage(StartActivity.this))
             startService(new Intent(MediaParsingService.ACTION_INIT, null, StartActivity.this, MediaParsingService.class)
@@ -104,4 +109,9 @@ public class StartActivity extends Activity {
         return AndroidUtil.isJellyBeanMR1OrLater && (AndroidDevices.isAndroidTv || !AndroidDevices.hasTsp ||
                 PreferenceManager.getDefaultSharedPreferences(this).getBoolean("tv_ui", false));
     }
+
+    @Override
+    public void onStorageAccessGranted() {
+        startPlaybackFromApp(getIntent());
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.java b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.java
index 672368039..167d1adf8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/StoragePermissionsDelegate.java
@@ -103,7 +103,7 @@ public class StoragePermissionsDelegate extends BaseHeadlessFragment {
         }
     }
 
-    public static void AskStoragePermission(@NonNull FragmentActivity activity) {
+    public static void askStoragePermission(@NonNull FragmentActivity activity) {
         final FragmentManager fm = activity.getSupportFragmentManager();
         Fragment fragment = fm.findFragmentByTag(TAG);
         if (fragment == null)
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 3e896b6ff..49c150565 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -3721,9 +3721,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
     public void onConnected(PlaybackService service) {
         mService = service;
         //We may not have the permission to access files
-        if (!Permissions.canReadStorage())
-            Permissions.checkReadStoragePermission(this, true);
-        else if (!mSwitchingView)
+        if (Permissions.checkReadStoragePermission(this, true) && !mSwitchingView)
             mHandler.sendEmptyMessage(START_PLAYBACK);
         mSwitchingView = false;
         mSettings.edit().putBoolean(PreferencesActivity.VIDEO_RESTORE, false).apply();
diff --git a/vlc-android/src/org/videolan/vlc/util/Permissions.java b/vlc-android/src/org/videolan/vlc/util/Permissions.java
index 2a1f71aac..ad877c614 100644
--- a/vlc-android/src/org/videolan/vlc/util/Permissions.java
+++ b/vlc-android/src/org/videolan/vlc/util/Permissions.java
@@ -84,18 +84,16 @@ public class Permissions {
                 Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
     }
 
-    public static void checkReadStoragePermission(FragmentActivity activity, boolean exit) {
+    public static boolean checkReadStoragePermission(FragmentActivity activity, boolean exit) {
         if (AndroidUtil.isMarshMallowOrLater && !canReadStorage()) {
-
             if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                     Manifest.permission.READ_EXTERNAL_STORAGE)) {
-
                 showStoragePermissionDialog(activity, exit);
-
-            } else {
+            } else
                 requestStoragePermission(activity);
-            }
+            return false;
         }
+        return true;
     }
 
     public static void checkDrawOverlaysPermission(FragmentActivity activity) {
@@ -244,6 +242,6 @@ public class Permissions {
 
     private static void requestStoragePermission(FragmentActivity activity) {
         if (activity != null)
-            StoragePermissionsDelegate.AskStoragePermission(activity);
+            StoragePermissionsDelegate.askStoragePermission(activity);
     }
 }



More information about the Android mailing list