move the getFileDocument extension in the activity extensions

This commit is contained in:
tibbi 2017-08-02 22:20:53 +02:00
parent cf0297c075
commit bbaba1e556
3 changed files with 34 additions and 34 deletions

View file

@ -29,7 +29,7 @@ ext {
propMinSdkVersion = 16
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '2.23.1'
propVersionName = '2.23.2'
kotlin_version = '1.1.3-2'
support_libs = '25.3.1'
}

View file

@ -9,6 +9,7 @@ import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Looper
import android.provider.DocumentsContract
import android.support.v4.provider.DocumentFile
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
@ -338,3 +339,35 @@ fun BaseSimpleActivity.getFileOutputStream(file: File, callback: (outputStream:
callback(FileOutputStream(file))
}
}
@SuppressLint("NewApi")
fun BaseSimpleActivity.getFileDocument(path: String): DocumentFile? {
if (!isLollipopPlus())
return null
var relativePath = path.substring(sdCardPath.length)
if (relativePath.startsWith(File.separator))
relativePath = relativePath.substring(1)
var document = DocumentFile.fromTreeUri(this, Uri.parse(baseConfig.treeUri))
val parts = relativePath.split("/")
for (i in 0..parts.size - 1) {
var currDocument = document.findFile(parts[i])
if (currDocument == null) {
// We need to assure that we transverse to the right directory!
if (i == parts.size - 1) {
// The last document should be the file we're looking for, not a directory
currDocument = document.createFile("", parts[i])
} else {
currDocument = document.createDirectory(parts[i])
}
if (currDocument == null) {
toast(R.string.unknown_error_occurred)
return null
}
}
document = currDocument
}
return document
}

View file

@ -119,39 +119,6 @@ fun Context.getMyFileUri(file: File): Uri {
Uri.fromFile(file)
}
@SuppressLint("NewApi")
fun Context.getFileDocument(path: String): DocumentFile? {
if (!isLollipopPlus())
return null
var relativePath = path.substring(sdCardPath.length)
if (relativePath.startsWith(File.separator))
relativePath = relativePath.substring(1)
var document = DocumentFile.fromTreeUri(this, Uri.parse(baseConfig.treeUri))
val parts = relativePath.split("/")
for (i in 0..parts.size - 1) {
var currDocument = document.findFile(parts[i])
if (currDocument == null) {
// We need to assure that we transverse to the right directory!
if (i == parts.size - 1) {
// The last document should be the file we're looking for, not a directory
currDocument = document.createFile("", parts[i])
} else {
currDocument = document.createDirectory(parts[i])
}
if (currDocument == null) {
// Half paths should not be tolerated. If the given path cannot
// be accessed, give up with an Exception to let the caller know.
throw UnsupportedOperationException("Failed to reach $path.")
}
}
document = currDocument
}
return document
}
@SuppressLint("NewApi")
fun Context.tryFastDocumentDelete(file: File, allowDeleteFolder: Boolean): Boolean {
val document = getFastDocument(file)