[Android] Get permission on play from 3rd party

Geoffrey Métais git at videolan.org
Thu Oct 17 18:37:21 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct 17 18:35:07 2019 +0200| [3fbfb3b1e6360110346b9f9c5d0f440a80f7add5] | committer: Geoffrey Métais

Get permission on play from 3rd party

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

 vlc-android/src/org/videolan/vlc/StartActivity.kt  | 23 +++++++++++-----------
 .../src/org/videolan/vlc/gui/SendCrashActivity.kt  |  3 ++-
 .../gui/helpers/hf/StoragePermissionsDelegate.kt   | 12 ++++++-----
 .../vlc/gui/onboarding/OnboardingActivity.kt       |  4 ++--
 4 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.kt b/vlc-android/src/org/videolan/vlc/StartActivity.kt
index 629a7be5b..0f554a0a4 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.kt
@@ -32,13 +32,13 @@ import android.text.TextUtils
 import android.util.Log
 import androidx.core.content.ContextCompat
 import androidx.fragment.app.FragmentActivity
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.*
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.medialibrary.MLServiceLocator
 import org.videolan.vlc.gui.BetaWelcomeActivity
 import org.videolan.vlc.gui.MainActivity
 import org.videolan.vlc.gui.SearchActivity
+import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate.Companion.getStoragePermission
 import org.videolan.vlc.gui.onboarding.ONBOARDING_DONE_KEY
 import org.videolan.vlc.gui.onboarding.startOnboarding
 import org.videolan.vlc.gui.tv.MainTvActivity
@@ -54,7 +54,7 @@ private const val SEND_CRASH_RESULT = 0
 private const val TAG = "VLC/StartActivity"
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
-class StartActivity : FragmentActivity() {
+class StartActivity : FragmentActivity(), CoroutineScope by MainScope() {
 
     private val idFromShortcut: Int
         get() {
@@ -195,17 +195,16 @@ class StartActivity : FragmentActivity() {
         }
     }
 
-    private fun startPlaybackFromApp(intent: Intent) {
-        if (intent.type?.startsWith("video") == true) {
-            try {
-                startActivity(intent.setClass(this, VideoPlayerActivity::class.java))
+    private fun startPlaybackFromApp(intent: Intent) = launch(start = CoroutineStart.UNDISPATCHED) {
+        if (Permissions.canReadStorage(applicationContext) || getStoragePermission()) when {
+            intent.type?.startsWith("video") == true -> try {
+                startActivity(intent.setClass(this at StartActivity, VideoPlayerActivity::class.java))
             } catch (ex: SecurityException) {
-                intent.data.let { MediaUtils.openMediaNoUi(it) }
+                intent.data?.let { MediaUtils.openMediaNoUi(it) }
             }
-        } else if (intent.data?.authority == getString(R.string.tv_provider_authority)) {
-            MediaUtils.openMediaNoUiFromTvContent(this, intent.data)
-        } else
-            intent.data.let { MediaUtils.openMediaNoUi(it) }
+            intent.data?.authority == getString(R.string.tv_provider_authority) -> MediaUtils.openMediaNoUiFromTvContent(this at StartActivity, intent.data)
+            else -> intent.data?.let { MediaUtils.openMediaNoUi(it) }
+        }
         finish()
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/SendCrashActivity.kt b/vlc-android/src/org/videolan/vlc/gui/SendCrashActivity.kt
index f424b94dc..8e4fadeae 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SendCrashActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/SendCrashActivity.kt
@@ -47,6 +47,7 @@ import org.videolan.vlc.R
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.databinding.SendCrashActivityBinding
 import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate
+import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate.Companion.getStoragePermission
 import org.videolan.vlc.util.*
 import java.io.File
 import java.lang.Runnable
@@ -94,7 +95,7 @@ class SendCrashActivity : AppCompatActivity(), DebugLogService.Client.Callback,
                 //get medialib db if needed
                 val attachments = ArrayList<Uri>()
                 if (binding.includeMedialibSwitch.isChecked) {
-                    if (StoragePermissionsDelegate.getStoragePermission(this at SendCrashActivity, true)) {
+                    if (getStoragePermission(true)) {
 
                         if (!::dbPath.isInitialized) {
                             val path = VLCApplication.appContext.getExternalFilesDir(null)?.absolutePath ?: return at withContext null
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 00e6579d9..7aa17ca98 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
@@ -21,6 +21,8 @@
  *  ***************************************************************************
  */
 
+ at file:Suppress("EXPERIMENTAL_API_USAGE")
+
 package org.videolan.vlc.gui.helpers.hf
 
 import android.Manifest
@@ -114,13 +116,13 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
             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, firstRun, upgrade)).run()
+                if (activity.getStoragePermission(write)) (cb ?: getAction(activity, firstRun, upgrade)).run()
             }
         }
 
-        suspend fun getStoragePermission(activity: FragmentActivity, write: Boolean) : Boolean{
-            if (activity.isFinishing) return false
-            val fm = activity.supportFragmentManager
+        suspend fun FragmentActivity.getStoragePermission(write: Boolean = false) : Boolean{
+            if (isFinishing) return false
+            val fm = supportFragmentManager
             var fragment: Fragment? = fm.findFragmentByTag(TAG)
             if (fragment == null) {
                 val args = Bundle()
@@ -143,7 +145,7 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
         }
 
         suspend fun FragmentActivity.getWritePermission(uri: Uri) = if (uri.path?.startsWith(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY) == true) {
-            if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage()) getStoragePermission(this, true)
+            if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage()) getStoragePermission(true)
             else withContext(Dispatchers.IO) { FileUtils.canWrite(uri) }
         } else getExtWritePermission(uri)
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingActivity.kt b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingActivity.kt
index ebe0b689a..ce9c48cc8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingActivity.kt
@@ -18,7 +18,7 @@ import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.MediaParsingService
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.MainActivity
-import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate
+import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate.Companion.getStoragePermission
 import org.videolan.vlc.gui.view.NonSwipeableViewPager
 import org.videolan.vlc.startMedialibrary
 import org.videolan.vlc.util.*
@@ -93,7 +93,7 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
             launch {
                 if (viewPager.currentItem == 0 && !viewModel.permissionGranted) {
                     viewModel.permissionGranted = Permissions.canReadStorage(applicationContext)
-                            || StoragePermissionsDelegate.getStoragePermission(this at OnboardingActivity, false)
+                            || getStoragePermission()
                     if (!viewModel.permissionGranted) {
                         return at launch
                     } else {



More information about the Android mailing list