private static void installRecentTasksCallback0(Object atm, ClassLoader classLoader) {
        XLog.i("RecentTaskHooks, installRecentTasksCallback0: %s", atm);
        // com.android.server.wm.RecentTasks$Callbacks
        @SuppressLint("PrivateApi")
        Class<?> recentTaskCallbackClass = RecentTasksHelper.INSTANCE.recentTaskCallbacksClass(classLoader);
        Object callbackInstance = Proxy.newProxyInstance(recentTaskCallbackClass.getClassLoader(),
                new Class[]{recentTaskCallbackClass},
                (o, method, objects) -> {
                    if ("onRecentTaskAdded".equals(method.getName())) {
                        onRecentTaskAdded(objects);
                    } else if ("onRecentTaskRemoved".equals(method.getName())) {
                        onRecentTaskRemoved(objects);
                    }
                    return ProxyUtils.relaxedMethodCallRes("RecentTaskHooks#Callback", o, method, objects);
                });
        XLog.i("RecentTaskHooks, callbackInstance: %s", callbackInstance);

        Object recentTasks = XposedHelpers.getObjectField(atm, "mRecentTasks");
        XLog.i("RecentTaskHooks recentTasks: %s", recentTasks);

        XposedHelpers.callMethod(recentTasks, "registerCallback", callbackInstance);
        XLog.i("RecentTaskHooks recentTasks installed.");
    }
We use this hook to listen for task add and removal.
android/android_framework/patch-magisk/patch-framework/src/main/java/github/tornaco/thanox/android/server/patch/framework/hooks/wm/RecentTaskHooks.java