1 package org.libreoffice.canvas;
2 
3 import android.graphics.Bitmap;
4 import android.graphics.PointF;
5 
6 import org.libreoffice.LOKitShell;
7 import org.libreoffice.LibreOfficeMainActivity;
8 import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
9 
10 /**
11  * Selection handle is a common class for "start", "middle" and "end" types
12  * of selection handles.
13  */
14 public abstract class SelectionHandle extends BitmapHandle {
15     private static final long MINIMUM_HANDLE_UPDATE_TIME = 50 * 1000000;
16 
17     private final PointF mDragStartPoint = new PointF();
18     private final PointF mDragDocumentPosition = new PointF();
19     private long mLastTime = 0;
20 
21     private LibreOfficeMainActivity mContext;
22 
SelectionHandle(LibreOfficeMainActivity context, Bitmap bitmap)23     public SelectionHandle(LibreOfficeMainActivity context, Bitmap bitmap) {
24         super(bitmap);
25         mContext = context;
26     }
27 
28     /**
29      * Start of a touch and drag action on the handle.
30      */
dragStart(PointF point)31     public void dragStart(PointF point) {
32         mDragStartPoint.x = point.x;
33         mDragStartPoint.y = point.y;
34         mDragDocumentPosition.x = mDocumentPosition.left;
35         mDragDocumentPosition.y = mDocumentPosition.top;
36     }
37 
38     /**
39      * End of a touch and drag action on the handle.
40      */
dragEnd(PointF point)41     public void dragEnd(PointF point) {
42     }
43 
44     /**
45      * Handle has been dragged.
46      */
dragging(PointF point)47     public void dragging(PointF point) {
48         long currentTime = System.nanoTime();
49         if (currentTime - mLastTime > MINIMUM_HANDLE_UPDATE_TIME) {
50             mLastTime = currentTime;
51             signalHandleMove(point.x, point.y);
52         }
53     }
54 
55     /**
56      * Signal to move the handle to a new position to LO.
57      */
signalHandleMove(float newX, float newY)58     private void signalHandleMove(float newX, float newY) {
59         ImmutableViewportMetrics viewportMetrics = mContext.getLayerClient().getViewportMetrics();
60         float zoom = viewportMetrics.zoomFactor;
61 
62         float deltaX = (newX - mDragStartPoint.x) / zoom;
63         float deltaY = (newY - mDragStartPoint.y) / zoom;
64 
65         PointF documentPoint = new PointF(mDragDocumentPosition.x + deltaX, mDragDocumentPosition.y + deltaY);
66 
67         LOKitShell.sendChangeHandlePositionEvent(getHandleType(), documentPoint);
68     }
69 
getHandleType()70     public abstract HandleType getHandleType();
71 
72     public enum HandleType { START, MIDDLE, END }
73 }
74