From 26d927f09f0252357fa405ff319878896675a154 Mon Sep 17 00:00:00 2001 From: Victor Elci Date: Mon, 25 Sep 2023 14:52:52 -0400 Subject: [PATCH] GH-1025 android: enable window.print() --- src/android/InAppBrowser.java | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index eed595016..c7aafc2b3 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -62,6 +62,10 @@ Licensed to the Apache Software Foundation (ASF) under one import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; +import android.print.PrintManager; +import android.print.PrintJob; +import android.print.PrintDocumentAdapter; +import android.print.PrintAttributes; import org.apache.cordova.CallbackContext; import org.apache.cordova.Config; @@ -971,9 +975,9 @@ public void onDownloadStart( } } } - ); + ); - // Add postMessage interface + // Add print/postMessage interfaces class JsObject { @JavascriptInterface public void postMessage(String data) { @@ -986,6 +990,21 @@ public void postMessage(String data) { LOG.e(LOG_TAG, "data object passed to postMessage has caused a JSON error."); } } + + @JavascriptInterface + public void print(String jobName) { + inAppWebView.post(() -> { + // Get a PrintManager instance + PrintManager printManager = (PrintManager) cordova.getActivity() + .getSystemService(Context.PRINT_SERVICE); + + PrintDocumentAdapter printAdapter = inAppWebView.createPrintDocumentAdapter(jobName); + + // Create a print job with name and adapter instance + printManager.print(jobName, printAdapter, + new PrintAttributes.Builder().build()); + }); + } } settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture); @@ -1375,6 +1394,9 @@ public void onPageFinished(WebView view, String url) { // Set the namespace for postMessage() injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null); + // override window.print + injectDeferredObject("window.print=()=>{cordova_iab.print(window.document.title);}", null); + // CB-10395 InAppBrowser's WebView not storing cookies reliable to local device storage CookieManager.getInstance().flush();