diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt index 89aa76e4..d3e29a79 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt @@ -304,7 +304,7 @@ class MainActivity : SimpleActivity() { try { val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) 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) } catch (e: Exception) { e.printStackTrace() @@ -325,7 +325,7 @@ class MainActivity : SimpleActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { super.onActivityResult(requestCode, resultCode, resultData) isAskingPermissions = false - if(requestCode == MANAGE_STORAGE_RC && isRPlus()){ + if (requestCode == MANAGE_STORAGE_RC && isRPlus()) { actionOnPermission?.invoke(Environment.isExternalStorageManager()) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt index 48caa7a5..dc332ffa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt @@ -327,13 +327,25 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList) { if (activity.getIsPathDirectory(path)) { val shouldShowHidden = activity.config.shouldShowHidden - if (activity.isPathOnOTG(path)) { - activity.getDocumentFile(path)?.listFiles()?.filter { if (shouldShowHidden) true else !it.name!!.startsWith(".") }?.forEach { - addFileUris(it.uri.toString(), paths) + when { + activity.isRestrictedAndroidDir(path) -> { + activity.getStorageItemsWithTreeUri(path, shouldShowHidden, false){ files-> + files.forEach { + addFileUris(activity.getPrimaryAndroidSAFUri(it.path).toString(), paths) + } + } } - } else { - File(path).listFiles()?.filter { if (shouldShowHidden) true else !it.name.startsWith('.') }?.forEach { - addFileUris(it.absolutePath, paths) + + activity.isPathOnOTG(path) -> { + activity.getDocumentFile(path)?.listFiles()?.filter { if (shouldShowHidden) true else !it.name!!.startsWith(".") }?.forEach { + addFileUris(it.uri.toString(), paths) + } + } + + else -> { + File(path).listFiles()?.filter { if (shouldShowHidden) true else !it.name.startsWith('.') }?.forEach { + addFileUris(it.absolutePath, paths) + } } } } else { @@ -363,7 +375,8 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList - val sourceFile = File(sourceFileDir.path) - if (activity.getDoesFilePathExist(source) && activity.getIsPathDirectory(source) && - sourceFile.list()?.isEmpty() == true && sourceFile.getProperSize(true) == 0L && sourceFile.getFileCount(true) == 0) { - val sourceFolder = sourceFile.toFileDirItem(activity) - activity.deleteFile(sourceFolder, true) { + val sourcePath = sourceFileDir.path + if (activity.isRestrictedAndroidDir(sourcePath) && activity.getDoesFilePathExist(sourcePath)) { + activity.deleteFile(sourceFileDir, true) { listener?.refreshItems() activity.runOnUiThread { finishActMode() } } } else { - listener?.refreshItems() - finishActMode() + val sourceFile = File(sourcePath) + if (activity.getDoesFilePathExist(source) && activity.getIsPathDirectory(source) && + sourceFile.list()?.isEmpty() == true && sourceFile.getProperSize(true) == 0L && sourceFile.getFileCount(true) == 0 + ) { + val sourceFolder = sourceFile.toFileDirItem(activity) + activity.deleteFile(sourceFolder, true) { + listener?.refreshItems() + activity.runOnUiThread { + finishActMode() + } + } + } else { + listener?.refreshItems() + finishActMode() + } } } } else { @@ -828,10 +852,13 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList { - if (isRPlus() && activity.isSAFOnlyRoot(path)) { - if (activity.createSAFOnlyDirectory(path)) { - success(alertDialog) - } else { - val error = String.format(activity.getString(R.string.could_not_create_folder), path) - activity.showErrorToast(error) - callback(false) + if (activity.isRestrictedAndroidDir(path)) { + activity.handlePrimarySAFDialog(path) { + if (!it) { + callback(false) + return@handlePrimarySAFDialog + } + if (activity.createSAFOnlyDirectory(path)) { + success(alertDialog) + } else { + val error = String.format(activity.getString(R.string.could_not_create_folder), path) + activity.showErrorToast(error) + callback(false) + } } } else { if (File(path).mkdirs()) { @@ -96,6 +102,22 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca private fun createFile(path: String, alertDialog: AlertDialog, callback: (Boolean) -> Unit) { try { 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.handleSAFDialog(path) { if (!it) { @@ -113,22 +135,13 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca success(alertDialog) } } - 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()) { - success(alertDialog) - } - } + path.startsWith(activity.internalStoragePath, true) -> { + if (File(path).createNewFile()) { + success(alertDialog) + } } + else -> { RootHelpers(activity).createFileFolder(path, true) { if (it) { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt index 0468818a..8174e4d0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt @@ -187,7 +187,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF val isSortingBySize = context!!.config.getFolderSorting(currentPath) and SORT_BY_SIZE != 0 val getProperChildCount = context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_LIST - if (isRPlus() && context.isSAFOnlyRoot(path)) { + if (context.isRestrictedAndroidDir(path)) { activity?.handlePrimarySAFDialog(path) { context.getStorageItemsWithTreeUri(path, context.config.shouldShowHidden, getProperChildCount) { callback(path, getListItemsFromFileDirItems(it)) @@ -214,7 +214,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF if (getProperChildCount) { items.filter { it.mIsDirectory }.forEach { if (context != null) { - val childrenCount = it.getDirectChildrenCount(context!!, showHidden) + val childrenCount = it.getDirectChildrenCount(activity as BaseSimpleActivity, showHidden) if (childrenCount != 0) { activity?.runOnUiThread { getRecyclerAdapter()?.updateChildCount(it.mPath, childrenCount) @@ -235,7 +235,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF var lastModified = lastModifieds.remove(curPath) 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) { if (isSortingBySize) { file.getProperSize(showHidden)