David PHAM-VAN

Add a timeout to onFinish()

@@ -134,22 +134,29 @@ public class PrintingJob extends PrintDocumentAdapter { @@ -134,22 +134,29 @@ public class PrintingJob extends PrintDocumentAdapter {
134 public void run() { 134 public void run() {
135 try { 135 try {
136 final boolean[] wait = {true}; 136 final boolean[] wait = {true};
  137 + int count = 5 * 60 * 10; // That's 10 minutes.
137 while (wait[0]) { 138 while (wait[0]) {
138 new Handler(Looper.getMainLooper()).post(new Runnable() { 139 new Handler(Looper.getMainLooper()).post(new Runnable() {
139 @Override 140 @Override
140 public void run() { 141 public void run() {
141 - int state = printJob.getInfo().getState(); 142 + int state = printJob == null ? PrintJobInfo.STATE_FAILED
  143 + : printJob.getInfo().getState();
142 144
143 if (state == PrintJobInfo.STATE_COMPLETED) { 145 if (state == PrintJobInfo.STATE_COMPLETED) {
144 printing.onCompleted(PrintingJob.this, true, ""); 146 printing.onCompleted(PrintingJob.this, true, "");
145 wait[0] = false; 147 wait[0] = false;
146 - } else if (state == PrintJobInfo.STATE_CANCELED) { 148 + } else if (state == PrintJobInfo.STATE_CANCELED
  149 + || state == PrintJobInfo.STATE_FAILED) {
147 printing.onCompleted(PrintingJob.this, false, "User canceled"); 150 printing.onCompleted(PrintingJob.this, false, "User canceled");
148 wait[0] = false; 151 wait[0] = false;
149 } 152 }
150 } 153 }
151 }); 154 });
152 155
  156 + if (--count <= 0) {
  157 + throw new Exception("Timeout waiting for the job to finish");
  158 + }
  159 +
153 if (wait[0]) { 160 if (wait[0]) {
154 Thread.sleep(200); 161 Thread.sleep(200);
155 } 162 }