Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93017da5 authored by Uwais Ashraf's avatar Uwais Ashraf
Browse files

Create shell command to clear all recent tasks.

Bug: 381317629
Flag: EXEMPT fix for test
Test: DismissSplitScreenByGoHome
Test: adb shell dumpsys activity service SystemUIService WMShell recents clearAll
Change-Id: If85d1f160b0ab215d92a64aeb2c19b8a125c8718
parent 2c3dcb27
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ public class RecentTasksController implements TaskStackListenerCallback,
    private final RecentTasksImpl mImpl = new RecentTasksImpl();
    private final ActivityTaskManager mActivityTaskManager;
    private final TaskStackTransitionObserver mTaskStackTransitionObserver;
    private final RecentsShellCommandHandler mRecentsShellCommandHandler;
    private RecentsTransitionHandler mTransitionHandler = null;
    private IRecentTasksListener mListener;
    private final boolean mPcFeatureEnabled;
@@ -167,6 +168,7 @@ public class RecentTasksController implements TaskStackListenerCallback,
        mDesktopUserRepositories = desktopUserRepositories;
        mTaskStackTransitionObserver = taskStackTransitionObserver;
        mMainExecutor = mainExecutor;
        mRecentsShellCommandHandler = new RecentsShellCommandHandler(this);
        shellInit.addInitCallback(this::onInit, this);
    }

@@ -183,6 +185,7 @@ public class RecentTasksController implements TaskStackListenerCallback,
        mShellController.addExternalInterface(IRecentTasks.DESCRIPTOR,
                this::createExternalInterface, this);
        mShellCommandHandler.addDumpCallback(this::dump, this);
        mShellCommandHandler.addCommandCallback("recents", mRecentsShellCommandHandler, this);
        mUserId = ActivityManager.getCurrentUser();
        mDesktopUserRepositories.ifPresent(
                desktopUserRepositories ->
@@ -656,6 +659,11 @@ public class RecentTasksController implements TaskStackListenerCallback,
        return mActivityTaskManager.removeTask(taskId);
    }

    /** Removes all recent tasks that are visible. */
    public void removeAllVisibleRecentTasks() throws RemoteException {
        ActivityTaskManager.getService().removeAllVisibleRecentTasks();
    }

    public void dump(@NonNull PrintWriter pw, String prefix) {
        final String innerPrefix = prefix + "  ";
        pw.println(prefix + TAG);
+51 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.recents

import android.os.RemoteException
import com.android.wm.shell.sysui.ShellCommandHandler.ShellCommandActionHandler
import java.io.PrintWriter

class RecentsShellCommandHandler(
    private val recentTasksController: RecentTasksController
) : ShellCommandActionHandler {
    override fun onShellCommand(args: Array<out String>, pw: PrintWriter): Boolean {
        when (args[0]) {
            "clearAll" -> return runClearAll(pw)
            else -> {
                pw.println("Invalid command: " + args[0])
                return false
            }
        }
    }

    override fun printShellCommandHelp(pw: PrintWriter, prefix: String) {
        pw.println("${prefix}clearAll")
        pw.println("$prefix  Clears all visible recent tasks.")
    }

    private fun runClearAll(pw: PrintWriter): Boolean {
        try {
            recentTasksController.removeAllVisibleRecentTasks()
        } catch (e: RemoteException) {
            pw.println("Exception while removing visible recent tasks:")
            e.printStackTrace(pw)
            return false
        }
        return true
    }
}
 No newline at end of file