|
| 1 | +/* |
| 2 | + * Copyright © 2019-2024 PSPDFKit GmbH. All rights reserved. |
| 3 | + * |
| 4 | + * The PSPDFKit Sample applications are licensed with a modified BSD license. |
| 5 | + * Please see License for details. This notice may not be removed from this file. |
| 6 | + */ |
| 7 | +package com.pspdfkit.example; |
| 8 | + |
| 9 | +import android.app.Activity; |
| 10 | +import android.content.Context; |
| 11 | +import android.content.Intent; |
| 12 | +import android.net.Uri; |
| 13 | +import android.os.Bundle; |
| 14 | +import android.widget.Button; |
| 15 | +import androidx.activity.result.ActivityResultLauncher; |
| 16 | +import androidx.activity.result.contract.ActivityResultContracts; |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import androidx.annotation.Nullable; |
| 19 | +import androidx.appcompat.app.AppCompatActivity; |
| 20 | +import com.pspdfkit.PSPDFKit; |
| 21 | +import com.pspdfkit.configuration.activity.PdfActivityConfiguration; |
| 22 | +import com.pspdfkit.configuration.page.PageFitMode; |
| 23 | +import com.pspdfkit.configuration.page.PageScrollDirection; |
| 24 | +import com.pspdfkit.document.download.DownloadJob; |
| 25 | +import com.pspdfkit.document.download.DownloadProgressFragment; |
| 26 | +import com.pspdfkit.document.download.DownloadRequest; |
| 27 | +import com.pspdfkit.ui.PdfActivityIntentBuilder; |
| 28 | +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; |
| 29 | +import io.reactivex.rxjava3.disposables.Disposable; |
| 30 | + |
| 31 | +public class MainActivity extends AppCompatActivity { |
| 32 | + private static final String DEMO_DOCUMENT_ASSET_NAME = "demo.pdf"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Upon launching this activity, we extract the demo PDF from the assets to the app's private |
| 36 | + * files. This is done only once, so that any edits performed in the document are actually |
| 37 | + * persisted. To see how the extraction is implemented, see {@link ExtractAssetTask}. |
| 38 | + */ |
| 39 | + private @Nullable Disposable documentExtraction; |
| 40 | + |
| 41 | + // You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed |
| 42 | + private final ActivityResultLauncher<Intent> filePickerActivityResultLauncher = |
| 43 | + registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { |
| 44 | + if (result.getResultCode() == Activity.RESULT_OK) { |
| 45 | + if (result.getData() == null) return; |
| 46 | + final var uri = result.getData().getData(); |
| 47 | + if (uri != null) { |
| 48 | + prepareAndShowDocument(uri); |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void onCreate(@Nullable final Bundle savedInstanceState) { |
| 55 | + super.onCreate(savedInstanceState); |
| 56 | + setContentView(R.layout.activity_main); |
| 57 | + |
| 58 | + final Button openDocumentButton = findViewById(R.id.main_btn_open_document); |
| 59 | + openDocumentButton.setOnClickListener(v -> launchSystemFilePicker()); |
| 60 | + |
| 61 | + final Button openDemoDocumentButton = findViewById(R.id.main_btn_open_example); |
| 62 | + openDemoDocumentButton.setOnClickListener(v -> prepareAndShowDemoDocument()); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void onDestroy() { |
| 67 | + super.onDestroy(); |
| 68 | + final Disposable runningDocumentExtraction = documentExtraction; |
| 69 | + if (runningDocumentExtraction != null) { |
| 70 | + runningDocumentExtraction.dispose(); |
| 71 | + documentExtraction = null; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Ensures that the given URI is openable or downloads the document before opening the document |
| 77 | + * using {@link #launchPdfActivity(Uri)}. |
| 78 | + */ |
| 79 | + private void prepareAndShowDocument(@NonNull final Uri uri) { |
| 80 | + // PSPDFKit supports direct opening of documents from various URI locations (including |
| 81 | + // assets, |
| 82 | + // local file URIs, content provider URIs, etc.). |
| 83 | + if (PSPDFKit.isOpenableUri(this, uri)) { |
| 84 | + launchPdfActivity(uri); |
| 85 | + } else { |
| 86 | + // Only document accessible as files are openable directly with PSPDFKit so we have to |
| 87 | + // transfer other documents to application cache |
| 88 | + final DownloadRequest request = |
| 89 | + new DownloadRequest.Builder(this).uri(uri).build(); |
| 90 | + // Start download of the PDF document from the given URL in a background thread. |
| 91 | + final DownloadJob job = DownloadJob.startDownload(request); |
| 92 | + // To visualize the download progress, we use a dedicated progress fragment. |
| 93 | + final DownloadProgressFragment fragment = new DownloadProgressFragment(); |
| 94 | + fragment.show(getSupportFragmentManager(), "download-fragment"); |
| 95 | + fragment.setJob(job); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** Opens a demo document from assets directory */ |
| 100 | + private void prepareAndShowDemoDocument() { |
| 101 | + // Prevent extracting the document multiple times (for example if the user hits the open |
| 102 | + // button twice). |
| 103 | + if (this.documentExtraction != null) return; |
| 104 | + // Extract the demo document from the assets into the app's private files directory. This is |
| 105 | + // done once, and allows to make changes to the document and to persist them. Every subsequent |
| 106 | + // call to extractAsync() will reuse the previously extracted file. |
| 107 | + this.documentExtraction = ExtractAssetTask.extractAsync(DEMO_DOCUMENT_ASSET_NAME, this) |
| 108 | + .observeOn(AndroidSchedulers.mainThread()) |
| 109 | + .subscribe((documentFile) -> { |
| 110 | + documentExtraction = null; |
| 111 | + prepareAndShowDocument(Uri.fromFile(documentFile)); |
| 112 | + }); |
| 113 | + } |
| 114 | + /** |
| 115 | + * Launches the {@link com.pspdfkit.ui.PdfActivity} for showing the document located at the |
| 116 | + * given URI. The URI must be openable (i.e. {@link PSPDFKit#isOpenableUri(Context, Uri)} has to |
| 117 | + * return {@code true} for the URI). |
| 118 | + */ |
| 119 | + private void launchPdfActivity(@NonNull final Uri uri) { |
| 120 | + // Configure features of the PdfActivity to launch. For an extensive set of configuration |
| 121 | + // options, have a look at the available builder methods. |
| 122 | + final PdfActivityConfiguration pspdfkitConfiguration = new PdfActivityConfiguration.Builder( |
| 123 | + getApplicationContext()) |
| 124 | + .scrollDirection(PageScrollDirection.HORIZONTAL) |
| 125 | + .showPageNumberOverlay() |
| 126 | + .showThumbnailGrid() |
| 127 | + .theme(R.style.PSPDFSimple_Theme) |
| 128 | + .themeDark(R.style.PSPDFSimple_Theme_Dark) |
| 129 | + .fitMode(PageFitMode.FIT_TO_WIDTH) |
| 130 | + .build(); |
| 131 | + // PdfActivity is launched like any other activity using an Intent. To create the |
| 132 | + // appropriate intent, you need to use the PdfActivityIntentBuilder which will handle adding all |
| 133 | + // relevant extras to the intent. |
| 134 | + final Intent intent = PdfActivityIntentBuilder.fromUri(MainActivity.this, uri) |
| 135 | + .configuration(pspdfkitConfiguration) |
| 136 | + .build(); |
| 137 | + startActivity(intent); |
| 138 | + } |
| 139 | + |
| 140 | + /** Opens the Android file picker for selecting a PDF document to show. */ |
| 141 | + private void launchSystemFilePicker() { |
| 142 | + final Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 143 | + openIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| 144 | + openIntent.setType("application/*"); |
| 145 | + filePickerActivityResultLauncher.launch(openIntent); |
| 146 | + } |
| 147 | +} |
0 commit comments