FreeRDP
Loading...
Searching...
No Matches
KeyboardAccessibilityService.java
1/*
2 Physical Keyboard Accessibility Service
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
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.accessibilityservice.AccessibilityService;
14import android.accessibilityservice.AccessibilityServiceInfo;
15import android.os.Handler;
16import android.os.Looper;
17import android.os.SystemClock;
18import android.view.InputDevice;
19import android.view.KeyEvent;
20import android.view.ViewConfiguration;
21import android.view.accessibility.AccessibilityEvent;
22
23public class KeyboardAccessibilityService extends AccessibilityService
24{
25 /* Android synthesizes key repeat when it dispatches a key to a window. Keys this service
26 consumes never reach one, so a held key arrives as a single down and up: repeat it here,
27 with the platform's own timings, until the matching up. */
28 private final Handler repeatHandler = new Handler(Looper.getMainLooper());
29 private KeyEvent repeatEvent;
30 private final Runnable repeatRunnable = new Runnable() {
31 @Override public void run()
32 {
33 SessionActivity session = SessionActivity.activeSession;
34 if (repeatEvent == null || session == null)
35 {
36 stopRepeat();
37 return;
38 }
39 repeatEvent = KeyEvent.changeTimeRepeat(repeatEvent, SystemClock.uptimeMillis(),
40 repeatEvent.getRepeatCount() + 1);
41 session.handleKeyEvent(repeatEvent);
42 repeatHandler.postDelayed(this, ViewConfiguration.getKeyRepeatDelay());
43 }
44 };
45
46 @Override public boolean onKeyEvent(KeyEvent event)
47 {
48 SessionActivity session = SessionActivity.activeSession;
49 if (session == null)
50 {
51 stopRepeat();
52 return super.onKeyEvent(event);
53 }
54
55 InputDevice device = event.getDevice();
56 if (device != null &&
57 (device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
58 return super.onKeyEvent(event);
59
60 switch (event.getKeyCode())
61 {
62 case KeyEvent.KEYCODE_VOLUME_UP:
63 case KeyEvent.KEYCODE_VOLUME_DOWN:
64 case KeyEvent.KEYCODE_POWER:
65 return super.onKeyEvent(event);
66 }
67
68 /* Some tablets map the physical ESC key to KEYCODE_BACK (scancode 1).*/
69 if (event.getScanCode() == 1 && event.getKeyCode() != KeyEvent.KEYCODE_ESCAPE)
70 {
71 event =
72 new KeyEvent(event.getDownTime(), event.getEventTime(), event.getAction(),
73 KeyEvent.KEYCODE_ESCAPE, event.getRepeatCount(), event.getMetaState());
74 }
75
76 boolean handled = session.handleKeyEvent(event);
77 trackRepeat(event, handled);
78 return handled;
79 }
80
81 private void trackRepeat(KeyEvent event, boolean handled)
82 {
83 switch (event.getAction())
84 {
85 case KeyEvent.ACTION_DOWN:
86 /* A new key ends the previous one's repeat, as it does on the platform. A down
87 that already carries a repeat count is being repeated by someone else. */
88 stopRepeat();
89 if (handled && event.getRepeatCount() == 0 && isRepeatable(event.getKeyCode()))
90 {
91 repeatEvent = event;
92 repeatHandler.postDelayed(repeatRunnable,
93 ViewConfiguration.getKeyRepeatTimeout());
94 }
95 break;
96 case KeyEvent.ACTION_UP:
97 if (repeatEvent != null && repeatEvent.getKeyCode() == event.getKeyCode())
98 stopRepeat();
99 break;
100 default:
101 break;
102 }
103 }
104
105 private static boolean isRepeatable(int keyCode)
106 {
107 switch (keyCode)
108 {
109 case KeyEvent.KEYCODE_CAPS_LOCK:
110 case KeyEvent.KEYCODE_NUM_LOCK:
111 case KeyEvent.KEYCODE_SCROLL_LOCK:
112 return false;
113 default:
114 return !KeyEvent.isModifierKey(keyCode);
115 }
116 }
117
118 private void stopRepeat()
119 {
120 repeatHandler.removeCallbacks(repeatRunnable);
121 repeatEvent = null;
122 }
123
124 @Override public void onServiceConnected()
125 {
126 AccessibilityServiceInfo info = new AccessibilityServiceInfo();
127 info.packageNames = new String[] { getApplicationContext().getPackageName() };
128 info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
129 info.notificationTimeout = 100;
130 info.flags = AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
131 info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
132 setServiceInfo(info);
133 }
134
135 @Override public void onAccessibilityEvent(AccessibilityEvent event)
136 {
137 }
138
139 @Override public void onInterrupt()
140 {
141 stopRepeat();
142 }
143
144 @Override public void onDestroy()
145 {
146 stopRepeat();
147 super.onDestroy();
148 }
149}