removing the OTG_PATH constant with special handling

This commit is contained in:
tibbi 2019-02-15 18:12:30 +01:00
parent cc7fe71fb6
commit f8b2e3f1f6
12 changed files with 44 additions and 59 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.7.13'
propVersionName = '5.7.16'
kotlin_version = '1.3.21'
}

View file

@ -169,7 +169,7 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
}
baseConfig.OTGTreeUri = resultData.dataString
baseConfig.OTGPartition = baseConfig.OTGTreeUri.removeSuffix("%3A").substringAfterLast('/').trimEnd('/')
baseConfig.OTGPath = "/storage${baseConfig.OTGPartition}"
baseConfig.OTGPath = "/storage/${baseConfig.OTGPartition}"
funAfterOTGPermission?.invoke(true)
funAfterOTGPermission = null
@ -221,7 +221,7 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
}
fun handleSAFDialog(path: String, callback: () -> Unit): Boolean {
return if (!path.startsWith(OTG_PATH) && isShowingSAFDialog(path, baseConfig.treeUri, OPEN_DOCUMENT_TREE)) {
return if (!isPathOnOTG(path) && isShowingSAFDialog(path, baseConfig.treeUri, OPEN_DOCUMENT_TREE)) {
funAfterSAFPermission = callback
true
} else {
@ -248,7 +248,7 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
if (isCopyOperation) {
startCopyMove(fileDirItems, destination, isCopyOperation, copyPhotoVideoOnly, copyHidden)
} else {
if (source.startsWith(OTG_PATH) || destination.startsWith(OTG_PATH) || isPathOnSD(source) || isPathOnSD(destination) || fileDirItems.first().isDirectory) {
if (isPathOnOTG(source) || isPathOnOTG(destination) || isPathOnSD(source) || isPathOnSD(destination) || fileDirItems.first().isDirectory) {
handleSAFDialog(source) {
startCopyMove(fileDirItems, destination, isCopyOperation, copyPhotoVideoOnly, copyHidden)
}

View file

@ -9,11 +9,7 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withC
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.formatSize
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.getOTGPublicPath
import com.simplemobiletools.commons.extensions.hasOTGConnected
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyRecyclerView
import kotlinx.android.synthetic.main.filepicker_list_item.view.*
@ -94,7 +90,7 @@ class FilepickerItemsAdapter(activity: BaseSimpleActivity, val fileDirItems: Lis
}
if (!activity.isDestroyed) {
if (hasOTGConnected && itemToLoad is String && itemToLoad.startsWith(OTG_PATH)) {
if (hasOTGConnected && itemToLoad is String && activity.isPathOnOTG(itemToLoad)) {
itemToLoad = itemToLoad.getOTGPublicPath(activity)
}
Glide.with(activity).load(itemToLoad).transition(withCrossFade()).apply(options).into(list_item_icon)

View file

@ -65,7 +65,7 @@ class CopyMoveTask(val activity: BaseSimpleActivity, val copyOnly: Boolean = fal
file.size = file.getProperSize(activity, copyHidden)
}
val newPath = "$mDestinationPath/${file.name}"
val fileExists = if (newPath.startsWith(OTG_PATH)) activity.getOTGFastDocumentFile(newPath)?.exists()
val fileExists = if (activity.isPathOnOTG(newPath)) activity.getOTGFastDocumentFile(newPath)?.exists()
?: false else File(newPath).exists()
if (getConflictResolution(conflictResolutions, newPath) != CONFLICT_SKIP || !fileExists) {
mMaxSize += (file.size / 1000).toInt()
@ -166,7 +166,7 @@ class CopyMoveTask(val activity: BaseSimpleActivity, val copyOnly: Boolean = fal
return
}
if (source.path.startsWith(OTG_PATH)) {
if (activity.isPathOnOTG(source.path)) {
val children = activity.getDocumentFile(source.path)?.listFiles() ?: return
for (child in children) {
val newPath = "$destinationPath/${child.name}"

View file

@ -10,7 +10,6 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.FilepickerItemsAdapter
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.Breadcrumbs
@ -170,7 +169,7 @@ class FilePickerDialog(val activity: BaseSimpleActivity,
}
private fun verifyPath() {
if (currPath.startsWith(OTG_PATH)) {
if (activity.isPathOnOTG(currPath)) {
val fileDocument = activity.getSomeDocumentFile(currPath) ?: return
if ((pickFile && fileDocument.isFile) || (!pickFile && fileDocument.isDirectory)) {
sendSuccess()
@ -184,7 +183,7 @@ class FilePickerDialog(val activity: BaseSimpleActivity,
}
private fun sendSuccess() {
currPath = if (currPath == OTG_PATH || currPath.length == 1) {
currPath = if (currPath.length == 1) {
currPath
} else {
currPath.trimEnd('/')
@ -194,7 +193,7 @@ class FilePickerDialog(val activity: BaseSimpleActivity,
}
private fun getItems(path: String, getProperFileSize: Boolean, callback: (List<FileDirItem>) -> Unit) {
if (path.startsWith(OTG_PATH)) {
if (activity.isPathOnOTG(path)) {
activity.getOTGItems(path, showHidden, getProperFileSize, callback)
} else {
getRegularItems(path, getProperFileSize, callback)

View file

@ -8,7 +8,6 @@ import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.OTG_PATH
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
/**
@ -68,7 +67,7 @@ class StoragePickerDialog(val activity: BaseSimpleActivity, currPath: String, va
otgButton.apply {
id = ID_OTG
text = resources.getString(R.string.usb)
isChecked = basePath == OTG_PATH
isChecked = basePath == context.otgPath
setOnClickListener { otgPicked() }
if (isChecked) {
defaultSelectedId = id
@ -108,7 +107,7 @@ class StoragePickerDialog(val activity: BaseSimpleActivity, currPath: String, va
private fun otgPicked() {
activity.handleOTGPermission {
if (it) {
callback(OTG_PATH)
callback(activity.otgPath)
mDialog.dismiss()
} else {
radioGroup.check(defaultSelectedId)

View file

@ -230,7 +230,7 @@ fun Activity.openEditorIntent(path: String, forceChooser: Boolean, applicationId
val extension = path.getFilenameExtension()
val newFilePath = File(parent, "$newFilename.$extension")
val outputUri = if (path.startsWith(OTG_PATH)) newUri else getFinalUriFromPath("$newFilePath", applicationId)
val outputUri = if (isPathOnOTG(path)) newUri else getFinalUriFromPath("$newFilePath", applicationId)
val resInfoList = packageManager.queryIntentActivities(this, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
@ -457,7 +457,7 @@ fun BaseSimpleActivity.deleteFileBg(fileDirItem: FileDirItem, allowDeleteFolder:
return
}
var fileDeleted = !path.startsWith(OTG_PATH) && ((!file.exists() && file.length() == 0L) || file.delete())
var fileDeleted = !isPathOnOTG(path) && ((!file.exists() && file.length() == 0L) || file.delete())
if (fileDeleted) {
runOnUiThread {
callback?.invoke(true)
@ -472,7 +472,7 @@ fun BaseSimpleActivity.deleteFileBg(fileDirItem: FileDirItem, allowDeleteFolder:
handleSAFDialog(path) {
trySAFFileDelete(fileDirItem, allowDeleteFolder, callback)
}
} else if (path.startsWith(OTG_PATH)) {
} else if (isPathOnOTG(path)) {
trySAFFileDelete(fileDirItem, allowDeleteFolder, callback)
}
}
@ -675,7 +675,7 @@ fun BaseSimpleActivity.getFileOutputStreamSync(path: String, mimeType: String, p
}
fun BaseSimpleActivity.getFileInputStreamSync(path: String): InputStream? {
return if (path.startsWith(OTG_PATH)) {
return if (isPathOnOTG(path)) {
val fileDocument = getSomeDocumentFile(path)
applicationContext.contentResolver.openInputStream(fileDocument?.uri)
} else {

View file

@ -15,7 +15,6 @@ import android.text.TextUtils
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.models.FileDirItem
@ -118,7 +117,7 @@ fun Context.getHumanReadablePath(path: String): String {
return getString(when (path) {
"/" -> R.string.root
internalStoragePath -> R.string.internal
OTG_PATH -> R.string.usb
otgPath -> R.string.usb
else -> R.string.sd_card
})
}
@ -128,7 +127,6 @@ fun Context.humanizePath(path: String): String {
val basePath = path.getBasePath(this)
return when (basePath) {
"/" -> "${getHumanReadablePath(basePath)}$trimmedPath"
OTG_PATH -> path.replaceFirst(basePath, "${getHumanReadablePath(basePath).trimEnd('/')}/").replace("//", "/")
else -> trimmedPath.replaceFirst(basePath, getHumanReadablePath(basePath))
}
}
@ -137,7 +135,9 @@ fun Context.getInternalStoragePath() = Environment.getExternalStorageDirectory()
fun Context.isPathOnSD(path: String) = sdCardPath.isNotEmpty() && path.startsWith(sdCardPath)
fun Context.needsStupidWritePermissions(path: String) = (isPathOnSD(path) || path.startsWith(OTG_PATH))
fun Context.isPathOnOTG(path: String) = otgPath.isNotEmpty() && path.startsWith(otgPath)
fun Context.needsStupidWritePermissions(path: String) = (isPathOnSD(path) || isPathOnOTG(path))
fun Context.hasProperStoredTreeUri(): Boolean {
val hasProperUri = contentResolver.persistedUriPermissions.any { it.uri.toString() == baseConfig.treeUri }
@ -174,7 +174,7 @@ fun Context.tryFastDocumentDelete(path: String, allowDeleteFolder: Boolean): Boo
}
fun Context.getFastDocumentFile(path: String): DocumentFile? {
if (path.startsWith(OTG_PATH)) {
if (isPathOnOTG(path)) {
return getOTGFastDocumentFile(path)
}
@ -195,17 +195,17 @@ fun Context.getOTGFastDocumentFile(path: String): DocumentFile? {
if (baseConfig.OTGPartition.isEmpty()) {
baseConfig.OTGPartition = baseConfig.OTGTreeUri.removeSuffix("%3A").substringAfterLast('/').trimEnd('/')
baseConfig.OTGPath = "/storage${baseConfig.OTGPartition}"
baseConfig.OTGPath = "/storage/${baseConfig.OTGPartition}"
}
val relativePath = Uri.encode(path.substring(OTG_PATH.length).trim('/'))
val relativePath = Uri.encode(path.substring(baseConfig.OTGPath.length).trim('/'))
val fullUri = "${baseConfig.OTGTreeUri}/document/${baseConfig.OTGPartition}%3A$relativePath"
return DocumentFile.fromSingleUri(this, Uri.parse(fullUri))
}
fun Context.getDocumentFile(path: String): DocumentFile? {
val isOTG = path.startsWith(OTG_PATH)
var relativePath = path.substring(if (isOTG) OTG_PATH.length else sdCardPath.length)
val isOTG = isPathOnOTG(path)
var relativePath = path.substring(if (isOTG) otgPath.length else sdCardPath.length)
if (relativePath.startsWith(File.separator)) {
relativePath = relativePath.substring(1)
}
@ -335,7 +335,7 @@ fun Context.getOTGItems(path: String, shouldShowHidden: Boolean, getProperFileSi
val parts = path.split("/").dropLastWhile { it.isEmpty() }
for (part in parts) {
if (path == OTG_PATH) {
if (path == otgPath) {
break
}
@ -360,7 +360,7 @@ fun Context.getOTGItems(path: String, shouldShowHidden: Boolean, getProperFileSi
val isDirectory = file.isDirectory
val filePath = file.uri.toString().substring(basePath.length)
val decodedPath = OTG_PATH + "/" + URLDecoder.decode(filePath, "UTF-8")
val decodedPath = otgPath + "/" + URLDecoder.decode(filePath, "UTF-8")
val fileSize = when {
getProperFileSize -> file.getItemSize(shouldShowHidden)
isDirectory -> 0L
@ -431,11 +431,11 @@ fun Context.trySAFFileDelete(fileDirItem: FileDirItem, allowDeleteFolder: Boolea
}
}
fun Context.getDoesFilePathExist(path: String) = if (path.startsWith(OTG_PATH)) getOTGFastDocumentFile(path)?.exists()
fun Context.getDoesFilePathExist(path: String) = if (isPathOnOTG(path)) getOTGFastDocumentFile(path)?.exists()
?: false else File(path).exists()
fun Context.getIsPathDirectory(path: String): Boolean {
return if (path.startsWith(OTG_PATH)) {
return if (isPathOnOTG(path)) {
getOTGFastDocumentFile(path)?.isDirectory ?: false
} else {
File(path).isDirectory

View file

@ -118,6 +118,7 @@ fun Context.showErrorToast(exception: Exception, length: Int = Toast.LENGTH_LONG
val Context.baseConfig: BaseConfig get() = BaseConfig.newInstance(this)
val Context.sdCardPath: String get() = baseConfig.sdCardPath
val Context.internalStoragePath: String get() = baseConfig.internalStoragePath
val Context.otgPath: String get() = baseConfig.OTGPath
fun Context.isFingerPrintSensorAvailable() = isMarshmallowPlus() && Reprint.isHardwarePresent()
@ -301,7 +302,7 @@ fun Context.getMimeTypeFromUri(uri: Uri): String {
}
fun Context.ensurePublicUri(path: String, applicationId: String): Uri? {
return if (path.startsWith(OTG_PATH)) {
return if (isPathOnOTG(path)) {
getDocumentFile(path)?.uri
} else {
val uri = Uri.parse(path)
@ -533,7 +534,7 @@ fun Context.storeNewYourAlarmSound(resultData: Intent): AlarmSound {
@RequiresApi(Build.VERSION_CODES.N)
fun Context.saveImageRotation(path: String, degrees: Int): Boolean {
if (!isPathOnSD(path) && !path.startsWith(OTG_PATH)) {
if (!isPathOnSD(path) && !isPathOnOTG(path)) {
saveExifRotation(ExifInterface(path), degrees)
return true
} else if (isNougatPlus()) {

View file

@ -23,14 +23,11 @@ fun String.getFilenameFromPath() = substring(lastIndexOf("/") + 1)
fun String.getFilenameExtension() = substring(lastIndexOf(".") + 1)
fun String.getBasePath(context: Context): String {
return if (startsWith(context.internalStoragePath)) {
context.internalStoragePath
} else if (!context.sdCardPath.isEmpty() && startsWith(context.sdCardPath)) {
context.sdCardPath
} else if (startsWith(OTG_PATH)) {
OTG_PATH
} else {
"/"
return when {
startsWith(context.internalStoragePath) -> context.internalStoragePath
context.isPathOnSD(this) -> context.sdCardPath
context.isPathOnOTG(this) -> context.otgPath
else -> "/"
}
}
@ -143,13 +140,7 @@ fun String.getGenericMimeType(): String {
return "$type/*"
}
fun String.getParentPath(): String {
var parent = removeSuffix("/${getFilenameFromPath()}")
if (parent == "otg:") {
parent = OTG_PATH
}
return parent
}
fun String.getParentPath() = removeSuffix("/${getFilenameFromPath()}")
fun String.getDuration() = getFileDurationSeconds()?.getFormattedDuration()
@ -232,7 +223,7 @@ fun String.getImageResolution(): Point? {
fun String.getPublicUri(context: Context) = context.getDocumentFile(this)?.uri ?: ""
fun String.getOTGPublicPath(context: Context) = "${context.baseConfig.OTGTreeUri}/document/${context.baseConfig.OTGPartition}%3A${substring(OTG_PATH.length).replace("/", "%2F")}"
fun String.getOTGPublicPath(context: Context) = "${context.baseConfig.OTGTreeUri}/document/${context.baseConfig.OTGPartition}%3A${substring(context.baseConfig.OTGPath.length).replace("/", "%2F")}"
fun String.substringTo(cnt: Int): String {
return if (isEmpty()) {

View file

@ -16,7 +16,6 @@ const val REAL_FILE_PATH = "real_file_path_2"
const val IS_FROM_GALLERY = "is_from_gallery"
const val BROADCAST_REFRESH_MEDIA = "com.simplemobiletools.REFRESH_MEDIA"
const val REFRESH_PATH = "refresh_path"
const val OTG_PATH = "otg:/"
const val IS_CUSTOMIZING_COLORS = "is_customizing_colors"
const val ALARM_SOUND_TYPE_ALARM = 1
const val ALARM_SOUND_TYPE_NOTIFICATION = 2

View file

@ -56,7 +56,7 @@ data class FileDirItem(val path: String, val name: String = "", var isDirectory:
}
fun getProperSize(context: Context, countHidden: Boolean): Long {
return if (path.startsWith(OTG_PATH)) {
return if (context.isPathOnOTG(path)) {
context.getDocumentFile(path)?.getItemSize(countHidden) ?: 0
} else {
File(path).getProperSize(countHidden)
@ -64,7 +64,7 @@ data class FileDirItem(val path: String, val name: String = "", var isDirectory:
}
fun getProperFileCount(context: Context, countHidden: Boolean): Int {
return if (path.startsWith(OTG_PATH)) {
return if (context.isPathOnOTG(path)) {
context.getDocumentFile(path)?.getFileCount(countHidden) ?: 0
} else {
File(path).getFileCount(countHidden)
@ -72,7 +72,7 @@ data class FileDirItem(val path: String, val name: String = "", var isDirectory:
}
fun getDirectChildrenCount(context: Context, countHiddenItems: Boolean): Int {
return if (path.startsWith(OTG_PATH)) {
return if (context.isPathOnOTG(path)) {
context.getDocumentFile(path)?.listFiles()?.filter { if (countHiddenItems) true else !it.name!!.startsWith(".") }?.size ?: 0
} else {
File(path).getDirectChildrenCount(countHiddenItems)
@ -80,7 +80,7 @@ data class FileDirItem(val path: String, val name: String = "", var isDirectory:
}
fun getLastModified(context: Context): Long {
return if (path.startsWith(OTG_PATH)) {
return if (context.isPathOnOTG(path)) {
context.getFastDocumentFile(path)?.lastModified() ?: 0L
} else {
File(path).lastModified()