Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PdfSource and RenderQuality #167

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions app/src/main/java/com/rajat/sample/pdfviewer/ComposeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rajat.pdfviewer.PdfRendererView
import com.rajat.pdfviewer.compose.PdfRendererViewCompose
import com.rajat.pdfviewer.PdfSource
import com.rajat.pdfviewer.compose.Pdf
import com.rajat.sample.pdfviewer.ui.theme.AndroidpdfviewerTheme
import java.io.File

Expand Down Expand Up @@ -103,7 +103,7 @@ fun MyPdfScreenFromUri(uri: Uri, modifier: Modifier = Modifier) {
val lifecycleOwner = LocalLifecycleOwner.current
PdfRendererViewCompose(
modifier = modifier,
uri = uri,
source = PdfSource.FromUri(uri),
lifecycleOwner = lifecycleOwner,
statusCallBack = object : PdfRendererView.StatusCallBack {
override fun onPdfLoadStart() {
Expand Down Expand Up @@ -138,7 +138,7 @@ fun MyPdfScreenFromUrl(url: String, modifier: Modifier = Modifier) {
val lifecycleOwner = LocalLifecycleOwner.current
PdfRendererViewCompose(
modifier = modifier,
url = url,
source = PdfSource.FromUrl(url),
lifecycleOwner = lifecycleOwner,
statusCallBack = object : PdfRendererView.StatusCallBack {
override fun onPdfLoadStart() {
Expand Down Expand Up @@ -174,7 +174,7 @@ fun MyPdfScreenFromFile() {
val lifecycleOwner = LocalLifecycleOwner.current
val pdfFile = File("path/to/your/file.pdf") // Replace with your file path
PdfRendererViewCompose(
file = pdfFile,
source = PdfSource.FromFile(pdfFile),
lifecycleOwner = lifecycleOwner
)
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.rajat.pdfviewer.PdfRendererView
import com.rajat.pdfviewer.PdfSource
import com.rajat.pdfviewer.PdfViewerActivity
import com.rajat.pdfviewer.util.saveTo
import com.rajat.sample.pdfviewer.databinding.ActivityMainBinding
Expand Down Expand Up @@ -96,8 +97,8 @@ class MainActivity : AppCompatActivity() {
//Page change. Not require
}
}
binding.pdfView.initWithUrl(
url = download_file_url2,
binding.pdfView.init(
source = PdfSource.FromUrl(download_file_url2),
lifecycleCoroutineScope = lifecycleScope,
lifecycle = lifecycle
)
Expand Down
91 changes: 70 additions & 21 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfRendererView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ class PdfRendererView @JvmOverloads constructor(
private var runnable = Runnable {}
private var enableLoadingForPages: Boolean = false
private var pdfRendererCoreInitialised = false
private var pageMargin: Rect = Rect(0,0,0,0)
private var pageMargin: Rect = Rect(0, 0, 0, 0)
var statusListener: StatusCallBack? = null
private var positionToUseForState: Int = 0
private var restoredScrollPosition: Int = NO_POSITION
private var disableScreenshots: Boolean = false
private var postInitializationAction: (() -> Unit)? = null
var pdfSource: PdfSource? = null
private set
var renderQuality: RenderQuality = RenderQuality.LOW

val totalPageCount: Int
get() {
Expand All @@ -73,28 +76,51 @@ class PdfRendererView @JvmOverloads constructor(
fun onPageChanged(currentPage: Int, totalPage: Int) {}
}

fun initWithUrl(
fun init(
source: PdfSource, headers: HeaderData = HeaderData(),
lifecycleCoroutineScope: LifecycleCoroutineScope,
lifecycle: Lifecycle
) {
pdfSource = source
when (source) {
is PdfSource.FromFile -> initWithFile(source.file)
is PdfSource.FromUri -> initWithUri(source.uri)
is PdfSource.FromUrl -> {
initWithUrl(
url = source.url,
headers = headers,
lifecycleCoroutineScope = lifecycleCoroutineScope,
lifecycle = lifecycle,
)
}
}
}

private fun initWithUrl(
url: String,
headers: HeaderData = HeaderData(),
lifecycleCoroutineScope: LifecycleCoroutineScope,
lifecycle: Lifecycle
) {
lifecycle.addObserver(this) // Register as LifecycleObserver
PdfDownloader(lifecycleCoroutineScope,headers,url, object : PdfDownloader.StatusListener {
PdfDownloader(lifecycleCoroutineScope, headers, url, object : PdfDownloader.StatusListener {
override fun getContext(): Context = context
override fun onDownloadStart() {
statusListener?.onPdfLoadStart()
}

override fun onDownloadProgress(currentBytes: Long, totalBytes: Long) {
var progress = (currentBytes.toFloat() / totalBytes.toFloat() * 100F).toInt()
if (progress >= 100)
progress = 100
statusListener?.onPdfLoadProgress(progress, currentBytes, totalBytes)
}

override fun onDownloadSuccess(absolutePath: String) {
initWithFile(File(absolutePath))
statusListener?.onPdfLoadSuccess(absolutePath)
}

override fun onError(error: Throwable) {
error.printStackTrace()
statusListener?.onError(error)
Expand All @@ -103,7 +129,8 @@ class PdfRendererView @JvmOverloads constructor(
}

fun initWithFile(file: File) {
init(file)
val fileDescriptor = PdfRendererCore.getFileDescriptor(file)
init(fileDescriptor)
}

@Throws(FileNotFoundException::class)
Expand All @@ -126,7 +153,7 @@ class PdfRendererView @JvmOverloads constructor(
var savedState = state
if (savedState is Bundle) {
val superState = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
savedState.getParcelable("superState",Parcelable::class.java)
savedState.getParcelable("superState", Parcelable::class.java)
} else {
savedState.getParcelable("superState")
}
Expand All @@ -137,16 +164,17 @@ class PdfRendererView @JvmOverloads constructor(
}
}

private fun init(file: File) {
val fileDescriptor = PdfRendererCore.getFileDescriptor(file)
init(fileDescriptor)
}

private fun init(fileDescriptor: ParcelFileDescriptor) {
// Proceed with safeFile
pdfRendererCore = PdfRendererCore(context, fileDescriptor)
pdfRendererCoreInitialised = true
pdfViewAdapter = PdfViewAdapter(context,pdfRendererCore, pageMargin, enableLoadingForPages)
pdfViewAdapter = PdfViewAdapter(
context = context,
renderer = pdfRendererCore,
pageSpacing = pageMargin,
enableLoadingForPages = enableLoadingForPages,
renderQuality = renderQuality
)
val v = LayoutInflater.from(context).inflate(R.layout.pdf_rendererview, this, false)
addView(v)
recyclerView = findViewById(R.id.recyclerView)
Expand Down Expand Up @@ -191,7 +219,8 @@ class PdfRendererView @JvmOverloads constructor(
val layoutManager = recyclerView.layoutManager as LinearLayoutManager

val firstVisiblePosition = layoutManager.findFirstVisibleItemPosition()
val firstCompletelyVisiblePosition = layoutManager.findFirstCompletelyVisibleItemPosition()
val firstCompletelyVisiblePosition =
layoutManager.findFirstCompletelyVisibleItemPosition()
val isPositionChanged = firstVisiblePosition != lastFirstVisiblePosition ||
firstCompletelyVisiblePosition != lastCompletelyVisiblePosition
if (isPositionChanged) {
Expand All @@ -204,14 +233,15 @@ class PdfRendererView @JvmOverloads constructor(
updatePageNumberDisplay(positionToUse)
lastFirstVisiblePosition = firstVisiblePosition
lastCompletelyVisiblePosition = firstCompletelyVisiblePosition
}else{
} else {
positionToUseForState = firstVisiblePosition
}
}

private fun updatePageNumberDisplay(position: Int) {
if (position != NO_POSITION) {
pageNo.text = context.getString(R.string.pdfView_page_no, position + 1, totalPageCount)
pageNo.text =
context.getString(R.string.pdfView_page_no, position + 1, totalPageCount)
pageNo.visibility = View.VISIBLE
if (position == 0) {
pageNo.postDelayed({ pageNo.visibility = View.GONE }, 3000)
Expand Down Expand Up @@ -257,24 +287,43 @@ class PdfRendererView @JvmOverloads constructor(
engine = PdfEngine.values().first { it.value == engineValue }
showDivider = typedArray.getBoolean(R.styleable.PdfRendererView_pdfView_showDivider, true)
divider = typedArray.getDrawable(R.styleable.PdfRendererView_pdfView_divider)
enableLoadingForPages = typedArray.getBoolean(R.styleable.PdfRendererView_pdfView_enableLoadingForPages, enableLoadingForPages)
val marginDim = typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_margin, 0)
enableLoadingForPages = typedArray.getBoolean(
R.styleable.PdfRendererView_pdfView_enableLoadingForPages,
enableLoadingForPages
)
val marginDim =
typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_margin, 0)
pageMargin = Rect(marginDim, marginDim, marginDim, marginDim).apply {
top = typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_marginTop, top)
left = typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_marginLeft, left)
right = typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_marginRight, right)
bottom = typedArray.getDimensionPixelSize(R.styleable.PdfRendererView_pdfView_page_marginBottom, bottom)
top = typedArray.getDimensionPixelSize(
R.styleable.PdfRendererView_pdfView_page_marginTop,
top
)
left = typedArray.getDimensionPixelSize(
R.styleable.PdfRendererView_pdfView_page_marginLeft,
left
)
right = typedArray.getDimensionPixelSize(
R.styleable.PdfRendererView_pdfView_page_marginRight,
right
)
bottom = typedArray.getDimensionPixelSize(
R.styleable.PdfRendererView_pdfView_page_marginBottom,
bottom
)
}
disableScreenshots = typedArray.getBoolean(R.styleable.PdfRendererView_pdfView_disableScreenshots, false)
disableScreenshots =
typedArray.getBoolean(R.styleable.PdfRendererView_pdfView_disableScreenshots, false)
applyScreenshotSecurity()
typedArray.recycle()
}

private fun applyScreenshotSecurity() {
if (disableScreenshots) {
// Disables taking screenshots and screen recording
(context as? Activity)?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}

fun closePdfRender() {
if (pdfRendererCoreInitialised) {
pdfRendererCore.closePdfRender()
Expand Down
10 changes: 10 additions & 0 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rajat.pdfviewer

import android.net.Uri
import java.io.File

sealed class PdfSource {
data class FromUrl(val url: String): PdfSource()
data class FromFile(val file: File): PdfSource()
data class FromUri(val uri: Uri): PdfSource()
}
Loading