/**
     * Cancel the current command.
     *
     * @param force If set, does not wait for the {@link PrintDocumentAdapter} to cancel. This
     *     should only be used if this is the last command send to the as otherwise the {@link
     *     PrintDocumentAdapter adapter} might get commands while it is still running the old one.
     */
    public final void cancel(boolean force) {
      if (isRunning()) {
        canceling();
        if (mCancellation != null) {
          try {
            mCancellation.cancel();
          } catch (RemoteException re) {
            Log.w(LOG_TAG, "Error while canceling", re);
          }
        }
      }

      if (isCanceling()) {
        if (force) {
          if (DEBUG) {
            Log.i(LOG_TAG, "[FORCE CANCEL] queued");
          }
          mHandler.sendMessageDelayed(
              mHandler.obtainMessage(AsyncCommandHandler.MSG_FORCE_CANCEL), FORCE_CANCEL_TIMEOUT);
        }

        return;
      }

      canceled();

      // Done.
      mDoneCallback.onDone();
    }
    private void handleOnLayoutStarted(ICancellationSignal cancellation, int sequence) {
      if (sequence != mSequence) {
        return;
      }

      if (DEBUG) {
        Log.i(LOG_TAG, "[CALLBACK] onLayoutStarted");
      }

      if (isCanceling()) {
        try {
          cancellation.cancel();
        } catch (RemoteException re) {
          Log.e(LOG_TAG, "Error cancelling", re);
          handleOnLayoutFailed(null, mSequence);
        }
      } else {
        mCancellation = cancellation;
      }
    }