|
1 | 1 | package org.grapheneos.pdfviewer;
|
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint;
|
4 |
| -import android.app.Activity; |
5 | 4 | import android.content.Intent;
|
6 | 5 | import android.content.res.ColorStateList;
|
7 | 6 | import android.graphics.Color;
|
|
26 | 25 | import android.widget.TextView;
|
27 | 26 | import android.widget.Toast;
|
28 | 27 |
|
| 28 | +import androidx.activity.result.ActivityResultCallback; |
| 29 | +import androidx.activity.result.ActivityResultLauncher; |
| 30 | +import androidx.activity.result.contract.ActivityResultContracts; |
29 | 31 | import androidx.annotation.NonNull;
|
30 | 32 | import androidx.annotation.Nullable;
|
31 | 33 | import androidx.fragment.app.Fragment;
|
@@ -74,9 +76,6 @@ public class PdfViewerFragment extends Fragment {
|
74 | 76 | "usb 'none'; " +
|
75 | 77 | "vr 'none'";
|
76 | 78 |
|
77 |
| - private static final int REQUEST_CODE_JUMP_PAGE = 1000; |
78 |
| - private static final int REQUEST_CODE_ACTION_OPEN_DOCUMENT = 1001; |
79 |
| - |
80 | 79 | private static final float MIN_ZOOM_RATIO = 0.5f;
|
81 | 80 | private static final float MAX_ZOOM_RATIO = 1.5f;
|
82 | 81 | private static final int ALPHA_LOW = 130;
|
@@ -184,25 +183,11 @@ public static PdfViewerFragment newInstance() {
|
184 | 183 | public void onCreate(Bundle savedInstanceState) {
|
185 | 184 | super.onCreate(savedInstanceState);
|
186 | 185 | setHasOptionsMenu(true);
|
187 |
| - } |
188 |
| - |
189 |
| - // Can be replaced when support for passing results between two Fragments |
190 |
| - // via new APIs on FragmentManager arrive (Androidx Fragment 1.3.0) |
191 |
| - @Override |
192 |
| - public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
193 |
| - if (requestCode == REQUEST_CODE_JUMP_PAGE) { |
194 |
| - if (resultCode == Activity.RESULT_OK) { |
195 |
| - final int newPage = data.getIntExtra(JumpToPageFragment.INTENT_KEY, -1); |
196 |
| - onJumpToPageInDocument(newPage); |
197 |
| - } |
198 |
| - } else if (requestCode == REQUEST_CODE_ACTION_OPEN_DOCUMENT) { |
199 |
| - if (resultCode == Activity.RESULT_OK && data != null) { |
200 |
| - mViewModel.setUri(data.getData()); |
201 |
| - mViewModel.setPage(1); |
202 |
| - loadPdf(true); |
203 |
| - requireActivity().invalidateOptionsMenu(); |
204 |
| - } |
205 |
| - } |
| 186 | + getParentFragmentManager().setFragmentResultListener(JumpToPageFragment.REQUEST_KEY, |
| 187 | + this, (requestKey, result) -> { |
| 188 | + final int newPage = result.getInt(JumpToPageFragment.BUNDLE_KEY); |
| 189 | + onJumpToPageInDocument(newPage); |
| 190 | + }); |
206 | 191 | }
|
207 | 192 |
|
208 | 193 | @Override
|
@@ -431,11 +416,20 @@ private void documentOrientationChanged(final int orientationDegreesOffset) {
|
431 | 416 | renderPage(0);
|
432 | 417 | }
|
433 | 418 |
|
| 419 | + private ActivityResultLauncher<String> mGetDocumentUriLauncher = registerForActivityResult( |
| 420 | + new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() { |
| 421 | + @Override |
| 422 | + public void onActivityResult(Uri uri) { |
| 423 | + if (uri != null) { |
| 424 | + mViewModel.setUri(uri); |
| 425 | + mViewModel.setPage(1); |
| 426 | + loadPdf(true); |
| 427 | + } |
| 428 | + } |
| 429 | + }); |
| 430 | + |
434 | 431 | private void openDocument() {
|
435 |
| - Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
436 |
| - intent.addCategory(Intent.CATEGORY_OPENABLE); |
437 |
| - intent.setType("application/pdf"); |
438 |
| - startActivityForResult(intent, REQUEST_CODE_ACTION_OPEN_DOCUMENT); |
| 432 | + mGetDocumentUriLauncher.launch("application/pdf"); |
439 | 433 | }
|
440 | 434 |
|
441 | 435 | private void zoomIn(float value, boolean end) {
|
@@ -595,9 +589,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
|
595 | 589 | return true;
|
596 | 590 |
|
597 | 591 | case R.id.action_jump_to_page:
|
598 |
| - final JumpToPageFragment fragment = new JumpToPageFragment(); |
599 |
| - fragment.setTargetFragment(this, REQUEST_CODE_JUMP_PAGE); |
600 |
| - fragment.show(getParentFragmentManager(), JumpToPageFragment.TAG); |
| 592 | + new JumpToPageFragment() |
| 593 | + .show(getParentFragmentManager(), JumpToPageFragment.TAG); |
601 | 594 | return true;
|
602 | 595 |
|
603 | 596 | default:
|
|
0 commit comments