Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / libreoffice / overlay / DocumentOverlay.java
blobf977866a282e54ba46d6d70b98174beac643c395
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 package org.libreoffice.overlay;
11 import android.graphics.RectF;
12 import android.util.Log;
14 import org.libreoffice.LOKitShell;
15 import org.libreoffice.LibreOfficeMainActivity;
16 import org.libreoffice.R;
17 import org.libreoffice.canvas.SelectionHandle;
18 import org.mozilla.gecko.gfx.Layer;
19 import org.mozilla.gecko.gfx.LayerView;
20 import org.mozilla.gecko.util.FloatUtils;
22 import java.util.List;
24 /**
25 * The DocumentOverlay is an overlay over the document. This class is responsible
26 * to setup the document overlay view, report visibility and position of its elements
27 * when they change and report any changes to the viewport.
29 public class DocumentOverlay {
30 private static final String LOGTAG = DocumentOverlay.class.getSimpleName();
32 private final DocumentOverlayView mDocumentOverlayView;
33 private final DocumentOverlayLayer mDocumentOverlayLayer;
35 private final long hidePageNumberRectDelayInMilliseconds = 500;
37 /**
38 * DocumentOverlayLayer responsibility is to get the changes to the viewport
39 * and report them to DocumentOverlayView.
41 private class DocumentOverlayLayer extends Layer {
42 private float mViewLeft;
43 private float mViewTop;
44 private float mViewZoom;
46 /**
47 * @see Layer#draw(org.mozilla.gecko.gfx.Layer.RenderContext)
49 @Override
50 public void draw(final RenderContext context) {
51 if (FloatUtils.fuzzyEquals(mViewLeft, context.viewport.left)
52 && FloatUtils.fuzzyEquals(mViewTop, context.viewport.top)
53 && FloatUtils.fuzzyEquals(mViewZoom, context.zoomFactor)) {
54 return;
57 mViewLeft = context.viewport.left;
58 mViewTop = context.viewport.top;
59 mViewZoom = context.zoomFactor;
61 LOKitShell.getMainHandler().post(new Runnable() {
62 public void run() {
63 mDocumentOverlayView.repositionWithViewport(mViewLeft, mViewTop, mViewZoom);
65 });
69 public DocumentOverlay(LibreOfficeMainActivity context, LayerView layerView) {
70 mDocumentOverlayView = context.findViewById(R.id.text_cursor_view);
71 mDocumentOverlayLayer = new DocumentOverlayLayer();
72 if (mDocumentOverlayView == null) {
73 Log.e(LOGTAG, "Failed to initialize TextCursorLayer - CursorView is null");
75 layerView.addLayer(mDocumentOverlayLayer);
76 mDocumentOverlayView.initialize(layerView);
79 public void setPartPageRectangles(List<RectF> rectangles) {
80 mDocumentOverlayView.setPartPageRectangles(rectangles);
83 /**
84 * Show the cursor at the defined cursor position on the overlay.
86 public void showCursor() {
87 LOKitShell.getMainHandler().post(new Runnable() {
88 public void run() {
89 mDocumentOverlayView.showCursor();
91 });
94 /**
95 * Hide the cursor at the defined cursor position on the overlay.
97 public void hideCursor() {
98 LOKitShell.getMainHandler().post(new Runnable() {
99 public void run() {
100 mDocumentOverlayView.hideCursor();
106 * Show the page number rectangle on the overlay.
108 public void showPageNumberRect() {
109 LOKitShell.getMainHandler().post(new Runnable() {
110 public void run() {
111 mDocumentOverlayView.showPageNumberRect();
117 * Hide the page number rectangle on the overlay.
119 public void hidePageNumberRect() {
120 LOKitShell.getMainHandler().postDelayed(new Runnable() {
121 public void run() {
122 mDocumentOverlayView.hidePageNumberRect();
124 }, hidePageNumberRectDelayInMilliseconds);
128 * Position the cursor to the input position on the overlay.
130 public void positionCursor(final RectF position) {
131 LOKitShell.getMainHandler().post(new Runnable() {
132 public void run() {
133 mDocumentOverlayView.changeCursorPosition(position);
139 * Show selections on the overlay.
141 public void showSelections() {
142 LOKitShell.getMainHandler().post(new Runnable() {
143 public void run() {
144 mDocumentOverlayView.showSelections();
150 * Hide selections on the overlay.
152 public void hideSelections() {
153 LOKitShell.getMainHandler().post(new Runnable() {
154 public void run() {
155 mDocumentOverlayView.hideSelections();
161 * Change the list of selections.
163 public void changeSelections(final List<RectF> selections) {
164 LOKitShell.getMainHandler().post(new Runnable() {
165 public void run() {
166 mDocumentOverlayView.changeSelections(selections);
172 * Show the graphic selection on the overlay.
174 public void showGraphicSelection() {
175 LOKitShell.getMainHandler().post(new Runnable() {
176 public void run() {
177 mDocumentOverlayView.showGraphicSelection();
183 * Hide the graphic selection.
185 public void hideGraphicSelection() {
186 LOKitShell.getMainHandler().post(new Runnable() {
187 public void run() {
188 mDocumentOverlayView.hideGraphicSelection();
194 * Change the graphic selection rectangle to the input rectangle.
196 public void changeGraphicSelection(final RectF rectangle) {
197 LOKitShell.getMainHandler().post(new Runnable() {
198 public void run() {
199 mDocumentOverlayView.changeGraphicSelection(rectangle);
205 * Show the handle (of input type) on the overlay.
207 public void showHandle(final SelectionHandle.HandleType type) {
208 LOKitShell.getMainHandler().post(new Runnable() {
209 public void run() {
210 mDocumentOverlayView.showHandle(type);
216 * Hide the handle (of input type).
218 public void hideHandle(final SelectionHandle.HandleType type) {
219 LOKitShell.getMainHandler().post(new Runnable() {
220 public void run() {
221 mDocumentOverlayView.hideHandle(type);
227 * Position the handle (of input type) position to the input rectangle.
229 public void positionHandle(final SelectionHandle.HandleType type, final RectF rectangle) {
230 LOKitShell.getMainHandler().post(new Runnable() {
231 public void run() {
232 mDocumentOverlayView.positionHandle(type, rectangle);
237 public RectF getCurrentCursorPosition() {
238 return mDocumentOverlayView.getCurrentCursorPosition();
241 public void setCalcHeadersController(CalcHeadersController calcHeadersController) {
242 mDocumentOverlayView.setCalcHeadersController(calcHeadersController);
245 public void showCellSelection(final RectF cellCursorRect) {
246 LOKitShell.getMainHandler().post(new Runnable() {
247 public void run() {
248 mDocumentOverlayView.showCellSelection(cellCursorRect);
253 public void showHeaderSelection(final RectF cellCursorRect) {
254 LOKitShell.getMainHandler().post(new Runnable() {
255 public void run() {
256 mDocumentOverlayView.showHeaderSelection(cellCursorRect);
261 public void showAdjustLengthLine(final boolean isRow, final CalcHeadersView view) {
262 LOKitShell.getMainHandler().post(new Runnable() {
263 @Override
264 public void run() {
265 mDocumentOverlayView.showAdjustLengthLine(isRow, view);
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */