Skip to content

Commit

Permalink
Add ability to choose a non permanent directory
Browse files Browse the repository at this point in the history
  • Loading branch information
K1rakishou committed Jul 18, 2020
1 parent ce35572 commit 774851b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
56 changes: 39 additions & 17 deletions fsaf/src/main/java/com/github/k1rakishou/fsaf/FileChooser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import android.provider.DocumentsContract
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.documentfile.provider.DocumentFile
import com.github.k1rakishou.fsaf.callback.*
import com.github.k1rakishou.fsaf.callback.ChooserCallback
import com.github.k1rakishou.fsaf.callback.FSAFActivityCallbacks
import com.github.k1rakishou.fsaf.callback.FileChooserCallback
import com.github.k1rakishou.fsaf.callback.FileCreateCallback
import com.github.k1rakishou.fsaf.callback.directory.DirectoryChooserCallback
import com.github.k1rakishou.fsaf.callback.directory.PermanentDirectoryChooserCallback
import com.github.k1rakishou.fsaf.callback.directory.TemporaryDirectoryCallback
import com.github.k1rakishou.fsaf.extensions.getMimeFromFilename

class FileChooser(
Expand Down Expand Up @@ -38,15 +44,20 @@ class FileChooser(
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.putExtra("android.content.extra.SHOW_ADVANCED", true)

intent.addFlags(
val flags = if (directoryChooserCallback is PermanentDirectoryChooserCallback) {
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
} else {
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
}

intent.addFlags(flags)

// Do not use any remote providers (dropbox/google drive/etc since they may not work correctly
// with ACTION_OPEN_DOCUMENT_TREE and persistable permissions)
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)

val nextRequestCode = ++requestCode
callbacksMap[nextRequestCode] = directoryChooserCallback as ChooserCallback
Expand Down Expand Up @@ -100,7 +111,7 @@ class FileChooser(
fsafActivityCallbacks?.let { callbacks ->
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)

Expand Down Expand Up @@ -298,13 +309,15 @@ class FileChooser(
return
}

val persist = (intent.flags and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0
if (!persist) {
val msg = "handleDirectoryChooserCallback() No grant persist uri permission given"
if (callback is PermanentDirectoryChooserCallback) {
val persist = (intent.flags and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0
if (!persist) {
val msg = "handleDirectoryChooserCallback() No grant persist uri permission given"

Log.e(TAG, msg)
callback.onCancel(msg)
return
Log.e(TAG, msg)
callback.onCancel(msg)
return
}
}

val uri = intent.data
Expand All @@ -316,13 +329,18 @@ class FileChooser(
return
}

Log.d(TAG, "treeUri = ${uri}")

if (callback is TemporaryDirectoryCallback) {
callback.onResult(uri)
return
}

val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION

val contentResolver = appContext.contentResolver
contentResolver.takePersistableUriPermission(uri, flags)
Log.d(TAG, "treeUri = ${uri}")

callback.onResult(uri)
}

Expand All @@ -337,14 +355,18 @@ class FileChooser(
}

if (!directory.exists()) {
Log.e(TAG, "Couldn't revoke permissions from directory because it does not exist, " +
"path = $directoryUri")
Log.e(
TAG, "Couldn't revoke permissions from directory because it does not exist, " +
"path = $directoryUri"
)
return false
}

if (!directory.isDirectory) {
Log.e(TAG, "Couldn't revoke permissions from directory it is not a directory, " +
"path = $directoryUri")
Log.e(
TAG, "Couldn't revoke permissions from directory it is not a directory, " +
"path = $directoryUri"
)
return false
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.k1rakishou.fsaf.callback.directory

import com.github.k1rakishou.fsaf.callback.ChooserCallback

abstract class DirectoryChooserCallback : ChooserCallback
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.k1rakishou.fsaf.callback.directory

abstract class PermanentDirectoryChooserCallback : DirectoryChooserCallback()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.k1rakishou.fsaf.callback.directory

abstract class TemporaryDirectoryCallback : DirectoryChooserCallback()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal const val BINARY_FILE_MIME_TYPE = "application/octet-stream"
internal const val CONTENT_TYPE = "${ContentResolver.SCHEME_CONTENT}://"
internal const val FILE_TYPE = "${ContentResolver.SCHEME_FILE}://"
internal val uriTypes = arrayOf(CONTENT_TYPE, FILE_TYPE)
internal const val DEFAULT_CAPACITY = 16

@Throws(IOException::class)
internal fun InputStream.copyInto(outputStream: OutputStream) {
Expand Down Expand Up @@ -78,11 +79,11 @@ internal fun String.splitIntoSegments(): List<String> = FSAFUtils.splitIntoSegme

internal fun safeCapacity(list: List<*>, divider: Int = 2): Int {
return if (list.size <= 1) {
1
DEFAULT_CAPACITY
} else {
val result = list.size / divider
if (result <= 1) {
1
DEFAULT_CAPACITY
} else {
result
}
Expand All @@ -91,11 +92,11 @@ internal fun safeCapacity(list: List<*>, divider: Int = 2): Int {

internal fun safeCapacity(string: String, divider: Int = 2): Int {
return if (string.length <= 1) {
1
DEFAULT_CAPACITY
} else {
val result = string.length / divider
if (result <= 1) {
1
DEFAULT_CAPACITY
} else {
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.github.k1rakishou.fsaf.FileChooser
import com.github.k1rakishou.fsaf.FileManager
import com.github.k1rakishou.fsaf.callback.DirectoryChooserCallback
import com.github.k1rakishou.fsaf.callback.FSAFActivityCallbacks
import com.github.k1rakishou.fsaf.callback.directory.TemporaryDirectoryCallback
import com.github.k1rakishou.fsaf.util.SAFHelper
import com.github.k1rakishou.fsaf_test_app.tests.TestSuite
import kotlinx.android.synthetic.main.activity_main.*
Expand Down Expand Up @@ -53,7 +53,7 @@ class MainActivity : AppCompatActivity(), FSAFActivityCallbacks {
updateControls()

open_document_tree_button.setOnClickListener {
fileChooser.openChooseDirectoryDialog(object : DirectoryChooserCallback() {
fileChooser.openChooseDirectoryDialog(object : TemporaryDirectoryCallback() {
override fun onResult(uri: Uri) {
println("treeUri = ${uri}")
Toast.makeText(
Expand Down

0 comments on commit 774851b

Please sign in to comment.