-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
PSPDFKit
committed
Jun 7, 2024
1 parent
34697d8
commit 8b4d9ef
Showing
53 changed files
with
1,195 additions
and
5,682 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
Binary file not shown.
57 changes: 57 additions & 0 deletions
57
android/src/main/java/com/pspdfkit/react/PDFDocumentModule.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,57 @@ | ||
package com.pspdfkit.react | ||
|
||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.pspdfkit.document.PdfDocument | ||
|
||
@ReactModule(name = PDFDocumentModule.NAME) | ||
class PDFDocumentModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { | ||
|
||
private var documents = mutableMapOf<Int, PdfDocument>() | ||
|
||
override fun getName(): String { | ||
return NAME | ||
} | ||
|
||
private fun getDocument(reference: Int): PdfDocument? { | ||
return this.documents[reference] | ||
} | ||
|
||
fun setDocument(document: PdfDocument, reference: Int) { | ||
this.documents[reference] = document | ||
} | ||
|
||
@ReactMethod fun getDocumentId(reference: Int, promise: Promise) { | ||
try { | ||
// Using uid here until Android exposes the documentId property. | ||
promise.resolve(this.getDocument(reference)?.uid) | ||
} catch (e: Throwable) { | ||
promise.reject("getDocumentId error", e) | ||
} | ||
} | ||
|
||
@ReactMethod fun invalidateCacheForPage(reference: Int, pageIndex: Int, promise: Promise) { | ||
try { | ||
this.getDocument(reference)?.invalidateCacheForPage(pageIndex) | ||
promise.resolve(true) | ||
} catch (e: Throwable) { | ||
promise.reject("invalidateCacheForPage error", e) | ||
} | ||
} | ||
|
||
@ReactMethod fun invalidateCache(reference: Int, promise: Promise) { | ||
try { | ||
this.getDocument(reference)?.invalidateCache() | ||
promise.resolve(true) | ||
} catch (e: Throwable) { | ||
promise.reject("invalidateCache error", e) | ||
} | ||
} | ||
|
||
companion object { | ||
const val NAME = "PDFDocumentManager" | ||
} | ||
} |
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
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
104 changes: 104 additions & 0 deletions
104
android/src/main/java/com/pspdfkit/react/helper/RemoteDocumentDownloader.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,104 @@ | ||
package com.pspdfkit.react.helper | ||
|
||
import android.content.Context | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.FragmentManager | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.pspdfkit.document.download.DownloadJob | ||
import com.pspdfkit.document.download.DownloadProgressFragment | ||
import com.pspdfkit.document.download.DownloadRequest | ||
import com.pspdfkit.document.download.source.DownloadSource | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.net.HttpURLConnection | ||
import java.net.MalformedURLException | ||
import java.net.URL | ||
import java.net.URLConnection | ||
|
||
class RemoteDocumentDownloader(private val remoteURL: String, | ||
private var destinationFileURL: String?, | ||
private val overwriteExisting: Boolean, | ||
private val context: Context, | ||
private val fragmentManager: FragmentManager) { | ||
|
||
fun startDownload(callback: (File?) -> Unit) { | ||
val source: WebDownloadSource = try { | ||
// Try to parse the URL pointing to the PDF document. | ||
WebDownloadSource(URL(remoteURL)) | ||
} catch (e: MalformedURLException) { | ||
// Error while trying to parse the PDF Download URL | ||
return | ||
} | ||
|
||
if (overwriteExisting && destinationFileURL != null) { | ||
val delete = File(destinationFileURL) | ||
if (delete.exists()) { | ||
delete.delete() | ||
} | ||
} | ||
|
||
val request = DownloadRequest.Builder(context) | ||
.source(source) | ||
.outputFile(if (destinationFileURL == null) | ||
File(context.getDir("documents", Context.MODE_PRIVATE), "temp.pdf") else | ||
File(destinationFileURL)) | ||
.overwriteExisting(overwriteExisting) | ||
.useTemporaryOutputFile(false) | ||
.build() | ||
|
||
val job = DownloadJob.startDownload(request) | ||
job.setProgressListener(object : DownloadJob.ProgressListenerAdapter() { | ||
override fun onComplete(output: File) { | ||
callback(output) | ||
} | ||
|
||
override fun onError(exception: Throwable) { | ||
callback(null) | ||
} | ||
}) | ||
val fragment = DownloadProgressFragment() | ||
fragment.show(fragmentManager, "download-fragment") | ||
fragment.job = job | ||
} | ||
} | ||
|
||
class WebDownloadSource constructor(private val documentURL: URL) : DownloadSource { | ||
/** | ||
* The open method needs to return an [InputStream] that will provide the complete document. | ||
*/ | ||
@Throws(IOException::class) | ||
override fun open(): InputStream { | ||
val connection = documentURL.openConnection() as HttpURLConnection | ||
connection.connect() | ||
return connection.inputStream | ||
} | ||
|
||
/** | ||
* If the length is available it can be returned here. This is optional, and can improve the reported download progress, since it will then contain | ||
* a percentage of download. | ||
*/ | ||
override fun getLength(): Long { | ||
var length = DownloadSource.UNKNOWN_DOWNLOAD_SIZE | ||
|
||
// We try to estimate the download size using the content length header. | ||
var urlConnection: URLConnection? = null | ||
try { | ||
urlConnection = documentURL.openConnection() | ||
val contentLength = urlConnection.contentLength | ||
if (contentLength != -1) { | ||
length = contentLength.toLong() | ||
} | ||
} catch (e: IOException) { | ||
// Error while trying to parse the PDF Download URL | ||
} finally { | ||
(urlConnection as? HttpURLConnection)?.disconnect() | ||
} | ||
return length | ||
} | ||
|
||
override fun toString(): String { | ||
return "WebDownloadSource{documentURL=$documentURL}" | ||
} | ||
} |
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.