diff --git a/app/src/main/java/com/termux/x11/LoriePreferences.java b/app/src/main/java/com/termux/x11/LoriePreferences.java index 8a00c28f8..aee98635f 100644 --- a/app/src/main/java/com/termux/x11/LoriePreferences.java +++ b/app/src/main/java/com/termux/x11/LoriePreferences.java @@ -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; @@ -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); diff --git a/app/src/main/java/com/termux/x11/MainActivity.java b/app/src/main/java/com/termux/x11/MainActivity.java index 72f468100..3129940aa 100644 --- a/app/src/main/java/com/termux/x11/MainActivity.java +++ b/app/src/main/java/com/termux/x11/MainActivity.java @@ -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(); diff --git a/app/src/main/java/com/termux/x11/utils/KeyInterceptor.java b/app/src/main/java/com/termux/x11/utils/KeyInterceptor.java index 88dc85fee..0b275ad7b 100644 --- a/app/src/main/java/com/termux/x11/utils/KeyInterceptor.java +++ b/app/src/main/java/com/termux/x11/utils/KeyInterceptor.java @@ -18,7 +18,8 @@ public class KeyInterceptor extends AccessibilityService { LinkedHashSet 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; @@ -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") @@ -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) { @@ -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; @@ -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() {} } diff --git a/app/src/main/res/xml/accessibility_service_config.xml b/app/src/main/res/xml/accessibility_service_config.xml index 21c0d8b3d..d481d58eb 100644 --- a/app/src/main/res/xml/accessibility_service_config.xml +++ b/app/src/main/res/xml/accessibility_service_config.xml @@ -1,10 +1,9 @@