[Android] Headless fragments: clean code

Geoffrey Métais git at videolan.org
Thu Jan 17 12:32:58 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 17 08:53:02 2019 +0100| [7787511cbf6af81bb73bebf6d652673b2f332bd3] | committer: Geoffrey Métais

Headless fragments: clean code

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

 .../vlc/gui/helpers/hf/BaseHeadlessFragment.kt     | 17 ++++++++------
 .../gui/helpers/hf/StoragePermissionsDelegate.kt   | 26 +++++++++++-----------
 .../vlc/gui/helpers/hf/WriteExternalDelegate.kt    |  6 +++--
 3 files changed, 27 insertions(+), 22 deletions(-)

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 d93786795..4d4349ef9 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
@@ -32,9 +32,8 @@ import kotlinx.coroutines.channels.SendChannel
 import kotlinx.coroutines.launch
 import org.videolan.vlc.util.AppScope
 
-open class
-BaseHeadlessFragment : Fragment() {
-    protected var mActivity: FragmentActivity? = null
+open class BaseHeadlessFragment : Fragment() {
+    protected var fragmentActivity: FragmentActivity? = null
     var channel: SendChannel<Unit>? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -44,20 +43,24 @@ BaseHeadlessFragment : Fragment() {
 
     override fun onAttach(context: Context?) {
         super.onAttach(context)
-        if (context is FragmentActivity) mActivity = context
+        fragmentActivity = context as? FragmentActivity
     }
 
     override fun onDetach() {
         super.onDetach()
-        mActivity = null
+        fragmentActivity = null
     }
 
     protected fun exit() {
-        if (mActivity?.isFinishing == false) mActivity!!.supportFragmentManager.beginTransaction().remove(this).commitAllowingStateLoss()
+        fragmentActivity?.run { if (!isFinishing) supportFragmentManager
+                    .beginTransaction()
+                    .remove(this at BaseHeadlessFragment)
+                    .commitAllowingStateLoss()
+        }
     }
 
     fun executePendingAction() {
-        channel?.let { it.offer(Unit) }
+        channel?.offer(Unit)
         channel = 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 3e5620c62..0fc7db43d 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
@@ -42,9 +42,9 @@ import videolan.org.commontools.LiveEvent
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 class StoragePermissionsDelegate : BaseHeadlessFragment() {
 
-    private var mFirstRun: Boolean = false
-    private var mUpgrade: Boolean = false
-    private var mWrite: Boolean = false
+    private var firstRun: Boolean = false
+    private var upgrade: Boolean = false
+    private var write: Boolean = false
 
     interface CustomActionController {
         fun onStorageAccessGranted()
@@ -52,22 +52,22 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        val intent = if (mActivity == null) null else mActivity!!.intent
+        val intent = if (fragmentActivity == null) null else fragmentActivity!!.intent
         if (intent !== null && intent.getBooleanExtra(EXTRA_UPGRADE, false)) {
-            mUpgrade = true
-            mFirstRun = intent.getBooleanExtra(EXTRA_FIRST_RUN, false)
+            upgrade = true
+            firstRun = intent.getBooleanExtra(EXTRA_FIRST_RUN, false)
             intent.removeExtra(EXTRA_UPGRADE)
             intent.removeExtra(EXTRA_FIRST_RUN)
         }
-        mWrite = arguments?.getBoolean("write") ?: false
+        write = arguments?.getBoolean("write") ?: false
         if (AndroidUtil.isMarshMallowOrLater && !canReadStorage(activity!!)) {
             if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
-                Permissions.showStoragePermissionDialog(mActivity, false)
+                Permissions.showStoragePermissionDialog(fragmentActivity, false)
             else
                 requestStorageAccess(false)
-        } else if (mWrite) {
+        } else if (write) {
             if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
-                Permissions.showStoragePermissionDialog(mActivity, false)
+                Permissions.showStoragePermissionDialog(fragmentActivity, false)
             else
                 requestStorageAccess(true)
         }
@@ -85,11 +85,11 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
                 val ctx = activity ?: return
                 if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                     if (ctx is CustomActionController) ctx.onStorageAccessGranted()
-                    else ctx.startMedialibrary(mFirstRun, mUpgrade, true)
+                    else ctx.startMedialibrary(firstRun, upgrade, true)
                     storageAccessGranted.value = true
                     exit()
-                } else if (mActivity != null) {
-                    Permissions.showStoragePermissionDialog(mActivity, false)
+                } else if (fragmentActivity != null) {
+                    Permissions.showStoragePermissionDialog(fragmentActivity, false)
                     if (!shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
                         exit()
                 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/WriteExternalDelegate.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/WriteExternalDelegate.kt
index b1b2fd342..95426643a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/hf/WriteExternalDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/hf/WriteExternalDelegate.kt
@@ -11,6 +11,7 @@ import androidx.fragment.app.FragmentActivity
 import androidx.documentfile.provider.DocumentFile
 import androidx.appcompat.app.AlertDialog
 import android.text.TextUtils
+import androidx.annotation.RequiresApi
 import kotlinx.coroutines.channels.Channel
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.vlc.R
@@ -28,6 +29,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
         showDialog()
     }
 
+    @TargetApi(Build.VERSION_CODES.O)
     private fun showDialog() {
         if (!isAdded) return
         val builder = AlertDialog.Builder(activity!!)
@@ -57,7 +59,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
         if (data !== null && requestCode == REQUEST_CODE_STORAGE_ACCES) {
             if (resultCode == Activity.RESULT_OK) {
                 val context = context ?: return
-                val treeUri = data.data
+                val treeUri = data.data ?: return
                 Settings.getInstance(context).edit()
                         .putString("tree_uri_$storage", treeUri.toString())
                         .apply()
@@ -102,7 +104,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
         }
 
         fun needsWritePermission(uri: Uri) : Boolean {
-            val path = uri.path
+            val path = uri.path ?: return false
             return AndroidUtil.isLolliPopOrLater && "file" == uri.scheme
                     && !TextUtils.isEmpty(path) && path.startsWith('/')
                     && !path.startsWith(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)



More information about the Android mailing list