From 13fb613ebf2ab48e8b3d15db225ccd57adae5d4d Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 6 Jun 2024 14:09:31 +0530 Subject: [PATCH] Improved the naming of unsupported mimeType handler class. * Improved the dialog's message and title. --- ...t.kt => UnsupportedMimeTypeHandlerTest.kt} | 48 +++++++++---------- .../core/main/CoreReaderFragment.kt | 8 ++-- .../core/main/CoreWebViewClient.kt | 8 ++-- .../kiwixmobile/core/main/WebViewCallback.kt | 2 +- .../core/utils/dialog/KiwixDialog.kt | 8 ++-- ...ndler.kt => UnsupportedMimeTypeHandler.kt} | 12 ++--- core/src/main/res/values/strings.xml | 4 +- 7 files changed, 45 insertions(+), 45 deletions(-) rename app/src/androidTest/java/org/kiwix/kiwixmobile/utils/{DownloadOrOpenEpubAndPdfHandlerTest.kt => UnsupportedMimeTypeHandlerTest.kt} (74%) rename core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/{DownloadOrOpenEpubAndPdfHandler.kt => UnsupportedMimeTypeHandler.kt} (87%) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/utils/DownloadOrOpenEpubAndPdfHandlerTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/utils/UnsupportedMimeTypeHandlerTest.kt similarity index 74% rename from app/src/androidTest/java/org/kiwix/kiwixmobile/utils/DownloadOrOpenEpubAndPdfHandlerTest.kt rename to app/src/androidTest/java/org/kiwix/kiwixmobile/utils/UnsupportedMimeTypeHandlerTest.kt index e3aa74970..939481c38 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/utils/DownloadOrOpenEpubAndPdfHandlerTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/utils/UnsupportedMimeTypeHandlerTest.kt @@ -34,12 +34,12 @@ import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower -import org.kiwix.kiwixmobile.core.utils.dialog.DownloadOrOpenEpubAndPdfHandler +import org.kiwix.kiwixmobile.core.utils.dialog.UnsupportedMimeTypeHandler import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import java.io.File import java.io.InputStream -class DownloadOrOpenEpubAndPdfHandlerTest { +class UnsupportedMimeTypeHandlerTest { private val demoUrl = "content://demoPdf.pdf" private val demoFileName = "demoPdf.pdf" private val sharedPreferenceUtil: SharedPreferenceUtil = mockk() @@ -49,7 +49,7 @@ class DownloadOrOpenEpubAndPdfHandlerTest { private val activity: Activity = mockk() private val webResourceResponse: WebResourceResponse = mockk() private val inputStream: InputStream = mockk() - private val downloadOrOpenEpubAndPdfHandler = DownloadOrOpenEpubAndPdfHandler( + private val unsupportedMimeTypeHandler = UnsupportedMimeTypeHandler( activity, sharedPreferenceUtil, alertDialogShower, @@ -78,23 +78,23 @@ class DownloadOrOpenEpubAndPdfHandlerTest { } returns "Saved media as $demoFileName to Downloads/org.kiwix…/" every { savedFile.path } returns "Emulated/0/Downloads/$demoFileName" every { savedFile.exists() } returns true - downloadOrOpenEpubAndPdfHandler.intent = mockk() - every { downloadOrOpenEpubAndPdfHandler.intent.setDataAndType(any(), any()) } returns mockk() - every { downloadOrOpenEpubAndPdfHandler.intent.setFlags(any()) } returns mockk() - every { downloadOrOpenEpubAndPdfHandler.intent.addFlags(any()) } returns mockk() + unsupportedMimeTypeHandler.intent = mockk() + every { unsupportedMimeTypeHandler.intent.setDataAndType(any(), any()) } returns mockk() + every { unsupportedMimeTypeHandler.intent.setFlags(any()) } returns mockk() + every { unsupportedMimeTypeHandler.intent.addFlags(any()) } returns mockk() } @Test fun testOpeningFileInExternalReaderApplication() { every { - downloadOrOpenEpubAndPdfHandler.intent.resolveActivity(activity.packageManager) + unsupportedMimeTypeHandler.intent.resolveActivity(activity.packageManager) } returns mockk() - every { activity.startActivity(downloadOrOpenEpubAndPdfHandler.intent) } returns mockk() + every { activity.startActivity(unsupportedMimeTypeHandler.intent) } returns mockk() val lambdaSlot = slot<() -> Unit>() - downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") + unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf") verify { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, + KiwixDialog.SaveOrOpenUnsupportedFiles, capture(lambdaSlot), any(), any() @@ -102,24 +102,24 @@ class DownloadOrOpenEpubAndPdfHandlerTest { } lambdaSlot.captured.invoke() verify { - activity.startActivity(downloadOrOpenEpubAndPdfHandler.intent) + activity.startActivity(unsupportedMimeTypeHandler.intent) } } @Test fun testOpeningFileWhenNoReaderApplicationInstalled() { every { - downloadOrOpenEpubAndPdfHandler.intent.resolveActivity(activity.packageManager) + unsupportedMimeTypeHandler.intent.resolveActivity(activity.packageManager) } returns null mockkStatic(Toast::class) justRun { Toast.makeText(activity, R.string.no_reader_application_installed, Toast.LENGTH_LONG).show() } val lambdaSlot = slot<() -> Unit>() - downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") + unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf") verify { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, + KiwixDialog.SaveOrOpenUnsupportedFiles, capture(lambdaSlot), any(), any() @@ -130,7 +130,7 @@ class DownloadOrOpenEpubAndPdfHandlerTest { } @Test - fun testFileDownloadingSuccessfull() { + fun testFileSavedSuccessfully() { val toastMessage = activity.getString(R.string.save_media_saved, savedFile.name) mockkStatic(Toast::class) justRun { @@ -141,13 +141,13 @@ class DownloadOrOpenEpubAndPdfHandlerTest { ).show() } val lambdaSlot = slot<() -> Unit>() - downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog( + unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog( demoUrl, "application/pdf" ) verify { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, + KiwixDialog.SaveOrOpenUnsupportedFiles, any(), capture(lambdaSlot), any() @@ -160,10 +160,10 @@ class DownloadOrOpenEpubAndPdfHandlerTest { @Test fun testUserClicksOnNoThanksButton() { val lambdaSlot = slot<() -> Unit>() - downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") + unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf") verify { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, + KiwixDialog.SaveOrOpenUnsupportedFiles, any(), any(), capture(lambdaSlot) @@ -174,8 +174,8 @@ class DownloadOrOpenEpubAndPdfHandlerTest { } @Test - fun testIfDownloadFailed() { - val downloadOrOpenEpubAndPdfHandler = DownloadOrOpenEpubAndPdfHandler( + fun testIfSavingFailed() { + val downloadOrOpenEpubAndPdfHandler = UnsupportedMimeTypeHandler( activity, sharedPreferenceUtil, alertDialogShower, @@ -186,10 +186,10 @@ class DownloadOrOpenEpubAndPdfHandlerTest { Toast.makeText(activity, R.string.save_media_error, Toast.LENGTH_LONG).show() } val lambdaSlot = slot<() -> Unit>() - downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(null, "application/pdf") + downloadOrOpenEpubAndPdfHandler.showSaveOrOpenUnsupportedFilesDialog(null, "application/pdf") verify { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, + KiwixDialog.SaveOrOpenUnsupportedFiles, any(), capture(lambdaSlot), any() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt index 9a270156b..11249f14a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt @@ -156,7 +156,7 @@ import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED_NEW_TAB import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX import org.kiwix.kiwixmobile.core.utils.UpdateUtils.reformatProviderUrl import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower -import org.kiwix.kiwixmobile.core.utils.dialog.DownloadOrOpenEpubAndPdfHandler +import org.kiwix.kiwixmobile.core.utils.dialog.UnsupportedMimeTypeHandler import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile @@ -325,7 +325,7 @@ abstract class CoreReaderFragment : @JvmField @Inject - var downloadOrOpenEpubAndPdfHandler: DownloadOrOpenEpubAndPdfHandler? = null + var unsupportedMimeTypeHandler: UnsupportedMimeTypeHandler? = null private var hideBackToTopTimer: CountDownTimer? = null private var documentSections: MutableList? = null private var isBackToTopEnabled = false @@ -1547,8 +1547,8 @@ abstract class CoreReaderFragment : externalLinkOpener?.openExternalUrl(intent) } - override fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) { - downloadOrOpenEpubAndPdfHandler?.showDownloadOrOpenEpubAndPdfDialog(url, documentType) + override fun showSaveOrOpenUnsupportedFilesDialog(url: String, documentType: String?) { + unsupportedMimeTypeHandler?.showSaveOrOpenUnsupportedFilesDialog(url, documentType) } protected fun openZimFile( diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt index b49625355..25b8ed045 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt @@ -44,14 +44,14 @@ open class CoreWebViewClient( url = convertLegacyUrl(url) urlWithAnchor = if (url.contains("#")) url else null if (zimReaderContainer.isRedirect(url)) { - if (handleEpubAndPdf(url)) { + if (handleUnsupportedFiles(url)) { return true } view.loadUrl(zimReaderContainer.getRedirect(url)) return true } if (url.startsWith(ZimFileReader.CONTENT_PREFIX)) { - return handleEpubAndPdf(url) + return handleUnsupportedFiles(url) } if (url.startsWith("javascript:")) { // Allow javascript for HTML functions and code execution (EX: night mode) @@ -77,10 +77,10 @@ open class CoreWebViewClient( } @Suppress("NestedBlockDepth") - private fun handleEpubAndPdf(url: String): Boolean { + private fun handleUnsupportedFiles(url: String): Boolean { val extension = MimeTypeMap.getFileExtensionFromUrl(url) if (DOCUMENT_TYPES.containsKey(extension)) { - callback.showDownloadOrOpenEpubAndPdfDialog(url, DOCUMENT_TYPES[extension]) + callback.showSaveOrOpenUnsupportedFilesDialog(url, DOCUMENT_TYPES[extension]) return true } return false diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/WebViewCallback.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/WebViewCallback.kt index 6b5ef57df..25c890250 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/WebViewCallback.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/WebViewCallback.kt @@ -25,7 +25,7 @@ interface WebViewCallback { fun webViewUrlFinishedLoading() fun webViewFailedLoading(failingUrl: String) fun openExternalUrl(intent: Intent) - fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) + fun showSaveOrOpenUnsupportedFilesDialog(url: String, documentType: String?) fun webViewProgressChanged(progress: Int, webView: WebView) fun webViewTitleUpdated(title: String) fun webViewPageChanged(page: Int, maxPages: Int) 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 e00015192..3264eec6d 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 @@ -108,11 +108,11 @@ sealed class KiwixDialog( cancelable = false ) - object DownloadOrOpenEpubAndPdf : KiwixDialog( - R.string.download_or_open_pdf_and_epub_content_dialog_title, - R.string.download_or_open_pdf_and_epub_content_dialog_message, + object SaveOrOpenUnsupportedFiles : KiwixDialog( + R.string.save_or_open_unsupported_files_dialog_title, + R.string.save_or_open_unsupported_files_dialog_message, R.string.open, - R.string.download, + R.string.save, neutralMessage = R.string.no_thanks ) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/UnsupportedMimeTypeHandler.kt similarity index 87% rename from core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/UnsupportedMimeTypeHandler.kt index 80c9821db..830a94f58 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/UnsupportedMimeTypeHandler.kt @@ -30,7 +30,7 @@ import org.kiwix.kiwixmobile.core.utils.files.FileUtils.downloadFileFromUrl import java.io.File import javax.inject.Inject -class DownloadOrOpenEpubAndPdfHandler @Inject constructor( +class UnsupportedMimeTypeHandler @Inject constructor( private val activity: Activity, private val sharedPreferenceUtil: SharedPreferenceUtil, private val alertDialogShower: AlertDialogShower, @@ -38,16 +38,16 @@ class DownloadOrOpenEpubAndPdfHandler @Inject constructor( ) { var intent: Intent = Intent(Intent.ACTION_VIEW) - fun showDownloadOrOpenEpubAndPdfDialog(url: String?, documentType: String?) { + fun showSaveOrOpenUnsupportedFilesDialog(url: String?, documentType: String?) { alertDialogShower.show( - KiwixDialog.DownloadOrOpenEpubAndPdf, - { openOrDownloadFile(url, documentType, true) }, - { openOrDownloadFile(url, documentType, false) }, + KiwixDialog.SaveOrOpenUnsupportedFiles, + { openOrSaveFile(url, documentType, true) }, + { openOrSaveFile(url, documentType, false) }, { } ) } - private fun openOrDownloadFile(url: String?, documentType: String?, openFile: Boolean) { + private fun openOrSaveFile(url: String?, documentType: String?, openFile: Boolean) { downloadFileFromUrl( url, null, diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 2df474630..94034e08d 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -373,6 +373,6 @@ ZIM file which has the reading content Selecting this language will prioritize displaying downloadable books in that language at the top. Go to previous screen - Download or Open this (Epub/Pdf) file? - Choosing Open will open this file in your external Epub/PDF reader application. + Save or Open this file? + Choosing Open will open this file in external reader application.