am 0d397697: am a2201272: Merge "Fix a print spooler crash when printing." into lmp-dev

* commit '0d397697d0df2b08f5aa0b0e27108e79ea04f764':
  Fix a print spooler crash when printing.
diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
index a4555f1..f779f87 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
@@ -110,13 +110,13 @@
         mRenderer.close(callback);
     }
 
-    public void destroy() {
+    public void destroy(Runnable callback) {
         throwIfNotClosed();
         mState = STATE_DESTROYED;
         if (DEBUG) {
             Log.i(LOG_TAG, "STATE_DESTROYED");
         }
-        doDestroy();
+        doDestroy(callback);
     }
 
     public void startPreload(int firstShownPage, int lastShownPage) {
@@ -163,19 +163,19 @@
         try {
             if (mState != STATE_DESTROYED) {
                 mCloseGuard.warnIfOpen();
-                doDestroy();
+                doDestroy(null);
             }
         } finally {
             super.finalize();
         }
     }
 
-    private void doDestroy() {
+    private void doDestroy(Runnable callback) {
         mState = STATE_DESTROYED;
         if (DEBUG) {
             Log.i(LOG_TAG, "STATE_DESTROYED");
         }
-        mRenderer.destroy();
+        mRenderer.destroy(callback);
     }
 
     private void throwIfNotOpened() {
@@ -536,7 +536,7 @@
             }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
         }
 
-        public void destroy() {
+        public void destroy(final Runnable callback) {
             new AsyncTask<Void, Void, Void>() {
                 @Override
                 protected Void doInBackground(Void... params) {
@@ -551,6 +551,10 @@
                     }
                     mPageContentCache.invalidate();
                     mPageContentCache.clear();
+                    if (callback != null) {
+                        callback.run();
+                    }
+
                 }
             }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
         }
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PageAdapter.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PageAdapter.java
index da8160a..20e774f 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PageAdapter.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PageAdapter.java
@@ -488,9 +488,9 @@
         return selectedPages;
     }
 
-    public void destroy() {
+    public void destroy(Runnable callback) {
         throwIfNotClosed();
-        doDestroy();
+        doDestroy(callback);
     }
 
     @Override
@@ -498,7 +498,7 @@
         try {
             if (mState != STATE_DESTROYED) {
                 mCloseGuard.warnIfOpen();
-                doDestroy();
+                doDestroy(null);
             }
         } finally {
             super.finalize();
@@ -745,8 +745,8 @@
         mPageContentRepository.stopPreload();
     }
 
-    private void doDestroy() {
-        mPageContentRepository.destroy();
+    private void doDestroy(Runnable callback) {
+        mPageContentRepository.destroy(callback);
         mCloseGuard.close();
         mState = STATE_DESTROYED;
         if (DEBUG) {
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
index c4b3262..56497d7 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
@@ -1562,12 +1562,18 @@
         if (mState != STATE_INITIALIZING) {
             mProgressMessageController.cancel();
             mPrinterRegistry.setTrackedPrinter(null);
-            mPrintPreviewController.destroy();
             mSpoolerProvider.destroy();
             mPrintedDocument.finish();
             mPrintedDocument.destroy();
+            mPrintPreviewController.destroy(new Runnable() {
+                @Override
+                public void run() {
+                    finish();
+                }
+            });
+        } else {
+            finish();
         }
-        finish();
     }
 
     private final class SpinnerItem<T> {
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java
index 0d45352..e4eab10 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java
@@ -192,12 +192,15 @@
         });
     }
 
-    public void destroy() {
+    public void destroy(Runnable callback) {
         if (mPageAdapter.isOpened()) {
-            mPageAdapter.close(null);
+            Message operation = mHandler.obtainMessage(MyHandler.MSG_CLOSE);
+            mHandler.enqueueOperation(operation);
         }
-        mRecyclerView.setAdapter(null);
-        mPageAdapter.destroy();
+
+        Message operation = mHandler.obtainMessage(MyHandler.MSG_DESTROY);
+        operation.obj = callback;
+        mHandler.enqueueOperation(operation);
     }
 
     @Override
@@ -292,7 +295,9 @@
                 } break;
 
                 case MSG_DESTROY: {
-                    mPageAdapter.destroy();
+                    Runnable callback = (Runnable) message.obj;
+                    mRecyclerView.setAdapter(null);
+                    mPageAdapter.destroy(callback);
                     handleNextOperation();
                 } break;