diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobChannelDelegate.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobChannelDelegate.java index 1a2165670..b15717f8c 100644 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobChannelDelegate.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobChannelDelegate.java @@ -62,6 +62,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result } } + public void onComplete() { + getChannel().invokeMethod("onComplete", null); + } + @Override public void dispose() { super.dispose(); diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobController.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobController.java index 2283e44f6..985dfd994 100644 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobController.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/print_job/PrintJobController.java @@ -28,16 +28,19 @@ public class PrintJobController implements Disposable { @Nullable public PrintJobSettings settings; - public PrintJobController(@NonNull String id, @NonNull android.print.PrintJob job, - @Nullable PrintJobSettings settings, @NonNull InAppWebViewFlutterPlugin plugin) { + public PrintJobController(@NonNull String id, @Nullable PrintJobSettings settings, + @NonNull InAppWebViewFlutterPlugin plugin) { this.id = id; this.plugin = plugin; - this.job = job; this.settings = settings; final MethodChannel channel = new MethodChannel(plugin.messenger, METHOD_CHANNEL_NAME_PREFIX + id); this.channelDelegate = new PrintJobChannelDelegate(this, channel); } - + + public void setJob(@Nullable android.print.PrintJob job) { + this.job = job; + } + public void cancel() { if (this.job != null) { this.job.cancel(); @@ -93,4 +96,8 @@ public void dispose() { } plugin = null; } + + public void onComplete() { + if (channelDelegate != null) channelDelegate.onComplete(); + } } diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java index bde634bb7..a5fe4ccab 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java @@ -17,9 +17,12 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.CancellationSignal; import android.os.Handler; import android.os.Looper; import android.os.Message; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintManager; @@ -1403,6 +1406,37 @@ public void setDesktopMode(final boolean enabled) { webSettings.setBuiltInZoomControls(enabled); } + interface PrintDocumentAdapterWrapperCallback { + void onFinish(); + } + + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + class PrintDocumentAdapterWrapper extends PrintDocumentAdapter { + @NonNull PrintDocumentAdapter delegate; + @Nullable PrintDocumentAdapterWrapperCallback callback; + + PrintDocumentAdapterWrapper(@NonNull PrintDocumentAdapter delegate, @Nullable PrintDocumentAdapterWrapperCallback callback) { + this.delegate = delegate; + this.callback = callback; + } + + @Override + public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { + this.delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras); + } + + @Override + public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { + this.delegate.onWrite(pages, destination, cancellationSignal, callback); + } + + @Override + public void onFinish() { + this.delegate.onFinish(); + if (this.callback != null) this.callback.onFinish(); + } + } + @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Nullable public String printCurrentPage(@Nullable PrintJobSettings settings) { @@ -1458,15 +1492,21 @@ public String printCurrentPage(@Nullable PrintJobSettings settings) { printAdapter = createPrintDocumentAdapter(); } - // Create a printCurrentPage job with name and adapter instance - android.print.PrintJob job = printManager.print(jobName, printAdapter, builder.build()); + PrintJobController printJobController = null; + String id = null; if (settings != null && settings.handledByClient && plugin.printJobManager != null) { - String id = UUID.randomUUID().toString(); - PrintJobController printJobController = new PrintJobController(id, job, settings, plugin); + id = UUID.randomUUID().toString(); + printJobController = new PrintJobController(id, settings, plugin); plugin.printJobManager.jobs.put(printJobController.id, printJobController); - return id; + printAdapter = new PrintDocumentAdapterWrapper(printAdapter, printJobController::onComplete); } + + // Create a printCurrentPage job with name and adapter instance + android.print.PrintJob job = printManager.print(jobName, printAdapter, builder.build()); + if (printJobController != null) printJobController.setJob(job); + + return id; } else { Log.e(LOG_TAG, "No PrintManager available"); } diff --git a/flutter_inappwebview_android/lib/src/print_job/print_job_controller.dart b/flutter_inappwebview_android/lib/src/print_job/print_job_controller.dart index 768311549..4ed0acb58 100644 --- a/flutter_inappwebview_android/lib/src/print_job/print_job_controller.dart +++ b/flutter_inappwebview_android/lib/src/print_job/print_job_controller.dart @@ -43,6 +43,9 @@ class AndroidPrintJobController extends PlatformPrintJobController Future _handleMethod(MethodCall call) async { switch (call.method) { + case "onComplete": + onComplete?.call(true, null); + break; default: throw UnimplementedError("Unimplemented ${call.method} method"); } diff --git a/flutter_inappwebview_platform_interface/lib/src/print_job/platform_print_job_controller.dart b/flutter_inappwebview_platform_interface/lib/src/print_job/platform_print_job_controller.dart index e2abce439..bd4ff6f8e 100644 --- a/flutter_inappwebview_platform_interface/lib/src/print_job/platform_print_job_controller.dart +++ b/flutter_inappwebview_platform_interface/lib/src/print_job/platform_print_job_controller.dart @@ -73,6 +73,7 @@ abstract class PlatformPrintJobController extends PlatformInterface ///A completion handler used to handle the conclusion of the print job (for instance, to reset state) and to handle any errors encountered in printing. /// ///**Officially Supported Platforms/Implementations**: + ///- Android ([Official API - PrintDocumentAdapter.onFinish](https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish())) ///- iOS ([Official API - UIPrintInteractionController.CompletionHandler](https://developer.apple.com/documentation/uikit/uiprintinteractioncontroller/completionhandler)) ///- MacOS ([Official API - NSPrintOperation.runModal](https://developer.apple.com/documentation/appkit/nsprintoperation/1532065-runmodal)) ///{@endtemplate}