Skip to content

Commit

Permalink
Fixing KeyInterceptor collapsing notification bar
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jul 15, 2024
1 parent d3bc0ef commit 647e787
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app/src/main/java/com/termux/x11/utils/KeyInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
Expand All @@ -17,6 +19,7 @@
public class KeyInterceptor extends AccessibilityService {
LinkedHashSet<Integer> pressedKeys = new LinkedHashSet<>();

private static final Handler handler = new Handler(Looper.getMainLooper());
private static KeyInterceptor self;
private static boolean launchedAutomatically = false;
private boolean enabled = false;
Expand Down Expand Up @@ -60,13 +63,29 @@ public static boolean isLaunched() {
return info != null && info.getId() != null;
}

private static final Runnable disableImmediatelyCallback = KeyInterceptor::disableImmediately;
private static void disableImmediately() {
if (self == null)
return;

android.util.Log.d("KeyInterceptor", "disabling interception service");
self.setServiceInfo(new AccessibilityServiceInfo() {{ flags = DEFAULT; }});
self.enabled = false;
}

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;
if (shouldBeEnabled) {
handler.removeCallbacks(disableImmediatelyCallback);
android.util.Log.d("KeyInterceptor", "enabling interception service");
self.setServiceInfo(new AccessibilityServiceInfo() {{ flags = FLAG_REQUEST_FILTER_KEY_EVENTS; }});
self.enabled = true;
} else
// In the case if service info is changed Android current dragging processes
// so it is impossible to pull notification bar or call recents screen by swiping activity up.
handler.postDelayed(disableImmediatelyCallback, 120000);
}
}

Expand Down

0 comments on commit 647e787

Please sign in to comment.