16
16
import android .view .View ;
17
17
import android .webkit .CookieManager ;
18
18
import android .webkit .JavascriptInterface ;
19
+ import android .webkit .RenderProcessGoneDetail ;
19
20
import android .webkit .WebResourceRequest ;
20
21
import android .webkit .WebResourceResponse ;
21
22
import android .webkit .WebSettings ;
58
59
public class PdfViewer extends AppCompatActivity implements LoaderManager .LoaderCallbacks <List <CharSequence >> {
59
60
private static final String TAG = "PdfViewer" ;
60
61
62
+ private static final String STATE_WEBVIEW_CRASHED = "webview_crashed" ;
61
63
private static final String STATE_URI = "uri" ;
62
64
private static final String STATE_PAGE = "page" ;
63
65
private static final String STATE_ZOOM_RATIO = "zoomRatio" ;
@@ -116,6 +118,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
116
118
private static final int STATE_END = 2 ;
117
119
private static final int PADDING = 10 ;
118
120
121
+ private boolean webViewCrashed ;
119
122
private Uri mUri ;
120
123
public int mPage ;
121
124
public int mNumPages ;
@@ -264,10 +267,19 @@ public String getPassword() {
264
267
}
265
268
}
266
269
270
+ private void showWebViewCrashed () {
271
+ binding .webviewAlertTitle .setText (getString (R .string .webview_crash_title ));
272
+ binding .webviewAlertMessage .setText (getString (R .string .webview_crash_message ));
273
+ binding .webviewAlertLayout .setVisibility (View .VISIBLE );
274
+ binding .webviewAlertReload .setVisibility (View .VISIBLE );
275
+ binding .webview .setVisibility (View .GONE );
276
+ }
277
+
267
278
@ Override
268
279
@ SuppressLint ({"SetJavaScriptEnabled" })
269
280
protected void onCreate (Bundle savedInstanceState ) {
270
281
super .onCreate (savedInstanceState );
282
+
271
283
binding = PdfviewerBinding .inflate (getLayoutInflater ());
272
284
setContentView (binding .getRoot ());
273
285
setSupportActionBar (binding .toolbar );
@@ -395,6 +407,17 @@ public void onPageFinished(WebView view, String url) {
395
407
invalidateOptionsMenu ();
396
408
loadPdfWithPassword (mEncryptedDocumentPassword );
397
409
}
410
+
411
+ @ Override
412
+ public boolean onRenderProcessGone (WebView view , RenderProcessGoneDetail detail ) {
413
+ if (detail .didCrash ()) {
414
+ webViewCrashed = true ;
415
+ showWebViewCrashed ();
416
+ purgeWebView ();
417
+ return true ;
418
+ }
419
+ return false ;
420
+ }
398
421
});
399
422
400
423
GestureHelper .attach (PdfViewer .this , binding .webview ,
@@ -455,6 +478,7 @@ public void onZoomEnd() {
455
478
}
456
479
457
480
if (savedInstanceState != null ) {
481
+ webViewCrashed = savedInstanceState .getBoolean (STATE_WEBVIEW_CRASHED );
458
482
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
459
483
mUri = savedInstanceState .getParcelable (STATE_URI , Uri .class );
460
484
} else {
@@ -468,7 +492,14 @@ public void onZoomEnd() {
468
492
mEncryptedDocumentPassword = savedInstanceState .getString (STATE_ENCRYPTED_DOCUMENT_PASSWORD );
469
493
}
470
494
471
- if (mUri != null ) {
495
+ binding .webviewAlertReload .setOnClickListener (v -> {
496
+ webViewCrashed = false ;
497
+ recreate ();
498
+ });
499
+
500
+ if (webViewCrashed ) {
501
+ showWebViewCrashed ();
502
+ } else if (mUri != null ) {
472
503
if ("file" .equals (mUri .getScheme ())) {
473
504
snackbar .setText (R .string .legacy_file_uri ).show ();
474
505
return ;
@@ -478,14 +509,16 @@ public void onZoomEnd() {
478
509
}
479
510
}
480
511
481
-
512
+ private void purgeWebView () {
513
+ binding .webview .removeJavascriptInterface ("channel" );
514
+ binding .getRoot ().removeView (binding .webview );
515
+ binding .webview .destroy ();
516
+ }
482
517
483
518
@ Override
484
519
protected void onDestroy () {
485
520
super .onDestroy ();
486
- binding .webview .removeJavascriptInterface ("channel" );
487
- binding .getRoot ().removeView (binding .webview );
488
- binding .webview .destroy ();
521
+ purgeWebView ();
489
522
maybeCloseInputStream ();
490
523
}
491
524
@@ -525,15 +558,18 @@ private void setToolbarTitleWithDocumentName() {
525
558
protected void onResume () {
526
559
super .onResume ();
527
560
528
- // The user could have left the activity to update the WebView
529
- invalidateOptionsMenu ();
530
- if (getWebViewRelease () >= MIN_WEBVIEW_RELEASE ) {
531
- binding .webviewOutOfDateLayout .setVisibility (View .GONE );
532
- binding .webview .setVisibility (View .VISIBLE );
533
- } else {
534
- binding .webview .setVisibility (View .GONE );
535
- binding .webviewOutOfDateMessage .setText (getString (R .string .webview_out_of_date_message , getWebViewRelease (), MIN_WEBVIEW_RELEASE ));
536
- binding .webviewOutOfDateLayout .setVisibility (View .VISIBLE );
561
+ if (!webViewCrashed ) {
562
+ // The user could have left the activity to update the WebView
563
+ invalidateOptionsMenu ();
564
+ if (getWebViewRelease () >= MIN_WEBVIEW_RELEASE ) {
565
+ binding .webviewAlertLayout .setVisibility (View .GONE );
566
+ binding .webview .setVisibility (View .VISIBLE );
567
+ } else {
568
+ binding .webview .setVisibility (View .GONE );
569
+ binding .webviewAlertTitle .setText (getString (R .string .webview_out_of_date_title ));
570
+ binding .webviewAlertMessage .setText (getString (R .string .webview_out_of_date_message , getWebViewRelease (), MIN_WEBVIEW_RELEASE ));
571
+ binding .webviewAlertLayout .setVisibility (View .VISIBLE );
572
+ }
537
573
}
538
574
}
539
575
@@ -645,6 +681,7 @@ private void hideSystemUi() {
645
681
@ Override
646
682
public void onSaveInstanceState (@ NonNull Bundle savedInstanceState ) {
647
683
super .onSaveInstanceState (savedInstanceState );
684
+ savedInstanceState .putBoolean (STATE_WEBVIEW_CRASHED , webViewCrashed );
648
685
savedInstanceState .putParcelable (STATE_URI , mUri );
649
686
savedInstanceState .putInt (STATE_PAGE , mPage );
650
687
savedInstanceState .putFloat (STATE_ZOOM_RATIO , mZoomRatio );
@@ -703,14 +740,22 @@ public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
703
740
mDocumentState = STATE_END ;
704
741
}
705
742
706
- enableDisableMenuItem (menu .findItem (R .id .action_open ), getWebViewRelease () >= MIN_WEBVIEW_RELEASE );
743
+
744
+ enableDisableMenuItem (menu .findItem (R .id .action_open ),
745
+ !webViewCrashed && getWebViewRelease () >= MIN_WEBVIEW_RELEASE );
707
746
enableDisableMenuItem (menu .findItem (R .id .action_share ), mUri != null );
708
747
enableDisableMenuItem (menu .findItem (R .id .action_next ), mPage < mNumPages );
709
748
enableDisableMenuItem (menu .findItem (R .id .action_previous ), mPage > 1 );
710
749
enableDisableMenuItem (menu .findItem (R .id .action_save_as ), mUri != null );
711
750
712
751
menu .findItem (R .id .action_outline ).setVisible (viewModel .hasOutline ());
713
752
753
+ if (webViewCrashed ) {
754
+ for (final int id : ids ) {
755
+ enableDisableMenuItem (menu .findItem (id ), false );
756
+ }
757
+ }
758
+
714
759
return true ;
715
760
}
716
761
0 commit comments