adding some extra SD/OTG path related checks

This commit is contained in:
tibbi 2019-09-03 18:30:57 +02:00
parent d984558a18
commit 1aa92e77fe
4 changed files with 26 additions and 12 deletions

View file

@ -28,12 +28,14 @@ import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.FileDirItem
import java.io.File
import java.util.*
import java.util.regex.Pattern
abstract class BaseSimpleActivity : AppCompatActivity() {
var copyMoveCallback: ((destinationPath: String) -> Unit)? = null
var actionOnPermission: ((granted: Boolean) -> Unit)? = null
var isAskingPermissions = false
var useDynamicTheme = true
var checkedDocumentPath = ""
private val GENERIC_PERM_HANDLER = 100
@ -173,12 +175,21 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) {
if (isProperSDFolder(resultData.data)) {
val partition = try {
checkedDocumentPath.substring(9, 18)
} catch (e: Exception) {
""
}
val sdOtgPattern = Pattern.compile(SD_OTG_SHORT)
if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val isProperPartition = partition.isEmpty() || (sdOtgPattern.matcher(partition).matches() && resultData.dataString!!.contains(partition))
if (isProperSDFolder(resultData.data!!) && isProperPartition) {
if (resultData.dataString == baseConfig.OTGTreeUri) {
toast(R.string.sd_card_usb_same)
return
}
saveTreeUri(resultData)
funAfterSAFPermission?.invoke(true)
funAfterSAFPermission = null
@ -187,8 +198,9 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, requestCode)
}
} else if (requestCode == OPEN_DOCUMENT_TREE_OTG && resultCode == Activity.RESULT_OK && resultData != null) {
if (isProperOTGFolder(resultData.data)) {
} else if (requestCode == OPEN_DOCUMENT_TREE_OTG && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val isProperPartition = partition.isEmpty() || (sdOtgPattern.matcher(partition).matches() && resultData.dataString!!.contains(partition))
if (isProperOTGFolder(resultData.data!!) && isProperPartition) {
if (resultData.dataString == baseConfig.treeUri) {
funAfterSAFPermission?.invoke(false)
toast(R.string.sd_card_usb_same)

View file

@ -96,7 +96,7 @@ fun Activity.isAppInstalledOnSDCard(): Boolean = try {
false
}
fun Activity.isShowingSAFDialog(path: String): Boolean {
fun BaseSimpleActivity.isShowingSAFDialog(path: String): Boolean {
return if (isPathOnSD(path) && (baseConfig.treeUri.isEmpty() || !hasProperStoredTreeUri(false))) {
runOnUiThread {
if (!isDestroyed && !isFinishing) {
@ -108,6 +108,7 @@ fun Activity.isShowingSAFDialog(path: String): Boolean {
}
if (resolveActivity(packageManager) != null) {
checkedDocumentPath = path
startActivityForResult(this, OPEN_DOCUMENT_TREE)
} else {
toast(R.string.unknown_error_occurred)
@ -124,14 +125,14 @@ fun Activity.isShowingSAFDialog(path: String): Boolean {
fun BaseSimpleActivity.isShowingOTGDialog(path: String): Boolean {
return if (isPathOnOTG(path) && (baseConfig.OTGTreeUri.isEmpty() || !hasProperStoredTreeUri(true))) {
showOTGPermissionDialog()
showOTGPermissionDialog(path)
true
} else {
false
}
}
fun Activity.showOTGPermissionDialog() {
fun BaseSimpleActivity.showOTGPermissionDialog(path: String) {
runOnUiThread {
if (!isDestroyed && !isFinishing) {
WritePermissionDialog(this, true) {
@ -141,6 +142,7 @@ fun Activity.showOTGPermissionDialog() {
}
if (resolveActivity(packageManager) != null) {
checkedDocumentPath = path
startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG)
} else {
toast(R.string.unknown_error_occurred)

View file

@ -13,9 +13,7 @@ import android.text.TextUtils
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.FileDirItem
import java.io.File
import java.net.URLDecoder
@ -28,7 +26,7 @@ fun Context.getSDCardPath(): String {
it != getInternalStoragePath() && (baseConfig.OTGPartition.isEmpty() || !it.endsWith(baseConfig.OTGPartition))
}
val fullSDpattern = Pattern.compile("^/storage/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$")
val fullSDpattern = Pattern.compile(SD_OTG_PATTERN)
var sdCardPath = directories.firstOrNull { fullSDpattern.matcher(it).matches() }
?: directories.firstOrNull { !physicalPaths.contains(it.toLowerCase()) } ?: ""
@ -43,7 +41,7 @@ fun Context.getSDCardPath(): String {
}
if (sdCardPath.isEmpty()) {
val SDpattern = Pattern.compile("^[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$")
val SDpattern = Pattern.compile(SD_OTG_SHORT)
try {
File("/storage").listFiles()?.forEach {
if (SDpattern.matcher(it.name).matches()) {

View file

@ -26,6 +26,8 @@ const val INVALID_NAVIGATION_BAR_COLOR = -1
const val CHOPPED_LIST_DEFAULT_SIZE = 50
const val SAVE_DISCARD_PROMPT_INTERVAL = 1000L
val DEFAULT_WIDGET_BG_COLOR = Color.parseColor("#33000000")
const val SD_OTG_PATTERN = "^/storage/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$"
const val SD_OTG_SHORT = "^[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$"
const val HOUR_MINUTES = 60
const val DAY_MINUTES = 24 * HOUR_MINUTES