Improved the naming of unsupported mimeType handler class.

* Improved the dialog's message and title.
This commit is contained in:
MohitMaliFtechiz 2024-06-06 14:09:31 +05:30 committed by Kelson
parent c928943851
commit 13fb613ebf
7 changed files with 45 additions and 45 deletions

View File

@ -34,12 +34,12 @@ import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
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.DownloadOrOpenEpubAndPdfHandler import org.kiwix.kiwixmobile.core.utils.dialog.UnsupportedMimeTypeHandler
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
class DownloadOrOpenEpubAndPdfHandlerTest { class UnsupportedMimeTypeHandlerTest {
private val demoUrl = "content://demoPdf.pdf" private val demoUrl = "content://demoPdf.pdf"
private val demoFileName = "demoPdf.pdf" private val demoFileName = "demoPdf.pdf"
private val sharedPreferenceUtil: SharedPreferenceUtil = mockk() private val sharedPreferenceUtil: SharedPreferenceUtil = mockk()
@ -49,7 +49,7 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
private val activity: Activity = mockk() private val activity: Activity = mockk()
private val webResourceResponse: WebResourceResponse = mockk() private val webResourceResponse: WebResourceResponse = mockk()
private val inputStream: InputStream = mockk() private val inputStream: InputStream = mockk()
private val downloadOrOpenEpubAndPdfHandler = DownloadOrOpenEpubAndPdfHandler( private val unsupportedMimeTypeHandler = UnsupportedMimeTypeHandler(
activity, activity,
sharedPreferenceUtil, sharedPreferenceUtil,
alertDialogShower, alertDialogShower,
@ -78,23 +78,23 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
} returns "Saved media as $demoFileName to Downloads/org.kiwix…/" } returns "Saved media as $demoFileName to Downloads/org.kiwix…/"
every { savedFile.path } returns "Emulated/0/Downloads/$demoFileName" every { savedFile.path } returns "Emulated/0/Downloads/$demoFileName"
every { savedFile.exists() } returns true every { savedFile.exists() } returns true
downloadOrOpenEpubAndPdfHandler.intent = mockk() unsupportedMimeTypeHandler.intent = mockk()
every { downloadOrOpenEpubAndPdfHandler.intent.setDataAndType(any(), any()) } returns mockk() every { unsupportedMimeTypeHandler.intent.setDataAndType(any(), any()) } returns mockk()
every { downloadOrOpenEpubAndPdfHandler.intent.setFlags(any()) } returns mockk() every { unsupportedMimeTypeHandler.intent.setFlags(any()) } returns mockk()
every { downloadOrOpenEpubAndPdfHandler.intent.addFlags(any()) } returns mockk() every { unsupportedMimeTypeHandler.intent.addFlags(any()) } returns mockk()
} }
@Test @Test
fun testOpeningFileInExternalReaderApplication() { fun testOpeningFileInExternalReaderApplication() {
every { every {
downloadOrOpenEpubAndPdfHandler.intent.resolveActivity(activity.packageManager) unsupportedMimeTypeHandler.intent.resolveActivity(activity.packageManager)
} returns mockk() } returns mockk()
every { activity.startActivity(downloadOrOpenEpubAndPdfHandler.intent) } returns mockk() every { activity.startActivity(unsupportedMimeTypeHandler.intent) } returns mockk()
val lambdaSlot = slot<() -> Unit>() val lambdaSlot = slot<() -> Unit>()
downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf")
verify { verify {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
capture(lambdaSlot), capture(lambdaSlot),
any(), any(),
any() any()
@ -102,24 +102,24 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
} }
lambdaSlot.captured.invoke() lambdaSlot.captured.invoke()
verify { verify {
activity.startActivity(downloadOrOpenEpubAndPdfHandler.intent) activity.startActivity(unsupportedMimeTypeHandler.intent)
} }
} }
@Test @Test
fun testOpeningFileWhenNoReaderApplicationInstalled() { fun testOpeningFileWhenNoReaderApplicationInstalled() {
every { every {
downloadOrOpenEpubAndPdfHandler.intent.resolveActivity(activity.packageManager) unsupportedMimeTypeHandler.intent.resolveActivity(activity.packageManager)
} returns null } returns null
mockkStatic(Toast::class) mockkStatic(Toast::class)
justRun { justRun {
Toast.makeText(activity, R.string.no_reader_application_installed, Toast.LENGTH_LONG).show() Toast.makeText(activity, R.string.no_reader_application_installed, Toast.LENGTH_LONG).show()
} }
val lambdaSlot = slot<() -> Unit>() val lambdaSlot = slot<() -> Unit>()
downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf")
verify { verify {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
capture(lambdaSlot), capture(lambdaSlot),
any(), any(),
any() any()
@ -130,7 +130,7 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
} }
@Test @Test
fun testFileDownloadingSuccessfull() { fun testFileSavedSuccessfully() {
val toastMessage = activity.getString(R.string.save_media_saved, savedFile.name) val toastMessage = activity.getString(R.string.save_media_saved, savedFile.name)
mockkStatic(Toast::class) mockkStatic(Toast::class)
justRun { justRun {
@ -141,13 +141,13 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
).show() ).show()
} }
val lambdaSlot = slot<() -> Unit>() val lambdaSlot = slot<() -> Unit>()
downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog( unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(
demoUrl, demoUrl,
"application/pdf" "application/pdf"
) )
verify { verify {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
any(), any(),
capture(lambdaSlot), capture(lambdaSlot),
any() any()
@ -160,10 +160,10 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
@Test @Test
fun testUserClicksOnNoThanksButton() { fun testUserClicksOnNoThanksButton() {
val lambdaSlot = slot<() -> Unit>() val lambdaSlot = slot<() -> Unit>()
downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(demoUrl, "application/pdf") unsupportedMimeTypeHandler.showSaveOrOpenUnsupportedFilesDialog(demoUrl, "application/pdf")
verify { verify {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
any(), any(),
any(), any(),
capture(lambdaSlot) capture(lambdaSlot)
@ -174,8 +174,8 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
} }
@Test @Test
fun testIfDownloadFailed() { fun testIfSavingFailed() {
val downloadOrOpenEpubAndPdfHandler = DownloadOrOpenEpubAndPdfHandler( val downloadOrOpenEpubAndPdfHandler = UnsupportedMimeTypeHandler(
activity, activity,
sharedPreferenceUtil, sharedPreferenceUtil,
alertDialogShower, alertDialogShower,
@ -186,10 +186,10 @@ class DownloadOrOpenEpubAndPdfHandlerTest {
Toast.makeText(activity, R.string.save_media_error, Toast.LENGTH_LONG).show() Toast.makeText(activity, R.string.save_media_error, Toast.LENGTH_LONG).show()
} }
val lambdaSlot = slot<() -> Unit>() val lambdaSlot = slot<() -> Unit>()
downloadOrOpenEpubAndPdfHandler.showDownloadOrOpenEpubAndPdfDialog(null, "application/pdf") downloadOrOpenEpubAndPdfHandler.showSaveOrOpenUnsupportedFilesDialog(null, "application/pdf")
verify { verify {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
any(), any(),
capture(lambdaSlot), capture(lambdaSlot),
any() any()

View File

@ -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.TAG_KIWIX
import org.kiwix.kiwixmobile.core.utils.UpdateUtils.reformatProviderUrl import org.kiwix.kiwixmobile.core.utils.UpdateUtils.reformatProviderUrl
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower 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.dialog.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile
@ -325,7 +325,7 @@ abstract class CoreReaderFragment :
@JvmField @JvmField
@Inject @Inject
var downloadOrOpenEpubAndPdfHandler: DownloadOrOpenEpubAndPdfHandler? = null var unsupportedMimeTypeHandler: UnsupportedMimeTypeHandler? = null
private var hideBackToTopTimer: CountDownTimer? = null private var hideBackToTopTimer: CountDownTimer? = null
private var documentSections: MutableList<DocumentSection>? = null private var documentSections: MutableList<DocumentSection>? = null
private var isBackToTopEnabled = false private var isBackToTopEnabled = false
@ -1547,8 +1547,8 @@ abstract class CoreReaderFragment :
externalLinkOpener?.openExternalUrl(intent) externalLinkOpener?.openExternalUrl(intent)
} }
override fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) { override fun showSaveOrOpenUnsupportedFilesDialog(url: String, documentType: String?) {
downloadOrOpenEpubAndPdfHandler?.showDownloadOrOpenEpubAndPdfDialog(url, documentType) unsupportedMimeTypeHandler?.showSaveOrOpenUnsupportedFilesDialog(url, documentType)
} }
protected fun openZimFile( protected fun openZimFile(

View File

@ -44,14 +44,14 @@ open class CoreWebViewClient(
url = convertLegacyUrl(url) url = convertLegacyUrl(url)
urlWithAnchor = if (url.contains("#")) url else null urlWithAnchor = if (url.contains("#")) url else null
if (zimReaderContainer.isRedirect(url)) { if (zimReaderContainer.isRedirect(url)) {
if (handleEpubAndPdf(url)) { if (handleUnsupportedFiles(url)) {
return true return true
} }
view.loadUrl(zimReaderContainer.getRedirect(url)) view.loadUrl(zimReaderContainer.getRedirect(url))
return true return true
} }
if (url.startsWith(ZimFileReader.CONTENT_PREFIX)) { if (url.startsWith(ZimFileReader.CONTENT_PREFIX)) {
return handleEpubAndPdf(url) return handleUnsupportedFiles(url)
} }
if (url.startsWith("javascript:")) { if (url.startsWith("javascript:")) {
// Allow javascript for HTML functions and code execution (EX: night mode) // Allow javascript for HTML functions and code execution (EX: night mode)
@ -77,10 +77,10 @@ open class CoreWebViewClient(
} }
@Suppress("NestedBlockDepth") @Suppress("NestedBlockDepth")
private fun handleEpubAndPdf(url: String): Boolean { private fun handleUnsupportedFiles(url: String): Boolean {
val extension = MimeTypeMap.getFileExtensionFromUrl(url) val extension = MimeTypeMap.getFileExtensionFromUrl(url)
if (DOCUMENT_TYPES.containsKey(extension)) { if (DOCUMENT_TYPES.containsKey(extension)) {
callback.showDownloadOrOpenEpubAndPdfDialog(url, DOCUMENT_TYPES[extension]) callback.showSaveOrOpenUnsupportedFilesDialog(url, DOCUMENT_TYPES[extension])
return true return true
} }
return false return false

View File

@ -25,7 +25,7 @@ interface WebViewCallback {
fun webViewUrlFinishedLoading() fun webViewUrlFinishedLoading()
fun webViewFailedLoading(failingUrl: String) fun webViewFailedLoading(failingUrl: String)
fun openExternalUrl(intent: Intent) fun openExternalUrl(intent: Intent)
fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) fun showSaveOrOpenUnsupportedFilesDialog(url: String, documentType: String?)
fun webViewProgressChanged(progress: Int, webView: WebView) fun webViewProgressChanged(progress: Int, webView: WebView)
fun webViewTitleUpdated(title: String) fun webViewTitleUpdated(title: String)
fun webViewPageChanged(page: Int, maxPages: Int) fun webViewPageChanged(page: Int, maxPages: Int)

View File

@ -108,11 +108,11 @@ sealed class KiwixDialog(
cancelable = false cancelable = false
) )
object DownloadOrOpenEpubAndPdf : KiwixDialog( object SaveOrOpenUnsupportedFiles : KiwixDialog(
R.string.download_or_open_pdf_and_epub_content_dialog_title, R.string.save_or_open_unsupported_files_dialog_title,
R.string.download_or_open_pdf_and_epub_content_dialog_message, R.string.save_or_open_unsupported_files_dialog_message,
R.string.open, R.string.open,
R.string.download, R.string.save,
neutralMessage = R.string.no_thanks neutralMessage = R.string.no_thanks
) )

View File

@ -30,7 +30,7 @@ import org.kiwix.kiwixmobile.core.utils.files.FileUtils.downloadFileFromUrl
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
class DownloadOrOpenEpubAndPdfHandler @Inject constructor( class UnsupportedMimeTypeHandler @Inject constructor(
private val activity: Activity, private val activity: Activity,
private val sharedPreferenceUtil: SharedPreferenceUtil, private val sharedPreferenceUtil: SharedPreferenceUtil,
private val alertDialogShower: AlertDialogShower, private val alertDialogShower: AlertDialogShower,
@ -38,16 +38,16 @@ class DownloadOrOpenEpubAndPdfHandler @Inject constructor(
) { ) {
var intent: Intent = Intent(Intent.ACTION_VIEW) var intent: Intent = Intent(Intent.ACTION_VIEW)
fun showDownloadOrOpenEpubAndPdfDialog(url: String?, documentType: String?) { fun showSaveOrOpenUnsupportedFilesDialog(url: String?, documentType: String?) {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf, KiwixDialog.SaveOrOpenUnsupportedFiles,
{ openOrDownloadFile(url, documentType, true) }, { openOrSaveFile(url, documentType, true) },
{ openOrDownloadFile(url, documentType, false) }, { openOrSaveFile(url, documentType, false) },
{ } { }
) )
} }
private fun openOrDownloadFile(url: String?, documentType: String?, openFile: Boolean) { private fun openOrSaveFile(url: String?, documentType: String?, openFile: Boolean) {
downloadFileFromUrl( downloadFileFromUrl(
url, url,
null, null,

View File

@ -373,6 +373,6 @@
<string name="zim_file_content_description">ZIM file which has the reading content</string> <string name="zim_file_content_description">ZIM file which has the reading content</string>
<string name="select_language_content_description">Selecting this language will prioritize displaying downloadable books in that language at the top.</string> <string name="select_language_content_description">Selecting this language will prioritize displaying downloadable books in that language at the top.</string>
<string name="toolbar_back_button_content_description">Go to previous screen</string> <string name="toolbar_back_button_content_description">Go to previous screen</string>
<string name="download_or_open_pdf_and_epub_content_dialog_title">Download or Open this (Epub/Pdf) file?</string> <string name="save_or_open_unsupported_files_dialog_title">Save or Open this file?</string>
<string name="download_or_open_pdf_and_epub_content_dialog_message">Choosing Open will open this file in your external Epub/PDF reader application.</string> <string name="save_or_open_unsupported_files_dialog_message">Choosing Open will open this file in external reader application.</string>
</resources> </resources>