Skip to content

Commit

Permalink
apacheGH-1025 android: enable window.print()
Browse files Browse the repository at this point in the history
  • Loading branch information
veelci committed Sep 25, 2023
1 parent d533f2a commit 26d927f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -971,9 +975,9 @@ public void onDownloadStart(
}
}
}
);
);

// Add postMessage interface
// Add print/postMessage interfaces
class JsObject {
@JavascriptInterface
public void postMessage(String data) {
Expand All @@ -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);
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 26d927f

Please sign in to comment.