[Android] Check read permission with StartActivity context

Geoffrey Métais git at videolan.org
Tue Oct 10 11:28:26 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Oct 10 10:37:39 2017 +0200| [8ee4529d99d0e8fa62848c6d7ed6cb4db08e9d38] | committer: Geoffrey Métais

Check read permission with StartActivity context

On Samsung Galaxy devices, app context seems to not be always ready

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

 vlc-android/src/org/videolan/vlc/StartActivity.java  | 20 ++++----------------
 .../src/org/videolan/vlc/util/Permissions.java       |  9 +++++++--
 2 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 5dcd4cc60..394734840 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -24,11 +24,9 @@
 package org.videolan.vlc;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
-import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
 
@@ -96,20 +94,10 @@ public class StartActivity extends Activity {
     }
 
     private void startMedialibrary(final boolean firstRun, final boolean upgrade) {
-        final Runnable runnable = new Runnable() {
-            @Override
-            public void run() {
-                if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage())
-                    startService(new Intent(MediaParsingService.ACTION_INIT, null, StartActivity.this, MediaParsingService.class)
-                            .putExtra(EXTRA_FIRST_RUN, firstRun)
-                            .putExtra(EXTRA_UPGRADE, upgrade));
-            }
-        };
-        final Context ctx = VLCApplication.getAppContext();
-        if (ctx != null)
-            runnable.run();
-        else
-            new Handler().postDelayed(runnable, 500);
+        if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage(StartActivity.this))
+            startService(new Intent(MediaParsingService.ACTION_INIT, null, StartActivity.this, MediaParsingService.class)
+                    .putExtra(EXTRA_FIRST_RUN, firstRun)
+                    .putExtra(EXTRA_UPGRADE, upgrade));
     }
 
     private boolean showTvUi() {
diff --git a/vlc-android/src/org/videolan/vlc/util/Permissions.java b/vlc-android/src/org/videolan/vlc/util/Permissions.java
index 4bb2d0663..2a1f71aac 100644
--- a/vlc-android/src/org/videolan/vlc/util/Permissions.java
+++ b/vlc-android/src/org/videolan/vlc/util/Permissions.java
@@ -73,9 +73,14 @@ public class Permissions {
 
     @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)
-            return VLCApplication.getAppContext().getExternalFilesDir(null) != null;
-        return !AndroidUtil.isMarshMallowOrLater || ContextCompat.checkSelfPermission(VLCApplication.getAppContext(),
+            return context.getExternalFilesDir(null) != null;
+        return !AndroidUtil.isMarshMallowOrLater || ContextCompat.checkSelfPermission(context,
                 Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
     }
 



More information about the Android mailing list