44public class SessionView
extends View
46 public static final float MAX_SCALE_FACTOR = 3.0f;
47 public static final float MIN_SCALE_FACTOR = 0.75f;
48 private static final String TAG =
"SessionView";
49 private static final float SCALE_FACTOR_DELTA = 0.0001f;
50 private static final float TOUCH_SCROLL_DELTA = 10.0f;
53 private BitmapDrawable surface;
54 private Stack<Rect> invalidRegions;
55 private int touchPointerPaddingWidth = 0;
56 private int touchPointerPaddingHeight = 0;
59 private boolean railMode =
false;
61 private float scaleFactor = 1.0f;
62 private Matrix scaleMatrix;
63 private Matrix invScaleMatrix;
64 private RectF invalidRegionF;
69 private float notchPxX = 1.0f;
70 private float notchPxY = 1.0f;
71 private float touchpadScrollX = 0;
72 private float touchpadScrollY = 0;
74 private int[] cursorPixels =
null;
75 private int cursorWidth = 0;
76 private int cursorHeight = 0;
77 private int cursorHotX = 0;
78 private int cursorHotY = 0;
82 public SessionView(Context context)
85 initSessionView(context);
88 public SessionView(Context context, AttributeSet attrs)
90 super(context, attrs);
91 initSessionView(context);
94 public SessionView(Context context, AttributeSet attrs,
int defStyle)
96 super(context, attrs, defStyle);
97 initSessionView(context);
100 private void initSessionView(Context context)
104 setFocusableInTouchMode(
true);
106 invalidRegions =
new Stack<>();
107 gestureDetector =
new GestureDetector(context,
new SessionGestureListener(),
null,
true);
108 ViewConfiguration vc = ViewConfiguration.get(context);
110 Math.max(1.0f, ViewConfigurationCompat.getScaledHorizontalScrollFactor(vc, context));
112 Math.max(1.0f, ViewConfigurationCompat.getScaledVerticalScrollFactor(vc, context));
113 doubleGestureDetector =
117 scaleMatrix =
new Matrix();
118 invScaleMatrix =
new Matrix();
119 invalidRegionF =
new RectF();
123 @Override
public boolean onHoverEvent(MotionEvent event)
125 if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE)
128 float x =
event.getX();
129 float y =
event.getY();
131 MotionEvent mappedEvent = mapTouchEvent(event);
132 sessionViewListener.onSessionViewMouseMove((
int)mappedEvent.getX(),
133 (
int)mappedEvent.getY());
134 mappedEvent.recycle();
140 public void setScaleGestureDetector(ScaleGestureDetector scaleGestureDetector)
142 doubleGestureDetector.setScaleGestureDetector(scaleGestureDetector);
147 this.sessionViewListener = sessionViewListener;
150 public void addInvalidRegion(Rect invalidRegion)
153 invalidRegionF.set(invalidRegion);
154 scaleMatrix.mapRect(invalidRegionF);
155 invalidRegionF.roundOut(invalidRegion);
157 invalidRegions.add(invalidRegion);
160 public void invalidateRegion()
162 invalidate(invalidRegions.pop());
167 surface = session.getSurface();
168 Bitmap bitmap = surface.getBitmap();
169 width = bitmap.getWidth();
170 height = bitmap.getHeight();
171 surface.setBounds(0, 0, width, height);
173 setMinimumWidth(width);
174 setMinimumHeight(height);
177 currentSession = session;
180 public float getZoom()
185 public void setRailMode(
boolean rail)
187 if (railMode == rail)
195 void onZoomChanged(
float zoom);
200 zoomChangedListener = l;
203 public void setZoom(
float factor)
205 scaleFactor = factor;
206 scaleMatrix.setScale(scaleFactor, scaleFactor);
207 invScaleMatrix.setScale(1.0f / scaleFactor, 1.0f / scaleFactor);
209 if (cursorPixels !=
null)
212 if (zoomChangedListener !=
null)
213 zoomChangedListener.onZoomChanged(scaleFactor);
218 public boolean isAtMaxZoom()
220 return (scaleFactor > (MAX_SCALE_FACTOR - SCALE_FACTOR_DELTA));
223 public boolean isAtMinZoom()
225 return (scaleFactor < (MIN_SCALE_FACTOR + SCALE_FACTOR_DELTA));
228 public boolean zoomIn(
float factor)
231 scaleFactor += factor;
232 if (scaleFactor > (MAX_SCALE_FACTOR - SCALE_FACTOR_DELTA))
234 scaleFactor = MAX_SCALE_FACTOR;
237 setZoom(scaleFactor);
241 public boolean zoomOut(
float factor)
244 scaleFactor -= factor;
245 if (scaleFactor < (MIN_SCALE_FACTOR + SCALE_FACTOR_DELTA))
247 scaleFactor = MIN_SCALE_FACTOR;
250 setZoom(scaleFactor);
254 public void setTouchPointerPadding(
int width,
int height)
256 touchPointerPaddingWidth = width;
257 touchPointerPaddingHeight = height;
261 public int getTouchPointerPaddingWidth()
263 return touchPointerPaddingWidth;
266 public int getTouchPointerPaddingHeight()
268 return touchPointerPaddingHeight;
271 @Override
public void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec)
273 Log.v(TAG, width +
"x" + height);
274 this.setMeasuredDimension((
int)(width * scaleFactor) + touchPointerPaddingWidth,
275 (
int)(height * scaleFactor) + touchPointerPaddingHeight);
278 @Override
public void onDraw(@NonNull Canvas canvas)
280 super.onDraw(canvas);
283 canvas.concat(scaleMatrix);
284 canvas.drawColor(Color.BLACK);
285 if (!railMode && surface !=
null)
287 surface.draw(canvas);
293 private MotionEvent mapTouchEvent(MotionEvent event)
295 MotionEvent mappedEvent = MotionEvent.obtain(event);
296 float[] coordinates = { mappedEvent.getX(), mappedEvent.getY() };
297 invScaleMatrix.mapPoints(coordinates);
298 mappedEvent.setLocation(coordinates[0], coordinates[1]);
303 private MotionEvent mapDoubleTouchEvent(MotionEvent event)
305 MotionEvent mappedEvent = MotionEvent.obtain(event);
306 float[] coordinates = { (mappedEvent.getX(0) + mappedEvent.getX(1)) / 2,
307 (mappedEvent.getY(0) + mappedEvent.getY(1)) / 2 };
308 invScaleMatrix.mapPoints(coordinates);
309 mappedEvent.setLocation(coordinates[0], coordinates[1]);
313 @Override
public boolean onTouchEvent(MotionEvent event)
318 if (event.isFromSource(InputDevice.SOURCE_MOUSE))
320 int action =
event.getActionMasked();
322 if (action == MotionEvent.ACTION_SCROLL)
323 return onGenericMotionEvent(event);
324 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
325 event.getClassification() == MotionEvent.CLASSIFICATION_TWO_FINGER_SWIPE)
327 handleTouchpadScroll(event);
330 if (action == MotionEvent.ACTION_MOVE && event.getButtonState() != 0)
332 MotionEvent mapped = mapTouchEvent(event);
333 sessionViewListener.onSessionViewMouseMove((
int)mapped.getX(), (
int)mapped.getY());
340 boolean res = gestureDetector.onTouchEvent(event);
341 res |= doubleGestureDetector.onTouchEvent(event);
346 private void handleTouchpadScroll(MotionEvent event)
348 if (event.getActionMasked() != MotionEvent.ACTION_MOVE)
355 touchpadScrollY += gestureDistance(event, MotionEvent.AXIS_GESTURE_SCROLL_Y_DISTANCE);
356 while (Math.abs(touchpadScrollY) >= notchPxY)
358 final boolean down = touchpadScrollY > 0;
359 touchpadScrollY += down ? -notchPxY : notchPxY;
360 sessionViewListener.onSessionViewScroll(down);
363 touchpadScrollX += gestureDistance(event, MotionEvent.AXIS_GESTURE_SCROLL_X_DISTANCE);
364 while (Math.abs(touchpadScrollX) >= notchPxX)
366 final boolean right = touchpadScrollX > 0;
367 touchpadScrollX += right ? -notchPxX : notchPxX;
368 sessionViewListener.onSessionViewHScroll(right);
373 private static float gestureDistance(MotionEvent event,
int axis)
376 for (
int i = 0; i <
event.getHistorySize(); i++)
377 distance += event.getHistoricalAxisValue(axis, 0, i);
378 return distance +
event.getAxisValue(axis);
382 @Override
public boolean onGenericMotionEvent(MotionEvent event)
384 final boolean isPointer =
event.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
388 final boolean isMouse =
event.isFromSource(InputDevice.SOURCE_MOUSE);
389 int action =
event.getActionMasked();
391 if (isMouse && (action == MotionEvent.ACTION_BUTTON_PRESS ||
392 action == MotionEvent.ACTION_BUTTON_RELEASE))
394 boolean down = action == MotionEvent.ACTION_BUTTON_PRESS;
395 MotionEvent mapped = mapTouchEvent(event);
396 int x = (int)mapped.getX();
397 int y = (int)mapped.getY();
400 switch (event.getActionButton())
402 case MotionEvent.BUTTON_PRIMARY:
404 sessionViewListener.onSessionViewBeginTouch();
405 sessionViewListener.onSessionViewLeftTouch(x, y, down);
407 sessionViewListener.onSessionViewEndTouch();
409 case MotionEvent.BUTTON_SECONDARY:
411 sessionViewListener.onSessionViewBeginTouch();
412 sessionViewListener.onSessionViewRightTouch(x, y, down);
414 case MotionEvent.BUTTON_TERTIARY:
415 sessionViewListener.onSessionViewMiddleTouch(x, y, down);
422 if (action == MotionEvent.ACTION_SCROLL)
424 float vScroll =
event.getAxisValue(MotionEvent.AXIS_VSCROLL);
425 float hScroll =
event.getAxisValue(MotionEvent.AXIS_HSCROLL);
427 sessionViewListener.onSessionViewScroll(vScroll > 0);
429 sessionViewListener.onSessionViewHScroll(hScroll > 0);
438 void onSessionViewBeginTouch();
440 void onSessionViewEndTouch();
442 void onSessionViewLeftTouch(
int x,
int y,
boolean down);
444 void onSessionViewMiddleTouch(
int x,
int y,
boolean down);
446 void onSessionViewRightTouch(
int x,
int y,
boolean down);
448 void onSessionViewMove(
int x,
int y);
450 void onSessionViewMouseMove(
int x,
int y);
452 void onSessionViewScroll(
boolean down);
454 void onSessionViewHScroll(
boolean right);
457 public void setRemoteCursor(
int[] pixels,
int width,
int height,
int hotX,
int hotY)
459 if (pixels ==
null || width == 0 || height == 0)
462 setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_NULL));
465 cursorPixels = pixels;
467 cursorHeight = height;
473 private void applyScaledCursor()
475 int scaledWidth = Math.max(1, (
int)(cursorWidth * scaleFactor));
476 int scaledHeight = Math.max(1, (
int)(cursorHeight * scaleFactor));
478 Bitmap.createBitmap(cursorPixels, cursorWidth, cursorHeight, Bitmap.Config.ARGB_8888);
479 Bitmap scaled = Bitmap.createScaledBitmap(bm, scaledWidth, scaledHeight,
true);
481 PointerIcon.create(scaled, cursorHotX * scaleFactor, cursorHotY * scaleFactor);
482 setPointerIcon(icon);
485 public void setDefaultCursor()
487 setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_ARROW));
490 private class SessionGestureListener
extends GestureDetector.SimpleOnGestureListener
492 boolean longPressInProgress =
false;
494 public boolean onDown(MotionEvent e)
499 public boolean onUp(MotionEvent e)
501 sessionViewListener.onSessionViewEndTouch();
505 public void onLongPress(MotionEvent e)
507 MotionEvent mappedEvent = mapTouchEvent(e);
508 sessionViewListener.onSessionViewBeginTouch();
509 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
510 (
int)mappedEvent.getY(),
true);
511 longPressInProgress =
true;
514 public void onLongPressUp(MotionEvent e)
516 MotionEvent mappedEvent = mapTouchEvent(e);
517 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
518 (
int)mappedEvent.getY(),
false);
519 longPressInProgress =
false;
520 sessionViewListener.onSessionViewEndTouch();
523 public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX,
float distanceY)
525 if (longPressInProgress)
527 MotionEvent mappedEvent = mapTouchEvent(e2);
528 sessionViewListener.onSessionViewMove((
int)mappedEvent.getX(),
529 (
int)mappedEvent.getY());
536 public boolean onDoubleTap(MotionEvent e)
539 MotionEvent mappedEvent = mapTouchEvent(e);
540 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
541 (
int)mappedEvent.getY(),
true);
542 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
543 (
int)mappedEvent.getY(),
false);
547 public boolean onSingleTapUp(MotionEvent e)
551 if (e.getButtonState() != 0)
555 MotionEvent mappedEvent = mapTouchEvent(e);
556 sessionViewListener.onSessionViewBeginTouch();
557 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
558 (
int)mappedEvent.getY(),
true);
559 sessionViewListener.onSessionViewLeftTouch((
int)mappedEvent.getX(),
560 (
int)mappedEvent.getY(),
false);
561 sessionViewListener.onSessionViewEndTouch();
566 private class SessionDoubleGestureListener
567 implements DoubleGestureDetector.OnDoubleGestureListener
569 private MotionEvent prevEvent =
null;
571 public boolean onDoubleTouchDown(MotionEvent e)
573 sessionViewListener.onSessionViewBeginTouch();
574 prevEvent = MotionEvent.obtain(e);
578 public boolean onDoubleTouchUp(MotionEvent e)
580 if (prevEvent !=
null)
585 sessionViewListener.onSessionViewEndTouch();
589 public boolean onDoubleTouchScroll(MotionEvent e1, MotionEvent e2)
592 float deltaY = e2.getY() - prevEvent.getY();
593 if (deltaY > TOUCH_SCROLL_DELTA)
595 sessionViewListener.onSessionViewScroll(
true);
597 prevEvent = MotionEvent.obtain(e2);
599 else if (deltaY < -TOUCH_SCROLL_DELTA)
601 sessionViewListener.onSessionViewScroll(
false);
603 prevEvent = MotionEvent.obtain(e2);
608 public boolean onDoubleTouchSingleTap(MotionEvent e)
611 MotionEvent mappedEvent = mapDoubleTouchEvent(e);
612 sessionViewListener.onSessionViewRightTouch((
int)mappedEvent.getX(),
613 (
int)mappedEvent.getY(),
true);
614 sessionViewListener.onSessionViewRightTouch((
int)mappedEvent.getX(),
615 (
int)mappedEvent.getY(),
false);
620 @Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
622 outAttrs.actionLabel =
null;
623 outAttrs.inputType = InputType.TYPE_NULL;
624 outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI |
625 EditorInfo.IME_FLAG_NO_FULLSCREEN;
626 return new BaseInputConnection(
this,
false);