This source file includes following definitions.
- JNINamespace
- exportToPdf
- setNativeAwPdfExporter
- getPrintDpi
- didExportPdf
- getPageWidth
- getPageHeight
- getDpi
- getLeftMargin
- getRightMargin
- getTopMargin
- getBottomMargin
- nativeExportToPdf
package org.chromium.android_webview;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PrintAttributes;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
@JNINamespace("android_webview")
public class AwPdfExporter {
private static final String TAG = "AwPdfExporter";
private long mNativeAwPdfExporter;
private ValueCallback<Boolean> mResultCallback;
private PrintAttributes mAttributes;
private ParcelFileDescriptor mFd;
private final ViewGroup mContainerView;
AwPdfExporter(ViewGroup containerView) {
mContainerView = containerView;
}
public void exportToPdf(final ParcelFileDescriptor fd, PrintAttributes attributes,
ValueCallback<Boolean> resultCallback, CancellationSignal cancellationSignal) {
if (fd == null) {
throw new IllegalArgumentException("fd cannot be null");
}
if (resultCallback == null) {
throw new IllegalArgumentException("resultCallback cannot be null");
}
if (mResultCallback != null) {
throw new IllegalStateException("printing is already pending");
}
if (attributes.getMediaSize() == null) {
throw new IllegalArgumentException("attributes must specify a media size");
}
if (attributes.getResolution() == null) {
throw new IllegalArgumentException("attributes must specify print resolution");
}
if (attributes.getMinMargins() == null) {
throw new IllegalArgumentException("attributes must specify margins");
}
if (mNativeAwPdfExporter == 0) {
resultCallback.onReceiveValue(false);
return;
}
mResultCallback = resultCallback;
mAttributes = attributes;
mFd = fd;
nativeExportToPdf(mNativeAwPdfExporter, mFd.getFd(), cancellationSignal);
}
@CalledByNative
private void setNativeAwPdfExporter(long nativePdfExporter) {
mNativeAwPdfExporter = nativePdfExporter;
if (nativePdfExporter == 0 && mResultCallback != null) {
mResultCallback.onReceiveValue(false);
mResultCallback = null;
}
}
private static int getPrintDpi(PrintAttributes attributes) {
int horizontalDpi = attributes.getResolution().getHorizontalDpi();
int verticalDpi = attributes.getResolution().getVerticalDpi();
if (horizontalDpi != verticalDpi) {
Log.w(TAG, "Horizontal and vertical DPIs differ. Using horizontal DPI " +
" hDpi=" + horizontalDpi + " vDPI=" + verticalDpi);
}
return horizontalDpi;
}
@CalledByNative
private void didExportPdf(boolean success) {
mResultCallback.onReceiveValue(success);
mResultCallback = null;
mAttributes = null;
mFd = null;
}
@CalledByNative
private int getPageWidth() {
return mAttributes.getMediaSize().getWidthMils();
}
@CalledByNative
private int getPageHeight() {
return mAttributes.getMediaSize().getHeightMils();
}
@CalledByNative
private int getDpi() {
return getPrintDpi(mAttributes);
}
@CalledByNative
private int getLeftMargin() {
return mAttributes.getMinMargins().getLeftMils();
}
@CalledByNative
private int getRightMargin() {
return mAttributes.getMinMargins().getRightMils();
}
@CalledByNative
private int getTopMargin() {
return mAttributes.getMinMargins().getTopMils();
}
@CalledByNative
private int getBottomMargin() {
return mAttributes.getMinMargins().getBottomMils();
}
private native void nativeExportToPdf(long nativeAwPdfExporter, int fd,
CancellationSignal cancellationSignal);
}