Cancel current work in PrintDocumentAdatper if printing is cancelled.

Layout and write may take some time during which the user can
cancel printing. Currently we wait for the last operation,
being write or layout, to complete before closing the print
dialog. Now in such a scenario we request a cancellation of
the ongoing operation.

bug:11329523

Change-Id: Ia9d747163cc73509369a86c8b5afc83b7ee54859
diff --git a/core/java/android/print/IPrintDocumentAdapter.aidl b/core/java/android/print/IPrintDocumentAdapter.aidl
index 9d384fb..2b95c12 100644
--- a/core/java/android/print/IPrintDocumentAdapter.aidl
+++ b/core/java/android/print/IPrintDocumentAdapter.aidl
@@ -37,4 +37,5 @@
     void write(in PageRange[] pages, in ParcelFileDescriptor fd,
             IWriteResultCallback callback, int sequence);
     void finish();
+    void cancel();
 }
diff --git a/core/java/android/print/PrintManager.java b/core/java/android/print/PrintManager.java
index bbfc307..d6d56bb 100644
--- a/core/java/android/print/PrintManager.java
+++ b/core/java/android/print/PrintManager.java
@@ -616,6 +616,18 @@
         }
 
         @Override
+        public void cancel() {
+            // Start not called or finish called or destroyed - nothing to do.
+            if (!mStartReqeusted || mFinishRequested || mDestroyed) {
+                return;
+            }
+            // Request cancellation of pending work if needed.
+            synchronized (mLock) {
+                cancelPreviousCancellableOperationLocked();
+            }
+        }
+
+        @Override
         public void onActivityPaused(Activity activity) {
             /* do nothing */
         }
diff --git a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
index 8f26361..6d0ecd7 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
@@ -357,6 +357,9 @@
         }
 
         public void cancel() {
+            if (isWorking()) {
+                mRemotePrintAdapter.cancel();
+            }
             mControllerState = CONTROLLER_STATE_CANCELLED;
         }
 
diff --git a/packages/PrintSpooler/src/com/android/printspooler/RemotePrintDocumentAdapter.java b/packages/PrintSpooler/src/com/android/printspooler/RemotePrintDocumentAdapter.java
index fd14af9..d9ccb5d 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/RemotePrintDocumentAdapter.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/RemotePrintDocumentAdapter.java
@@ -137,4 +137,15 @@
             Log.e(LOG_TAG, "Error calling finish()", re);
         }
     }
+
+    public void cancel() {
+        if (DEBUG) {
+            Log.i(LOG_TAG, "cancel()");
+        }
+        try {
+            mRemoteInterface.cancel();
+        } catch (RemoteException re) {
+            Log.e(LOG_TAG, "Error calling cancel()", re);
+        }
+    }
 }