mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install (#2654)
* fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install * fix(shortcuts): bind only same-signer relays, recheck enablement per call, drop timed-out keys
This commit is contained in:
1 parent
5dd973587f
commit
de5f0fe6df
52 files changed
+750
-296
No files matched your search
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
android:icon="@android:drawable/sym_def_app_icon"
|
||||
android:label="@string/app_name">
|
||||
<service android:name=".ShortcutsService"
|
||||
android:label="@string/service_name"
|
||||
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.accessibilityservice.AccessibilityService" />
|
||||
</intent-filter>
|
||||
<meta-data android:name="android.accessibilityservice" android:resource="@xml/shortcuts_service" />
|
||||
</service>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,254 @@
|
||||
package com.droiddeck.shortcuts;
|
||||
|
||||
import android.accessibilityservice.AccessibilityService;
|
||||
import android.accessibilityservice.AccessibilityServiceInfo;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.IBinder;
|
||||
import android.os.Parcel;
|
||||
import android.os.RemoteException;
|
||||
import android.util.SparseArray;
|
||||
import android.view.Display;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.accessibility.AccessibilityNodeInfo;
|
||||
import android.view.accessibility.AccessibilityWindowInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class ShortcutsService extends AccessibilityService {
|
||||
private final CopyOnWriteArrayList<Link> links = new CopyOnWriteArrayList<>();
|
||||
private HandlerThread windowsThread;
|
||||
private Handler windows;
|
||||
private boolean watchingWindows;
|
||||
private boolean receiving;
|
||||
private final Runnable snapshot = this::sendWindows;
|
||||
|
||||
private final BroadcastReceiver packages = new BroadcastReceiver() {
|
||||
@Override public void onReceive(Context context, Intent intent) { bindAll(); }
|
||||
};
|
||||
|
||||
private final class Link implements ServiceConnection {
|
||||
final ComponentName component;
|
||||
volatile IBinder binder;
|
||||
volatile int flags;
|
||||
|
||||
Link(ComponentName component) { this.component = component; }
|
||||
|
||||
@Override public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
binder = service;
|
||||
flags = config(service);
|
||||
if ((flags & Relay.WANTS_WINDOWS) != 0) watchWindows();
|
||||
}
|
||||
|
||||
@Override public void onServiceDisconnected(ComponentName name) { binder = null; }
|
||||
|
||||
@Override public void onBindingDied(ComponentName name) { drop(this); bindAll(); }
|
||||
|
||||
@Override public void onNullBinding(ComponentName name) { drop(this); }
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onServiceConnected() {
|
||||
windowsThread = new HandlerThread("shortcuts-windows");
|
||||
windowsThread.start();
|
||||
windows = new Handler(windowsThread.getLooper());
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
|
||||
filter.addDataScheme("package");
|
||||
registerReceiver(packages, filter);
|
||||
receiving = true;
|
||||
bindAll();
|
||||
}
|
||||
|
||||
private void bindAll() {
|
||||
List<ResolveInfo> found = getPackageManager().queryIntentServices(new Intent(Relay.ACTION), 0);
|
||||
for (ResolveInfo info : found) {
|
||||
// Keys and window contents only go to a relay signed with the same key as this companion.
|
||||
if (getPackageManager().checkSignatures(getPackageName(), info.serviceInfo.packageName)
|
||||
!= PackageManager.SIGNATURE_MATCH) continue;
|
||||
ComponentName component = new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
|
||||
boolean linked = false;
|
||||
for (Link link : links) if (link.component.equals(component)) linked = true;
|
||||
if (linked) continue;
|
||||
Link link = new Link(component);
|
||||
try {
|
||||
if (bindService(new Intent(Relay.ACTION).setComponent(component), link, Context.BIND_AUTO_CREATE)) links.add(link);
|
||||
else unbindService(link);
|
||||
} catch (RuntimeException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drop(Link link) {
|
||||
links.remove(link);
|
||||
try { unbindService(link); } catch (RuntimeException ignored) {}
|
||||
}
|
||||
|
||||
private static int config(IBinder binder) {
|
||||
Parcel data = Parcel.obtain();
|
||||
Parcel reply = Parcel.obtain();
|
||||
try {
|
||||
data.writeInterfaceToken(Relay.DESCRIPTOR);
|
||||
binder.transact(Relay.CONFIG, data, reply, 0);
|
||||
return reply.readInt();
|
||||
} catch (RemoteException | RuntimeException e) {
|
||||
return 0;
|
||||
} finally {
|
||||
data.recycle();
|
||||
reply.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
private void watchWindows() {
|
||||
if (Build.VERSION.SDK_INT < 30) return;
|
||||
if (!watchingWindows) {
|
||||
watchingWindows = true;
|
||||
AccessibilityServiceInfo info = getServiceInfo();
|
||||
if (info != null) {
|
||||
info.eventTypes |= AccessibilityEvent.TYPE_WINDOWS_CHANGED | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
|
||||
info.flags |= AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
|
||||
setServiceInfo(info);
|
||||
}
|
||||
}
|
||||
windows.removeCallbacks(snapshot);
|
||||
windows.post(snapshot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onKeyEvent(KeyEvent event) {
|
||||
boolean taken = false;
|
||||
for (Link link : links) {
|
||||
IBinder binder = link.binder;
|
||||
if (binder == null) continue;
|
||||
Parcel data = Parcel.obtain();
|
||||
Parcel reply = Parcel.obtain();
|
||||
try {
|
||||
data.writeInterfaceToken(Relay.DESCRIPTOR);
|
||||
event.writeToParcel(data, 0);
|
||||
binder.transact(Relay.KEY, data, reply, 0);
|
||||
if (reply.readInt() != 0) taken = true;
|
||||
} catch (RemoteException | RuntimeException ignored) {
|
||||
} finally {
|
||||
data.recycle();
|
||||
reply.recycle();
|
||||
}
|
||||
}
|
||||
return taken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccessibilityEvent(AccessibilityEvent event) {
|
||||
if (!watchingWindows || event == null || Build.VERSION.SDK_INT < 30) return;
|
||||
int type = event.getEventType();
|
||||
if (type == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
|
||||
CharSequence pkg = event.getPackageName();
|
||||
int display = event.getDisplayId();
|
||||
if (pkg == null || display == Display.DEFAULT_DISPLAY) return;
|
||||
String name = pkg.toString();
|
||||
long at = event.getEventTime();
|
||||
sendAll(Relay.ARRIVAL, data -> {
|
||||
data.writeInt(display);
|
||||
data.writeString(name);
|
||||
data.writeLong(at);
|
||||
});
|
||||
} else if (type != AccessibilityEvent.TYPE_WINDOWS_CHANGED) {
|
||||
return;
|
||||
}
|
||||
windows.removeCallbacks(snapshot);
|
||||
windows.postDelayed(snapshot, 150);
|
||||
}
|
||||
|
||||
private void sendWindows() {
|
||||
if (Build.VERSION.SDK_INT < 30) return;
|
||||
SparseArray<List<AccessibilityWindowInfo>> all;
|
||||
try { all = getWindowsOnAllDisplays(); } catch (RuntimeException e) { return; }
|
||||
Map<Integer, List<Relay.Window>> displays = new LinkedHashMap<>();
|
||||
for (int i = 0; i < all.size(); i++) {
|
||||
if (all.keyAt(i) == Display.DEFAULT_DISPLAY) continue;
|
||||
List<Relay.Window> windows = new ArrayList<>();
|
||||
for (AccessibilityWindowInfo window : all.valueAt(i)) {
|
||||
Rect bounds = new Rect();
|
||||
window.getBoundsInScreen(bounds);
|
||||
windows.add(new Relay.Window(window.getId(), window.getType(), window.getLayer(), bounds, packageOf(window)));
|
||||
}
|
||||
displays.put(all.keyAt(i), windows);
|
||||
}
|
||||
byte[] bytes = Relay.encode(displays);
|
||||
sendAll(Relay.WINDOWS, data -> data.writeByteArray(bytes));
|
||||
}
|
||||
|
||||
private static String packageOf(AccessibilityWindowInfo window) {
|
||||
try {
|
||||
AccessibilityNodeInfo root = window.getRoot();
|
||||
if (root == null || root.getPackageName() == null) return null;
|
||||
return root.getPackageName().toString();
|
||||
} catch (RuntimeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private interface Writer { void write(Parcel data); }
|
||||
|
||||
private void sendAll(int code, Writer writer) {
|
||||
for (Link link : links) {
|
||||
IBinder binder = link.binder;
|
||||
if (binder == null) continue;
|
||||
if (code != Relay.INTERRUPT && (link.flags & Relay.WANTS_WINDOWS) == 0) continue;
|
||||
Parcel data = Parcel.obtain();
|
||||
try {
|
||||
data.writeInterfaceToken(Relay.DESCRIPTOR);
|
||||
writer.write(data);
|
||||
binder.transact(code, data, null, IBinder.FLAG_ONEWAY);
|
||||
} catch (RemoteException | RuntimeException ignored) {
|
||||
} finally {
|
||||
data.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInterrupt() {
|
||||
sendAll(Relay.INTERRUPT, data -> {});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onUnbind(Intent intent) {
|
||||
release();
|
||||
return super.onUnbind(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
release();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private void release() {
|
||||
if (receiving) {
|
||||
receiving = false;
|
||||
try { unregisterReceiver(packages); } catch (RuntimeException ignored) {}
|
||||
}
|
||||
for (Link link : links) drop(link);
|
||||
if (windowsThread != null) {
|
||||
windows.removeCallbacksAndMessages(null);
|
||||
windowsThread.quitSafely();
|
||||
windowsThread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">DroidDeck system keys</string>
|
||||
<string name="service_name">DroidDeck system keys</string>
|
||||
<string name="service_description">Keep Alt+Tab, the Win key and the Home button inside a focused DroidDeck session instead of Android.</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:accessibilityFeedbackType="feedbackGeneric"
|
||||
android:accessibilityFlags="flagRequestFilterKeyEvents"
|
||||
android:canRequestFilterKeyEvents="true"
|
||||
android:canRetrieveWindowContent="true"
|
||||
android:description="@string/service_description" />
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.droiddeck.shortcuts;
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.os.Parcel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class Relay {
|
||||
public static final String PACKAGE = "com.droiddeck.shortcuts";
|
||||
public static final String SERVICE = PACKAGE + ".ShortcutsService";
|
||||
public static final String ACTION = "com.droiddeck.shortcuts.RELAY";
|
||||
public static final String DESCRIPTOR = "com.droiddeck.shortcuts.Relay";
|
||||
|
||||
public static final int CONFIG = 1;
|
||||
public static final int KEY = 2;
|
||||
public static final int INTERRUPT = 3;
|
||||
public static final int ARRIVAL = 4;
|
||||
public static final int WINDOWS = 5;
|
||||
|
||||
public static final int WANTS_WINDOWS = 1;
|
||||
|
||||
public static final class Window {
|
||||
public final int id;
|
||||
public final int type;
|
||||
public final int layer;
|
||||
public final Rect bounds;
|
||||
public final String pkg;
|
||||
|
||||
public Window(int id, int type, int layer, Rect bounds, String pkg) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.layer = layer;
|
||||
this.bounds = bounds;
|
||||
this.pkg = pkg;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] encode(Map<Integer, List<Window>> displays) {
|
||||
Parcel parcel = Parcel.obtain();
|
||||
try {
|
||||
parcel.writeInt(displays.size());
|
||||
for (Map.Entry<Integer, List<Window>> display : displays.entrySet()) {
|
||||
parcel.writeInt(display.getKey());
|
||||
parcel.writeInt(display.getValue().size());
|
||||
for (Window window : display.getValue()) {
|
||||
parcel.writeInt(window.id);
|
||||
parcel.writeInt(window.type);
|
||||
parcel.writeInt(window.layer);
|
||||
parcel.writeInt(window.bounds.left);
|
||||
parcel.writeInt(window.bounds.top);
|
||||
parcel.writeInt(window.bounds.right);
|
||||
parcel.writeInt(window.bounds.bottom);
|
||||
parcel.writeString(window.pkg);
|
||||
}
|
||||
}
|
||||
return parcel.marshall();
|
||||
} finally {
|
||||
parcel.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Integer, List<Window>> decode(byte[] bytes) {
|
||||
Parcel parcel = Parcel.obtain();
|
||||
try {
|
||||
parcel.unmarshall(bytes, 0, bytes.length);
|
||||
parcel.setDataPosition(0);
|
||||
Map<Integer, List<Window>> displays = new LinkedHashMap<>();
|
||||
for (int d = parcel.readInt(); d > 0; d--) {
|
||||
int display = parcel.readInt();
|
||||
List<Window> windows = new ArrayList<>();
|
||||
for (int w = parcel.readInt(); w > 0; w--) {
|
||||
int id = parcel.readInt();
|
||||
int type = parcel.readInt();
|
||||
int layer = parcel.readInt();
|
||||
Rect bounds = new Rect(parcel.readInt(), parcel.readInt(), parcel.readInt(), parcel.readInt());
|
||||
windows.add(new Window(id, type, layer, bounds, parcel.readString()));
|
||||
}
|
||||
displays.put(display, windows);
|
||||
}
|
||||
return displays;
|
||||
} finally {
|
||||
parcel.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
private Relay() {}
|
||||
}
|
||||
Reference in new issue
Block a user