[Android] Permissions: Don't expose fragment deferred field
Geoffrey Métais
git at videolan.org
Thu Jan 17 17:39:13 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 17 17:37:52 2019 +0100| [b7f5f57df321ec19214eddb833d6499dac04de7a] | committer: Geoffrey Métais
Permissions: Don't expose fragment deferred field
> https://code.videolan.org/videolan/vlc-android/commit/b7f5f57df321ec19214eddb833d6499dac04de7a
---
.../videolan/vlc/gui/helpers/hf/BaseHeadlessFragment.kt | 17 ++++++++++-------
.../vlc/gui/helpers/hf/StoragePermissionsDelegate.kt | 10 +++++-----
.../vlc/gui/helpers/hf/WriteExternalDelegate.kt | 14 +++++++-------
3 files changed, 22 insertions(+), 19 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 8f8f287b0..91391f886 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
@@ -29,7 +29,7 @@ import androidx.fragment.app.Fragment
import kotlinx.coroutines.CompletableDeferred
open class BaseHeadlessFragment : Fragment() {
- val deferred = CompletableDeferred<Boolean>()
+ protected val deferredGrant = CompletableDeferred<Boolean>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -37,12 +37,15 @@ open class BaseHeadlessFragment : Fragment() {
}
protected fun exit() {
- activity?.run { if (!isFinishing) supportFragmentManager
- .beginTransaction()
- .remove(this at BaseHeadlessFragment)
- .commitAllowingStateLoss()
+ activity?.run {
+ if (!isFinishing) supportFragmentManager
+ .beginTransaction()
+ .remove(this at BaseHeadlessFragment)
+ .commitAllowingStateLoss()
}
}
-}
-internal fun IntArray.isGranted() = isNotEmpty() && get(0) == PackageManager.PERMISSION_GRANTED
\ No newline at end of file
+ suspend fun awaitGrant() = deferredGrant.await()
+
+ protected fun IntArray.isGranted() = isNotEmpty() && get(0) == PackageManager.PERMISSION_GRANTED
+}
\ No newline at end of file
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 a32409890..3eebe2dfb 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
@@ -83,7 +83,7 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
val ctx = activity ?: return
if (grantResults.isGranted()) {
storageAccessGranted.value = true
- deferred.complete(true)
+ deferredGrant.complete(true)
exit()
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
@@ -91,14 +91,14 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
return
} else {
storageAccessGranted.value = false
- deferred.complete(false)
+ deferredGrant.complete(false)
exit()
}
}
storageAccessGranted.value = false
- deferred.complete(false)
+ deferredGrant.complete(false)
}
- Permissions.PERMISSION_WRITE_STORAGE_TAG -> deferred.complete(grantResults.isGranted())
+ Permissions.PERMISSION_WRITE_STORAGE_TAG -> deferredGrant.complete(grantResults.isGranted())
}
}
@@ -130,7 +130,7 @@ class StoragePermissionsDelegate : BaseHeadlessFragment() {
(fragment as StoragePermissionsDelegate).requestStorageAccess(write)
return false //Fragment is already waiting for answear
}
- return fragment.deferred.await()
+ return fragment.awaitGrant()
}
private fun getAction(activity: FragmentActivity, firstRun: Boolean, upgrade: Boolean) = Runnable {
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 309e48119..68e5a9261 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
@@ -38,7 +38,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
.setPositiveButton(R.string.ok) { _, _ ->
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
storage = arguments?.getString(KEY_STORAGE_PATH)?.apply { intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Uri.parse(this)) }
- startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCES)
+ startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCESS)
}
.setNeutralButton(getString(R.string.dialog_sd_wizard)) { _, _ -> showHelpDialog() }.create().show()
}
@@ -56,7 +56,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
@TargetApi(Build.VERSION_CODES.KITKAT)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
- if (data !== null && requestCode == REQUEST_CODE_STORAGE_ACCES) {
+ if (data !== null && requestCode == REQUEST_CODE_STORAGE_ACCESS) {
if (resultCode == Activity.RESULT_OK) {
val context = context ?: return
val treeUri = data.data ?: return
@@ -72,24 +72,24 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
val file = DocumentFile.fromTreeUri(context, uriPermission.uri)
if (treeFile?.name == file?.name) {
contentResolver.releasePersistableUriPermission(uriPermission.uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
- deferred.complete(false)
+ deferredGrant.complete(false)
return
}
}
// else set permission
contentResolver.takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
- deferred.complete(true)
+ deferredGrant.complete(true)
return
}
}
- deferred.complete(false)
+ deferredGrant.complete(false)
}
companion object {
internal const val TAG = "VLC/WriteExternal"
internal const val KEY_STORAGE_PATH = "VLC/storage_path"
- private const val REQUEST_CODE_STORAGE_ACCES = 42
+ private const val REQUEST_CODE_STORAGE_ACCESS = 42
fun askForExtWrite(activity: FragmentActivity?, uri: Uri, cb: Runnable? = null) {
AppScope.launch {
@@ -103,7 +103,7 @@ class WriteExternalDelegate : BaseHeadlessFragment() {
val fragment = WriteExternalDelegate()
fragment.arguments = Bundle(1).apply { putString(KEY_STORAGE_PATH, storage) }
activity.supportFragmentManager.beginTransaction().add(fragment, TAG).commitAllowingStateLoss()
- return fragment.deferred.await()
+ return fragment.awaitGrant()
}
fun needsWritePermission(uri: Uri) : Boolean {
More information about the Android
mailing list