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

Commit 9f35ca99 authored by Eugene Susla's avatar Eugene Susla
Browse files

Use PooledLambda in print code

This replaces the usage of handler message types with PooledLambda

Test: atest CtsPrintTestCases
Change-Id: I19b01278b67b5fe18d48a2e0bb8300bbe1413a63
parent 4ab3a172
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -683,6 +683,23 @@ public class Handler {
        return enqueueMessage(queue, msg, 0);
    }

    /**
     * Executes the message synchronously if called on the same thread this handler corresponds to,
     * or {@link #sendMessage pushes it to the queue} otherwise
     *
     * @return Returns true if the message was successfully ran or placed in to the
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     * @hide
     */
    public final boolean executeOrSendMessage(Message msg) {
        if (mLooper == Looper.myLooper()) {
            dispatchMessage(msg);
            return true;
        }
        return sendMessage(msg);
    }

    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
+68 −94
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.graphics.drawable.Icon;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.ParcelFileDescriptor;
@@ -58,11 +59,11 @@ import android.util.Xml;
import android.util.proto.ProtoOutputStream;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.os.HandlerCaller;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.Preconditions;
import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.printspooler.R;
import com.android.printspooler.util.ApprovedPrintServices;

@@ -116,8 +117,6 @@ public final class PrintSpoolerService extends Service {

    private IPrintSpoolerClient mClient;

    private HandlerCaller mHandlerCaller;

    private PersistenceManager mPersistanceManager;

    private NotificationController mNotificationController;
@@ -134,8 +133,6 @@ public final class PrintSpoolerService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        mHandlerCaller = new HandlerCaller(this, getMainLooper(),
                new HandlerCallerCallback(), false);

        mPersistanceManager = new PersistenceManager();
        mNotificationController = new NotificationController(PrintSpoolerService.this);
@@ -230,59 +227,45 @@ public final class PrintSpoolerService extends Service {
    }

    private void sendOnPrintJobQueued(PrintJobInfo printJob) {
        Message message = mHandlerCaller.obtainMessageO(
                HandlerCallerCallback.MSG_ON_PRINT_JOB_QUEUED, printJob);
        mHandlerCaller.executeOrSendMessage(message);
        Message message = PooledLambda.obtainMessage(
                PrintSpoolerService::onPrintJobQueued, this, printJob);
        Handler.getMain().executeOrSendMessage(message);
    }

    private void sendOnAllPrintJobsForServiceHandled(ComponentName service) {
        Message message = mHandlerCaller.obtainMessageO(
                HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED, service);
        mHandlerCaller.executeOrSendMessage(message);
        Message message = PooledLambda.obtainMessage(
                PrintSpoolerService::onAllPrintJobsForServiceHandled, this, service);
        Handler.getMain().executeOrSendMessage(message);
    }

    private void sendOnAllPrintJobsHandled() {
        Message message = mHandlerCaller.obtainMessage(
                HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_HANDLED);
        mHandlerCaller.executeOrSendMessage(message);
        Message message = PooledLambda.obtainMessage(
                PrintSpoolerService::onAllPrintJobsHandled, this);
        Handler.getMain().executeOrSendMessage(message);
    }

    private final class HandlerCallerCallback implements HandlerCaller.Callback {
        public static final int MSG_SET_CLIENT = 1;
        public static final int MSG_ON_PRINT_JOB_QUEUED = 2;
        public static final int MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED = 3;
        public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 4;
        public static final int MSG_CHECK_ALL_PRINTJOBS_HANDLED = 5;
        public static final int MSG_ON_PRINT_JOB_STATE_CHANGED = 6;

        @Override
        public void executeMessage(Message message) {
            switch (message.what) {
                case MSG_SET_CLIENT: {
                    synchronized (mLock) {
                        mClient = (IPrintSpoolerClient) message.obj;
    private void onPrintJobStateChanged(PrintJobInfo printJob) {
        if (mClient != null) {
                            Message msg = mHandlerCaller.obtainMessage(
                                    HandlerCallerCallback.MSG_CHECK_ALL_PRINTJOBS_HANDLED);
                            mHandlerCaller.sendMessageDelayed(msg,
                                    CHECK_ALL_PRINTJOBS_HANDLED_DELAY);
            try {
                mClient.onPrintJobStateChanged(printJob);
            } catch (RemoteException re) {
                Slog.e(LOG_TAG, "Error notify for print job state change.", re);
            }
        }
    }
                } break;

                case MSG_ON_PRINT_JOB_QUEUED: {
                    PrintJobInfo printJob = (PrintJobInfo) message.obj;
    private void onAllPrintJobsHandled() {
        if (mClient != null) {
            try {
                            mClient.onPrintJobQueued(printJob);
                mClient.onAllPrintJobsHandled();
            } catch (RemoteException re) {
                            Slog.e(LOG_TAG, "Error notify for a queued print job.", re);
                Slog.e(LOG_TAG, "Error notify for all print job handled.", re);
            }
        }
    }
                } break;

                case MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED: {
                    ComponentName service = (ComponentName) message.obj;
    private void onAllPrintJobsForServiceHandled(ComponentName service) {
        if (mClient != null) {
            try {
                mClient.onAllPrintJobsForServiceHandled(service);
@@ -291,32 +274,26 @@ public final class PrintSpoolerService extends Service {
                        + " handled.", re);
            }
        }
                } break;
    }

                case MSG_ON_ALL_PRINT_JOBS_HANDLED: {
    private void onPrintJobQueued(PrintJobInfo printJob) {
        if (mClient != null) {
            try {
                            mClient.onAllPrintJobsHandled();
                mClient.onPrintJobQueued(printJob);
            } catch (RemoteException re) {
                            Slog.e(LOG_TAG, "Error notify for all print job handled.", re);
                Slog.e(LOG_TAG, "Error notify for a queued print job.", re);
            }
        }
    }
                } break;

                case MSG_CHECK_ALL_PRINTJOBS_HANDLED: {
                    checkAllPrintJobsHandled();
                } break;

                case MSG_ON_PRINT_JOB_STATE_CHANGED: {
    private void setClient(IPrintSpoolerClient client) {
        synchronized (mLock) {
            mClient = client;
            if (mClient != null) {
                        PrintJobInfo printJob = (PrintJobInfo) message.obj;
                        try {
                            mClient.onPrintJobStateChanged(printJob);
                        } catch (RemoteException re) {
                            Slog.e(LOG_TAG, "Error notify for print job state change.", re);
                        }
                    }
                } break;
                Message msg = PooledLambda.obtainMessage(
                        PrintSpoolerService::checkAllPrintJobsHandled, this);
                Handler.getMain().sendMessageDelayed(msg,
                        CHECK_ALL_PRINTJOBS_HANDLED_DELAY);
            }
        }
    }
@@ -379,10 +356,9 @@ public final class PrintSpoolerService extends Service {
            addPrintJobLocked(printJob);
            setPrintJobState(printJob.getId(), PrintJobInfo.STATE_CREATED, null);

            Message message = mHandlerCaller.obtainMessageO(
                    HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED,
                    printJob);
            mHandlerCaller.executeOrSendMessage(message);
            Message message = PooledLambda.obtainMessage(
                    PrintSpoolerService::onPrintJobStateChanged, this, printJob);
            Handler.getMain().executeOrSendMessage(message);
        }
    }

@@ -546,10 +522,9 @@ public final class PrintSpoolerService extends Service {
     * @param printJob The updated print job.
     */
    private void notifyPrintJobUpdated(PrintJobInfo printJob) {
        Message message = mHandlerCaller.obtainMessageO(
                HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED,
                printJob);
        mHandlerCaller.executeOrSendMessage(message);
        Message message = PooledLambda.obtainMessage(
                PrintSpoolerService::onPrintJobStateChanged, this, printJob);
        Handler.getMain().executeOrSendMessage(message);

        mNotificationController.onUpdateNotifications(mPrintJobs);
    }
@@ -742,10 +717,9 @@ public final class PrintSpoolerService extends Service {
                }
                mNotificationController.onUpdateNotifications(mPrintJobs);

                Message message = mHandlerCaller.obtainMessageO(
                        HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED,
                        printJob);
                mHandlerCaller.executeOrSendMessage(message);
                Message message = PooledLambda.obtainMessage(
                        PrintSpoolerService::onPrintJobStateChanged, this, printJob);
                Handler.getMain().executeOrSendMessage(message);
            }
        }
    }
@@ -1472,9 +1446,9 @@ public final class PrintSpoolerService extends Service {

        @Override
        public void setClient(IPrintSpoolerClient client) {
            Message message = mHandlerCaller.obtainMessageO(
                    HandlerCallerCallback.MSG_SET_CLIENT, client);
            mHandlerCaller.executeOrSendMessage(message);
            Message message = PooledLambda.obtainMessage(
                    PrintSpoolerService::setClient, PrintSpoolerService.this, client);
            Handler.getMain().executeOrSendMessage(message);
        }

        @Override
+17 −32
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.printspooler.model;

import android.annotation.NonNull;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
@@ -39,6 +38,7 @@ import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.util.Log;

import com.android.internal.util.function.pooled.PooledLambda;
import com.android.printspooler.R;
import com.android.printspooler.util.PageRangeUtils;

@@ -549,6 +549,9 @@ public final class RemotePrintDocument {
    }

    private static abstract class AsyncCommand implements Runnable {
        /** Message indicated the desire to {@link #forceCancel} a command */
        static final int MSG_FORCE_CANCEL = 0;

        private static final int STATE_PENDING = 0;
        private static final int STATE_RUNNING = 1;
        private static final int STATE_COMPLETED = 2;
@@ -574,7 +577,7 @@ public final class RemotePrintDocument {

        public AsyncCommand(Looper looper, IPrintDocumentAdapter adapter, RemotePrintDocumentInfo document,
                CommandDoneCallback doneCallback) {
            mHandler = new AsyncCommandHandler(looper);
            mHandler = new Handler(looper);
            mAdapter = adapter;
            mDocument = document;
            mDoneCallback = doneCallback;
@@ -594,12 +597,12 @@ public final class RemotePrintDocument {
         */
        protected void removeForceCancel() {
            if (DEBUG) {
                if (mHandler.hasMessages(AsyncCommandHandler.MSG_FORCE_CANCEL)) {
                if (mHandler.hasMessages(MSG_FORCE_CANCEL)) {
                    Log.i(LOG_TAG, "[FORCE CANCEL] Removed");
                }
            }

            mHandler.removeMessages(AsyncCommandHandler.MSG_FORCE_CANCEL);
            mHandler.removeMessages(MSG_FORCE_CANCEL);
        }

        /**
@@ -628,7 +631,8 @@ public final class RemotePrintDocument {
                        Log.i(LOG_TAG, "[FORCE CANCEL] queued");
                    }
                    mHandler.sendMessageDelayed(
                            mHandler.obtainMessage(AsyncCommandHandler.MSG_FORCE_CANCEL),
                            PooledLambda.obtainMessage(AsyncCommand::forceCancel, this)
                                    .setWhat(MSG_FORCE_CANCEL),
                            FORCE_CANCEL_TIMEOUT);
                }

@@ -698,21 +702,7 @@ public final class RemotePrintDocument {
            return mError;
        }

        /**
         * Handler for the async command.
         */
        private class AsyncCommandHandler extends Handler {
            /** Message indicated the desire for to force cancel a command */
            final static int MSG_FORCE_CANCEL = 0;

            AsyncCommandHandler(@NonNull Looper looper) {
                super(looper);
            }

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_FORCE_CANCEL:
        private void forceCancel() {
            if (isCanceling()) {
                if (DEBUG) {
                    Log.i(LOG_TAG, "[FORCE CANCEL] executed");
@@ -722,11 +712,6 @@ public final class RemotePrintDocument {

                mDoneCallback.onDone();
            }
                        break;
                    default:
                        // not reached;
                }
            }
        }
    }

+27 −115
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.print;

import static com.android.internal.print.DumpUtils.writePrinterId;
import static com.android.internal.util.dump.DumpUtils.writeComponentName;
import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;

import android.annotation.FloatRange;
import android.annotation.NonNull;
@@ -33,8 +34,6 @@ import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -86,8 +85,6 @@ final class RemotePrintService implements DeathRecipient {

    private final RemotePrintServiceClient mPrintServiceClient;

    private final Handler mHandler;

    private IPrintService mPrintService;

    private boolean mBinding;
@@ -128,7 +125,6 @@ final class RemotePrintService implements DeathRecipient {
        mIntent = new Intent().setComponent(mComponentName);
        mUserId = userId;
        mSpooler = spooler;
        mHandler = new MyHandler(context.getMainLooper());
        mPrintServiceClient = new RemotePrintServiceClient(this);
    }

@@ -137,7 +133,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void destroy() {
        mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleDestroy, this));
    }

    private void handleDestroy() {
@@ -163,7 +160,8 @@ final class RemotePrintService implements DeathRecipient {

    @Override
    public void binderDied() {
        mHandler.sendEmptyMessage(MyHandler.MSG_BINDER_DIED);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleBinderDied, this));
    }

    private void handleBinderDied() {
@@ -177,7 +175,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void onAllPrintJobsHandled() {
        mHandler.sendEmptyMessage(MyHandler.MSG_ON_ALL_PRINT_JOBS_HANDLED);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleOnAllPrintJobsHandled, this));
    }

    private void handleOnAllPrintJobsHandled() {
@@ -209,8 +208,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void onRequestCancelPrintJob(PrintJobInfo printJob) {
        mHandler.obtainMessage(MyHandler.MSG_ON_REQUEST_CANCEL_PRINT_JOB,
                printJob).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleRequestCancelPrintJob, this, printJob));
    }

    private void handleRequestCancelPrintJob(final PrintJobInfo printJob) {
@@ -235,8 +234,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void onPrintJobQueued(PrintJobInfo printJob) {
        mHandler.obtainMessage(MyHandler.MSG_ON_PRINT_JOB_QUEUED,
                printJob).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleOnPrintJobQueued, this, printJob));
    }

    private void handleOnPrintJobQueued(final PrintJobInfo printJob) {
@@ -262,7 +261,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void createPrinterDiscoverySession() {
        mHandler.sendEmptyMessage(MyHandler.MSG_CREATE_PRINTER_DISCOVERY_SESSION);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleCreatePrinterDiscoverySession, this));
    }

    private void handleCreatePrinterDiscoverySession() {
@@ -288,7 +288,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void destroyPrinterDiscoverySession() {
        mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY_PRINTER_DISCOVERY_SESSION);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleDestroyPrinterDiscoverySession, this));
    }

    private void handleDestroyPrinterDiscoverySession() {
@@ -325,8 +326,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void startPrinterDiscovery(List<PrinterId> priorityList) {
        mHandler.obtainMessage(MyHandler.MSG_START_PRINTER_DISCOVERY,
                priorityList).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleStartPrinterDiscovery, this, priorityList));
    }

    private void handleStartPrinterDiscovery(final List<PrinterId> priorityList) {
@@ -356,7 +357,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void stopPrinterDiscovery() {
        mHandler.sendEmptyMessage(MyHandler.MSG_STOP_PRINTER_DISCOVERY);
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleStopPrinterDiscovery, this));
    }

    private void handleStopPrinterDiscovery() {
@@ -387,8 +389,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void validatePrinters(List<PrinterId> printerIds) {
        mHandler.obtainMessage(MyHandler.MSG_VALIDATE_PRINTERS,
                printerIds).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleValidatePrinters, this, printerIds));
    }

    private void handleValidatePrinters(final List<PrinterId> printerIds) {
@@ -413,8 +415,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void startPrinterStateTracking(@NonNull PrinterId printerId) {
        mHandler.obtainMessage(MyHandler.MSG_START_PRINTER_STATE_TRACKING,
                printerId).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleStartPrinterStateTracking, this, printerId));
    }

    /**
@@ -424,8 +426,8 @@ final class RemotePrintService implements DeathRecipient {
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon
     */
    public void requestCustomPrinterIcon(@NonNull PrinterId printerId) {
        mHandler.obtainMessage(MyHandler.MSG_REQUEST_CUSTOM_PRINTER_ICON,
                printerId).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleRequestCustomPrinterIcon, this, printerId));
    }

    /**
@@ -481,8 +483,8 @@ final class RemotePrintService implements DeathRecipient {
    }

    public void stopPrinterStateTracking(PrinterId printerId) {
        mHandler.obtainMessage(MyHandler.MSG_STOP_PRINTER_STATE_TRACKING,
                printerId).sendToTarget();
        Handler.getMain().sendMessage(obtainMessage(
                RemotePrintService::handleStopPrinterStateTracking, this, printerId));
    }

    private void handleStopPrinterStateTracking(final PrinterId printerId) {
@@ -672,96 +674,6 @@ final class RemotePrintService implements DeathRecipient {
        }
    }

    private final class MyHandler extends Handler {
        public static final int MSG_CREATE_PRINTER_DISCOVERY_SESSION = 1;
        public static final int MSG_DESTROY_PRINTER_DISCOVERY_SESSION = 2;
        public static final int MSG_START_PRINTER_DISCOVERY = 3;
        public static final int MSG_STOP_PRINTER_DISCOVERY = 4;
        public static final int MSG_VALIDATE_PRINTERS = 5;
        public static final int MSG_START_PRINTER_STATE_TRACKING = 6;
        public static final int MSG_STOP_PRINTER_STATE_TRACKING = 7;
        public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 8;
        public static final int MSG_ON_REQUEST_CANCEL_PRINT_JOB = 9;
        public static final int MSG_ON_PRINT_JOB_QUEUED = 10;
        public static final int MSG_DESTROY = 11;
        public static final int MSG_BINDER_DIED = 12;
        public static final int MSG_REQUEST_CUSTOM_PRINTER_ICON = 13;

        public MyHandler(Looper looper) {
            super(looper, null, false);
        }

        @Override
        @SuppressWarnings("unchecked")
        public void handleMessage(Message message) {
            if (mDestroyed) {
                Slog.w(LOG_TAG, "Not handling " + message + " as service for " + mComponentName
                        + " is already destroyed");
                return;
            }
            switch (message.what) {
                case MSG_CREATE_PRINTER_DISCOVERY_SESSION: {
                    handleCreatePrinterDiscoverySession();
                } break;

                case MSG_DESTROY_PRINTER_DISCOVERY_SESSION: {
                    handleDestroyPrinterDiscoverySession();
                } break;

                case MSG_START_PRINTER_DISCOVERY: {
                    List<PrinterId> priorityList = (ArrayList<PrinterId>) message.obj;
                    handleStartPrinterDiscovery(priorityList);
                } break;

                case MSG_STOP_PRINTER_DISCOVERY: {
                    handleStopPrinterDiscovery();
                } break;

                case MSG_VALIDATE_PRINTERS: {
                    List<PrinterId> printerIds = (List<PrinterId>) message.obj;
                    handleValidatePrinters(printerIds);
                } break;

                case MSG_START_PRINTER_STATE_TRACKING: {
                    PrinterId printerId = (PrinterId) message.obj;
                    handleStartPrinterStateTracking(printerId);
                } break;

                case MSG_STOP_PRINTER_STATE_TRACKING: {
                    PrinterId printerId = (PrinterId) message.obj;
                    handleStopPrinterStateTracking(printerId);
                } break;

                case MSG_ON_ALL_PRINT_JOBS_HANDLED: {
                    handleOnAllPrintJobsHandled();
                } break;

                case MSG_ON_REQUEST_CANCEL_PRINT_JOB: {
                    PrintJobInfo printJob = (PrintJobInfo) message.obj;
                    handleRequestCancelPrintJob(printJob);
                } break;

                case MSG_ON_PRINT_JOB_QUEUED: {
                    PrintJobInfo printJob = (PrintJobInfo) message.obj;
                    handleOnPrintJobQueued(printJob);
                } break;

                case MSG_DESTROY: {
                    handleDestroy();
                } break;

                case MSG_BINDER_DIED: {
                    handleBinderDied();
                } break;

                case MSG_REQUEST_CUSTOM_PRINTER_ICON: {
                    PrinterId printerId = (PrinterId) message.obj;
                    handleRequestCustomPrinterIcon(printerId);
                } break;
            }
        }
    }

    private static final class RemotePrintServiceClient extends IPrintServiceClient.Stub {
        private final WeakReference<RemotePrintService> mWeakService;

+27 −54

File changed.

Preview size limit exceeded, changes collapsed.