FreeRDP
Loading...
Searching...
No Matches
SessionView.java
1/*
2 Android Session view
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.content.Context;
14import android.graphics.Bitmap;
15import android.graphics.Canvas;
16import android.graphics.Color;
17import android.graphics.Matrix;
18import android.graphics.Rect;
19import android.graphics.RectF;
20import android.graphics.drawable.BitmapDrawable;
21import android.os.Build;
22import android.text.InputType;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.InputDevice;
26import android.view.MotionEvent;
27import android.view.PointerIcon;
28import android.view.ScaleGestureDetector;
29import android.view.View;
30import android.view.ViewConfiguration;
31import android.view.inputmethod.BaseInputConnection;
32import android.view.inputmethod.EditorInfo;
33import android.view.inputmethod.InputConnection;
34
35import androidx.annotation.NonNull;
36import androidx.core.view.ViewConfigurationCompat;
37
38import com.freerdp.freerdpcore.application.SessionState;
39import com.freerdp.freerdpcore.utils.DoubleGestureDetector;
40import com.freerdp.freerdpcore.utils.GestureDetector;
41
42import java.util.Stack;
43
44public class SessionView extends View
45{
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;
51 private int width;
52 private int height;
53 private BitmapDrawable surface;
54 private Stack<Rect> invalidRegions;
55 private int touchPointerPaddingWidth = 0;
56 private int touchPointerPaddingHeight = 0;
57 private SessionViewListener sessionViewListener = null;
58 private OnZoomChangedListener zoomChangedListener = null;
59 private boolean railMode = false;
60 // helpers for scaling gesture handling
61 private float scaleFactor = 1.0f;
62 private Matrix scaleMatrix;
63 private Matrix invScaleMatrix;
64 private RectF invalidRegionF;
65 private GestureDetector gestureDetector;
66 private SessionState currentSession;
67
68 // Pixels of touchpad travel per wheel notch, and leftover between events.
69 private float notchPxX = 1.0f;
70 private float notchPxY = 1.0f;
71 private float touchpadScrollX = 0;
72 private float touchpadScrollY = 0;
73
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;
79
80 // private static final String TAG = "FreeRDP.SessionView";
81 private DoubleGestureDetector doubleGestureDetector;
82 public SessionView(Context context)
83 {
84 super(context);
85 initSessionView(context);
86 }
87
88 public SessionView(Context context, AttributeSet attrs)
89 {
90 super(context, attrs);
91 initSessionView(context);
92 }
93
94 public SessionView(Context context, AttributeSet attrs, int defStyle)
95 {
96 super(context, attrs, defStyle);
97 initSessionView(context);
98 }
99
100 private void initSessionView(Context context)
101 {
102 // Ensure the view is focusable so the soft keyboard can attach to it (required for API 30+)
103 setFocusable(true);
104 setFocusableInTouchMode(true);
105
106 invalidRegions = new Stack<>();
107 gestureDetector = new GestureDetector(context, new SessionGestureListener(), null, true);
108 ViewConfiguration vc = ViewConfiguration.get(context);
109 notchPxX =
110 Math.max(1.0f, ViewConfigurationCompat.getScaledHorizontalScrollFactor(vc, context));
111 notchPxY =
112 Math.max(1.0f, ViewConfigurationCompat.getScaledVerticalScrollFactor(vc, context));
113 doubleGestureDetector =
114 new DoubleGestureDetector(context, null, new SessionDoubleGestureListener());
115
116 scaleFactor = 1.0f;
117 scaleMatrix = new Matrix();
118 invScaleMatrix = new Matrix();
119 invalidRegionF = new RectF();
120 }
121
122 /* External Mouse Hover */
123 @Override public boolean onHoverEvent(MotionEvent event)
124 {
125 if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE)
126 {
127 // Handle hover move event
128 float x = event.getX();
129 float y = event.getY();
130 // Perform actions based on the hover position (x, y)
131 MotionEvent mappedEvent = mapTouchEvent(event);
132 sessionViewListener.onSessionViewMouseMove((int)mappedEvent.getX(),
133 (int)mappedEvent.getY());
134 mappedEvent.recycle();
135 }
136 // Return true to indicate that you've handled the event
137 return true;
138 }
139
140 public void setScaleGestureDetector(ScaleGestureDetector scaleGestureDetector)
141 {
142 doubleGestureDetector.setScaleGestureDetector(scaleGestureDetector);
143 }
144
145 public void setSessionViewListener(SessionViewListener sessionViewListener)
146 {
147 this.sessionViewListener = sessionViewListener;
148 }
149
150 public void addInvalidRegion(Rect invalidRegion)
151 {
152 // correctly transform invalid region depending on current scaling
153 invalidRegionF.set(invalidRegion);
154 scaleMatrix.mapRect(invalidRegionF);
155 invalidRegionF.roundOut(invalidRegion);
156
157 invalidRegions.add(invalidRegion);
158 }
159
160 public void invalidateRegion()
161 {
162 invalidate(invalidRegions.pop());
163 }
164
165 public void onSurfaceChange(SessionState session)
166 {
167 surface = session.getSurface();
168 Bitmap bitmap = surface.getBitmap();
169 width = bitmap.getWidth();
170 height = bitmap.getHeight();
171 surface.setBounds(0, 0, width, height);
172
173 setMinimumWidth(width);
174 setMinimumHeight(height);
175
176 requestLayout();
177 currentSession = session;
178 }
179
180 public float getZoom()
181 {
182 return scaleFactor;
183 }
184
185 public void setRailMode(boolean rail)
186 {
187 if (railMode == rail)
188 return;
189 railMode = rail;
190 invalidate();
191 }
192
193 public interface OnZoomChangedListener
194 {
195 void onZoomChanged(float zoom);
196 }
197
198 public void setOnZoomChangedListener(OnZoomChangedListener l)
199 {
200 zoomChangedListener = l;
201 }
202
203 public void setZoom(float factor)
204 {
205 scaleFactor = factor;
206 scaleMatrix.setScale(scaleFactor, scaleFactor);
207 invScaleMatrix.setScale(1.0f / scaleFactor, 1.0f / scaleFactor);
208
209 if (cursorPixels != null)
210 applyScaledCursor();
211
212 if (zoomChangedListener != null)
213 zoomChangedListener.onZoomChanged(scaleFactor);
214
215 requestLayout();
216 }
217
218 public boolean isAtMaxZoom()
219 {
220 return (scaleFactor > (MAX_SCALE_FACTOR - SCALE_FACTOR_DELTA));
221 }
222
223 public boolean isAtMinZoom()
224 {
225 return (scaleFactor < (MIN_SCALE_FACTOR + SCALE_FACTOR_DELTA));
226 }
227
228 public boolean zoomIn(float factor)
229 {
230 boolean res = true;
231 scaleFactor += factor;
232 if (scaleFactor > (MAX_SCALE_FACTOR - SCALE_FACTOR_DELTA))
233 {
234 scaleFactor = MAX_SCALE_FACTOR;
235 res = false;
236 }
237 setZoom(scaleFactor);
238 return res;
239 }
240
241 public boolean zoomOut(float factor)
242 {
243 boolean res = true;
244 scaleFactor -= factor;
245 if (scaleFactor < (MIN_SCALE_FACTOR + SCALE_FACTOR_DELTA))
246 {
247 scaleFactor = MIN_SCALE_FACTOR;
248 res = false;
249 }
250 setZoom(scaleFactor);
251 return res;
252 }
253
254 public void setTouchPointerPadding(int width, int height)
255 {
256 touchPointerPaddingWidth = width;
257 touchPointerPaddingHeight = height;
258 requestLayout();
259 }
260
261 public int getTouchPointerPaddingWidth()
262 {
263 return touchPointerPaddingWidth;
264 }
265
266 public int getTouchPointerPaddingHeight()
267 {
268 return touchPointerPaddingHeight;
269 }
270
271 @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
272 {
273 Log.v(TAG, width + "x" + height);
274 this.setMeasuredDimension((int)(width * scaleFactor) + touchPointerPaddingWidth,
275 (int)(height * scaleFactor) + touchPointerPaddingHeight);
276 }
277
278 @Override public void onDraw(@NonNull Canvas canvas)
279 {
280 super.onDraw(canvas);
281
282 canvas.save();
283 canvas.concat(scaleMatrix);
284 canvas.drawColor(Color.BLACK);
285 if (!railMode && surface != null)
286 {
287 surface.draw(canvas);
288 }
289 canvas.restore();
290 }
291
292 // perform mapping on the touch event's coordinates according to the current scaling
293 private MotionEvent mapTouchEvent(MotionEvent event)
294 {
295 MotionEvent mappedEvent = MotionEvent.obtain(event);
296 float[] coordinates = { mappedEvent.getX(), mappedEvent.getY() };
297 invScaleMatrix.mapPoints(coordinates);
298 mappedEvent.setLocation(coordinates[0], coordinates[1]);
299 return mappedEvent;
300 }
301
302 // perform mapping on the double touch event's coordinates according to the current scaling
303 private MotionEvent mapDoubleTouchEvent(MotionEvent event)
304 {
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]);
310 return mappedEvent;
311 }
312
313 @Override public boolean onTouchEvent(MotionEvent event)
314 {
315 // Physical mouse events: bypass gesture detector entirely.
316 // Buttons are handled in onGenericMotionEvent; hover moves in onHoverEvent.
317 // Only ACTION_MOVE with a button held (drag) needs handling here.
318 if (event.isFromSource(InputDevice.SOURCE_MOUSE))
319 {
320 int action = event.getActionMasked();
321 // Some ROMs (e.g. HarmonyOS) deliver the wheel as a touch event.
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)
326 {
327 handleTouchpadScroll(event);
328 return true;
329 }
330 if (action == MotionEvent.ACTION_MOVE && event.getButtonState() != 0)
331 {
332 MotionEvent mapped = mapTouchEvent(event);
333 sessionViewListener.onSessionViewMouseMove((int)mapped.getX(), (int)mapped.getY());
334 mapped.recycle();
335 return true;
336 }
337 return true;
338 }
339
340 boolean res = gestureDetector.onTouchEvent(event);
341 res |= doubleGestureDetector.onTouchEvent(event);
342 return res;
343 }
344
345 // Handle physical touchpad two-finger scroll gesture.
346 private void handleTouchpadScroll(MotionEvent event)
347 {
348 if (event.getActionMasked() != MotionEvent.ACTION_MOVE)
349 {
350 touchpadScrollX = 0;
351 touchpadScrollY = 0;
352 return;
353 }
354
355 touchpadScrollY += gestureDistance(event, MotionEvent.AXIS_GESTURE_SCROLL_Y_DISTANCE);
356 while (Math.abs(touchpadScrollY) >= notchPxY)
357 {
358 final boolean down = touchpadScrollY > 0;
359 touchpadScrollY += down ? -notchPxY : notchPxY;
360 sessionViewListener.onSessionViewScroll(down);
361 }
362
363 touchpadScrollX += gestureDistance(event, MotionEvent.AXIS_GESTURE_SCROLL_X_DISTANCE);
364 while (Math.abs(touchpadScrollX) >= notchPxX)
365 {
366 final boolean right = touchpadScrollX > 0;
367 touchpadScrollX += right ? -notchPxX : notchPxX;
368 sessionViewListener.onSessionViewHScroll(right);
369 }
370 }
371
372 // Sum batched historical and current sample deltas for a gesture axis.
373 private static float gestureDistance(MotionEvent event, int axis)
374 {
375 float distance = 0;
376 for (int i = 0; i < event.getHistorySize(); i++)
377 distance += event.getHistoricalAxisValue(axis, 0, i);
378 return distance + event.getAxisValue(axis);
379 }
380
381 // Handle all physical mouse buttons here; finger taps come via onSingleTapUp.
382 @Override public boolean onGenericMotionEvent(MotionEvent event)
383 {
384 final boolean isPointer = event.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
385 if (!isPointer)
386 return false;
387
388 final boolean isMouse = event.isFromSource(InputDevice.SOURCE_MOUSE);
389 int action = event.getActionMasked();
390
391 if (isMouse && (action == MotionEvent.ACTION_BUTTON_PRESS ||
392 action == MotionEvent.ACTION_BUTTON_RELEASE))
393 {
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();
398 mapped.recycle();
399
400 switch (event.getActionButton())
401 {
402 case MotionEvent.BUTTON_PRIMARY:
403 if (down)
404 sessionViewListener.onSessionViewBeginTouch();
405 sessionViewListener.onSessionViewLeftTouch(x, y, down);
406 if (!down)
407 sessionViewListener.onSessionViewEndTouch();
408 return true;
409 case MotionEvent.BUTTON_SECONDARY:
410 if (down)
411 sessionViewListener.onSessionViewBeginTouch();
412 sessionViewListener.onSessionViewRightTouch(x, y, down);
413 return true;
414 case MotionEvent.BUTTON_TERTIARY:
415 sessionViewListener.onSessionViewMiddleTouch(x, y, down);
416 return true;
417 default:
418 return true; // consume unknown buttons silently
419 }
420 }
421
422 if (action == MotionEvent.ACTION_SCROLL)
423 {
424 float vScroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
425 float hScroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
426 if (vScroll != 0)
427 sessionViewListener.onSessionViewScroll(vScroll > 0);
428 if (hScroll != 0)
429 sessionViewListener.onSessionViewHScroll(hScroll > 0);
430 return true;
431 }
432
433 return false;
434 }
435
436 public interface SessionViewListener
437 {
438 void onSessionViewBeginTouch();
439
440 void onSessionViewEndTouch();
441
442 void onSessionViewLeftTouch(int x, int y, boolean down);
443
444 void onSessionViewMiddleTouch(int x, int y, boolean down);
445
446 void onSessionViewRightTouch(int x, int y, boolean down);
447
448 void onSessionViewMove(int x, int y);
449
450 void onSessionViewMouseMove(int x, int y);
451
452 void onSessionViewScroll(boolean down);
453
454 void onSessionViewHScroll(boolean right);
455 }
456
457 public void setRemoteCursor(int[] pixels, int width, int height, int hotX, int hotY)
458 {
459 if (pixels == null || width == 0 || height == 0)
460 {
461 cursorPixels = null;
462 setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_NULL));
463 return;
464 }
465 cursorPixels = pixels;
466 cursorWidth = width;
467 cursorHeight = height;
468 cursorHotX = hotX;
469 cursorHotY = hotY;
470 applyScaledCursor();
471 }
472
473 private void applyScaledCursor()
474 {
475 int scaledWidth = Math.max(1, (int)(cursorWidth * scaleFactor));
476 int scaledHeight = Math.max(1, (int)(cursorHeight * scaleFactor));
477 Bitmap bm =
478 Bitmap.createBitmap(cursorPixels, cursorWidth, cursorHeight, Bitmap.Config.ARGB_8888);
479 Bitmap scaled = Bitmap.createScaledBitmap(bm, scaledWidth, scaledHeight, true);
480 PointerIcon icon =
481 PointerIcon.create(scaled, cursorHotX * scaleFactor, cursorHotY * scaleFactor);
482 setPointerIcon(icon);
483 }
484
485 public void setDefaultCursor()
486 {
487 setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_ARROW));
488 }
489
490 private class SessionGestureListener extends GestureDetector.SimpleOnGestureListener
491 {
492 boolean longPressInProgress = false;
493
494 public boolean onDown(MotionEvent e)
495 {
496 return true;
497 }
498
499 public boolean onUp(MotionEvent e)
500 {
501 sessionViewListener.onSessionViewEndTouch();
502 return true;
503 }
504
505 public void onLongPress(MotionEvent e)
506 {
507 MotionEvent mappedEvent = mapTouchEvent(e);
508 sessionViewListener.onSessionViewBeginTouch();
509 sessionViewListener.onSessionViewLeftTouch((int)mappedEvent.getX(),
510 (int)mappedEvent.getY(), true);
511 longPressInProgress = true;
512 }
513
514 public void onLongPressUp(MotionEvent e)
515 {
516 MotionEvent mappedEvent = mapTouchEvent(e);
517 sessionViewListener.onSessionViewLeftTouch((int)mappedEvent.getX(),
518 (int)mappedEvent.getY(), false);
519 longPressInProgress = false;
520 sessionViewListener.onSessionViewEndTouch();
521 }
522
523 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
524 {
525 if (longPressInProgress)
526 {
527 MotionEvent mappedEvent = mapTouchEvent(e2);
528 sessionViewListener.onSessionViewMove((int)mappedEvent.getX(),
529 (int)mappedEvent.getY());
530 return true;
531 }
532
533 return false;
534 }
535
536 public boolean onDoubleTap(MotionEvent e)
537 {
538 // send 2nd click for double click
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);
544 return true;
545 }
546
547 public boolean onSingleTapUp(MotionEvent e)
548 {
549 // Physical mouse buttons are handled via ACTION_BUTTON_PRESS in onGenericMotionEvent.
550 // If buttonState is non-zero this event came from a physical mouse button
551 if (e.getButtonState() != 0)
552 return false;
553
554 // Finger touch -> left click
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();
562 return true;
563 }
564 }
565
566 private class SessionDoubleGestureListener
567 implements DoubleGestureDetector.OnDoubleGestureListener
568 {
569 private MotionEvent prevEvent = null;
570
571 public boolean onDoubleTouchDown(MotionEvent e)
572 {
573 sessionViewListener.onSessionViewBeginTouch();
574 prevEvent = MotionEvent.obtain(e);
575 return true;
576 }
577
578 public boolean onDoubleTouchUp(MotionEvent e)
579 {
580 if (prevEvent != null)
581 {
582 prevEvent.recycle();
583 prevEvent = null;
584 }
585 sessionViewListener.onSessionViewEndTouch();
586 return true;
587 }
588
589 public boolean onDoubleTouchScroll(MotionEvent e1, MotionEvent e2)
590 {
591 // calc if user scrolled up or down (or if any scrolling happened at all)
592 float deltaY = e2.getY() - prevEvent.getY();
593 if (deltaY > TOUCH_SCROLL_DELTA)
594 {
595 sessionViewListener.onSessionViewScroll(true);
596 prevEvent.recycle();
597 prevEvent = MotionEvent.obtain(e2);
598 }
599 else if (deltaY < -TOUCH_SCROLL_DELTA)
600 {
601 sessionViewListener.onSessionViewScroll(false);
602 prevEvent.recycle();
603 prevEvent = MotionEvent.obtain(e2);
604 }
605 return true;
606 }
607
608 public boolean onDoubleTouchSingleTap(MotionEvent e)
609 {
610 // send single click
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);
616 return true;
617 }
618 }
619
620 @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs)
621 {
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);
627 }
628}