31public class SessionInputManager
35 private static final String TAG =
"FreeRDP.SessionInputManager";
37 private static final int SCROLLING_TIMEOUT = 16;
38 private static final int SCROLLING_DISTANCE = 12;
39 private static final int SCROLLING_EDGE_MARGIN = 16;
40 private static final int MOVE_EVENT_THROTTLE_MS = 16;
42 private static final int MSG_SEND_MOVE_EVENT = 1;
43 private static final int MSG_SCROLLING_REQUESTED = 2;
45 private final Context context;
51 private final PinchZoomListener pinchZoomListener =
new PinchZoomListener();
54 private long instance = 0;
55 private Bitmap bitmap;
56 private int screenWidth;
57 private int screenHeight;
58 private long lastMoveSentTime = 0;
60 private boolean softInputRequested =
false;
62 private boolean softInputVisible =
false;
64 private final Handler handler;
69 this.context = context;
70 this.scrollView = scrollView;
71 this.sessionView = sessionView;
72 this.touchPointerView = touchPointerView;
73 this.keyboard = keyboard;
74 this.handler =
new InputHandler();
77 this.keyboardMapper.init(context);
79 keyboard.setListener(
this);
83 public void attachSession(
long instance, Bitmap surface)
85 this.instance = instance;
86 this.bitmap = surface;
87 this.lastMoveSentTime = 0;
88 keyboardMapper.reset(
this);
92 public void setBitmap(Bitmap bitmap)
98 public ScaleGestureDetector.OnScaleGestureListener getPinchZoomListener()
100 return pinchZoomListener;
104 public void setScreenSize(
int width,
int height)
106 this.screenWidth = width;
107 this.screenHeight = height;
111 public void toggleKeyboard()
113 if (keyboard.getVisibility() == View.VISIBLE)
119 keyboard.setExpanded(
false,
false);
120 keyboard.setVisibility(View.VISIBLE);
121 setSoftInputState(
true);
126 public void hideKeyboards()
128 keyboard.setExpanded(
false,
false);
129 keyboard.setVisibility(View.GONE);
130 setSoftInputState(
false);
131 keyboardMapper.clearlAllModifiers();
133 scrollView.post(this::refreshSystemBars);
137 public boolean handleKeyboardBack()
139 if (keyboard.getVisibility() != View.VISIBLE)
142 if (keyboard.isExpanded())
144 keyboard.setExpanded(
false,
false);
147 scrollView.requestApplyInsets();
156 public boolean isSoftInputActive()
158 return keyboard.getVisibility() == View.VISIBLE && (softInputRequested || softInputVisible);
163 public void onImeVisibilityChanged(
boolean visible)
165 if (visible == softInputVisible)
167 softInputVisible = visible;
169 softInputRequested =
false;
173 private void refreshSystemBars()
179 private void setSoftInputState(
boolean state)
181 softInputRequested = state;
183 softInputVisible =
false;
184 InputMethodManager mgr =
185 (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
189 sessionView.requestFocus();
190 mgr.showSoftInput(sessionView, InputMethodManager.SHOW_IMPLICIT);
194 mgr.hideSoftInputFromWindow(sessionView.getWindowToken(), 0);
197 scrollView.requestApplyInsets();
201 public void cancelPendingEvents()
203 handler.removeMessages(MSG_SEND_MOVE_EVENT);
207 public boolean onGenericMotionEvent(MotionEvent e)
211 if (e.getAction() != MotionEvent.ACTION_SCROLL)
214 final float vScroll = e.getAxisValue(MotionEvent.AXIS_VSCROLL);
215 final float hScroll = e.getAxisValue(MotionEvent.AXIS_HSCROLL);
217 onSessionViewScroll(vScroll > 0);
219 onSessionViewHScroll(hScroll > 0);
224 public boolean onAndroidKeyEvent(KeyEvent event)
229 keyboardMapper.setPhysicalKeyboardDeviceLayout(
231 return keyboardMapper.processAndroidKeyEvent(event);
236 public boolean onAndroidKeyLongPress(
int keyCode)
240 if (keyCode == KeyEvent.KEYCODE_BACK)
249 public boolean handleBackAsAltF4()
255 keyboardMapper.sendAltF4();
260 public void toggleTouchPointer()
262 if (touchPointerView.getVisibility() == View.VISIBLE)
264 touchPointerView.setVisibility(View.INVISIBLE);
265 sessionView.setTouchPointerPadding(0, 0);
269 touchPointerView.setVisibility(View.VISIBLE);
270 sessionView.setTouchPointerPadding(touchPointerView.getPointerWidth(),
271 touchPointerView.getPointerHeight());
278 private void sendDelayedMoveEvent(
int x,
int y)
280 final long now = SystemClock.uptimeMillis();
281 final long elapsed = now - lastMoveSentTime;
283 cancelDelayedMoveEvent();
284 if (elapsed >= MOVE_EVENT_THROTTLE_MS)
286 lastMoveSentTime = now;
291 final long delay = MOVE_EVENT_THROTTLE_MS - elapsed;
292 handler.sendMessageDelayed(Message.obtain(
null, MSG_SEND_MOVE_EVENT, x, y), delay);
296 private void cancelDelayedMoveEvent()
298 handler.removeMessages(MSG_SEND_MOVE_EVENT);
301 private Point mapScreenCoordToSessionCoord(
int x,
int y)
303 View container = scrollView.getChildCount() > 0 ? scrollView.getChildAt(0) : sessionView;
304 int mappedX = (int)((
float)(x - container.getLeft() + scrollView.getScrollX()) /
305 sessionView.getZoom());
306 int mappedY = (int)((
float)(y - container.getTop() + scrollView.getScrollY()) /
307 sessionView.getZoom());
314 if (mappedX > bitmap.getWidth())
315 mappedX = bitmap.getWidth();
316 if (mappedY > bitmap.getHeight())
317 mappedY = bitmap.getHeight();
319 return new Point(mappedX, mappedY);
325 @Override
public void onSessionViewBeginTouch()
327 scrollView.setScrollEnabled(
false);
330 @Override
public void onSessionViewEndTouch()
332 scrollView.setScrollEnabled(
true);
335 @Override
public void onSessionViewLeftTouch(
int x,
int y,
boolean down)
339 cancelDelayedMoveEvent();
340 LibFreeRDP.sendCursorEvent(instance, x, y,
Mouse.getLeftButtonEvent(context, down));
343 @Override
public void onSessionViewMiddleTouch(
int x,
int y,
boolean down)
347 cancelDelayedMoveEvent();
348 LibFreeRDP.sendCursorEvent(instance, x, y,
Mouse.getMiddleButtonEvent(down));
351 @Override
public void onSessionViewRightTouch(
int x,
int y,
boolean down)
355 cancelDelayedMoveEvent();
356 LibFreeRDP.sendCursorEvent(instance, x, y,
Mouse.getRightButtonEvent(context, down));
359 @Override
public void onSessionViewMove(
int x,
int y)
363 sendDelayedMoveEvent(x, y);
366 @Override
public void onSessionViewMouseMove(
int x,
int y)
373 @Override
public void onSessionViewScroll(
boolean down)
377 LibFreeRDP.sendCursorEvent(instance, 0, 0,
Mouse.getScrollEvent(context, down));
380 @Override
public void onSessionViewHScroll(
boolean right)
384 LibFreeRDP.sendCursorEvent(instance, 0, 0,
Mouse.getHScrollEvent(context, right));
390 @Override
public void onTouchPointerClose()
392 touchPointerView.setVisibility(View.INVISIBLE);
393 sessionView.setTouchPointerPadding(0, 0);
396 @Override
public void onTouchPointerLeftClick(
int x,
int y,
boolean down)
400 Point p = mapScreenCoordToSessionCoord(x, y);
401 LibFreeRDP.sendCursorEvent(instance, p.x, p.y,
Mouse.getLeftButtonEvent(context, down));
404 @Override
public void onTouchPointerRightClick(
int x,
int y,
boolean down)
408 Point p = mapScreenCoordToSessionCoord(x, y);
409 LibFreeRDP.sendCursorEvent(instance, p.x, p.y,
Mouse.getRightButtonEvent(context, down));
412 @Override
public void onTouchPointerMiddleClick(
int x,
int y,
boolean down)
416 Point p = mapScreenCoordToSessionCoord(x, y);
417 LibFreeRDP.sendCursorEvent(instance, p.x, p.y,
Mouse.getMiddleButtonEvent(down));
420 @Override
public void onTouchPointerMove(
int x,
int y)
424 Point p = mapScreenCoordToSessionCoord(x, y);
428 !handler.hasMessages(MSG_SCROLLING_REQUESTED))
430 handler.sendEmptyMessageDelayed(MSG_SCROLLING_REQUESTED, SCROLLING_TIMEOUT);
434 @Override
public void onTouchPointerMoveEnd()
436 handler.removeMessages(MSG_SCROLLING_REQUESTED);
439 @Override
public void onTouchPointerScroll(
int amount)
443 LibFreeRDP.sendCursorEvent(instance, 0, 0,
Mouse.getScrollEvent(context, amount));
446 @Override
public void onTouchPointerHScroll(
int amount)
450 LibFreeRDP.sendCursorEvent(instance, 0, 0,
Mouse.getHScrollEvent(context, amount));
453 @Override
public void onTouchPointerToggleKeyboard()
458 @Override
public void onTouchPointerResetScrollZoom()
460 sessionView.setZoom(1.0f);
461 scrollView.scrollTo(0, 0);
467 @Override
public void processVirtualKey(
int virtualKeyCode,
boolean down)
471 LibFreeRDP.sendKeyEvent(instance, virtualKeyCode, down);
474 @Override
public void processUnicodeKey(
int unicodeKey)
478 if (
LibFreeRDP.isUnicodeInputSupported(instance))
480 LibFreeRDP.sendUnicodeKeyEvent(instance, unicodeKey,
true);
481 LibFreeRDP.sendUnicodeKeyEvent(instance, unicodeKey,
false);
484 keyboardMapper.processUnicodeFallback(unicodeKey);
487 @Override
public boolean isUnicodeInputSupported()
489 return instance != 0 &&
LibFreeRDP.isUnicodeInputSupported(instance);
492 @Override
public void processKeyAsUnicode(
int virtualKeyCode,
int unicodeKey,
int modmask)
496 LibFreeRDP.sendKeyAsUnicodeEvent(instance, virtualKeyCode, unicodeKey, modmask,
true);
497 LibFreeRDP.sendKeyAsUnicodeEvent(instance, virtualKeyCode, unicodeKey, modmask,
false);
500 @Override
public void switchKeyboard(
int keyboardType)
502 switch (keyboardType)
517 @Override
public void modifiersChanged()
519 keyboard.refreshModifiers();
525 @Override
public void onKey(
int keycode)
527 keyboardMapper.processCustomKeyEvent(keycode);
530 @Override
public void onKeyLock(
int keycode)
532 keyboardMapper.processCustomKeyLock(keycode);
535 @Override
public int getModifierState(
int keycode)
537 return keyboardMapper.getModifierState(keycode);
540 @Override
public void onExpandedChanged(
boolean expanded)
543 setSoftInputState(!expanded);
549 private class InputHandler
extends Handler
553 super(Looper.getMainLooper());
556 @Override
public void handleMessage(Message msg)
560 case MSG_SEND_MOVE_EVENT:
563 lastMoveSentTime = SystemClock.uptimeMillis();
564 LibFreeRDP.sendCursorEvent(instance, msg.arg1, msg.arg2,
Mouse.getMoveEvent());
567 case MSG_SCROLLING_REQUESTED:
571 float[] pointerPos = touchPointerView.getPointerPosition();
572 final int ow = touchPointerView.getWidth();
573 final int oh = touchPointerView.getHeight();
574 final int pw = touchPointerView.getPointerWidth();
575 final int ph = touchPointerView.getPointerHeight();
577 if (pointerPos[0] >= ow - pw - SCROLLING_EDGE_MARGIN)
578 scrollX = SCROLLING_DISTANCE;
579 else if (pointerPos[0] <= SCROLLING_EDGE_MARGIN)
580 scrollX = -SCROLLING_DISTANCE;
582 if (pointerPos[1] >= oh - ph - SCROLLING_EDGE_MARGIN)
583 scrollY = SCROLLING_DISTANCE;
584 else if (pointerPos[1] <= SCROLLING_EDGE_MARGIN)
585 scrollY = -SCROLLING_DISTANCE;
587 scrollView.scrollBy(scrollX, scrollY);
589 final int maxX = sessionView.getWidth() - scrollView.getWidth();
590 final int maxY = sessionView.getHeight() - scrollView.getHeight();
591 if ((scrollX < 0 && scrollView.getScrollX() <= 0) ||
592 (scrollX > 0 && scrollView.getScrollX() >= maxX))
594 if ((scrollY < 0 && scrollView.getScrollY() <= 0) ||
595 (scrollY > 0 && scrollView.getScrollY() >= maxY))
598 if (scrollX != 0 || scrollY != 0)
599 handler.sendEmptyMessageDelayed(MSG_SCROLLING_REQUESTED, SCROLLING_TIMEOUT);
609 private class PinchZoomListener
extends ScaleGestureDetector.SimpleOnScaleGestureListener
611 private float scaleFactor = 1.0f;
613 @Override
public boolean onScaleBegin(ScaleGestureDetector detector)
615 scrollView.setScrollEnabled(
false);
619 @Override
public boolean onScale(ScaleGestureDetector detector)
622 scaleFactor *= detector.getScaleFactor();
623 scaleFactor = Math.max(
SessionView.MIN_SCALE_FACTOR,
624 Math.min(scaleFactor,
SessionView.MAX_SCALE_FACTOR));
625 sessionView.setZoom(scaleFactor);
627 if (!sessionView.isAtMinZoom() && !sessionView.isAtMaxZoom())
630 float transOriginX = scrollView.getScrollX() * detector.getScaleFactor();
631 float transOriginY = scrollView.getScrollY() * detector.getScaleFactor();
635 (scrollView.getScrollX() + detector.getFocusX()) * detector.getScaleFactor();
637 (scrollView.getScrollY() + detector.getFocusY()) * detector.getScaleFactor();
642 scrollView.scrollBy((
int)((transCenterX - transOriginX) - detector.getFocusX()),
643 (
int)((transCenterY - transOriginY) - detector.getFocusY()));
649 @Override
public void onScaleEnd(ScaleGestureDetector de)
651 scrollView.setScrollEnabled(
true);