Move to Android N-MR1 SDK.
[android_tools.git] / sdk / sources / android-25 / android / print / PrintFileDocumentAdapter.java
blob747400d486bf9a7dafa59f993d28032207555a8b
1 /*
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;
31 import java.io.File;
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;
38 /**
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.
45 * @hide
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;
59 /**
60 * Constructor.
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) {
68 if (file == null) {
69 throw new IllegalArgumentException("File cannot be null!");
71 if (documentInfo == null) {
72 throw new IllegalArgumentException("documentInfo cannot be null!");
74 mContext = context;
75 mFile = file;
76 mDocumentInfo = documentInfo;
79 @Override
80 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
81 CancellationSignal cancellationSignal, LayoutResultCallback callback,
82 Bundle metadata) {
83 callback.onLayoutFinished(mDocumentInfo, false);
86 @Override
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,
91 (Void[]) null);
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() {
108 @Override
109 public void onCancel() {
110 cancel(true);
115 @Override
116 protected Void doInBackground(Void... params) {
117 InputStream in = null;
118 OutputStream out = new FileOutputStream(mDestination.getFileDescriptor());
119 final byte[] buffer = new byte[8192];
120 try {
121 in = new FileInputStream(mFile);
122 while (true) {
123 if (isCancelled()) {
124 break;
126 final int readByteCount = in.read(buffer);
127 if (readByteCount < 0) {
128 break;
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));
136 } finally {
137 IoUtils.closeQuietly(in);
138 IoUtils.closeQuietly(out);
140 return null;
143 @Override
144 protected void onPostExecute(Void result) {
145 mResultCallback.onWriteFinished(new PageRange[] {PageRange.ALL_PAGES});
148 @Override
149 protected void onCancelled(Void result) {
150 mResultCallback.onWriteFailed(mContext.getString(
151 R.string.write_fail_reason_cancelled));