Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / libreoffice / PresentationActivity.java
blobede7c0c401013afd7922c76b93b5a922303aad49
1 package org.libreoffice;
3 import android.content.Intent;
4 import android.os.Bundle;
5 import androidx.annotation.Nullable;
6 import androidx.core.view.GestureDetectorCompat;
7 import androidx.appcompat.app.AppCompatActivity;
8 import android.view.GestureDetector;
9 import android.view.KeyEvent;
10 import android.view.MotionEvent;
11 import android.view.View;
12 import android.webkit.WebView;
13 import android.widget.Button;
14 import android.widget.ImageButton;
16 public class PresentationActivity extends AppCompatActivity {
18 private static final String LOGTAG = PresentationActivity.class.getSimpleName();
19 WebView mWebView;
20 View mGestureView;
22 @Override
23 protected void onCreate(@Nullable Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
26 View decorView = getWindow().getDecorView();
27 // Hide the status bar.
28 int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
29 decorView.setSystemUiVisibility(uiOptions);
31 setContentView(R.layout.presentation_mode);
33 // get intent and url
34 Intent intent = getIntent();
35 String filePath = intent.getDataString();
37 // set up WebView
38 mWebView = findViewById(R.id.presentation_view);
39 mWebView.getSettings().setJavaScriptEnabled(true);
40 mWebView.setOnTouchListener(new View.OnTouchListener() {
41 @Override
42 public boolean onTouch(View v, MotionEvent event) {
43 return true;
45 });
47 // set up buttons within presentation_gesture_view
48 ImageButton prevButton = findViewById(R.id.slide_show_nav_prev);
49 ImageButton nextButton = findViewById(R.id.slide_show_nav_next);
50 Button backButton = findViewById(R.id.slide_show_nav_back);
52 prevButton.setOnClickListener(new View.OnClickListener() {
53 @Override
54 public void onClick(View v) {
55 pageLeft();
57 });
58 nextButton.setOnClickListener(new View.OnClickListener() {
59 @Override
60 public void onClick(View v) {
61 pageRight();
63 });
64 backButton.setOnClickListener(new View.OnClickListener() {
65 @Override
66 public void onClick(View v) {
67 onBackPressed();
69 });
71 // set up presentation_gesture_view
72 mGestureView = findViewById(R.id.presentation_gesture_view);
73 final GestureDetectorCompat gestureDetector =
74 new GestureDetectorCompat(this, new PresentationGestureViewListener());
75 mGestureView.setOnTouchListener(new View.OnTouchListener() {
76 @Override
77 public boolean onTouch(View v, MotionEvent event) {
78 return gestureDetector.onTouchEvent(event);
80 });
82 // load url
83 mWebView.loadUrl(filePath);
86 private class PresentationGestureViewListener extends GestureDetector.SimpleOnGestureListener {
87 private static final int SWIPE_VELOCITY_THRESHOLD = 100;
88 private static final int SCROLL_THRESHOLD = 10; // if scrollCounter is larger than this, a page switch is triggered
89 private int scrollCounter = 0; // a counter for measuring scrolling distance
91 @Override
92 public boolean onDown(MotionEvent e) {
93 return true;
96 @Override
97 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
98 boolean result = false;
99 try {
100 float diffY = e2.getY() - e1.getY();
101 float diffX = e2.getX() - e1.getX();
102 if (Math.abs(diffX) > Math.abs(diffY)) {
103 if (Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
104 if (diffX > 0) {
105 pageRight();
106 } else {
107 pageLeft();
109 result = true;
112 } catch (Exception exception) {
113 exception.printStackTrace();
115 return result;
118 @Override
119 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
120 boolean result = false;
121 try {
122 float diffY = e2.getY() - e1.getY();
123 float diffX = e2.getX() - e1.getX();
124 if (Math.abs(diffX) < Math.abs(diffY)) {
125 if (distanceY > 0) {
126 scrollCounter++;
127 if (scrollCounter >= SCROLL_THRESHOLD) {
128 pageRight();
129 scrollCounter = 0;
131 } else {
132 scrollCounter--;
133 if (scrollCounter <= -SCROLL_THRESHOLD) {
134 pageLeft();
135 scrollCounter = 0;
138 result = true;
140 } catch (Exception exception) {
141 exception.printStackTrace();
143 return result;
146 @Override
147 public boolean onSingleTapUp(MotionEvent e) {
148 if (e.getX() < mGestureView.getWidth()/3) {
149 pageLeft();
150 } else if (e.getX() < mGestureView.getWidth()*2/3) {
151 hideControlButtons();
152 } else {
153 pageRight();
155 return true;
159 private void hideControlButtons() {
160 View[] views= {findViewById(R.id.slide_show_nav_prev),findViewById(R.id.slide_show_nav_next),findViewById(R.id.slide_show_nav_back)} ;
161 for (View view : views) {
162 if (view.getVisibility() == View.GONE) {
163 view.setVisibility(View.VISIBLE);
164 } else if (view.getVisibility() == View.VISIBLE) {
165 view.setVisibility(View.GONE);
170 private void pageLeft() {
171 mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
174 private void pageRight() {
175 mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));