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

Commit 126913cb authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Ignore commands if service connection is destroyed.

Test: Print CTS tests (currently affected by b/33449823). Print spooler
      unit test
Fixes: 33106285
Change-Id: I5c771b17992ef627ce5ac3a66a02358c4540893c
parent 9cf894cd
Loading
Loading
Loading
Loading
+35 −24
Original line number Original line Diff line number Diff line
@@ -132,8 +132,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleDestroy() {
    private void handleDestroy() {
        throwIfDestroyed();

        // Stop tracking printers.
        // Stop tracking printers.
        stopTrackingAllPrinters();
        stopTrackingAllPrinters();


@@ -174,7 +172,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleOnAllPrintJobsHandled() {
    private void handleOnAllPrintJobsHandled() {
        throwIfDestroyed();
        mHasActivePrintJobs = false;
        mHasActivePrintJobs = false;
        if (!isBound()) {
        if (!isBound()) {
            // The service is dead and neither has active jobs nor discovery
            // The service is dead and neither has active jobs nor discovery
@@ -208,7 +205,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleRequestCancelPrintJob(final PrintJobInfo printJob) {
    private void handleRequestCancelPrintJob(final PrintJobInfo printJob) {
        throwIfDestroyed();
        if (!isBound()) {
        if (!isBound()) {
            ensureBound();
            ensureBound();
            mPendingCommands.add(new Runnable() {
            mPendingCommands.add(new Runnable() {
@@ -235,7 +231,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleOnPrintJobQueued(final PrintJobInfo printJob) {
    private void handleOnPrintJobQueued(final PrintJobInfo printJob) {
        throwIfDestroyed();
        mHasActivePrintJobs = true;
        mHasActivePrintJobs = true;
        if (!isBound()) {
        if (!isBound()) {
            ensureBound();
            ensureBound();
@@ -262,7 +257,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleCreatePrinterDiscoverySession() {
    private void handleCreatePrinterDiscoverySession() {
        throwIfDestroyed();
        mHasPrinterDiscoverySession = true;
        mHasPrinterDiscoverySession = true;
        if (!isBound()) {
        if (!isBound()) {
            ensureBound();
            ensureBound();
@@ -289,7 +283,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleDestroyPrinterDiscoverySession() {
    private void handleDestroyPrinterDiscoverySession() {
        throwIfDestroyed();
        mHasPrinterDiscoverySession = false;
        mHasPrinterDiscoverySession = false;
        if (!isBound()) {
        if (!isBound()) {
            // The service is dead and neither has active jobs nor discovery
            // The service is dead and neither has active jobs nor discovery
@@ -328,7 +321,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleStartPrinterDiscovery(final List<PrinterId> priorityList) {
    private void handleStartPrinterDiscovery(final List<PrinterId> priorityList) {
        throwIfDestroyed();
        // Take a note that we are doing discovery.
        // Take a note that we are doing discovery.
        mDiscoveryPriorityList = new ArrayList<PrinterId>();
        mDiscoveryPriorityList = new ArrayList<PrinterId>();
        if (priorityList != null) {
        if (priorityList != null) {
@@ -359,7 +351,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleStopPrinterDiscovery() {
    private void handleStopPrinterDiscovery() {
        throwIfDestroyed();
        // We are not doing discovery anymore.
        // We are not doing discovery anymore.
        mDiscoveryPriorityList = null;
        mDiscoveryPriorityList = null;
        if (!isBound()) {
        if (!isBound()) {
@@ -392,7 +383,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleValidatePrinters(final List<PrinterId> printerIds) {
    private void handleValidatePrinters(final List<PrinterId> printerIds) {
        throwIfDestroyed();
        if (!isBound()) {
        if (!isBound()) {
            ensureBound();
            ensureBound();
            mPendingCommands.add(new Runnable() {
            mPendingCommands.add(new Runnable() {
@@ -419,23 +409,40 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    /**
    /**
     * Request the custom printer icon for a printer.
     * Queue a request for a custom printer icon for a printer.
     *
     *
     * @param printerId the id of the printer the icon should be loaded for
     * @param printerId the id of the printer the icon should be loaded for
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon()
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon
     */
     */
    public void requestCustomPrinterIcon(@NonNull PrinterId printerId) {
    public void requestCustomPrinterIcon(@NonNull PrinterId printerId) {
        mHandler.obtainMessage(MyHandler.MSG_REQUEST_CUSTOM_PRINTER_ICON,
                printerId).sendToTarget();
    }

    /**
     * Request a custom printer icon for a printer.
     *
     * @param printerId the id of the printer the icon should be loaded for
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon
     */
    private void handleRequestCustomPrinterIcon(@NonNull PrinterId printerId) {
        if (!isBound()) {
            ensureBound();
            mPendingCommands.add(() -> handleRequestCustomPrinterIcon(printerId));
        } else {
            if (DEBUG) {
                Slog.i(LOG_TAG, "[user: " + mUserId + "] requestCustomPrinterIcon()");
            }

            try {
            try {
            if (isBound()) {
                mPrintService.requestCustomPrinterIcon(printerId);
                mPrintService.requestCustomPrinterIcon(printerId);
            }
            } catch (RemoteException re) {
            } catch (RemoteException re) {
                Slog.e(LOG_TAG, "Error requesting icon for " + printerId, re);
                Slog.e(LOG_TAG, "Error requesting icon for " + printerId, re);
            }
            }
        }
        }
    }


    private void handleStartPrinterStateTracking(final @NonNull PrinterId printerId) {
    private void handleStartPrinterStateTracking(final @NonNull PrinterId printerId) {
        throwIfDestroyed();
        // Take a note we are tracking the printer.
        // Take a note we are tracking the printer.
        if (mTrackedPrinterList == null) {
        if (mTrackedPrinterList == null) {
            mTrackedPrinterList = new ArrayList<PrinterId>();
            mTrackedPrinterList = new ArrayList<PrinterId>();
@@ -467,7 +474,6 @@ final class RemotePrintService implements DeathRecipient {
    }
    }


    private void handleStopPrinterStateTracking(final PrinterId printerId) {
    private void handleStopPrinterStateTracking(final PrinterId printerId) {
        throwIfDestroyed();
        // We are no longer tracking the printer.
        // We are no longer tracking the printer.
        if (mTrackedPrinterList == null || !mTrackedPrinterList.remove(printerId)) {
        if (mTrackedPrinterList == null || !mTrackedPrinterList.remove(printerId)) {
            return;
            return;
@@ -581,12 +587,6 @@ final class RemotePrintService implements DeathRecipient {
        }
        }
    }
    }


    private void throwIfDestroyed() {
        if (mDestroyed) {
            throw new IllegalStateException("Cannot interact with a destroyed service");
        }
    }

    private class RemoteServiceConneciton implements ServiceConnection {
    private class RemoteServiceConneciton implements ServiceConnection {
        @Override
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        public void onServiceConnected(ComponentName name, IBinder service) {
@@ -657,6 +657,7 @@ final class RemotePrintService implements DeathRecipient {
        public static final int MSG_ON_PRINT_JOB_QUEUED = 10;
        public static final int MSG_ON_PRINT_JOB_QUEUED = 10;
        public static final int MSG_DESTROY = 11;
        public static final int MSG_DESTROY = 11;
        public static final int MSG_BINDER_DIED = 12;
        public static final int MSG_BINDER_DIED = 12;
        public static final int MSG_REQUEST_CUSTOM_PRINTER_ICON = 13;


        public MyHandler(Looper looper) {
        public MyHandler(Looper looper) {
            super(looper, null, false);
            super(looper, null, false);
@@ -665,6 +666,11 @@ final class RemotePrintService implements DeathRecipient {
        @Override
        @Override
        @SuppressWarnings("unchecked")
        @SuppressWarnings("unchecked")
        public void handleMessage(Message message) {
        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) {
            switch (message.what) {
                case MSG_CREATE_PRINTER_DISCOVERY_SESSION: {
                case MSG_CREATE_PRINTER_DISCOVERY_SESSION: {
                    handleCreatePrinterDiscoverySession();
                    handleCreatePrinterDiscoverySession();
@@ -719,6 +725,11 @@ final class RemotePrintService implements DeathRecipient {
                case MSG_BINDER_DIED: {
                case MSG_BINDER_DIED: {
                    handleBinderDied();
                    handleBinderDied();
                } break;
                } break;

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