Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1025 android: enable window.print() #1026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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