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 ;
@@ -648,7 +632,9 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
648632 final int current = _isPreviewVisible ? _appSettings .getDocumentViewFontSize (_document .path ) : _appSettings .getDocumentFontSize (_document .path );
649633 MarkorDialogFactory .showFontSizeDialog (activity , current , (newSize ) -> {
650634 if (_isPreviewVisible ) {
651- _webView .getSettings ().setTextZoom ((int ) (newSize * VIEW_FONT_SCALE ));
635+ if (_webView != null ) {
636+ _webView .getSettings ().setTextZoom ((int ) (newSize * VIEW_FONT_SCALE ));
637+ }
652638 _appSettings .setDocumentViewFontSize (_document .path , newSize );
653639 } else {
654640 _hlEditor .setTextSize (TypedValue .COMPLEX_UNIT_SP , (float ) newSize );
@@ -667,7 +653,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
667653 }
668654 }
669655 }
670-
671656 public void checkTextChangeState () {
672657 final boolean isTextChanged = !_document .isContentSame (_hlEditor .getText ());
673658 Drawable d ;
@@ -703,12 +688,14 @@ private void showHideActionBar() {
703688 final View parent = activity .findViewById (R .id .document__fragment__edit__text_actions_bar__scrolling_parent );
704689 final View viewScroll = activity .findViewById (R .id .document__fragment_view_webview );
705690
706- if (bar != null && parent != null && _verticalScrollView != null && viewScroll != null ) {
691+ if (bar != null && parent != null && _verticalScrollView != null ) {
707692 final boolean hide = _textActionsBar .getChildCount () == 0 ;
708693 parent .setVisibility (hide ? View .GONE : View .VISIBLE );
709694 final int marginBottom = hide ? 0 : (int ) getResources ().getDimension (R .dimen .textactions_bar_height );
710695 setMarginBottom (_verticalScrollView , marginBottom );
711- setMarginBottom (viewScroll , marginBottom );
696+ if (viewScroll != null ) {
697+ setMarginBottom (viewScroll , marginBottom );
698+ }
712699 }
713700 }
714701 }
@@ -865,6 +852,9 @@ private boolean isDisplayedAtMainActivity() {
865852 }
866853
867854 public void updateViewModeText () {
855+ if (_webView == null ) {
856+ return ;
857+ }
868858 // Don't let text to view mode crash app
869859 try {
870860 _format .getConverter ().convertMarkupShowInWebView (_document , getTextString (), getActivity (), _webView , _nextConvertToPrintMode , _lineNumbersView .isLineNumbersEnabled ());
@@ -877,6 +867,37 @@ public void setViewModeVisibility(final boolean show) {
877867 setViewModeVisibility (show , true );
878868 }
879869
870+ private void setupWebViewIfNeeded (final Activity activity ) {
871+ if (_webView == null ) {
872+ _webView = (WebView ) _webViewStub .inflate ();
873+ _webView .setWebChromeClient (new GsWebViewChromeClient (_webView , activity , activity .findViewById (R .id .document__fragment_fullscreen_overlay )));
874+ _webView .addJavascriptInterface (this , "Android" );
875+ _webView .setBackgroundColor (Color .TRANSPARENT );
876+ WebSettings webSettings = _webView .getSettings ();
877+ webSettings .setBuiltInZoomControls (true );
878+ webSettings .setDisplayZoomControls (false );
879+ webSettings .setTextZoom ((int ) (_appSettings .getDocumentViewFontSize (_document .path ) * VIEW_FONT_SCALE ));
880+ webSettings .setCacheMode (WebSettings .LOAD_CACHE_ELSE_NETWORK );
881+ webSettings .setDatabaseEnabled (true );
882+ webSettings .setGeolocationEnabled (false );
883+ webSettings .setJavaScriptEnabled (true );
884+ webSettings .setDomStorageEnabled (true );
885+ webSettings .setAllowFileAccess (true );
886+ webSettings .setAllowContentAccess (true );
887+ webSettings .setAllowFileAccessFromFileURLs (true );
888+ webSettings .setAllowUniversalAccessFromFileURLs (false );
889+ webSettings .setMediaPlaybackRequiresUserGesture (false );
890+
891+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && BuildConfig .IS_TEST_BUILD && BuildConfig .DEBUG ) {
892+ WebView .setWebContentsDebuggingEnabled (true ); // Inspect on computer chromium browser: chrome://inspect/#devices
893+ }
894+
895+ _webViewClient = new MarkorWebViewClient (_webView , activity );
896+ _webView .setWebViewClient (_webViewClient );
897+ }
898+ }
899+
900+ @ SuppressLint ({"AddJavascriptInterface" , "SetJavaScriptEnabled" })
880901 public void setViewModeVisibility (boolean show , final boolean animate ) {
881902 final Activity activity = getActivity ();
882903 if (activity == null ) {
@@ -887,13 +908,16 @@ public void setViewModeVisibility(boolean show, final boolean animate) {
887908 _format .getActions ().recreateActionButtons (_textActionsBar , show ? ActionButtonBase .ActionItem .DisplayMode .VIEW : ActionButtonBase .ActionItem .DisplayMode .EDIT );
888909 showHideActionBar ();
889910 if (show ) {
911+ setupWebViewIfNeeded (activity );
890912 updateViewModeText ();
891913 _cu .showSoftKeyboard (activity , false , _hlEditor );
892914 _hlEditor .clearFocus ();
893915 _hlEditor .postDelayed (() -> _cu .showSoftKeyboard (activity , false , _hlEditor ), 300 );
894916 GsContextUtils .fadeInOut (_webView , _verticalScrollView , animate );
895917 } else {
896- _webViewClient .setRestoreScrollY (_webView .getScrollY ());
918+ if (_webView != null ) {
919+ _webViewClient .setRestoreScrollY (_webView .getScrollY ());
920+ }
897921 GsContextUtils .fadeInOut (_verticalScrollView , _webView , animate );
898922 }
899923
@@ -932,10 +956,12 @@ protected boolean onToolbarLongClicked(View v) {
932956
933957 @ Override
934958 public void onDestroy () {
935- try {
936- _webView .loadUrl ("about:blank" );
937- _webView .destroy ();
938- } catch (Exception ignored ) {
959+ if (_webView != null ) {
960+ try {
961+ _webView .loadUrl ("about:blank" );
962+ _webView .destroy ();
963+ } catch (Exception ignored ) {
964+ }
939965 }
940966 super .onDestroy ();
941967 }
0 commit comments