mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 10:46:53 -04:00
Improved the naming of unsupported mimeType handler class.
* Improved the dialog's message and title.
This commit is contained in:
parent
c928943851
commit
13fb613ebf
@ -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()
|
@ -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<DocumentSection>? = 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(
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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,
|
@ -373,6 +373,6 @@
|
||||
<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="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="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_title">Save or Open this file?</string>
|
||||
<string name="save_or_open_unsupported_files_dialog_message">Choosing Open will open this file in external reader application.</string>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user