/**
     * 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 handleOnWriteFinished(PageRange[] pages, int sequence) {
      if (sequence != mSequence) {
        return;
      }

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

      PageRange[] writtenPages = PageRangeUtils.normalize(pages);
      PageRange[] printedPages =
          PageRangeUtils.computePrintedPages(mPages, writtenPages, mPageCount);

      // Handle if we got invalid pages
      if (printedPages != null) {
        mDocument.writtenPages = writtenPages;
        mDocument.printedPages = printedPages;
        completed();
      } else {
        mDocument.writtenPages = null;
        mDocument.printedPages = null;
        failed(mContext.getString(R.string.print_error_default_message));
      }

      // Release the remote cancellation interface.
      mCancellation = null;

      // Done.
      mWriteDoneCallback.onDone();
    }
    private void handleOnWriteCanceled(int sequence) {
      if (sequence != mSequence) {
        return;
      }

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

      canceled();

      // Release the remote cancellation interface.
      mCancellation = null;

      // Done.
      mWriteDoneCallback.onDone();
    }