SandboxedPdfLoader


public final class SandboxedPdfLoader implements PdfLoader


A PdfLoader implementation that opens PDF documents through a sandboxed Android service.

This class establishes a connection with the PdfDocumentServiceImpl bound service and attempts to load a PDF document using the PdfRenderer. If successful, it returns a SandboxedPdfDocument instance, which provides a remote interface for interacting with the document.

The loading process involves:

  1. Establishing a connection with the service using a PdfServiceConnection.

  2. Opening a ParcelFileDescriptor for the PDF document.

  3. Passing the file descriptor to the service for loading and rendering.

  4. Creating a SandboxedPdfDocument instance upon successful loading.

Summary

Public constructors

SandboxedPdfLoader(
    @NonNull Context context,
    @NonNull CoroutineContext coroutineContext
)

Creates a new SandboxedPdfLoader instance.

Public methods

@NonNull PdfDocument
openDocument(@NonNull Uri uri, String password)

Asynchronously opens a PDF document from the specified Uri.

static final @NonNull PdfSandboxHandle

Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the SandboxedPdfLoader or PdfDocument.

Public constructors

SandboxedPdfLoader

Added in 1.0.0-alpha10
public SandboxedPdfLoader(
    @NonNull Context context,
    @NonNull CoroutineContext coroutineContext
)

Creates a new SandboxedPdfLoader instance.

Parameters
@NonNull Context context

The Context required for accessing system services.

@NonNull CoroutineContext coroutineContext

The CoroutineContext used for asynchronous operations. This context is resolved internally to ensure an appropriate ContinuationInterceptor is present.

  • If the provided coroutineContext already contains a ContinuationInterceptor, that interceptor will be used.

  • If the provided coroutineContext does not contain a ContinuationInterceptor, Dispatchers.IO will be automatically added to it to handle I/O-bound tasks such as opening file descriptors and interacting with the PDF service.

  • Providing a Job in this coroutineContext is an error.

Public methods

openDocument

public @NonNull PdfDocument openDocument(@NonNull Uri uri, String password)

Asynchronously opens a PDF document from the specified Uri.

Parameters
@NonNull Uri uri

The URI of the PDF document to open.

String password

(Optional) The password to unlock the document if it is encrypted.

Returns
@NonNull PdfDocument

The opened PdfDocument.

Throws
androidx.pdf.PdfPasswordException

If the provided password is incorrect.

java.io.IOException

If an error occurs while opening the document.

startInitialization

Added in 1.0.0-alpha10
public static final @NonNull PdfSandboxHandle startInitialization(@NonNull Context context)

Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the SandboxedPdfLoader or PdfDocument.

The returned PdfSandboxHandle represents a session and must be closed by the caller when no longer needed.

Calling this method is optional. Any document operation via SandboxedPdfLoader and PdfDocument will initialize the resources internally on demand, but may experience increased startup time.

Parameters
@NonNull Context context

A Context of component to be associated with pdf session.

Returns
@NonNull PdfSandboxHandle

A PdfSandboxHandle representing an active pdf session.

See also
PdfSandboxHandle