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

Commit 52680b5d authored by Philip P. Moltmann's avatar Philip P. Moltmann Committed by Android (Google) Code Review
Browse files

Merge "Ignore commands if service connection is destroyed."

parents e08b07ca 126913cb
Loading
Loading
Loading
Loading
+35 −24
Original line number Diff line number Diff line
@@ -132,8 +132,6 @@ final class RemotePrintService implements DeathRecipient {
    }

    private void handleDestroy() {
        throwIfDestroyed();

        // Stop tracking printers.
        stopTrackingAllPrinters();

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

    private void handleOnAllPrintJobsHandled() {
        throwIfDestroyed();
        mHasActivePrintJobs = false;
        if (!isBound()) {
            // 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) {
        throwIfDestroyed();
        if (!isBound()) {
            ensureBound();
            mPendingCommands.add(new Runnable() {
@@ -235,7 +231,6 @@ final class RemotePrintService implements DeathRecipient {
    }

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

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

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

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

    private void handleValidatePrinters(final List<PrinterId> printerIds) {
        throwIfDestroyed();
        if (!isBound()) {
            ensureBound();
            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
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon()
     * @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon
     */
    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 {
            if (isBound()) {
                mPrintService.requestCustomPrinterIcon(printerId);
            }
            } catch (RemoteException re) {
                Slog.e(LOG_TAG, "Error requesting icon for " + printerId, re);
            }
        }
    }

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

    private void handleStopPrinterStateTracking(final PrinterId printerId) {
        throwIfDestroyed();
        // We are no longer tracking the printer.
        if (mTrackedPrinterList == null || !mTrackedPrinterList.remove(printerId)) {
            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 {
        @Override
        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_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);
@@ -665,6 +666,11 @@ final class RemotePrintService implements DeathRecipient {
        @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();
@@ -719,6 +725,11 @@ final class RemotePrintService implements DeathRecipient {
                case MSG_BINDER_DIED: {
                    handleBinderDied();
                } break;

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