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

Commit 619f3200 authored by Stefan Andonian's avatar Stefan Andonian Committed by Android (Google) Code Review
Browse files

Merge "[V2] Allow DumpViewHierarchy cmd to be interruptible without breaking." into main

parents f225ab5e e9bd23ff
Loading
Loading
Loading
Loading
+33 −17
Original line number Diff line number Diff line
@@ -130,6 +130,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -216,6 +218,7 @@ public class LauncherAppsService extends SystemService {
        private final ShortcutChangeHandler mShortcutChangeHandler;

        private final Handler mCallbackHandler;
        private final ExecutorService mOnDumpExecutor = Executors.newSingleThreadExecutor();

        private PackageInstallerService mPackageInstallerService;

@@ -1569,25 +1572,38 @@ public class LauncherAppsService extends SystemService {
         */
        private void forEachViewCaptureWindow(
                @NonNull BiConsumer<String, InputStream> outputtingConsumer) {
            try {
                // This multi-threading prevents ctrl-C command line command aborting from putting
                // the mDumpCallbacks RemoteCallbackList in a bad Broadcast state. We need to wait
                // for it to complete even though it is on a background thread.
                mOnDumpExecutor.submit(() -> {
                    try {
                        for (int i = mDumpCallbacks.beginBroadcast() - 1; i >= 0; i--) {
                            String packageName = (String) mDumpCallbacks.getBroadcastCookie(i);
                            String fileName = WM_TRACE_DIR + packageName + "_" + i + VC_FILE_SUFFIX;

                            try {
                    // Order is important here. OnDump needs to be called before the BiConsumer
                    // accepts & starts blocking on reading the input stream.
                                // Order is important here. OnDump needs to be called before the
                                // BiConsumer accepts & starts blocking on reading the input stream.
                                ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
                                mDumpCallbacks.getBroadcastItem(i).onDump(pipe[1]);

                    InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
                                InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(
                                        pipe[0]);
                                outputtingConsumer.accept(fileName, is);
                                is.close();
                            } catch (Exception e) {
                                Log.d(TAG, "failed to pipe view capture data", e);
                            }
                        }
                    } finally {
                        mDumpCallbacks.finishBroadcast();
                    }
                }).get();
            } catch (InterruptedException | ExecutionException e) {
                Log.e(TAG, "background work was interrupted", e);
            }
        }

        @RequiresPermission(READ_FRAME_BUFFER)
        @Override