Fix potential crash and infinite loop (Closes #61)

This commit is contained in:
Sebastián Katzer 2016-07-24 10:20:26 +02:00
parent 147f562d0a
commit 5c6c646e6d

View File

@ -47,12 +47,32 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/**
* Plugin to print HTML documents. Therefore it creates an invisible web view
* that loads the markup data. Once the page has been fully rendered it takes
* the print adapter of that web view and initializes a print job.
*/
public class Printer extends CordovaPlugin { public class Printer extends CordovaPlugin {
/**
* The web view that loads all the content.
*/
private WebView view; private WebView view;
/**
* Reference is necessary to invoke the callback in the onresume event.
* Without its not possible to determine the status of the job.
*/
private PrintJob job;
/**
* Reference is necessary to invoke the callback in the onresume event.
*/
private CallbackContext command; private CallbackContext command;
/**
* Default name of the printed document (PDF-Printer).
*/
private static final String DEFAULT_DOC_NAME = "unknown"; private static final String DEFAULT_DOC_NAME = "unknown";
/** /**
@ -221,37 +241,24 @@ public class Printer extends CordovaPlugin {
longEdge ? 2 : 4); longEdge ? 2 : 4);
} }
PrintJob job = printManager.print( job = printManager.print(docName, adapter, builder.build());
docName, adapter, builder.build());
invokeCallbackOnceCompletedOrCanceled(job);
view = null; view = null;
} }
}); });
} }
/** /**
* Invokes the callback once the print job is complete or was canceled. * Invokes the callback once the print job is complete or has been canceled.
*
* @param job
* The reference to the print job
*/ */
private void invokeCallbackOnceCompletedOrCanceled (final PrintJob job) { @Override
cordova.getThreadPool().execute(new Runnable() { public void onResume (boolean multitasking) {
@Override PluginResult res = new PluginResult(
public void run() { PluginResult.Status.OK, job.isStarted() || job.isCompleted());
for (;;) {
if (job.isCancelled() || job.isCompleted() || job.isFailed()) {
PluginResult res = new PluginResult(
PluginResult.Status.OK, job.isCompleted());
command.sendPluginResult(res); job = null;
break; command.sendPluginResult(res);
}
} super.onResume(multitasking);
}
});
} }
/** /**