handle copy/move, share, create new directory

This commit is contained in:
darthpaul
2021-11-02 07:45:33 +00:00
parent 26d79d5f4a
commit 6dbfdb0c24
4 changed files with 81 additions and 41 deletions

View File

@ -304,7 +304,7 @@ class MainActivity : SimpleActivity() {
try { try {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT") intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse(String.format("package:%s", applicationContext.packageName)) intent.data = Uri.parse("package:$packageName")
startActivityForResult(intent, MANAGE_STORAGE_RC) startActivityForResult(intent, MANAGE_STORAGE_RC)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
@ -325,7 +325,7 @@ class MainActivity : SimpleActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData) super.onActivityResult(requestCode, resultCode, resultData)
isAskingPermissions = false isAskingPermissions = false
if(requestCode == MANAGE_STORAGE_RC && isRPlus()){ if (requestCode == MANAGE_STORAGE_RC && isRPlus()) {
actionOnPermission?.invoke(Environment.isExternalStorageManager()) actionOnPermission?.invoke(Environment.isExternalStorageManager())
} }
} }

View File

@ -327,15 +327,27 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
private fun addFileUris(path: String, paths: ArrayList<String>) { private fun addFileUris(path: String, paths: ArrayList<String>) {
if (activity.getIsPathDirectory(path)) { if (activity.getIsPathDirectory(path)) {
val shouldShowHidden = activity.config.shouldShowHidden val shouldShowHidden = activity.config.shouldShowHidden
if (activity.isPathOnOTG(path)) { when {
activity.isRestrictedAndroidDir(path) -> {
activity.getStorageItemsWithTreeUri(path, shouldShowHidden, false){ files->
files.forEach {
addFileUris(activity.getPrimaryAndroidSAFUri(it.path).toString(), paths)
}
}
}
activity.isPathOnOTG(path) -> {
activity.getDocumentFile(path)?.listFiles()?.filter { if (shouldShowHidden) true else !it.name!!.startsWith(".") }?.forEach { activity.getDocumentFile(path)?.listFiles()?.filter { if (shouldShowHidden) true else !it.name!!.startsWith(".") }?.forEach {
addFileUris(it.uri.toString(), paths) addFileUris(it.uri.toString(), paths)
} }
} else { }
else -> {
File(path).listFiles()?.filter { if (shouldShowHidden) true else !it.name.startsWith('.') }?.forEach { File(path).listFiles()?.filter { if (shouldShowHidden) true else !it.name.startsWith('.') }?.forEach {
addFileUris(it.absolutePath, paths) addFileUris(it.absolutePath, paths)
} }
} }
}
} else { } else {
paths.add(path) paths.add(path)
} }
@ -363,7 +375,8 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
RadioItem(OPEN_AS_IMAGE, res.getString(R.string.image_file)), RadioItem(OPEN_AS_IMAGE, res.getString(R.string.image_file)),
RadioItem(OPEN_AS_AUDIO, res.getString(R.string.audio_file)), RadioItem(OPEN_AS_AUDIO, res.getString(R.string.audio_file)),
RadioItem(OPEN_AS_VIDEO, res.getString(R.string.video_file)), RadioItem(OPEN_AS_VIDEO, res.getString(R.string.video_file)),
RadioItem(OPEN_AS_OTHER, res.getString(R.string.other_file))) RadioItem(OPEN_AS_OTHER, res.getString(R.string.other_file))
)
RadioGroupDialog(activity, items) { RadioGroupDialog(activity, items) {
activity.tryOpenPathIntent(getFirstSelectedItemPath(), false, it as Int) activity.tryOpenPathIntent(getFirstSelectedItemPath(), false, it as Int)
@ -387,9 +400,19 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
activity.copyMoveFilesTo(files, source, it, isCopyOperation, false, activity.config.shouldShowHidden) { activity.copyMoveFilesTo(files, source, it, isCopyOperation, false, activity.config.shouldShowHidden) {
if (!isCopyOperation) { if (!isCopyOperation) {
files.forEach { sourceFileDir -> files.forEach { sourceFileDir ->
val sourceFile = File(sourceFileDir.path) val sourcePath = sourceFileDir.path
if (activity.isRestrictedAndroidDir(sourcePath) && activity.getDoesFilePathExist(sourcePath)) {
activity.deleteFile(sourceFileDir, true) {
listener?.refreshItems()
activity.runOnUiThread {
finishActMode()
}
}
} else {
val sourceFile = File(sourcePath)
if (activity.getDoesFilePathExist(source) && activity.getIsPathDirectory(source) && if (activity.getDoesFilePathExist(source) && activity.getIsPathDirectory(source) &&
sourceFile.list()?.isEmpty() == true && sourceFile.getProperSize(true) == 0L && sourceFile.getFileCount(true) == 0) { sourceFile.list()?.isEmpty() == true && sourceFile.getProperSize(true) == 0L && sourceFile.getFileCount(true) == 0
) {
val sourceFolder = sourceFile.toFileDirItem(activity) val sourceFolder = sourceFile.toFileDirItem(activity)
activity.deleteFile(sourceFolder, true) { activity.deleteFile(sourceFolder, true) {
listener?.refreshItems() listener?.refreshItems()
@ -402,6 +425,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
finishActMode() finishActMode()
} }
} }
}
} else { } else {
listener?.refreshItems() listener?.refreshItems()
finishActMode() finishActMode()
@ -828,10 +852,13 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
path path
} }
if (hasOTGConnected && itemToLoad is String && activity.isPathOnOTG(itemToLoad) && baseConfig.OTGTreeUri.isNotEmpty() && baseConfig.OTGPartition.isNotEmpty()) { if (activity.isRestrictedAndroidDir(path)) {
itemToLoad = activity.getPrimaryAndroidSAFUri(path)
} else if (hasOTGConnected && itemToLoad is String && activity.isPathOnOTG(itemToLoad) && baseConfig.OTGTreeUri.isNotEmpty() && baseConfig.OTGPartition.isNotEmpty()) {
itemToLoad = getOTGPublicPath(itemToLoad) itemToLoad = getOTGPublicPath(itemToLoad)
} }
return itemToLoad return itemToLoad
} }

View File

@ -67,7 +67,12 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
success(alertDialog) success(alertDialog)
} }
path.startsWith(activity.internalStoragePath, true) -> { path.startsWith(activity.internalStoragePath, true) -> {
if (isRPlus() && activity.isSAFOnlyRoot(path)) { if (activity.isRestrictedAndroidDir(path)) {
activity.handlePrimarySAFDialog(path) {
if (!it) {
callback(false)
return@handlePrimarySAFDialog
}
if (activity.createSAFOnlyDirectory(path)) { if (activity.createSAFOnlyDirectory(path)) {
success(alertDialog) success(alertDialog)
} else { } else {
@ -75,6 +80,7 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
activity.showErrorToast(error) activity.showErrorToast(error)
callback(false) callback(false)
} }
}
} else { } else {
if (File(path).mkdirs()) { if (File(path).mkdirs()) {
success(alertDialog) success(alertDialog)
@ -96,6 +102,22 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
private fun createFile(path: String, alertDialog: AlertDialog, callback: (Boolean) -> Unit) { private fun createFile(path: String, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
try { try {
when { when {
activity.isRestrictedAndroidDir(path) -> {
activity.handlePrimarySAFDialog(path) {
if (!it) {
callback(false)
return@handlePrimarySAFDialog
}
if (activity.createSAFOnlyFile(path)) {
success(alertDialog)
} else {
val error = String.format(activity.getString(R.string.could_not_create_file), path)
activity.showErrorToast(error)
callback(false)
}
}
}
activity.needsStupidWritePermissions(path) -> { activity.needsStupidWritePermissions(path) -> {
activity.handleSAFDialog(path) { activity.handleSAFDialog(path) {
if (!it) { if (!it) {
@ -113,22 +135,13 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
success(alertDialog) success(alertDialog)
} }
} }
path.startsWith(activity.internalStoragePath, true) -> { path.startsWith(activity.internalStoragePath, true) -> {
if (isRPlus() && activity.isSAFOnlyRoot(path)) {
if (activity.createSAFOnlyFile(path)) {
success(alertDialog)
} else {
val error = String.format(activity.getString(R.string.could_not_create_file), path)
activity.showErrorToast(error)
callback(false)
}
} else {
if (File(path).createNewFile()) { if (File(path).createNewFile()) {
success(alertDialog) success(alertDialog)
} }
} }
}
else -> { else -> {
RootHelpers(activity).createFileFolder(path, true) { RootHelpers(activity).createFileFolder(path, true) {
if (it) { if (it) {

View File

@ -187,7 +187,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
val isSortingBySize = context!!.config.getFolderSorting(currentPath) and SORT_BY_SIZE != 0 val isSortingBySize = context!!.config.getFolderSorting(currentPath) and SORT_BY_SIZE != 0
val getProperChildCount = context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_LIST val getProperChildCount = context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_LIST
if (isRPlus() && context.isSAFOnlyRoot(path)) { if (context.isRestrictedAndroidDir(path)) {
activity?.handlePrimarySAFDialog(path) { activity?.handlePrimarySAFDialog(path) {
context.getStorageItemsWithTreeUri(path, context.config.shouldShowHidden, getProperChildCount) { context.getStorageItemsWithTreeUri(path, context.config.shouldShowHidden, getProperChildCount) {
callback(path, getListItemsFromFileDirItems(it)) callback(path, getListItemsFromFileDirItems(it))
@ -214,7 +214,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
if (getProperChildCount) { if (getProperChildCount) {
items.filter { it.mIsDirectory }.forEach { items.filter { it.mIsDirectory }.forEach {
if (context != null) { if (context != null) {
val childrenCount = it.getDirectChildrenCount(context!!, showHidden) val childrenCount = it.getDirectChildrenCount(activity as BaseSimpleActivity, showHidden)
if (childrenCount != 0) { if (childrenCount != 0) {
activity?.runOnUiThread { activity?.runOnUiThread {
getRecyclerAdapter()?.updateChildCount(it.mPath, childrenCount) getRecyclerAdapter()?.updateChildCount(it.mPath, childrenCount)
@ -235,7 +235,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
var lastModified = lastModifieds.remove(curPath) var lastModified = lastModifieds.remove(curPath)
val isDirectory = if (lastModified != null) false else file.isDirectory val isDirectory = if (lastModified != null) false else file.isDirectory
val children = if (isDirectory && getProperChildCount) file.getDirectChildrenCount(showHidden) else 0 val children = if (isDirectory && getProperChildCount) file.getDirectChildrenCount(context, showHidden) else 0
val size = if (isDirectory) { val size = if (isDirectory) {
if (isSortingBySize) { if (isSortingBySize) {
file.getProperSize(showHidden) file.getProperSize(showHidden)