Skip to content

Commit

Permalink
add keyboard accessibility service
Browse files Browse the repository at this point in the history
  • Loading branch information
lyc8503 committed Apr 13, 2024
1 parent b8084c2 commit 619b80c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/src/main/java/com/limelight/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class Game extends Activity implements SurfaceHolder.Callback,
OnGenericMotionListener, OnTouchListener, NvConnectionListener, EvdevListener,
OnSystemUiVisibilityChangeListener, GameGestures, StreamView.InputCallbacks,
PerfOverlayListener, UsbDriverService.UsbDriverStateListener, View.OnKeyListener {

public static Game instance = null;
private int lastButtonState = 0;

// Only 2 touches are supported
Expand Down Expand Up @@ -121,6 +123,9 @@ public class Game extends Activity implements SurfaceHolder.Callback,
private boolean displayedFailureDialog = false;
private boolean connecting = false;
private boolean connected = false;
public boolean isConnected() {
return connected;
}
private boolean autoEnterPip = false;
private boolean surfaceCreated = false;
private boolean attemptedConnection = false;
Expand Down Expand Up @@ -185,6 +190,8 @@ public void onServiceDisconnected(ComponentName componentName) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

instance = this;

UiHelper.setLocale(this);

// We don't want a title bar
Expand Down Expand Up @@ -1072,6 +1079,8 @@ protected void onPause() {
protected void onStop() {
super.onStop();

instance = null;

SpinnerDialog.closeDialogs(this);
Dialog.closeDialogs();

Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/com/limelight/KeyboardAccessibilityService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.limelight;

import android.accessibilityservice.AccessibilityService;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;

import java.util.Arrays;
import java.util.List;

public class KeyboardAccessibilityService extends AccessibilityService {

private final static List<Integer> BLACKLISTED_KEYS = Arrays.asList(KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_POWER);

@Override
public boolean onKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();

if (Game.instance != null && Game.instance.isConnected() && !BLACKLISTED_KEYS.contains(keyCode)) {
// Preventing default will disable shortcut actions like alt+tab and etc.
if (action == KeyEvent.ACTION_DOWN) {
Game.instance.handleKeyDown(event);
return true;
} else if (action == KeyEvent.ACTION_UP) {
Game.instance.handleKeyUp(event);
return true;
}
}

return super.onKeyEvent(event);
}

@Override
public void onServiceConnected() {
LimeLog.info("Keyboard service is connected");
}

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {

}

@Override
public void onInterrupt() {

}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,7 @@
<string name="analogscroll_none">None (both sticks move the mouse)</string>
<string name="analogscroll_right">Right analog stick</string>
<string name="analogscroll_left">Left analog stick</string>

<string name="keyboard_service_label">Moonlight Physical Keyboard Service</string>
<string name="accessibility_description_text">These permissions are only used to detect when buttons are pressed so they can be sent to PC without triggering default action on your device.\n\nMoonlight does NOT observe the text you type and does NOT collect your passwords, credit card numbers or any other information.</string>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/keyboard_accessibility_service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeContextClicked|typeViewClicked|typeViewFocused|typeViewScrolled|typeViewSelected|typeWindowContentChanged|typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:accessibilityFlags="flagDefault|flagRequestFilterKeyEvents|flagRetrieveInteractiveWindows"
android:canRequestFilterKeyEvents="true"
android:description="@string/accessibility_description_text"
android:notificationTimeout="100"
android:settingsActivity=""
android:summary="@string/keyboard_service_label" />
16 changes: 15 additions & 1 deletion app/src/nonRoot/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@

<!-- Non-root application name -->
<!-- FIXME: We should set extractNativeLibs=false but this breaks installation on the Fire TV 3 -->
<application android:label="@string/app_label" />
<application android:label="@string/app_label">
<service
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:name="com.limelight.KeyboardAccessibilityService"
android:label="@string/keyboard_service_label"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/keyboard_accessibility_service" />
</service>
</application>
</manifest>

0 comments on commit 619b80c

Please sign in to comment.