add immersive mode

This commit is contained in:
Tom G 2019-08-17 01:42:51 +02:00 committed by Daniel Micay
parent 83091f8147
commit 5fc3f790a6
2 changed files with 50 additions and 2 deletions

View File

@ -185,6 +185,10 @@ function onRenderPage(lazy) {
} }
} }
function isTextSelected() {
return window.getSelection().toString() !== "";
}
pdfjsLib.getDocument("https://localhost/placeholder.pdf").promise.then(function(newDoc) { pdfjsLib.getDocument("https://localhost/placeholder.pdf").promise.then(function(newDoc) {
pdfDoc = newDoc; pdfDoc = newDoc;
channel.setNumPages(pdfDoc.numPages); channel.setNumPages(pdfDoc.numPages);

View File

@ -8,10 +8,13 @@ import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity; import android.view.Gravity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest; import android.webkit.WebResourceRequest;
@ -28,7 +31,6 @@ import androidx.loader.content.Loader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -138,7 +140,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
} }
@Override @Override
@SuppressLint("SetJavaScriptEnabled") @SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -217,6 +219,31 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
} }
}); });
final GestureDetector detector = new GestureDetector(PdfViewer.this,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
if (mUri != null) {
mWebView.evaluateJavascript("isTextSelected()", selection -> {
if (!Boolean.valueOf(selection)) {
if ((getWindow().getDecorView().getSystemUiVisibility() &
View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
hideSystemUi();
} else {
showSystemUi();
}
}
});
return true;
}
return false;
}
});
mWebView.setOnTouchListener((view, motionEvent) -> {
detector.onTouchEvent(motionEvent);
return false;
});
mTextView = new TextView(this); mTextView = new TextView(this);
mTextView.setBackgroundColor(Color.DKGRAY); mTextView.setBackgroundColor(Color.DKGRAY);
mTextView.setTextColor(ColorStateList.valueOf(Color.WHITE)); mTextView.setTextColor(ColorStateList.valueOf(Color.WHITE));
@ -318,6 +345,23 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
} }
} }
private void showSystemUi() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
private void hideSystemUi() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
@Override @Override
public void onSaveInstanceState(Bundle savedInstanceState) { public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState); super.onSaveInstanceState(savedInstanceState);