Update the print sample/test code.

Change-Id: I126ab9edadd1e1f93c0232f5f0bafc227461a4d7
diff --git a/PrintApp/AndroidManifest.xml b/PrintApp/AndroidManifest.xml
index 7528f28..1442178 100644
--- a/PrintApp/AndroidManifest.xml
+++ b/PrintApp/AndroidManifest.xml
@@ -4,8 +4,8 @@
     android:versionName="1.0" >
 
     <uses-sdk
-        android:minSdkVersion="KeyLimePie"
-        android:targetSdkVersion="KeyLimePie" />
+        android:minSdkVersion="19"
+        android:targetSdkVersion="19" />
 
     <application
         android:icon="@drawable/ic_launcher"
diff --git a/PrintApp/res/menu/activity_main.xml b/PrintApp/res/menu/activity_main.xml
index 31bb883..060c09a 100644
--- a/PrintApp/res/menu/activity_main.xml
+++ b/PrintApp/res/menu/activity_main.xml
@@ -1,8 +1,4 @@
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:id="@+id/menu_settings"
-        android:title="@string/menu_settings"
-        android:orderInCategory="100"
-        android:showAsAction="never" />
     <item android:id="@+id/menu_print"
         android:title="@string/print"
         android:orderInCategory="100"
diff --git a/PrintApp/src/foo/bar/print/PrintActivity.java b/PrintApp/src/foo/bar/print/PrintActivity.java
index 5d1eee7..c8868f3 100644
--- a/PrintApp/src/foo/bar/print/PrintActivity.java
+++ b/PrintApp/src/foo/bar/print/PrintActivity.java
@@ -24,6 +24,7 @@
 import android.os.CancellationSignal;
 import android.os.CancellationSignal.OnCancelListener;
 import android.os.ParcelFileDescriptor;
+import android.os.SystemClock;
 import android.print.PageRange;
 import android.print.PrintAttributes;
 import android.print.PrintDocumentAdapter;
@@ -50,7 +51,7 @@
 
     public static final String LOG_TAG = "PrintActivity";
 
