diff --git a/plugin.xml b/plugin.xml index 9ae64d9..f9a19d6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,26 +1,24 @@ + + diff --git a/src/android/PrintAdapter.java b/src/android/PrintAdapter.java index 35bbfd7..3e82e69 100644 --- a/src/android/PrintAdapter.java +++ b/src/android/PrintAdapter.java @@ -1,4 +1,6 @@ /* + Copyright 2013 Sebastián Katzer + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information @@ -27,7 +29,6 @@ import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintDocumentInfo; import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.support.v4.print.PrintHelper; import java.io.FileOutputStream; @@ -49,7 +50,7 @@ class PrintAdapter extends PrintDocumentAdapter { private final @NonNull InputStream input; // The callback to inform once the job is done - private final @Nullable PrintHelper.OnPrintFinishCallback callback; + private final @NonNull PrintHelper.OnPrintFinishCallback callback; /** * Constructor @@ -59,7 +60,7 @@ class PrintAdapter extends PrintDocumentAdapter { * @param callback The callback to inform once the job is done. */ PrintAdapter (@NonNull String jobName, @NonNull InputStream input, - @Nullable PrintHelper.OnPrintFinishCallback callback) + @NonNull PrintHelper.OnPrintFinishCallback callback) { this.jobName = jobName; this.input = input; @@ -109,7 +110,7 @@ class PrintAdapter extends PrintDocumentAdapter { } /** - * Close input stream once the printing is done. + * Closes the input stream and invokes the callback. */ @Override public void onFinish () @@ -118,8 +119,6 @@ class PrintAdapter extends PrintDocumentAdapter { PrintContent.close(input); - if (callback != null) { - callback.onFinish(); - } + callback.onFinish(); } } diff --git a/src/android/PrintContent.java b/src/android/PrintContent.java index 8e5e140..41ab594 100644 --- a/src/android/PrintContent.java +++ b/src/android/PrintContent.java @@ -1,4 +1,6 @@ /* + Copyright 2013 Sebastián Katzer + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information diff --git a/src/android/PrintManager.java b/src/android/PrintManager.java index 1ce5bc5..8b96599 100644 --- a/src/android/PrintManager.java +++ b/src/android/PrintManager.java @@ -1,22 +1,22 @@ /* - Copyright 2013-2016 appPlant GmbH + Copyright 2013 Sebastián Katzer - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. */ package de.appplant.cordova.plugin.printer; @@ -26,6 +26,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; +import android.print.PrintJob; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.print.PrintHelper; @@ -41,10 +42,15 @@ import java.io.InputStream; import static android.content.Context.PRINT_SERVICE; import static android.os.Build.VERSION.SDK_INT; +import static android.print.PrintJobInfo.STATE_COMPLETED; import static de.appplant.cordova.plugin.printer.PrintContent.ContentType.UNSUPPORTED; class PrintManager { + interface OnPrintFinishCallback { + void onFinish (boolean completed); + } + // The application context private final @NonNull Context context; @@ -110,8 +116,7 @@ class PrintManager { */ @SuppressWarnings("ConstantConditions") void print (@Nullable String content, @NonNull JSONObject settings, - @NonNull WebView view, - @Nullable PrintHelper.OnPrintFinishCallback callback) + @NonNull WebView view, @NonNull OnPrintFinishCallback callback) { switch (PrintContent.getContentType(content, context)) { @@ -123,26 +128,57 @@ class PrintManager { break; case HTML: if (content == null || content.isEmpty()) { - printWebView(view, settings); + printWebView(view, settings, callback); } else { - printText(content, "text/html", settings); + printHtml(content, settings, callback); } break; case UNSUPPORTED: // TODO unsupported content case PLAIN: - printText(content, "text/plain", settings); + printText(content, settings, callback); } } + /** + * Prints the HTML content. + * + * @param content The HTML text to print. + * @param settings Additional settings how to render the content. + * @param callback The function to invoke once the job is done. + */ + private void printHtml (@Nullable String content, + @NonNull JSONObject settings, + @NonNull OnPrintFinishCallback callback) + { + printContent(content, "text/html", settings, callback); + } + + /** + * Prints the plain text content. + * + * @param content The plain text to print. + * @param settings Additional settings how to render the content. + * @param callback The function to invoke once the job is done. + */ + private void printText (@Nullable String content, + @NonNull JSONObject settings, + @NonNull OnPrintFinishCallback callback) + { + printContent(content, "text/plain", settings, callback); + } + /** * Prints the markup content. * * @param content The HTML markup to print. + * @param mimeType The mime type to render. * @param settings Additional settings how to render the content. + * @param callback The function to invoke once the job is done. */ - private void printText (@Nullable String content, @NonNull String mimeType, - @NonNull JSONObject settings) + private void printContent (@Nullable String content, @NonNull String mimeType, + @NonNull JSONObject settings, + @NonNull OnPrintFinishCallback callback) { ((Activity) context).runOnUiThread(() -> { view = this.createWebView(settings); @@ -155,7 +191,7 @@ class PrintManager { @Override public void onPageFinished (WebView view, String url) { - printWebView(PrintManager.this.view, settings); + printWebView(PrintManager.this.view, settings, callback); PrintManager.this.view = null; } }); @@ -169,9 +205,11 @@ class PrintManager { * * @param view The web view instance to print. * @param settings Additional settings how to render the content. + * @param callback The function to invoke once the job is done. */ private void printWebView (@NonNull WebView view, - @NonNull JSONObject settings) + @NonNull JSONObject settings, + @NonNull OnPrintFinishCallback callback) { PrintOptions options = new PrintOptions(settings); String jobName = options.getJobName(); @@ -185,7 +223,9 @@ class PrintManager { adapter = view.createPrintDocumentAdapter(); } - printAdapter(adapter, options); + PrintProxy proxy = new PrintProxy(adapter, () -> callback.onFinish(isPrintJobCompleted(jobName))); + + printAdapter(proxy, options); }); } @@ -197,7 +237,7 @@ class PrintManager { * @param callback The function to invoke once the job is done. */ private void printPdf (@NonNull String path, @NonNull JSONObject settings, - @Nullable PrintHelper.OnPrintFinishCallback callback) + @NonNull OnPrintFinishCallback callback) { InputStream stream = PrintContent.open(path, context); @@ -205,7 +245,7 @@ class PrintManager { PrintOptions options = new PrintOptions(settings); String jobName = options.getJobName(); - PrintAdapter adapter = new PrintAdapter(jobName, stream, callback); + PrintAdapter adapter = new PrintAdapter(jobName, stream, () -> callback.onFinish(isPrintJobCompleted(jobName))); printAdapter(adapter, options); } @@ -233,7 +273,7 @@ class PrintManager { * @param callback The function to invoke once the job is done. */ private void printImage (@NonNull String path, @NonNull JSONObject settings, - @Nullable PrintHelper.OnPrintFinishCallback callback) + @NonNull OnPrintFinishCallback callback) { Bitmap bitmap = PrintContent.decode(path, context); @@ -245,7 +285,7 @@ class PrintManager { options.decoratePrintHelper(printer); - printer.printBitmap(jobName, bitmap, callback); + printer.printBitmap(jobName, bitmap, () -> callback.onFinish(isPrintJobCompleted(jobName))); } /** @@ -283,6 +323,37 @@ class PrintManager { return view; } + /** + * Finds the print job by its name. + * + * @param jobName The name of the print job. + * + * @return null if it could not find any job with this label. + */ + @Nullable + private PrintJob findPrintJobByName (@NonNull String jobName) + { + for (PrintJob job : getPrintService().getPrintJobs()) { + if (job.getInfo().getLabel().equals(jobName)) { + return job; + } + } + + return null; + } + + /** + * Returns if the print job is done. + * + * @param jobName The name of the print job. + */ + private boolean isPrintJobCompleted (@NonNull String jobName) + { + PrintJob job = findPrintJobByName(jobName); + + return (job == null || job.getInfo().getState() <= STATE_COMPLETED); + } + /** * Returns the print service of the app. */ diff --git a/src/android/PrintOptions.java b/src/android/PrintOptions.java index 25165eb..e8413c6 100644 --- a/src/android/PrintOptions.java +++ b/src/android/PrintOptions.java @@ -1,4 +1,6 @@ /* + Copyright 2013 Sebastián Katzer + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information diff --git a/src/android/PrintProxy.java b/src/android/PrintProxy.java new file mode 100644 index 0000000..b217622 --- /dev/null +++ b/src/android/PrintProxy.java @@ -0,0 +1,84 @@ +/* + Copyright 2013 Sebastián Katzer + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package de.appplant.cordova.plugin.printer; + +import android.os.Bundle; +import android.os.CancellationSignal; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.support.annotation.NonNull; +import android.support.v4.print.PrintHelper; + +/** + * Simple delegate class to have access to the onFinish method. + */ +class PrintProxy extends PrintDocumentAdapter { + + // Holds the delegate object + private final @NonNull PrintDocumentAdapter delegate; + + // The callback to inform once the job is done + private final @NonNull PrintHelper.OnPrintFinishCallback callback; + + /** + * Constructor + * + * @param adapter The real adapter. + * @param callback The callback to invoke once the printing is done. + */ + PrintProxy (@NonNull PrintDocumentAdapter adapter, + @NonNull PrintHelper.OnPrintFinishCallback callback) + { + this.delegate = adapter; + this.callback = callback; + } + + @Override + public void onLayout (PrintAttributes oldAttributes, + PrintAttributes newAttributes, + CancellationSignal cancellationSignal, + LayoutResultCallback callback, + Bundle bundle) + { + delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, bundle); + } + + @Override + public void onWrite (PageRange[] range, + ParcelFileDescriptor dest, + CancellationSignal cancellationSignal, + WriteResultCallback callback) + { + delegate.onWrite(range, dest, cancellationSignal, callback); + } + + /** + * Invokes the callback. + */ + @Override + public void onFinish () { + super.onFinish(); + callback.onFinish(); + } +} diff --git a/src/android/Printer.java b/src/android/Printer.java index 02f4c99..01b0c6d 100644 --- a/src/android/Printer.java +++ b/src/android/Printer.java @@ -1,32 +1,34 @@ /* - Copyright 2013-2016 appPlant GmbH + Copyright 2013 Sebastián Katzer - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. */ package de.appplant.cordova.plugin.printer; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.webkit.WebView; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.PluginResult; +import org.apache.cordova.PluginResult.Status; import org.json.JSONArray; import org.json.JSONObject; @@ -90,10 +92,7 @@ public class Printer extends CordovaPlugin { PrintManager pm = new PrintManager(cordova.getContext()); boolean printable = pm.canPrintItem(item); - PluginResult res = new PluginResult( - PluginResult.Status.OK, printable); - - callback.sendPluginResult(res); + sendPluginResult(callback, printable); }); } @@ -108,7 +107,7 @@ public class Printer extends CordovaPlugin { JSONArray utis = PrintManager.getPrintableUTIs(); PluginResult res = new PluginResult( - PluginResult.Status.OK, utis); + Status.OK, utis); callback.sendPluginResult(res); }); @@ -129,7 +128,21 @@ public class Printer extends CordovaPlugin { PrintManager pm = new PrintManager(cordova.getContext()); WebView view = (WebView) webView.getView(); - pm.print(content, settings, view, callback::success); + pm.print(content, settings, view, (boolean completed) -> sendPluginResult(callback, completed)); }); } + + /** + * Sends the result back to the client. + * + * @param callback The callback to invoke. + * @param value The argument to pass with. + */ + private void sendPluginResult (@NonNull CallbackContext callback, + boolean value) + { + PluginResult result = new PluginResult(Status.OK, value); + + callback.sendPluginResult(result); + } } diff --git a/src/windows/PrinterProxy.js b/src/windows/PrinterProxy.js index 981368d..e3b9a97 100644 --- a/src/windows/PrinterProxy.js +++ b/src/windows/PrinterProxy.js @@ -1,25 +1,25 @@ /* globals Windows: true */ /* - Copyright 2013-2016 appPlant GmbH + Copyright 2013 Sebastián Katzer - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ var Printing = Windows.Graphics.Printing, PrintManager = Windows.Graphics.Printing.PrintManager; diff --git a/www/printer.js b/www/printer.js index fd7fb13..68a90f8 100755 --- a/www/printer.js +++ b/www/printer.js @@ -1,4 +1,6 @@ /* + Copyright 2013 Sebastián Katzer + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information