public RemotePrintDocument(
     Context context,
     IPrintDocumentAdapter adapter,
     MutexFileProvider fileProvider,
     RemoteAdapterDeathObserver deathObserver,
     UpdateResultCallbacks callbacks) {
   mPrintDocumentAdapter = adapter;
   mLooper = context.getMainLooper();
   mContext = context;
   mAdapterDeathObserver = deathObserver;
   mDocumentInfo = new RemotePrintDocumentInfo();
   mDocumentInfo.fileProvider = fileProvider;
   mUpdateCallbacks = callbacks;
   connectToRemoteDocument();
 }
 @Override
 public void onDone() {
   if (mCurrentCommand.isCompleted()) {
     if (mCurrentCommand instanceof LayoutCommand) {
       // If there is a next command after a layout is done, then another
       // update was issued and the next command is another layout, so we
       // do nothing. However, if there is no next command we may need to
       // ask for some pages given we do not already have them or we do
       // but the content has changed.
       if (mNextCommand == null) {
         if (mUpdateSpec.pages != null
             && (mDocumentInfo.changed
                 || (mDocumentInfo.info.getPageCount()
                         != PrintDocumentInfo.PAGE_COUNT_UNKNOWN
                     && !PageRangeUtils.contains(
                         mDocumentInfo.writtenPages,
                         mUpdateSpec.pages,
                         mDocumentInfo.info.getPageCount())))) {
           mNextCommand =
               new WriteCommand(
                   mContext,
                   mLooper,
                   mPrintDocumentAdapter,
                   mDocumentInfo,
                   mDocumentInfo.info.getPageCount(),
                   mUpdateSpec.pages,
                   mDocumentInfo.fileProvider,
                   mCommandResultCallback);
         } else {
           if (mUpdateSpec.pages != null) {
             // If we have the requested pages, update which ones to be printed.
             mDocumentInfo.printedPages =
                 PageRangeUtils.computePrintedPages(
                     mUpdateSpec.pages,
                     mDocumentInfo.writtenPages,
                     mDocumentInfo.info.getPageCount());
           }
           // Notify we are done.
           mState = STATE_UPDATED;
           notifyUpdateCompleted();
         }
       }
     } else {
       // We always notify after a write.
       mState = STATE_UPDATED;
       notifyUpdateCompleted();
     }
     runPendingCommand();
   } else if (mCurrentCommand.isFailed()) {
     mState = STATE_FAILED;
     CharSequence error = mCurrentCommand.getError();
     mCurrentCommand = null;
     mNextCommand = null;
     mUpdateSpec.reset();
     notifyUpdateFailed(error);
   } else if (mCurrentCommand.isCanceled()) {
     if (mState == STATE_CANCELING) {
       mState = STATE_CANCELED;
       notifyUpdateCanceled();
     }
     runPendingCommand();
   }
 }