-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1585 from minvws/5664
5664
- Loading branch information
Showing
16 changed files
with
370 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
holder/src/main/java/nl/rijksoverheid/ctr/holder/pdf/PDfPreviewInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright (c) 2023 De Staat der Nederlanden, Ministerie van Volksgezondheid, Welzijn en Sport. | ||
* Licensed under the EUROPEAN UNION PUBLIC LICENCE v. 1.2 | ||
* | ||
* SPDX-License-Identifier: EUPL-1.2 | ||
* | ||
*/ | ||
package nl.rijksoverheid.ctr.holder.pdf | ||
|
||
data class PDfPreviewInfo(val content: String, val initialZoom: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
holder/src/main/java/nl/rijksoverheid/ctr/holder/pdf/PdfPreview.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (c) 2023 De Staat der Nederlanden, Ministerie van Volksgezondheid, Welzijn en Sport. | ||
* Licensed under the EUROPEAN UNION PUBLIC LICENCE v. 1.2 | ||
* | ||
* SPDX-License-Identifier: EUPL-1.2 | ||
* | ||
*/ | ||
package nl.rijksoverheid.ctr.holder.pdf | ||
|
||
sealed class PdfPreview { | ||
class Success(val info: PDfPreviewInfo) : PdfPreview() | ||
object Error : PdfPreview() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
holder/src/main/java/nl/rijksoverheid/ctr/holder/pdf/PdfPreviewViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2023 De Staat der Nederlanden, Ministerie van Volksgezondheid, Welzijn en Sport. | ||
* Licensed under the EUROPEAN UNION PUBLIC LICENCE v. 1.2 | ||
* | ||
* SPDX-License-Identifier: EUPL-1.2 | ||
* | ||
*/ | ||
package nl.rijksoverheid.ctr.holder.pdf | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import java.io.File | ||
import kotlinx.coroutines.launch | ||
import nl.rijksoverheid.ctr.shared.livedata.Event | ||
|
||
abstract class PdfPreviewViewModel : ViewModel() { | ||
val previewLiveData = MutableLiveData<Event<PdfPreview>>() | ||
abstract fun generatePreview(screenWidth: Int, filesDir: File) | ||
} | ||
|
||
class PdfPreviewViewModelImpl( | ||
private val previewPdfUseCase: PreviewPdfUseCase | ||
) : PdfPreviewViewModel() { | ||
override fun generatePreview(screenWidth: Int, filesDir: File) { | ||
viewModelScope.launch { | ||
val pdfPreviewResult = previewPdfUseCase.generatePreview(screenWidth, filesDir) | ||
if (pdfPreviewResult != null) { | ||
previewLiveData.postValue(Event(PdfPreview.Success(pdfPreviewResult))) | ||
} else { | ||
previewLiveData.postValue(Event(PdfPreview.Error)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
holder/src/main/java/nl/rijksoverheid/ctr/holder/pdf/PreviewPdfUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package nl.rijksoverheid.ctr.holder.pdf | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import android.graphics.pdf.PdfRenderer | ||
import android.os.ParcelFileDescriptor | ||
import android.util.Base64 | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
interface PreviewPdfUseCase { | ||
suspend fun generatePreview(screenWidth: Int, filesDir: File): PDfPreviewInfo? | ||
} | ||
|
||
class PreviewPdfUseCaseImpl : PreviewPdfUseCase { | ||
override suspend fun generatePreview(screenWidth: Int, filesDir: File): PDfPreviewInfo? { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
val pdfFile = File(filesDir, PdfWebViewFragment.pdfFileName) | ||
val parcelFileDescriptor = | ||
ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY) | ||
val pdfRenderer = PdfRenderer(parcelFileDescriptor) | ||
val bitmaps = mutableListOf<Bitmap>() | ||
for (i in 0 until pdfRenderer.pageCount) { | ||
val currentPage = pdfRenderer.openPage(i) | ||
val bitmap = Bitmap.createBitmap( | ||
currentPage.width, | ||
currentPage.height, | ||
Bitmap.Config.ARGB_8888 | ||
) | ||
currentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY) | ||
bitmaps.add(bitmap) | ||
currentPage.close() | ||
} | ||
pdfRenderer.close() | ||
|
||
val bitmapWidth = bitmaps.first().width | ||
val initialZoom = ((screenWidth.toFloat() / bitmapWidth.toFloat()) * 100).toInt() | ||
|
||
PDfPreviewInfo( | ||
content = pdfBitmap(bitmaps), | ||
initialZoom = initialZoom | ||
) | ||
} catch (exception: Exception) { | ||
null | ||
} | ||
} | ||
} | ||
|
||
private fun pdfBitmap(bitmaps: List<Bitmap>): String { | ||
val width = bitmaps.first().width | ||
val pageHeight = bitmaps.first().height | ||
val height = bitmaps.first().height * bitmaps.size | ||
val comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | ||
val comboImage = Canvas(comboBitmap) | ||
bitmaps.forEachIndexed { index, bitmap -> | ||
comboImage.drawBitmap(bitmap, 0f, (index * pageHeight).toFloat(), null) | ||
} | ||
|
||
val byteArrayOutputStream = ByteArrayOutputStream() | ||
comboBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream) | ||
val byteArray = byteArrayOutputStream.toByteArray() | ||
return "data:image/png;base64,${Base64.encodeToString(byteArray, Base64.DEFAULT)}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.