1 package org
.libreoffice
;
3 import android
.content
.Context
;
4 import android
.os
.Bundle
;
5 import android
.os
.CancellationSignal
;
6 import android
.os
.ParcelFileDescriptor
;
7 import android
.print
.PageRange
;
8 import android
.print
.PrintAttributes
;
9 import android
.print
.PrintDocumentAdapter
;
10 import android
.print
.PrintDocumentInfo
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.FileOutputStream
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
17 import java
.io
.OutputStream
;
19 public class PDFDocumentAdapter
extends PrintDocumentAdapter
{
23 public PDFDocumentAdapter(Context mContext
, String pdfFile
) {
24 this.mContext
= mContext
;
25 this.pdfFile
= pdfFile
;
29 public void onLayout(PrintAttributes oldAttributes
, PrintAttributes newAttributes
, CancellationSignal cancellationSignal
, LayoutResultCallback callback
, Bundle extras
) {
30 if (cancellationSignal
.isCanceled()) {
31 callback
.onLayoutCancelled();
34 File f
= new File(pdfFile
);
35 PrintDocumentInfo
.Builder builder
=
36 new PrintDocumentInfo
.Builder(f
.getName());
37 builder
.setContentType(PrintDocumentInfo
.CONTENT_TYPE_DOCUMENT
)
38 .setPageCount(PrintDocumentInfo
.PAGE_COUNT_UNKNOWN
)
40 callback
.onLayoutFinished(builder
.build(),
41 !newAttributes
.equals(oldAttributes
));
46 public void onWrite(PageRange
[] pages
, ParcelFileDescriptor destination
, CancellationSignal cancellationSignal
, WriteResultCallback callback
) {
48 OutputStream out
=null;
50 File file
= new File(pdfFile
);
51 in
= new FileInputStream(file
);
52 out
=new FileOutputStream(destination
.getFileDescriptor());
54 byte[] buf
=new byte[in
.available()];
57 while ((size
=in
.read(buf
)) >= 0
58 && !cancellationSignal
.isCanceled()) {
59 out
.write(buf
, 0, size
);
62 if (cancellationSignal
.isCanceled()) {
63 callback
.onWriteCancelled();
66 callback
.onWriteFinished(new PageRange
[] { PageRange
.ALL_PAGES
});
70 callback
.onWriteFailed(e
.getMessage());
78 catch (IOException e
) {