Fix potential crash and infinite loop (Closes #61)
This commit is contained in:
parent
147f562d0a
commit
5c6c646e6d
@ -47,12 +47,32 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* The web view that loads all the content.
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Default name of the printed document (PDF-Printer).
|
||||
*/
|
||||
private static final String DEFAULT_DOC_NAME = "unknown";
|
||||
|
||||
/**
|
||||
@ -221,37 +241,24 @@ public class Printer extends CordovaPlugin {
|
||||
longEdge ? 2 : 4);
|
||||
}
|
||||
|
||||
PrintJob job = printManager.print(
|
||||
docName, adapter, builder.build());
|
||||
|
||||
invokeCallbackOnceCompletedOrCanceled(job);
|
||||
|
||||
job = printManager.print(docName, adapter, builder.build());
|
||||
view = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the callback once the print job is complete or was canceled.
|
||||
*
|
||||
* @param job
|
||||
* The reference to the print job
|
||||
* Invokes the callback once the print job is complete or has been canceled.
|
||||
*/
|
||||
private void invokeCallbackOnceCompletedOrCanceled (final PrintJob job) {
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (;;) {
|
||||
if (job.isCancelled() || job.isCompleted() || job.isFailed()) {
|
||||
public void onResume (boolean multitasking) {
|
||||
PluginResult res = new PluginResult(
|
||||
PluginResult.Status.OK, job.isCompleted());
|
||||
PluginResult.Status.OK, job.isStarted() || job.isCompleted());
|
||||
|
||||
job = null;
|
||||
command.sendPluginResult(res);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
super.onResume(multitasking);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user