Skip to content

Commit

Permalink
Fixed: ZIM file not moving/Copying on Android 14 tablet.
Browse files Browse the repository at this point in the history
* When opening the ZIM file via file picker or deep linking, we were obtaining the file path using the getZimFileFromUri() method to perform the renaming for the move functionality. However, on Android 14 tablets, the URI was not resolved correctly by this method, leading to a "ZIM file not found" error.
* To fix this issue, we removed the dependency on the `getZimFileFromUri()` method in the Play Store variant, as future Android versions may handle URIs differently. We refactored our code so that the copy/move functionality works independently of this method and now works directly with Android's provided URI.
  • Loading branch information
MohitMaliDeveloper committed Sep 24, 2024
1 parent bc394dc commit 0596a60
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CopyMoveFileHandler @Inject constructor(
) {
private var fileCopyMoveCallback: FileCopyMoveCallback? = null
private var selectedFileUri: Uri? = null
private var selectedFile: File? = null
private var selectedFile: DocumentFile? = null
private var copyMovePreparingDialog: Dialog? = null
private var progressBarDialog: AlertDialog? = null
private var lifecycleScope: CoroutineScope? = null
Expand All @@ -87,8 +87,8 @@ class CopyMoveFileHandler @Inject constructor(
}
}

fun showMoveFileToPublicDirectoryDialog(uri: Uri? = null, file: File? = null) {
setSelectedFileAndUri(uri, file)
fun showMoveFileToPublicDirectoryDialog(uri: Uri? = null, documentFile: DocumentFile? = null) {
setSelectedFileAndUri(uri, documentFile)
if (!sharedPreferenceUtil.copyMoveZimFilePermissionDialog) {
showMoveToPublicDirectoryPermissionDialog()
} else {
Expand All @@ -98,9 +98,9 @@ class CopyMoveFileHandler @Inject constructor(
}
}

fun setSelectedFileAndUri(uri: Uri?, file: File?) {
fun setSelectedFileAndUri(uri: Uri?, documentFile: DocumentFile?) {
selectedFileUri = uri
selectedFile = file
selectedFile = documentFile
}

fun setFileCopyMoveCallback(fileCopyMoveCallback: FileCopyMoveCallback?) {
Expand Down Expand Up @@ -226,9 +226,7 @@ class CopyMoveFileHandler @Inject constructor(
val sourceUri = selectedFileUri ?: throw FileNotFoundException("Selected file not found")
showProgressDialog()
var moveSuccess = false
if (tryMoveWithRenamingFile(destinationFile)) {
moveSuccess = true
} else if (tryMoveWithDocumentContract(sourceUri)) {
if (tryMoveWithDocumentContract(sourceUri)) {
moveSuccess = true
} else {
moveSuccess = true
Expand All @@ -249,15 +247,12 @@ class CopyMoveFileHandler @Inject constructor(
}
}

private fun tryMoveWithRenamingFile(destinationFile: File): Boolean =
selectedFile?.renameTo(destinationFile) == true

@Suppress("UnsafeCallOnNullableType")
private fun tryMoveWithDocumentContract(selectedUri: Uri): Boolean {
return try {
val contentResolver = activity.contentResolver
if (documentCanMove(selectedUri, contentResolver)) {
val sourceParentFolderUri = DocumentFile.fromFile(selectedFile?.parentFile!!).uri
val sourceParentFolderUri = selectedFile?.parentFile!!.uri
val destinationFolderUri = DocumentFile.fromFile(File(sharedPreferenceUtil.prefStorage)).uri

DocumentsContract.moveDocument(
Expand Down Expand Up @@ -361,7 +356,7 @@ class CopyMoveFileHandler @Inject constructor(
} ?: throw FileNotFoundException("The selected file could not be opened")
}

fun getDestinationFile(): File {
private fun getDestinationFile(): File {
val root = File(sharedPreferenceUtil.prefStorage)
val fileName = selectedFile?.name ?: ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
Expand Down Expand Up @@ -403,12 +404,13 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
}

private fun handleSelectedFileUri(uri: Uri) {
getZimFileFromUri(uri)?.let { file ->
if (sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
copyMoveFileHandler?.showMoveFileToPublicDirectoryDialog(uri, file)
} else {
navigateToReaderFragment(file)
}
if (sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) {
copyMoveFileHandler?.showMoveFileToPublicDirectoryDialog(
uri,
DocumentFile.fromSingleUri(requireActivity(), uri)
)
} else {
getZimFileFromUri(uri)?.let(::navigateToReaderFragment)
}
}

Expand Down

0 comments on commit 0596a60

Please sign in to comment.