-    private final Object mLock = new Object();
+    private static final int PAGE_COUNT = 5;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -67,25 +68,23 @@
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
-        if (!super.onOptionsItemSelected(item)) {
-            if (item.getItemId() == R.id.menu_print) {
-                printView();
-                return true;
-            }
+        if (item.getItemId() == R.id.menu_print) {
+            printView();
+            return true;
         }
-        return false;
+        return super.onOptionsItemSelected(item);
     }
 
     private void printView() {
-
         PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
-
         final View view = findViewById(R.id.content);
 
-        PrintJob printJob = printManager.print("Print_View",
+        final PrintJob printJob = printManager.print("Print_View",
             new PrintDocumentAdapter() {
-                private PrintedPdfDocument mPdfDocument;
-                private boolean mCancelled;
+                private static final int RESULT_LAYOUT_FAILED = 1;
+                private static final int RESULT_LAYOUT_FINISHED = 2;
+
+                private PrintAttributes mPrintAttributes;
 
                 @Override
                 public void onStart() {
@@ -94,44 +93,74 @@
                 }
 
                 @Override
-                public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
-                        CancellationSignal cancellationSignal, LayoutResultCallback callback,
-                        Bundle metadata) {
+                public void onLayout(final PrintAttributes oldAttributes,
+                        final PrintAttributes newAttributes,
+                        final CancellationSignal cancellationSignal,
+                        final LayoutResultCallback callback,
+                        final Bundle metadata) {
+
                     Log.i(LOG_TAG, "onLayout[oldAttributes: " + oldAttributes
                             + ", newAttributes: " + newAttributes + "] preview: "
                             + metadata.getBoolean(PrintDocumentAdapter.EXTRA_PRINT_PREVIEW));
 
-                    mPdfDocument = new PrintedPdfDocument(PrintActivity.this, newAttributes);
-
-                    final boolean cancelled;
-                    synchronized (mLock) {
-                        mCancelled = false;
-                        cancelled = mCancelled;
-                    }
-
-                    if (cancelled) {
-                        mPdfDocument.close();
-                        mPdfDocument = null;
-                        callback.onLayoutCancelled();
-                    } else {
-                        PrintDocumentInfo info = new PrintDocumentInfo
-                                .Builder("print_view.pdf")
-                                .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
-                                .setPageCount(1)
-                                .build();
-                        callback.onLayoutFinished(info, false);
-                    }
-
-                    cancellationSignal.setOnCancelListener(new OnCancelListener() {
+                    new AsyncTask<Void, Void, Integer>() {
                         @Override
-                        public void onCancel() {
-                            Log.i(LOG_TAG, "onLayout#onCancel()");
-                            synchronized (mLock) {
-                                mCancelled = true;
-                                mLock.notifyAll();
+                        protected void onPreExecute() {
+                            cancellationSignal.setOnCancelListener(new OnCancelListener() {
+                                @Override
+                                public void onCancel() {
+                                    cancel(true);
+                                }
+                            });
+                            mPrintAttributes = newAttributes;
+                        }
+
+                        @Override
+                        protected Integer doInBackground(Void... params) {
+                            try {
+                                for (int i = 0; i < PAGE_COUNT; i++) {
+                                    if (isCancelled()) {
+                                        return null;
+                                    }
+                                    pretendDoingLayoutWork();
+                                }
+                                return RESULT_LAYOUT_FINISHED;
+                            } catch (Exception e) {
+                                return RESULT_LAYOUT_FAILED;
                             }
-                          }
-                    });
+                        }
+
+                        @Override
+                        protected void onPostExecute(Integer result) {
+                            switch (result) {
+                                case RESULT_LAYOUT_FAILED: {
+                                    Log.i(LOG_TAG, "onLayout#onLayoutFailed()");
+                                    callback.onLayoutFailed(null);
+                                } break;
+
+                                case RESULT_LAYOUT_FINISHED: {
+                                    Log.i(LOG_TAG, "onLayout#onLayoutFinished()");
+                                    PrintDocumentInfo info = new PrintDocumentInfo
+                                            .Builder("print_view.pdf")
+                                            .setContentType(PrintDocumentInfo
+                                                    .CONTENT_TYPE_DOCUMENT)
+                                            .setPageCount(PAGE_COUNT)
+                                            .build();
+                                    callback.onLayoutFinished(info, false);
+                                } break;
+                            }
+                        }
+
+                        @Override
+                        protected void onCancelled(Integer result) {
+                            Log.i(LOG_TAG, "onLayout#onLayoutCancelled()");
+                            callback.onLayoutCancelled();
+                        }
+
+                        private void pretendDoingLayoutWork() throws Exception {
+                            SystemClock.sleep(100);
+                        }
+                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
                 }
 
                 @Override
@@ -142,80 +171,95 @@
                     Log.i(LOG_TAG, "onWrite[pages: " + Arrays.toString(pages) +"]");
 
                     final SparseIntArray writtenPagesArray = new SparseIntArray();
-                    final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
+                    final PrintedPdfDocument pdfDocument = new PrintedPdfDocument(
+                            PrintActivity.this, mPrintAttributes);
+
+                    new AsyncTask<Void, Void, Integer>() {
+                        private static final int RESULT_WRITE_FAILED = 1;
+                        private static final int RESULT_WRITE_FINISHED = 2;
+
                             @Override
                         protected void onPreExecute() {
-                            synchronized (mLock) {
-                                for (int i = 0; i < 1; i++) {
-                                    if (isCancelled()) {
-                                        mPdfDocument.close();
-                                        mPdfDocument = null;
-                                        callback.onWriteCancelled();
-                                        break;
-                                    }
-                                    if (containsPage(pages, i)) {
-                                        writtenPagesArray.append(writtenPagesArray.size(), i);
-                                        Page page = mPdfDocument.startPage(i);
-                                        view.draw(page.getCanvas());
-                                        mPdfDocument.finishPage(page);
-                                    }
+                            canclleationSignal.setOnCancelListener(new OnCancelListener() {
+                                @Override
+                                public void onCancel() {
+                                    cancel(true);
+                                }
+                            });
+
+                            for (int i = 0; i < PAGE_COUNT; i++) {
+                                if (isCancelled()) {
+                                    return;
+                                }
+
+                                SystemClock.sleep(100);
+
+                                if (containsPage(pages, i)) {
+                                    writtenPagesArray.append(writtenPagesArray.size(), i);
+                                    Page page = pdfDocument.startPage(i);
+                                    view.draw(page.getCanvas());
+                                    pdfDocument.finishPage(page);
                                 }
                             }
                         }
 
                         @Override
-                        protected Void doInBackground(Void... params) {
+                        protected Integer doInBackground(Void... params) {
                             try {
-                                mPdfDocument.writeTo(new FileOutputStream(
+                                pdfDocument.writeTo(new FileOutputStream(
                                         destination.getFileDescriptor()));
-                                mPdfDocument.writeTo(new FileOutputStream(
-                                        destination.getFileDescriptor()));
+                                return RESULT_WRITE_FINISHED;
                             } catch (IOException ioe) {
-                                mPdfDocument.close();
-                                callback.onWriteFailed(null);
-                                return null;
-                            }
-
-                            mPdfDocument.close();
-                            mPdfDocument = null;
-
-                            List<PageRange> pageRanges = new ArrayList<PageRange>();
-
-                            int start = -1;
-                            int end = -1;
-                            final int writtenPageCount = writtenPagesArray.size(); 
-                            for (int i = 0; i < writtenPageCount; i++) {
-                                if (start < 0) {
-                                    start = writtenPagesArray.valueAt(i);
-                                }
-                                int oldEnd = end = start;
-                                while (i < writtenPageCount && (end - oldEnd) <= 1) {
-                                    oldEnd = end;
-                                    end = writtenPagesArray.valueAt(i);
-                                    i++;
-                                }
-                                PageRange pageRange = new PageRange(start, end);
-                                pageRanges.add(pageRange);
-                                start = end = -1;
-                            }
-
-                            PageRange[] writtenPages = new PageRange[pageRanges.size()];
-                            pageRanges.toArray(writtenPages);
-                            callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
-                                return null;
-                            }
-                        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
-
-                    canclleationSignal.setOnCancelListener(new OnCancelListener() {
-                        @Override
-                        public void onCancel() {
-                            synchronized (mLock) {
-                                Log.i(LOG_TAG, "onWrite#onCancel()");
-                                task.cancel(true);
-                                mLock.notifyAll();
+                                return RESULT_WRITE_FAILED;
                             }
                         }
-                    });
+
+                        @Override
+                        protected void onPostExecute(Integer result) {
+                            switch (result) {
+                                case RESULT_WRITE_FAILED: {
+                                    Log.i(LOG_TAG, "onWrite#onWriteFailed()");
+                                    callback.onWriteFailed(null);
+                                } break;
+
+                                case RESULT_WRITE_FINISHED: {
+                                    Log.i(LOG_TAG, "onWrite#onWriteFinished()");
+                                    List<PageRange> pageRanges = new ArrayList<PageRange>();
+
+                                    int start = -1;
+                                    int end = -1;
+                                    final int writtenPageCount = writtenPagesArray.size(); 
+                                    for (int i = 0; i < writtenPageCount; i++) {
+                                        if (start < 0) {
+                                            start = writtenPagesArray.valueAt(i);
+                                        }
+                                        int oldEnd = end = start;
+                                        while (i < writtenPageCount && (end - oldEnd) <= 1) {
+                                            oldEnd = end;
+                                            end = writtenPagesArray.valueAt(i);
+                                            i++;
+                                        }
+                                        PageRange pageRange = new PageRange(start, end);
+                                        pageRanges.add(pageRange);
+                                        start = end = -1;
+                                    }
+
+                                    PageRange[] writtenPages = new PageRange[pageRanges.size()];
+                                    pageRanges.toArray(writtenPages);
+                                    callback.onWriteFinished(writtenPages);
+                                } break;
+                            }
+
+                            pdfDocument.close();
+                        }
+
+                        @Override
+                        protected void onCancelled(Integer result) {
+                            Log.i(LOG_TAG, "onWrite#onWriteCancelled()");
+                            callback.onWriteCancelled();
+                            pdfDocument.close();
+                        }
+                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
                 }
 
                 @Override
@@ -238,6 +282,22 @@
         }, null);
 
         if (printJob != null) {
+//            view.postDelayed(new Runnable() {
+//                @Override
+//                public void run() {
+//                    PrintManager printManager = (PrintManager)
+//                            getSystemService(Context.PRINT_SERVICE);
+//                    List<PrintJob> printJobs = printManager.getPrintJobs();
+//                    Log.i(LOG_TAG, "========================================");
+//                    final int printJobCount = printJobs.size();
+//                    for (int i = 0; i < printJobCount; i++) {
+//                        PrintJob printJob = printJobs.get(i);
+//                        Log.i(LOG_TAG, printJob.getInfo().toString());
+//                    }
+//                    Log.i(LOG_TAG, "========================================\n\n");
+//                    view.postDelayed(this, 20000);
+//                }
+//            }, 20000);
             /* Yay, we scheduled something to be printed!!! */
         }
     }
diff --git a/PrintService/AndroidManifest.xml b/PrintService/AndroidManifest.xml
index 65f4ac1..69870cd 100644
--- a/PrintService/AndroidManifest.xml
+++ b/PrintService/AndroidManifest.xml
@@ -4,8 +4,8 @@
     android:versionName="1.0" >
 
     <uses-sdk
-        android:minSdkVersion="KeyLimePie"
-        android:targetSdkVersion="KeyLimePie" />
+        android:minSdkVersion="19"
+        android:targetSdkVersion="19" />
 
     <application
         android:icon="@drawable/ic_launcher"
diff --git a/PrintService/src/foo/bar/printservice/MyPrintService.java b/PrintService/src/foo/bar/printservice/MyPrintService.java
index 25917aa..884fbe9 100644
--- a/PrintService/src/foo/bar/printservice/MyPrintService.java
+++ b/PrintService/src/foo/bar/printservice/MyPrintService.java
@@ -54,7 +54,7 @@
 
     private static final String LOG_TAG = "MyPrintService";
 
-    private static final long STANDARD_DELAY_MILLIS = 10000;
+    private static final long STANDARD_DELAY_MILLIS = 10000000;
 
     static final String INTENT_EXTRA_ACTION_TYPE = "INTENT_EXTRA_ACTION_TYPE";
     static final String INTENT_EXTRA_PRINT_JOB_ID = "INTENT_EXTRA_PRINT_JOB_ID";
@@ -138,7 +138,7 @@
             return;
         }
         mProcessedPrintJobs.remove(printJobId);
-        if (printJob.isQueued() || printJob.isStarted()) {
+        if (printJob.isQueued() || printJob.isStarted() || printJob.isBlocked()) {
             mHandler.removeMessages(MyHandler.MSG_HANDLE_DO_PRINT_JOB);
             mHandler.removeMessages(MyHandler.MSG_HANDLE_FAIL_PRINT_JOB);
             printJob.cancel();
@@ -341,7 +341,8 @@
             for (int i = 0; i < 10; i++) {
                 String name = "Printer " + i;
                 PrinterInfo printer = new PrinterInfo
-                        .Builder(generatePrinterId(name), name, PrinterInfo.STATUS_IDLE)
+                        .Builder(generatePrinterId(name), name, (i % 2 == 1)
+                        ? PrinterInfo.STATUS_UNAVAILABLE : PrinterInfo.STATUS_IDLE)
                         .build();
                 mFakePrinters.add(printer);
             }
@@ -412,19 +413,18 @@
         }
 
         private void addFirstBatchFakePrinters() {
-            List<PrinterInfo> printers = mFakePrinters.subList(0, mFakePrinters.size()/* / 2*/);
+            List<PrinterInfo> printers = mFakePrinters.subList(0, mFakePrinters.size() / 2);
             addPrinters(printers);
         }
 
         private void addSecondBatchFakePrinters() {
 //            List<PrinterInfo> printers = mFakePrinters.subList(mFakePrinters.size() / 2,
 //                    mFakePrinters.size());
-            List<PrinterInfo> printers = new ArrayList<PrinterInfo>();
-            final int printerCount = mFakePrinters.size();
-            for (int i = printerCount - 1; i >= 0; i--) {
-                printers.add(mFakePrinters.get(i));
-            }
-            addPrinters(printers);
+//            final int printerCount = mFakePrinters.size();
+//            for (int i = printerCount - 1; i >= 0; i--) {
+//                printers.add(mFakePrinters.get(i));
+//            }
+//            addPrinters(printers);
         }
 
         private PrinterInfo findPrinterInfo(PrinterId printerId) {