Merge pull request #4170 from kiwix/Fixes#4045

Showing a warning dialog when copying/moving the ZIM chunks in PS version.
This commit is contained in:
Kelson 2025-01-19 16:20:29 +01:00 committed by GitHub
commit 1949d29973
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 4 deletions

View File

@ -52,6 +52,7 @@ import org.kiwix.kiwixmobile.core.utils.INTERNAL_SELECT_POSITION
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.kiwixmobile.main.KiwixMainActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.zimManager.Fat32Checker import org.kiwix.kiwixmobile.zimManager.Fat32Checker
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.Companion.FOUR_GIGABYTES_IN_KILOBYTES import org.kiwix.kiwixmobile.zimManager.Fat32Checker.Companion.FOUR_GIGABYTES_IN_KILOBYTES
@ -405,6 +406,9 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
suspend fun isValidZimFile(destinationFile: File): Boolean =
FileUtils.isSplittedZimFile(destinationFile.name) || validateZimFileValid(destinationFile)
fun handleInvalidZimFile(destinationFile: File, sourceUri: Uri) { fun handleInvalidZimFile(destinationFile: File, sourceUri: Uri) {
val errorMessage = activity.getString(R.string.error_file_invalid) val errorMessage = activity.getString(R.string.error_file_invalid)
if (isMoveOperation) { if (isMoveOperation) {
@ -428,7 +432,7 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
suspend fun isValidZimFile(destinationFile: File): Boolean { private suspend fun validateZimFileValid(destinationFile: File): Boolean {
var archive: Archive? = null var archive: Archive? = null
return try { return try {
// create archive object, and check if it has the mainEntry or not to validate the ZIM file. // create archive object, and check if it has the mainEntry or not to validate the ZIM file.

View File

@ -93,6 +93,7 @@ import org.kiwix.kiwixmobile.core.utils.SimpleRecyclerViewScrollListener.Compani
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.files.FileUtils import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.isSplittedZimFile
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
@ -422,7 +423,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
// If the file name is not found, then let them to copy the file // If the file name is not found, then let them to copy the file
// and we will handle this later. // and we will handle this later.
val fileName = documentFile?.name val fileName = documentFile?.name
if (fileName != null && !FileUtils.isValidZimFile(fileName)) { if (fileName != null && !isValidZimFile(fileName)) {
activity.toast(string.error_file_invalid) activity.toast(string.error_file_invalid)
return@launch return@launch
} }
@ -439,6 +440,9 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
} }
} }
private fun isValidZimFile(fileName: String): Boolean =
FileUtils.isValidZimFile(fileName) || FileUtils.isSplittedZimFile(fileName)
private suspend fun getZimFileFromUri( private suspend fun getZimFileFromUri(
uri: Uri uri: Uri
): File? { ): File? {
@ -633,11 +637,11 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
!shouldShowRationalePermission() !shouldShowRationalePermission()
override fun onFileCopied(file: File) { override fun onFileCopied(file: File) {
navigateToReaderFragment(file = file) validateAndOpenZimInReader(file)
} }
override fun onFileMoved(file: File) { override fun onFileMoved(file: File) {
navigateToReaderFragment(file = file) validateAndOpenZimInReader(file)
} }
override fun onError(errorMessage: String) { override fun onError(errorMessage: String) {
@ -694,4 +698,16 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
copyMoveFileHandler?.copyMoveZIMFileInSelectedStorage(storageDevice) copyMoveFileHandler?.copyMoveZIMFileInSelectedStorage(storageDevice)
} }
} }
private fun validateAndOpenZimInReader(file: File) {
if (isSplittedZimFile(file.path)) {
showWarningDialogForSplittedZimFile()
} else {
navigateToReaderFragment(file = file)
}
}
private fun showWarningDialogForSplittedZimFile() {
dialogShower.show(KiwixDialog.ShowWarningAboutSplittedZimFile)
}
} }

View File

@ -118,6 +118,14 @@ sealed class KiwixDialog(
cancelable = false cancelable = false
) )
data object ShowWarningAboutSplittedZimFile : KiwixDialog(
R.string.verify_zim_chunk_dialog_title,
R.string.verify_zim_chunks_dialog_message,
android.R.string.ok,
null,
cancelable = false
)
object SaveOrOpenUnsupportedFiles : KiwixDialog( object SaveOrOpenUnsupportedFiles : KiwixDialog(
R.string.save_or_open_unsupported_files_dialog_title, R.string.save_or_open_unsupported_files_dialog_title,
R.string.save_or_open_unsupported_files_dialog_message, R.string.save_or_open_unsupported_files_dialog_message,

View File

@ -489,6 +489,19 @@ object FileUtils {
fun isValidZimFile(filePath: String): Boolean = fun isValidZimFile(filePath: String): Boolean =
filePath.endsWith(".zim") || filePath.endsWith(".zimaa") filePath.endsWith(".zim") || filePath.endsWith(".zimaa")
/**
* Determines whether the given file path corresponds to a split ZIM file.
*
* A split ZIM file has an extension that starts with ".zima" followed by a single character,
* such as ".zimaa", ".zimab", etc. This method checks if the file path ends with this specific pattern.
*
* @param filePath The file path to evaluate.
* @return `true` if the file path corresponds to a split ZIM file, `false` otherwise.
*/
@JvmStatic
fun isSplittedZimFile(filePath: String): Boolean =
filePath.matches(Regex(".*\\.zima.$"))
/** /**
* Get the main storage path for a given storage name (SD card or USB stick). * Get the main storage path for a given storage name (SD card or USB stick).
* *

View File

@ -334,6 +334,8 @@
<string name="moving_zim_file">Moving ZIM file…</string> <string name="moving_zim_file">Moving ZIM file…</string>
<string name="copy_move_files_dialog_description">Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there?</string> <string name="copy_move_files_dialog_description">Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there?</string>
<string name="copy_file_error_message">Error in copying the ZIM file: %s.</string> <string name="copy_file_error_message">Error in copying the ZIM file: %s.</string>
<string name="verify_zim_chunk_dialog_title">Be cautious: Ensure all ZIM chunks have been properly moved/copied!</string>
<string name="verify_zim_chunks_dialog_message">You can verify them inside “Android/media/org.kiwix…/” folder.\nOnce done, refresh the library screen by swiping down. The split ZIM file will then appear in your library.</string>
<string name="why_copy_move_files_to_app_directory">Why copy/move files to app public directory?</string> <string name="why_copy_move_files_to_app_directory">Why copy/move files to app public directory?</string>
<string name="copy_move_files_to_app_directory_description">Due to Google Play policies on Android 11 and above, our app can no longer directly access files stored elsewhere on your device. To let you view your selected files, we need to move or copy them into a special folder within our application directory. This allows us to access and open the files.</string> <string name="copy_move_files_to_app_directory_description">Due to Google Play policies on Android 11 and above, our app can no longer directly access files stored elsewhere on your device. To let you view your selected files, we need to move or copy them into a special folder within our application directory. This allows us to access and open the files.</string>
<string name="choose_storage_to_copy_move_zim_file">Choose storage to copy/move ZIM file</string> <string name="choose_storage_to_copy_move_zim_file">Choose storage to copy/move ZIM file</string>