From be81ca540524eaeed597ebe45dd22817c0b00446 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 8 Jan 2025 18:08:25 +0530 Subject: [PATCH 1/5] Showing a warning dialog when copying/moving the ZIM chunks in PS version. --- .../library/LocalLibraryFragment.kt | 20 +++++++++++++++++-- .../core/utils/dialog/KiwixDialog.kt | 8 ++++++++ .../kiwixmobile/core/utils/files/FileUtils.kt | 13 ++++++++++++ core/src/main/res/values/strings.xml | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt index 013211f74..bc505d1e9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt @@ -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.KiwixDialog 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.BooksOnDiskAdapter import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem @@ -633,11 +634,11 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal !shouldShowRationalePermission() override fun onFileCopied(file: File) { - navigateToReaderFragment(file = file) + validateAndOpenZimInReader(file) } override fun onFileMoved(file: File) { - navigateToReaderFragment(file = file) + validateAndOpenZimInReader(file) } override fun onError(errorMessage: String) { @@ -694,4 +695,19 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal copyMoveFileHandler?.copyMoveZIMFileInSelectedStorage(storageDevice) } } + + private fun validateAndOpenZimInReader(file: File) { + if (isSplittedZimFile(file.path)) { + showWarningDialogForSplittedZimFile() + } else { + navigateToReaderFragment(file = file) + } + } + + private fun showWarningDialogForSplittedZimFile() { + dialogShower.show( + KiwixDialog.ShowWarningAboutSplittedZimFile, + {} + ) + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index 7bf1861cd..ac6c71798 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -118,6 +118,14 @@ sealed class KiwixDialog( cancelable = false ) + data object ShowWarningAboutSplittedZimFile : KiwixDialog( + null, + R.string.verify_zim_chunk_copied_moved_properly, + android.R.string.ok, + android.R.string.ok, + cancelable = false + ) + object SaveOrOpenUnsupportedFiles : KiwixDialog( R.string.save_or_open_unsupported_files_dialog_title, R.string.save_or_open_unsupported_files_dialog_message, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index b4d482946..d4f437357 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -489,6 +489,19 @@ object FileUtils { fun isValidZimFile(filePath: String): Boolean = 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). * diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 9721e4e00..9eba4713c 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -334,6 +334,7 @@ Moving ZIM file… Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there? Error in copying the ZIM file: %s. + Be cautious: verify that all the ZIM chunks have been moved/copied properly! Why copy/move files to app public directory? 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. Choose storage to copy/move ZIM file From 4e16102bc47b0c5138f94eb14d3751e20c3eba80 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 9 Jan 2025 12:34:55 +0530 Subject: [PATCH 2/5] Improved the copying/moving of ZIM chunks. * Now all the ZIM chunks are copying/moving correctly. * Improved the warning message to inform user what should they do after copying/moving all the ZIM chunks. * Implement a feature to directly view the copied/moved ZIM chunks by clicking on the button in warning dialog. --- .../library/CopyMoveFileHandler.kt | 6 ++++- .../library/LocalLibraryFragment.kt | 27 +++++++++++++++++-- .../core/utils/dialog/KiwixDialog.kt | 2 +- core/src/main/res/values/strings.xml | 3 ++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt index 4b118dd79..0f9e9d5ff 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt @@ -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.dialog.AlertDialogShower 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.zimManager.Fat32Checker 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) { val errorMessage = activity.getString(R.string.error_file_invalid) 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 return try { // create archive object, and check if it has the mainEntry or not to validate the ZIM file. diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt index bc505d1e9..ea200e5e8 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt @@ -44,6 +44,7 @@ import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.core.content.FileProvider import androidx.core.net.toUri import androidx.core.view.MenuHost import androidx.core.view.MenuProvider @@ -423,7 +424,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal // If the file name is not found, then let them to copy the file // and we will handle this later. val fileName = documentFile?.name - if (fileName != null && !FileUtils.isValidZimFile(fileName)) { + if (fileName != null && !isValidZimFile(fileName)) { activity.toast(string.error_file_invalid) return@launch } @@ -440,6 +441,9 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal } } + private fun isValidZimFile(fileName: String): Boolean = + FileUtils.isValidZimFile(fileName) || FileUtils.isSplittedZimFile(fileName) + private suspend fun getZimFileFromUri( uri: Uri ): File? { @@ -707,7 +711,26 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal private fun showWarningDialogForSplittedZimFile() { dialogShower.show( KiwixDialog.ShowWarningAboutSplittedZimFile, - {} + ::openCopiedMovedDirectory ) } + + private fun openCopiedMovedDirectory() { + val downloadedDirectoryPath = FileProvider.getUriForFile( + requireActivity(), + requireActivity().applicationContext.packageName + ".fileprovider", + File(sharedPreferenceUtil.prefStorage) + ) + val intent = Intent(Intent.ACTION_VIEW).apply { + setDataAndType(downloadedDirectoryPath, "*/*") + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + try { + requireActivity().startActivity(intent) + } catch (ignore: Exception) { + // TODO change the message + activity.toast(org.kiwix.kiwixmobile.core.R.string.no_reader_application_installed) + } + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index ac6c71798..5cde30ef2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -121,7 +121,7 @@ sealed class KiwixDialog( data object ShowWarningAboutSplittedZimFile : KiwixDialog( null, R.string.verify_zim_chunk_copied_moved_properly, - android.R.string.ok, + R.string.view, android.R.string.ok, cancelable = false ) diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 9eba4713c..b8c39f203 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -334,7 +334,8 @@ Moving ZIM file… Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there? Error in copying the ZIM file: %s. - Be cautious: verify that all the ZIM chunks have been moved/copied properly! + Be cautious: verify that all the ZIM chunks have been moved/copied properly! \nAfter copying/moving all the ZIM chunks please refresh the library screen by swipe down. The Split ZIM file will add in library. + View Why copy/move files to app public directory? 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. Choose storage to copy/move ZIM file From 10734ec75fe3e6b76221911bf7e21cf81e7506e6 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 9 Jan 2025 15:24:58 +0530 Subject: [PATCH 3/5] Improved the warning message we are showing to the user after copying/moving the ZIm chunks. --- .../java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt | 2 +- core/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index 5cde30ef2..46e42f59d 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -120,7 +120,7 @@ sealed class KiwixDialog( data object ShowWarningAboutSplittedZimFile : KiwixDialog( null, - R.string.verify_zim_chunk_copied_moved_properly, + R.string.ensure_zim_chunk_copied_moved_properly, R.string.view, android.R.string.ok, cancelable = false diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index b8c39f203..e87b28016 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -334,7 +334,7 @@ Moving ZIM file… Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there? Error in copying the ZIM file: %s. - Be cautious: verify that all the ZIM chunks have been moved/copied properly! \nAfter copying/moving all the ZIM chunks please refresh the library screen by swipe down. The Split ZIM file will add in library. + Be cautious: Ensure all ZIM chunks have been properly moved or copied. You can verify them by clicking the “View” button. Once done, refresh the library screen by swiping down. The split ZIM file will then appear in your library. View Why copy/move files to app public directory? 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. From c69efb152345a8c42b1c5db268f87704bb090241 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 9 Jan 2025 16:07:13 +0530 Subject: [PATCH 4/5] Improved the showing of the warning dialog so that all the instructions are clear to the user. --- .../org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt | 4 ++-- core/src/main/res/values/strings.xml | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index 46e42f59d..17b3c1eec 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -119,8 +119,8 @@ sealed class KiwixDialog( ) data object ShowWarningAboutSplittedZimFile : KiwixDialog( - null, - R.string.ensure_zim_chunk_copied_moved_properly, + R.string.verify_zim_chunk_dialog_title, + R.string.verify_zim_chunks_dialog_message, R.string.view, android.R.string.ok, cancelable = false diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index e87b28016..44de264ae 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -334,7 +334,8 @@ Moving ZIM file… Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there? Error in copying the ZIM file: %s. - Be cautious: Ensure all ZIM chunks have been properly moved or copied. You can verify them by clicking the “View” button. Once done, refresh the library screen by swiping down. The split ZIM file will then appear in your library. + Be cautious: Ensure all ZIM chunks have been properly moved/copied! + You can verify them by clicking the “View” button. Once done, refresh the library screen by swiping down. The split ZIM file will then appear in your library. View Why copy/move files to app public directory? 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. From 4fe022c6a20dc3835d2696b59bfce4eb10fbd5f1 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 9 Jan 2025 18:54:28 +0530 Subject: [PATCH 5/5] Improved the message of Warning dialog to show user where they can verify the ZIM chunks are copied/moved properly. --- .../library/LocalLibraryFragment.kt | 25 +------------------ .../core/utils/dialog/KiwixDialog.kt | 2 +- core/src/main/res/values/strings.xml | 3 +-- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt index ea200e5e8..51d8875dc 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt @@ -44,7 +44,6 @@ import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat -import androidx.core.content.FileProvider import androidx.core.net.toUri import androidx.core.view.MenuHost import androidx.core.view.MenuProvider @@ -709,28 +708,6 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal } private fun showWarningDialogForSplittedZimFile() { - dialogShower.show( - KiwixDialog.ShowWarningAboutSplittedZimFile, - ::openCopiedMovedDirectory - ) - } - - private fun openCopiedMovedDirectory() { - val downloadedDirectoryPath = FileProvider.getUriForFile( - requireActivity(), - requireActivity().applicationContext.packageName + ".fileprovider", - File(sharedPreferenceUtil.prefStorage) - ) - val intent = Intent(Intent.ACTION_VIEW).apply { - setDataAndType(downloadedDirectoryPath, "*/*") - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - } - try { - requireActivity().startActivity(intent) - } catch (ignore: Exception) { - // TODO change the message - activity.toast(org.kiwix.kiwixmobile.core.R.string.no_reader_application_installed) - } + dialogShower.show(KiwixDialog.ShowWarningAboutSplittedZimFile) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index 17b3c1eec..39201be07 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -121,8 +121,8 @@ sealed class KiwixDialog( data object ShowWarningAboutSplittedZimFile : KiwixDialog( R.string.verify_zim_chunk_dialog_title, R.string.verify_zim_chunks_dialog_message, - R.string.view, android.R.string.ok, + null, cancelable = false ) diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 44de264ae..995cc2e12 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -335,8 +335,7 @@ Kiwix requires the ZIM file to be in its own data directory. Do you want to copy or move it there? Error in copying the ZIM file: %s. Be cautious: Ensure all ZIM chunks have been properly moved/copied! - You can verify them by clicking the “View” button. Once done, refresh the library screen by swiping down. The split ZIM file will then appear in your library. - View + 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. Why copy/move files to app public directory? 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. Choose storage to copy/move ZIM file