Skip to content

Commit

Permalink
Introduced a Open/Download dialog for Epub/Pdf files.
Browse files Browse the repository at this point in the history
* This was requested in #3453 and this PR is related to the Epub opening issue so we are introducing this here.
  • Loading branch information
MohitMaliDeveloper committed Mar 8, 2024
1 parent e3c122b commit 246b371
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
override fun createWebView(attrs: AttributeSet?): ToolbarScrollingKiwixWebView {
return ToolbarScrollingKiwixWebView(
requireContext(), this, attrs!!, activityMainRoot as ViewGroup, videoView!!,
CoreWebViewClient(this, zimReaderContainer!!, sharedPreferenceUtil!!),
CoreWebViewClient(this, zimReaderContainer!!),
toolbarContainer!!, bottomToolbar!!, sharedPreferenceUtil = sharedPreferenceUtil!!,
parentNavigationBar = requireActivity().findViewById(R.id.bottom_nav_view)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +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.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile
Expand Down Expand Up @@ -317,6 +318,10 @@ abstract class CoreReaderFragment :
@JvmField
@Inject
var externalLinkOpener: ExternalLinkOpener? = null

@JvmField
@Inject
var downloadOrOpenEpubAndPdfHandler: DownloadOrOpenEpubAndPdfHandler? = null
private var hideBackToTopTimer: CountDownTimer? = null
private var documentSections: MutableList<DocumentSection>? = null
private var isBackToTopEnabled = false
Expand Down Expand Up @@ -1163,7 +1168,7 @@ abstract class CoreReaderFragment :
return if (activityMainRoot != null) {
ToolbarScrollingKiwixWebView(
requireActivity(), this, attrs!!, (activityMainRoot as ViewGroup?)!!, videoView!!,
CoreWebViewClient(this, zimReaderContainer!!, sharedPreferenceUtil!!),
CoreWebViewClient(this, zimReaderContainer!!),

Check warning on line 1171 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt#L1171

Added line #L1171 was not covered by tests
toolbarContainer!!, bottomToolbar!!,
sharedPreferenceUtil!!
)
Expand Down Expand Up @@ -1470,6 +1475,10 @@ abstract class CoreReaderFragment :
externalLinkOpener?.openExternalUrl(intent)
}

override fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) {
downloadOrOpenEpubAndPdfHandler?.showDownloadOrOpenEpubAndPdfDialog(url, documentType)
}

Check warning on line 1480 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt#L1480

Added line #L1480 was not covered by tests

protected fun openZimFile(
file: File?,
isCustomApp: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.kiwix.kiwixmobile.core.main

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
Expand All @@ -27,18 +26,14 @@ import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.content.FileProvider
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.downloadFileFromUrl

open class CoreWebViewClient(
protected val callback: WebViewCallback,
protected val zimReaderContainer: ZimReaderContainer,
private val sharedPreferenceUtil: SharedPreferenceUtil
protected val zimReaderContainer: ZimReaderContainer
) : WebViewClient() {
private var urlWithAnchor: String? = null

Expand Down Expand Up @@ -85,26 +80,7 @@ open class CoreWebViewClient(
private fun handleEpubAndPdf(url: String): Boolean {
val extension = MimeTypeMap.getFileExtensionFromUrl(url)
if (DOCUMENT_TYPES.containsKey(extension)) {
downloadFileFromUrl(
url,
null,
zimReaderContainer,
sharedPreferenceUtil
)?.let {
if (it.exists()) {
val context: Context = instance
val uri = FileProvider.getUriForFile(
context,
context.packageName + ".fileprovider", it
)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, DOCUMENT_TYPES[extension])
flags = Intent.FLAG_ACTIVITY_NO_HISTORY
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
callback.openExternalUrl(intent)
}
}
callback.showDownloadOrOpenEpubAndPdfDialog(url, DOCUMENT_TYPES[extension])

Check warning on line 83 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreWebViewClient.kt#L83

Added line #L83 was not covered by tests
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface WebViewCallback {
fun webViewUrlFinishedLoading()
fun webViewFailedLoading(failingUrl: String)
fun openExternalUrl(intent: Intent)
fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?)
fun webViewProgressChanged(progress: Int, webView: WebView)
fun webViewTitleUpdated(title: String)
fun webViewPageChanged(page: Int, maxPages: Int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.kiwix.kiwixmobile.core.utils.dialog

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.core.content.FileProvider
import org.kiwix.kiwixmobile.core.R
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.files.FileUtils.downloadFileFromUrl
import java.io.File
import javax.inject.Inject

class DownloadOrOpenEpubAndPdfHandler @Inject constructor(
private val activity: Activity,
private val sharedPreferenceUtil: SharedPreferenceUtil,
private val alertDialogShower: AlertDialogShower,
private val zimReaderContainer: ZimReaderContainer
) {

fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) {
alertDialogShower.show(
KiwixDialog.DownloadOrOpenEpubAndPdf,
{ openOrDownloadFile(url, documentType, true) },
{ openOrDownloadFile(url, documentType, false) },
{ }

Check warning on line 45 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L41-L45

Added lines #L41 - L45 were not covered by tests
)
}

Check warning on line 47 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L47

Added line #L47 was not covered by tests

private fun openOrDownloadFile(url: String, documentType: String?, openFile: Boolean) {
downloadFileFromUrl(
url,
null,
zimReaderContainer,
sharedPreferenceUtil

Check warning on line 54 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L51-L54

Added lines #L51 - L54 were not covered by tests
)?.let { savedFile ->
if (openFile) {
openFile(savedFile, documentType)

Check warning on line 57 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L57

Added line #L57 was not covered by tests
} else {
activity.toast(activity.getString(R.string.save_media_saved, savedFile.name)).also {
Log.e("DownloadOrOpenEpubAndPdf", "File downloaded at = ${savedFile.path}")
}

Check warning on line 61 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L59-L61

Added lines #L59 - L61 were not covered by tests
}
} ?: run {
activity.toast(R.string.save_media_error)
}
}

Check warning on line 66 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L63-L66

Added lines #L63 - L66 were not covered by tests

private fun openFile(savedFile: File, documentType: String?) {
if (savedFile.exists()) {
val uri = FileProvider.getUriForFile(
activity,
"${activity.packageName}.fileprovider",
savedFile

Check warning on line 73 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L70-L73

Added lines #L70 - L73 were not covered by tests
)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, documentType)
flags = Intent.FLAG_ACTIVITY_NO_HISTORY
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

Check warning on line 79 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L75-L79

Added lines #L75 - L79 were not covered by tests
if (intent.resolveActivity(activity.packageManager) != null) {
activity.startActivity(intent)

Check warning on line 81 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L81

Added line #L81 was not covered by tests
} else {
activity.toast(R.string.no_reader_application_installed)

Check warning on line 83 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L83

Added line #L83 was not covered by tests
}
}
}

Check warning on line 86 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L86

Added line #L86 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ 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,
R.string.open,
R.string.download,
neutralMessage = R.string.no_thanks

Check warning on line 116 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt#L111-L116

Added lines #L111 - L116 were not covered by tests
)

data class ShowHotspotDetails(override val args: List<Any>) :
KiwixDialog(
R.string.hotspot_turned_on,
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,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>
</resources>

0 comments on commit 246b371

Please sign in to comment.