Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package `in`.testpress.course.fragments

import `in`.testpress.course.R
import `in`.testpress.course.util.CachedPdfPathProvider
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -18,6 +19,7 @@ class AIChatPdfFragment : Fragment() {
companion object {
private const val ARG_CONTENT_ID = "contentId"
private const val ARG_COURSE_ID = "courseId"
private const val ARG_PDF_PATH = "pdfPath"
}

override fun onCreateView(
Expand Down Expand Up @@ -47,6 +49,12 @@ class AIChatPdfFragment : Fragment() {
putBoolean(IS_AUTHENTICATION_REQUIRED, true)
}

webViewFragment.setListener(object : WebViewFragment.Listener {
override fun onWebViewInitializationSuccess() {
setupJavaScriptInterface(webViewFragment)
}
})

childFragmentManager.beginTransaction()
.replace(R.id.aiPdf_view_fragment, webViewFragment)
.commit()
Expand All @@ -59,4 +67,17 @@ class AIChatPdfFragment : Fragment() {
?: throw IllegalStateException("Base URL not configured.")
return "$baseUrl/courses/$courseId/contents/$contentId/?content_detail_v2=true"
}

private fun setupJavaScriptInterface(webViewFragment: WebViewFragment) {
val pdfPath = requireArguments().getString(ARG_PDF_PATH, "")
webViewFragment.webView.settings.apply {
allowFileAccessFromFileURLs = true
allowUniversalAccessFromFileURLs = true
}

webViewFragment.addJavascriptInterface(
CachedPdfPathProvider(requireActivity(), pdfPath),
"AndroidPdfCache"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class DocumentViewerFragment : BaseContentDetailFragment(), PdfDownloadListener,
val args = Bundle()
args.putLong("contentId", contentId)
args.putLong("courseId", content.courseId ?: -1L)
args.putString("pdfPath", pdfDownloadManager.getCachedPdfPath())
aiChatFragment?.arguments = args
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package `in`.testpress.course.util

import android.app.Activity
import `in`.testpress.util.BaseJavaScriptInterface
import android.webkit.JavascriptInterface
import java.io.File

class CachedPdfPathProvider(
activity: Activity,
private val pdfPath: String
) : BaseJavaScriptInterface(activity) {

@JavascriptInterface
fun getCachedPDFPath(): String {
if (isPDFCached()) return "file://$pdfPath" else return ""
}

@JavascriptInterface
fun isPDFCached(): Boolean {
return !pdfPath.isEmpty() && File(pdfPath).exists()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ open class PDFDownloadManager(
fun get(): File {
return fileEncryptAndDecryptUtil.decrypt()
}

fun getCachedPdfPath(): String {
return if (isDownloaded()) get().absolutePath else ""
}
}

interface PdfDownloadListener {
Expand Down