[Android] Always check permissions with current context

Geoffrey Métais git at videolan.org
Tue Feb 6 13:37:29 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb  6 12:56:48 2018 +0100| [d1e7bee303451927fd4339f8f842c4acefed6cfc] | committer: Geoffrey Métais

Always check permissions with current context

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

 vlc-android/src/org/videolan/vlc/PlaybackService.java              | 4 ++--
 .../src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java     | 2 +-
 vlc-android/src/org/videolan/vlc/gui/MainActivity.java             | 2 +-
 vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java        | 4 ++--
 .../src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java        | 2 +-
 vlc-android/src/org/videolan/vlc/util/Permissions.java             | 7 +------
 6 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 11c375123..4fdac0970 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -228,7 +228,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
 
     private MedialibraryReceiver mLibraryReceiver = null;
     private void registerMedialibrary(final Runnable action) {
-        if (!Permissions.canReadStorage()) return;
+        if (!Permissions.canReadStorage(this)) return;
         final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
         if (mLibraryReceiver == null) {
             mLibraryReceiver = new MedialibraryReceiver();
@@ -1864,7 +1864,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
     @Nullable
     @Override
     public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
-        return Permissions.canReadStorage() ? new BrowserRoot(BrowserProvider.ID_ROOT, null) : null;
+        return Permissions.canReadStorage(PlaybackService.this) ? new BrowserRoot(BrowserProvider.ID_ROOT, null) : null;
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index c7cc73582..79402a8dd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -95,7 +95,7 @@ public class AudioPlayerContainerActivity extends BaseActivity implements Playba
         //Init Medialibrary if KO
         if (savedInstanceState != null) {
             VLCApplication.setLocale();
-            if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage())
+            if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage(this))
                 startService(new Intent(Constants.ACTION_INIT, null, this, MediaParsingService.class));
         }
         super.onCreate(savedInstanceState);
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 951455c67..acfe021fc 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -236,7 +236,7 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
             showFragment(mCurrentFragmentId);
         if (mMediaLibrary.isInitiated()) {
             /* Load media items from database and storage */
-            if (mScanNeeded && Permissions.canReadStorage())
+            if (mScanNeeded && Permissions.canReadStorage(this))
                 startService(new Intent(Constants.ACTION_RELOAD, null,this, MediaParsingService.class));
             else if (!currentIdIsExtension())
                 restoreCurrentList();
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index 10afc41c5..c425bd6d9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -131,7 +131,7 @@ public class MainTvActivity extends BaseTvActivity implements OnItemViewSelected
         }
 
         // Delay access permission dialog prompt to avoid background corruption
-        if (!Permissions.canReadStorage())
+        if (!Permissions.canReadStorage(this))
             mHandler.postDelayed(new Runnable() {
                 @Override
                 public void run() {
@@ -173,7 +173,7 @@ public class MainTvActivity extends BaseTvActivity implements OnItemViewSelected
         /*
          * skip browser and show directly Audio Player if a song is playing
          */
-        if ((mRowsAdapter == null || mRowsAdapter.size() == 0) && Permissions.canReadStorage())
+        if ((mRowsAdapter == null || mRowsAdapter.size() == 0) && Permissions.canReadStorage(this))
             update();
         else {
             updateBrowsers();
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
index 47848eaf1..dff988ddf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.java
@@ -58,7 +58,7 @@ public abstract class BaseTvActivity extends PlaybackServiceActivity implements
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         //Init Medialibrary if KO
-        if (savedInstanceState != null && !VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage())
+        if (savedInstanceState != null && !VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage(this))
             startService(new Intent(Constants.ACTION_INIT, null, this, MediaParsingService.class));
         super.onCreate(savedInstanceState);
         mMediaLibrary = VLCApplication.getMLInstance();
diff --git a/vlc-android/src/org/videolan/vlc/util/Permissions.java b/vlc-android/src/org/videolan/vlc/util/Permissions.java
index fc73a3ddf..b249eb305 100644
--- a/vlc-android/src/org/videolan/vlc/util/Permissions.java
+++ b/vlc-android/src/org/videolan/vlc/util/Permissions.java
@@ -72,11 +72,6 @@ public class Permissions {
         return !AndroidUtil.isMarshMallowOrLater || Settings.System.canWrite(context);
     }
 
-    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
-    public static boolean canReadStorage() {
-        return canReadStorage(VLCApplication.getAppContext());
-    }
-
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
     public static boolean canReadStorage(Context context) {
         if (!AndroidUtil.isICSOrLater)
@@ -89,7 +84,7 @@ public class Permissions {
         return canWriteStorage(VLCApplication.getAppContext());
     }
 
-    public static boolean canWriteStorage(Context context) {
+    private static boolean canWriteStorage(Context context) {
         return ContextCompat.checkSelfPermission(context,
                 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
     }



More information about the Android mailing list