2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android
.print
;
19 import android
.content
.Context
;
20 import android
.os
.AsyncTask
;
21 import android
.os
.Bundle
;
22 import android
.os
.CancellationSignal
;
23 import android
.os
.CancellationSignal
.OnCancelListener
;
24 import android
.os
.ParcelFileDescriptor
;
25 import android
.util
.Log
;
27 import com
.android
.internal
.R
;
29 import libcore
.io
.IoUtils
;
32 import java
.io
.FileInputStream
;
33 import java
.io
.FileOutputStream
;
34 import java
.io
.IOException
;
35 import java
.io
.InputStream
;
36 import java
.io
.OutputStream
;
39 * Adapter for printing PDF files. This class could be useful if you
40 * want to print a file and intercept when the system is ready
41 * spooling the data, so you can delete the file if it is a
42 * temporary one. To achieve this one must override {@link #onFinish()}
43 * and delete the file yourself.
47 public class PrintFileDocumentAdapter
extends PrintDocumentAdapter
{
49 private static final String LOG_TAG
= "PrintedFileDocAdapter";
51 private final Context mContext
;
53 private final File mFile
;
55 private final PrintDocumentInfo mDocumentInfo
;
57 private WriteFileAsyncTask mWriteFileAsyncTask
;
62 * @param context Context for accessing resources.
63 * @param file The PDF file to print.
64 * @param documentInfo The information about the printed file.
66 public PrintFileDocumentAdapter(Context context
, File file
,
67 PrintDocumentInfo documentInfo
) {
69 throw new IllegalArgumentException("File cannot be null!");
71 if (documentInfo
== null) {
72 throw new IllegalArgumentException("documentInfo cannot be null!");
76 mDocumentInfo
= documentInfo
;
80 public void onLayout(PrintAttributes oldAttributes
, PrintAttributes newAttributes
,
81 CancellationSignal cancellationSignal
, LayoutResultCallback callback
,
83 callback
.onLayoutFinished(mDocumentInfo
, false);
87 public void onWrite(PageRange
[] pages
, ParcelFileDescriptor destination
,
88 CancellationSignal cancellationSignal
, WriteResultCallback callback
) {
89 mWriteFileAsyncTask
= new WriteFileAsyncTask(destination
, cancellationSignal
, callback
);
90 mWriteFileAsyncTask
.executeOnExecutor(AsyncTask
.THREAD_POOL_EXECUTOR
,
94 private final class WriteFileAsyncTask
extends AsyncTask
<Void
, Void
, Void
> {
96 private final ParcelFileDescriptor mDestination
;
98 private final WriteResultCallback mResultCallback
;
100 private final CancellationSignal mCancellationSignal
;
102 public WriteFileAsyncTask(ParcelFileDescriptor destination
,
103 CancellationSignal cancellationSignal
, WriteResultCallback callback
) {
104 mDestination
= destination
;
105 mResultCallback
= callback
;
106 mCancellationSignal
= cancellationSignal
;
107 mCancellationSignal
.setOnCancelListener(new OnCancelListener() {
109 public void onCancel() {
116 protected Void
doInBackground(Void
... params
) {
117 InputStream in
= null;
118 OutputStream out
= new FileOutputStream(mDestination
.getFileDescriptor());
119 final byte[] buffer
= new byte[8192];
121 in
= new FileInputStream(mFile
);
126 final int readByteCount
= in
.read(buffer
);
127 if (readByteCount
< 0) {
130 out
.write(buffer
, 0, readByteCount
);
132 } catch (IOException ioe
) {
133 Log
.e(LOG_TAG
, "Error writing data!", ioe
);
134 mResultCallback
.onWriteFailed(mContext
.getString(
135 R
.string
.write_fail_reason_cannot_write
));
137 IoUtils
.closeQuietly(in
);
138 IoUtils
.closeQuietly(out
);
144 protected void onPostExecute(Void result
) {
145 mResultCallback
.onWriteFinished(new PageRange
[] {PageRange
.ALL_PAGES
});
149 protected void onCancelled(Void result
) {
150 mResultCallback
.onWriteFailed(mContext
.getString(
151 R
.string
.write_fail_reason_cancelled
));