Skip to content

Commit

Permalink
Refactoring KeyInterceptor
Browse files Browse the repository at this point in the history
Making KeyInterceptor listen for events only in the case if Termux:X11 has window focus and drop listening if Termux:X11 loses focus and there are no pressed keys.
  • Loading branch information
twaik committed Jul 15, 2024
1 parent 05457b6 commit 033c14a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/com/termux/x11/LoriePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -309,9 +308,9 @@ void updatePreferencesLayout() {
setEnabled("enableAccessibilityServiceAutomatically", !prefs.dexMetaKeyCapture.get());
setEnabled("pauseKeyInterceptingWithEsc", prefs.dexMetaKeyCapture.get() ||
prefs.enableAccessibilityServiceAutomatically.get() ||
KeyInterceptor.isEnabled());
setEnabled("enableAccessibilityServiceAutomatically", prefs.enableAccessibilityServiceAutomatically.get() || KeyInterceptor.isEnabled());
setEnabled("filterOutWinkey", prefs.enableAccessibilityServiceAutomatically.get() || KeyInterceptor.isEnabled());
KeyInterceptor.isLaunched());
setEnabled("enableAccessibilityServiceAutomatically", prefs.enableAccessibilityServiceAutomatically.get() || KeyInterceptor.isLaunched());
setEnabled("filterOutWinkey", prefs.enableAccessibilityServiceAutomatically.get() || KeyInterceptor.isLaunched());

boolean displayStretchEnabled = "exact".contentEquals(prefs.displayResolutionMode.get()) || "custom".contentEquals(prefs.displayResolutionMode.get());
setEnabled("displayStretch", displayStretchEnabled);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
KeyInterceptor.recheck();
prefs.recheckStoringSecondaryDisplayPreferences();
Window window = getWindow();
View decorView = window.getDecorView();
Expand Down
32 changes: 19 additions & 13 deletions app/src/main/java/com/termux/x11/utils/KeyInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class KeyInterceptor extends AccessibilityService {
LinkedHashSet<Integer> pressedKeys = new LinkedHashSet<>();

private static KeyInterceptor self;
private static boolean enabledAutomatically = false;
private static boolean launchedAutomatically = false;
private boolean enabled = false;

public KeyInterceptor() {
self = this;
Expand All @@ -28,7 +29,7 @@ public static void launch(@NonNull Context ctx) {
try {
Settings.Secure.putString(ctx.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "com.termux.x11/.utils.KeyInterceptor");
Settings.Secure.putString(ctx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, "1");
enabledAutomatically = true;
launchedAutomatically = true;
} catch (SecurityException e) {
new AlertDialog.Builder(ctx)
.setTitle("Permission denied")
Expand All @@ -44,7 +45,7 @@ public static void launch(@NonNull Context ctx) {
}

public static void shutdown(boolean onlyIfEnabledAutomatically) {
if (onlyIfEnabledAutomatically && !enabledAutomatically)
if (onlyIfEnabledAutomatically && !launchedAutomatically)
return;

if (self != null) {
Expand All @@ -54,11 +55,21 @@ public static void shutdown(boolean onlyIfEnabledAutomatically) {
}
}

public static boolean isEnabled() {
public static boolean isLaunched() {
AccessibilityServiceInfo info = self == null ? null : self.getServiceInfo();
return info != null && info.getId() != null;
}

public static void recheck() {
MainActivity a = MainActivity.getInstance();
boolean shouldBeEnabled = (a != null && self != null) && (a.hasWindowFocus() || !self.pressedKeys.isEmpty());
if (self != null && shouldBeEnabled != self.enabled) {
android.util.Log.d("KeyInterceptor", (shouldBeEnabled ? "en" : "dis") + "abling interception");
self.setServiceInfo(new AccessibilityServiceInfo() {{ flags = shouldBeEnabled ? FLAG_REQUEST_FILTER_KEY_EVENTS : DEFAULT; }});
self.enabled = shouldBeEnabled;
}
}

@Override
public boolean onKeyEvent(KeyEvent event) {
boolean ret = false;
Expand All @@ -80,19 +91,14 @@ public boolean onKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP)
pressedKeys.remove(event.getKeyCode());

recheck();

return ret;
}

@Override
public void onAccessibilityEvent(AccessibilityEvent e) {
// Disable self if it is automatically started on device boot or when activity finishes.
if (MainActivity.getInstance() == null || MainActivity.getInstance().isFinishing()) {
android.util.Log.d("KeyInterceptor", "finishing");
shutdown(false);
}
}
public void onAccessibilityEvent(AccessibilityEvent e) {}

@Override
public void onInterrupt() {
}
public void onInterrupt() {}
}
9 changes: 4 additions & 5 deletions app/src/main/res/xml/accessibility_service_config.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagRequestFilterKeyEvents|flagReportViewIds"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="50"
android:canRetrieveWindowContent="true"
android:accessibilityEventTypes=""
android:accessibilityFlags=""
android:canRetrieveWindowContent="false"
android:settingsActivity=".LoriePreferences"
android:packageNames="@null"
android:canRequestFilterKeyEvents="true" />

0 comments on commit 033c14a

Please sign in to comment.