2727import android .view .SubMenu ;
2828import android .view .View ;
2929import android .view .ViewGroup ;
30+ import android .view .ViewStub ;
3031import android .view .Window ;
3132import android .view .WindowManager ;
3233import android .webkit .JavascriptInterface ;
@@ -94,6 +95,7 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
9495
9596 private HighlightingEditor _hlEditor ;
9697 private WebView _webView ;
98+ private ViewStub _webViewStub ;
9799 private MarkorWebViewClient _webViewClient ;
98100 private ViewGroup _editorHolder ;
99101 private ViewGroup _textActionsBar ;
@@ -139,7 +141,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
139141 _hlEditor = view .findViewById (R .id .document__fragment__edit__highlighting_editor );
140142 _editorHolder = view .findViewById (R .id .document__fragment__edit__editor_holder );
141143 _textActionsBar = view .findViewById (R .id .document__fragment__edit__text_actions_bar );
142- _webView = view .findViewById (R .id .document__fragment_view_webview );
144+ _webViewStub = view .findViewById (R .id .document__fragment_webview_stub );
143145 _verticalScrollView = view .findViewById (R .id .document__fragment__edit__content_editor__scrolling_parent );
144146 _lineNumbersView = view .findViewById (R .id .document__fragment__edit__line_numbers_view );
145147 _cu = new MarkorContextUtils (activity );
@@ -159,34 +161,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
159161 _lineNumbersView .setup (_hlEditor );
160162 _lineNumbersView .setLineNumbersEnabled (_appSettings .getDocumentLineNumbersEnabled (_document .path ));
161163
162- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && _appSettings .getSetWebViewFulldrawing ()) {
163- WebView .enableSlowWholeDocumentDraw ();
164- }
165-
166- _webViewClient = new MarkorWebViewClient (_webView , activity );
167- _webView .setWebChromeClient (new GsWebViewChromeClient (_webView , activity , view .findViewById (R .id .document__fragment_fullscreen_overlay )));
168- _webView .setWebViewClient (_webViewClient );
169- _webView .addJavascriptInterface (this , "Android" );
170- _webView .setBackgroundColor (Color .TRANSPARENT );
171- WebSettings webSettings = _webView .getSettings ();
172- webSettings .setBuiltInZoomControls (true );
173- webSettings .setDisplayZoomControls (false );
174- webSettings .setTextZoom ((int ) (_appSettings .getDocumentViewFontSize (_document .path ) * VIEW_FONT_SCALE ));
175- webSettings .setCacheMode (WebSettings .LOAD_CACHE_ELSE_NETWORK );
176- webSettings .setDatabaseEnabled (true );
177- webSettings .setGeolocationEnabled (false );
178- webSettings .setJavaScriptEnabled (true );
179- webSettings .setDomStorageEnabled (true );
180- webSettings .setAllowFileAccess (true );
181- webSettings .setAllowContentAccess (true );
182- webSettings .setAllowFileAccessFromFileURLs (true );
183- webSettings .setAllowUniversalAccessFromFileURLs (false );
184- webSettings .setMediaPlaybackRequiresUserGesture (false );
185-
186- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && BuildConfig .IS_TEST_BUILD && BuildConfig .DEBUG ) {
187- WebView .setWebContentsDebuggingEnabled (true ); // Inspect on computer chromium browser: chrome://inspect/#devices
188- }
189-
190164 // Upon construction, the document format has been determined from extension etc
191165 // Here we replace it with the last saved format.
192166 applyTextFormat (_appSettings .getDocumentFormat (_document .path , _document .getFormat ()));
@@ -257,14 +231,14 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
257231 });
258232 }
259233
260- _verticalScrollView .getViewTreeObserver ().addOnGlobalLayoutListener (() -> {
261- _hlEditor .post (() -> {
262- final int height = _verticalScrollView .getHeight ();
263- if (height != _hlEditor .getMinHeight ()) {
264- _hlEditor .setMinHeight (height );
265- }
266- });
234+ final Runnable ensureMinHeight = () -> _hlEditor .post (() -> {
235+ final int height = _verticalScrollView .getHeight ();
236+ if (height > 0 && height != _hlEditor .getMinHeight ()) {
237+ _hlEditor .setMinHeight (height );
238+ }
267239 });
240+ _verticalScrollView .addOnLayoutChangeListener ((v , left , top , right , bottom , oldLeft , oldTop , oldRight , oldBottom ) -> ensureMinHeight .run ());
241+ _verticalScrollView .post (ensureMinHeight );
268242 }
269243
270244 @ Override
@@ -289,7 +263,9 @@ protected void onFragmentFirstTimeVisible() {
289263
290264 @ Override
291265 public void onResume () {
292- _webView .onResume ();
266+ if (_webView != null ) {
267+ _webView .onResume ();
268+ }
293269 loadDocument ();
294270 if (_editTextUndoRedoHelper != null && _editTextUndoRedoHelper .getTextView () != _hlEditor ) {
295271 _editTextUndoRedoHelper .setTextView (_hlEditor );
@@ -300,7 +276,9 @@ public void onResume() {
300276 @ Override
301277 public void onPause () {
302278 saveDocument (false );
303- _webView .onPause ();
279+ if (_webView != null ) {
280+ _webView .onPause ();
281+ }
304282 _appSettings .addRecentFile (_document .file );
305283 _appSettings .setDocumentPreviewState (_document .path , _isPreviewVisible );
306284 _appSettings .setLastEditPosition (_document .path , TextViewUtils .getSelection (_hlEditor )[0 ]);
@@ -364,13 +342,17 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
364342 _menuSearchViewForViewMode .setOnQueryTextListener (new SearchView .OnQueryTextListener () {
365343 @ Override
366344 public boolean onQueryTextSubmit (String text ) {
367- _webView .findNext (true );
345+ if (_webView != null ) {
346+ _webView .findNext (true );
347+ }
368348 return true ;
369349 }
370350
371351 @ Override
372352 public boolean onQueryTextChange (String text ) {
373- _webView .findAllAsync (text );
353+ if (_webView != null ) {
354+ _webView .findAllAsync (text );
355+ }
374356 return true ;
375357 }
376358 });
@@ -551,16 +533,18 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
551533 _nextConvertToPrintMode = true ;
552534 setViewModeVisibility (true );
553535 Toast .makeText (activity , R .string .please_wait , Toast .LENGTH_LONG ).show ();
554- _webView .postDelayed (() -> {
555- if (itemId == R .id .action_share_pdf ) {
556- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
557- _cu .printOrCreatePdfFromWebview (_webView , _document , getTextString ().contains ("beamer\n " ));
536+ if (_webView != null ) {
537+ _webView .postDelayed (() -> {
538+ if (itemId == R .id .action_share_pdf ) {
539+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
540+ _cu .printOrCreatePdfFromWebview (_webView , _document , getTextString ().contains ("beamer\n " ));
541+ }
542+ } else {
543+ Bitmap bmp = _cu .getBitmapFromWebView (_webView , itemId == R .id .action_share_image );
544+ _cu .shareImage (getContext (), bmp , null );
558545 }
559- } else {
560- Bitmap bmp = _cu .getBitmapFromWebView (_webView , itemId == R .id .action_share_image );
561- _cu .shareImage (getContext (), bmp , null );
562- }
563- }, 7000 );
546+ }, 7000 );
547+ }
564548 }
565549
566550 return true ;
@@ -654,9 +638,11 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
654638 final int current = _isPreviewVisible ? _appSettings .getDocumentViewFontSize (_document .path ) : _appSettings .getDocumentFontSize (_document .path );
655639 MarkorDialogFactory .showFontSizeDialog (activity , current , (newSize ) -> {
656640 if (_isPreviewVisible ) {
657- _webView .getSettings ().setTextZoom ((int ) (newSize * VIEW_FONT_SCALE ));
658- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT && _lineNumbersView .isLineNumbersEnabled ()) {
659- _webView .evaluateJavascript ("refreshLineNumbers();" , null );
641+ if (_webView != null ) {
642+ _webView .getSettings ().setTextZoom ((int ) (newSize * VIEW_FONT_SCALE ));
643+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT && _lineNumbersView .isLineNumbersEnabled ()) {
644+ _webView .evaluateJavascript ("refreshLineNumbers();" , null );
645+ }
660646 }
661647 _appSettings .setDocumentViewFontSize (_document .path , newSize );
662648 } else {
@@ -676,7 +662,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
676662 }
677663 }
678664 }
679-
680665 public void checkTextChangeState () {
681666 final boolean isTextChanged = !_document .isContentSame (_hlEditor .getText ());
682667 Drawable d ;
@@ -712,12 +697,14 @@ private void showHideActionBar() {
712697 final View parent = activity .findViewById (R .id .document__fragment__edit__text_actions_bar__scrolling_parent );
713698 final View viewScroll = activity .findViewById (R .id .document__fragment_view_webview );
714699
715- if (bar != null && parent != null && _verticalScrollView != null && viewScroll != null ) {
700+ if (bar != null && parent != null && _verticalScrollView != null ) {
716701 final boolean hide = _textActionsBar .getChildCount () == 0 ;
717702 parent .setVisibility (hide ? View .GONE : View .VISIBLE );
718703 final int marginBottom = hide ? 0 : (int ) getResources ().getDimension (R .dimen .textactions_bar_height );
719704 setMarginBottom (_verticalScrollView , marginBottom );
720- setMarginBottom (viewScroll , marginBottom );
705+ if (viewScroll != null ) {
706+ setMarginBottom (viewScroll , marginBottom );
707+ }
721708 }
722709 }
723710 }
@@ -874,6 +861,9 @@ private boolean isDisplayedAtMainActivity() {
874861 }
875862
876863 public void updateViewModeText () {
864+ if (_webView == null ) {
865+ return ;
866+ }
877867 // Don't let text to view mode crash app
878868 try {
879869 _format .getConverter ().convertMarkupShowInWebView (_document , getTextString (), getActivity (), _webView , _nextConvertToPrintMode , _lineNumbersView .isLineNumbersEnabled ());
@@ -886,6 +876,37 @@ public void setViewModeVisibility(final boolean show) {
886876 setViewModeVisibility (show , true );
887877 }
888878
879+ private void setupWebViewIfNeeded (final Activity activity ) {
880+ if (_webView == null ) {
881+ _webView = (WebView ) _webViewStub .inflate ();
882+ _webView .setWebChromeClient (new GsWebViewChromeClient (_webView , activity , activity .findViewById (R .id .document__fragment_fullscreen_overlay )));
883+ _webView .addJavascriptInterface (this , "Android" );
884+ _webView .setBackgroundColor (Color .TRANSPARENT );
885+ WebSettings webSettings = _webView .getSettings ();
886+ webSettings .setBuiltInZoomControls (true );
887+ webSettings .setDisplayZoomControls (false );
888+ webSettings .setTextZoom ((int ) (_appSettings .getDocumentViewFontSize (_document .path ) * VIEW_FONT_SCALE ));
889+ webSettings .setCacheMode (WebSettings .LOAD_CACHE_ELSE_NETWORK );
890+ webSettings .setDatabaseEnabled (true );
891+ webSettings .setGeolocationEnabled (false );
892+ webSettings .setJavaScriptEnabled (true );
893+ webSettings .setDomStorageEnabled (true );
894+ webSettings .setAllowFileAccess (true );
895+ webSettings .setAllowContentAccess (true );
896+ webSettings .setAllowFileAccessFromFileURLs (true );
897+ webSettings .setAllowUniversalAccessFromFileURLs (false );
898+ webSettings .setMediaPlaybackRequiresUserGesture (false );
899+
900+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && BuildConfig .IS_TEST_BUILD && BuildConfig .DEBUG ) {
901+ WebView .setWebContentsDebuggingEnabled (true ); // Inspect on computer chromium browser: chrome://inspect/#devices
902+ }
903+
904+ _webViewClient = new MarkorWebViewClient (_webView , activity );
905+ _webView .setWebViewClient (_webViewClient );
906+ }
907+ }
908+
909+ @ SuppressLint ({"AddJavascriptInterface" , "SetJavaScriptEnabled" })
889910 public void setViewModeVisibility (boolean show , final boolean animate ) {
890911 final Activity activity = getActivity ();
891912 if (activity == null ) {
@@ -896,13 +917,16 @@ public void setViewModeVisibility(boolean show, final boolean animate) {
896917 _format .getActions ().recreateActionButtons (_textActionsBar , show ? ActionButtonBase .ActionItem .DisplayMode .VIEW : ActionButtonBase .ActionItem .DisplayMode .EDIT );
897918 showHideActionBar ();
898919 if (show ) {
920+ setupWebViewIfNeeded (activity );
899921 updateViewModeText ();
900922 _cu .showSoftKeyboard (activity , false , _hlEditor );
901923 _hlEditor .clearFocus ();
902924 _hlEditor .postDelayed (() -> _cu .showSoftKeyboard (activity , false , _hlEditor ), 300 );
903925 GsContextUtils .fadeInOut (_webView , _verticalScrollView , animate );
904926 } else {
905- _webViewClient .setRestoreScrollY (_webView .getScrollY ());
927+ if (_webView != null ) {
928+ _webViewClient .setRestoreScrollY (_webView .getScrollY ());
929+ }
906930 GsContextUtils .fadeInOut (_verticalScrollView , _webView , animate );
907931 }
908932
@@ -941,10 +965,12 @@ protected boolean onToolbarLongClicked(View v) {
941965
942966 @ Override
943967 public void onDestroy () {
944- try {
945- _webView .loadUrl ("about:blank" );
946- _webView .destroy ();
947- } catch (Exception ignored ) {
968+ if (_webView != null ) {
969+ try {
970+ _webView .loadUrl ("about:blank" );
971+ _webView .destroy ();
972+ } catch (Exception ignored ) {
973+ }
948974 }
949975 super .onDestroy ();
950976 }
0 commit comments