Clarify synchronization between PrintActivity and RemotePrintDocument

The general theme of these changes is to always delay any action until
the printDocument finishes a command. This is done:
- Before callinng for into a different activity to select a name for the
  PDF
- Before finishing

The second theme is to fix the canceling behavior of
RemotePrintDocument.AsyncCommand.

There are four bugs fixed in this review:

(1)
When the RemotePrintDocument.AsyncCommand is canceled it goes into the
"canceling" state. When it is canceled again it should stay in this
state. Before it went to "canceled" but the command was still running.
(see AsyncCommand#cancel()).

(2)
When finishing the PrintActivity in PrintActivity.doFinish() we cancel
the RemotePrintDocument. If there is a command still in progress (i.e.
isUpdating()) and it finished as canceled we used to call doFinish()
again and then try to double-clean up which lead to exceptions.

The new behavior is that is the PrintActivity is calling doFinish()
while a command is still in progress (i.e. isUpdating()) we delay the
cleanup until the command finishes. The command might finish as
canceled, completed or failed. Hence we have to call doFinish() in the
callbacks for all three cases.

(3)
When canceling there might have still been a nextCommand ready, hence
canceling does not stop execution of the RemotePrintDocument which could
lead to running commands while finshing.

(4)
When getting the location to store the PDF at a command might still be
in progress. This lead to half executed commands and caused issues once
we try to continue after the select-location-activity returns

Bug: 24713704
Bug: 24973884
Change-Id: Ied90fe8dc9bd6ea7f8b3e4ce4f922e477015568d
diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
index 684a1de..2ae3ec62 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
@@ -282,12 +282,10 @@
             Log.i(LOG_TAG, "[CALLED] cancel()");
         }
 
-        if (mState == STATE_CANCELING) {
-            return;
-        }
+        mNextCommand = null;
 
         if (mState != STATE_UPDATING) {
-            throw new IllegalStateException("Cannot cancel in state:" + stateToString(mState));
+            return;
         }
 
         mState = STATE_CANCELING;
@@ -568,6 +566,8 @@
                         Log.w(LOG_TAG, "Error while canceling", re);
                     }
                 }
+            } else if (isCanceling()) {
+                // Nothing to do
             } else {
                 canceled();
 
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
index d0cf635..08cd0b6 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
@@ -320,9 +320,7 @@
                 if (isFinishing() || (isFinalState(mState) && !mPrintedDocument.isUpdating())) {
                     return;
                 }
-                if (mPrintedDocument.isUpdating()) {
-                    mPrintedDocument.cancel();
-                }
+                mPrintedDocument.cancel();
                 setState(STATE_PRINT_CANCELED);
                 doFinish();
             }
@@ -372,12 +370,14 @@
             spooler.updatePrintJobUserConfigurableOptionsNoPersistence(mPrintJob);
 
             switch (mState) {
-                case STATE_PRINT_CONFIRMED: {
-                    spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_QUEUED, null);
-                } break;
-
                 case STATE_PRINT_COMPLETED: {
-                    spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_COMPLETED, null);
+                    if (mCurrentPrinter == mDestinationSpinnerAdapter.getPdfPrinter()) {
+                        spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_COMPLETED,
+                                null);
+                    } else {
+                        spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_QUEUED,
+                                null);
+                    }
                 } break;
 
                 case STATE_CREATE_FILE_FAILED: {
@@ -491,6 +491,8 @@
                 requestCreatePdfFileOrFinish();
             } break;
 
+            case STATE_CREATE_FILE_FAILED:
+            case STATE_PRINT_COMPLETED:
             case STATE_PRINT_CANCELED: {
                 doFinish();
             } break;
@@ -528,8 +530,12 @@
                 requestCreatePdfFileOrFinish();
             } break;
 
+            case STATE_CREATE_FILE_FAILED:
+            case STATE_PRINT_COMPLETED:
             case STATE_PRINT_CANCELED: {
                 updateOptionsUi();
+
+                doFinish();
             } break;
 
             default: {
@@ -550,6 +556,12 @@
         mProgressMessageController.cancel();
         ensureErrorUiShown(error, PrintErrorFragment.ACTION_RETRY);
 
+        if (mState == STATE_CREATE_FILE_FAILED
+                || mState == STATE_PRINT_COMPLETED
+                || mState == STATE_PRINT_CANCELED) {
+            doFinish();
+        }
+
         setState(STATE_UPDATE_FAILED);
 
         updateOptionsUi();
@@ -644,7 +656,6 @@
 
     private void onStartCreateDocumentActivityResult(int resultCode, Intent data) {
         if (resultCode == RESULT_OK && data != null) {
-            setState(STATE_PRINT_COMPLETED);
             updateOptionsUi();
             final Uri uri = data.getData();
             // Calling finish here does not invoke lifecycle callbacks but we
@@ -657,6 +668,10 @@
             });
         } else if (resultCode == RESULT_CANCELED) {
             mState = STATE_CONFIGURING;
+
+            // The previous update might have been canceled
+            updateDocument(false);
+
             updateOptionsUi();
         } else {
             setState(STATE_CREATE_FILE_FAILED);
@@ -875,9 +890,9 @@
     }
 
     private static boolean isFinalState(int state) {
-        return state == STATE_PRINT_CONFIRMED
-                || state == STATE_PRINT_CANCELED
-                || state == STATE_PRINT_COMPLETED;
+        return state == STATE_PRINT_CANCELED
+                || state == STATE_PRINT_COMPLETED
+                || state == STATE_CREATE_FILE_FAILED;
     }
 
     private void updateSelectedPagesFromPreview() {
@@ -998,6 +1013,8 @@
     }
 
     private void requestCreatePdfFileOrFinish() {
+        mPrintedDocument.cancel();
+
         if (mCurrentPrinter == mDestinationSpinnerAdapter.getPdfPrinter()) {
             startCreateDocumentActivity();
         } else {
@@ -1113,9 +1130,7 @@
     private void cancelPrint() {
         setState(STATE_PRINT_CANCELED);
         updateOptionsUi();
-        if (mPrintedDocument.isUpdating()) {
-            mPrintedDocument.cancel();
-        }
+        mPrintedDocument.cancel();
         doFinish();
     }
 
@@ -1874,9 +1889,7 @@
     public void onPrinterUnavailable(PrinterInfo printer) {
         if (mCurrentPrinter.getId().equals(printer.getId())) {
             setState(STATE_PRINTER_UNAVAILABLE);
-            if (mPrintedDocument.isUpdating()) {
-                mPrintedDocument.cancel();
-            }
+            mPrintedDocument.cancel();
             ensureErrorUiShown(getString(R.string.print_error_printer_unavailable),
                     PrintErrorFragment.ACTION_NONE);
             updateOptionsUi();
@@ -1933,12 +1946,18 @@
                 if (writeToUri != null) {
                     mPrintedDocument.writeContent(getContentResolver(), writeToUri);
                 }
+                setState(STATE_PRINT_COMPLETED);
                 doFinish();
             }
         }).transform();
     }
 
     private void doFinish() {
+        if (mPrintedDocument.isUpdating()) {
+            // The printedDocument will call doFinish() when the current command finishes
+            return;
+        }
+
         if (mPrinterRegistry != null) {
             mPrinterRegistry.setTrackedPrinter(null);
         }