PrintFileDocumentAdapter.java revision 19fba5d34a444ce145ab0d3a12c578d9d5d3f38c
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package android.print; 18 19import android.content.Context; 20import android.os.AsyncTask; 21import android.os.Bundle; 22import android.os.CancellationSignal; 23import android.os.CancellationSignal.OnCancelListener; 24import android.os.ParcelFileDescriptor; 25import android.util.Log; 26 27import com.android.internal.R; 28 29import libcore.io.IoUtils; 30 31import java.io.File; 32import java.io.FileInputStream; 33import java.io.FileOutputStream; 34import java.io.IOException; 35import java.io.InputStream; 36import java.io.OutputStream; 37 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. 44 */ 45public class PrintFileDocumentAdapter extends PrintDocumentAdapter { 46 47 private static final String LOG_TAG = "PrintedFileDocumentAdapter"; 48 49 private final Context mContext; 50 51 private final File mFile; 52 53 private final PrintDocumentInfo mDocumentInfo; 54 55 private WriteFileAsyncTask mWriteFileAsyncTask; 56 57 /** 58 * Constructor. 59 * 60 * @param context Context for accessing resources. 61 * @param file The PDF file to print. 62 * @param documentInfo The information about the printed file. 63 */ 64 public PrintFileDocumentAdapter(Context context, File file, 65 PrintDocumentInfo documentInfo) { 66 if (file == null) { 67 throw new IllegalArgumentException("File cannot be null!"); 68 } 69 if (documentInfo == null) { 70 throw new IllegalArgumentException("documentInfo cannot be null!"); 71 } 72 mContext = context; 73 mFile = file; 74 mDocumentInfo = documentInfo; 75 } 76 77 @Override 78 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, 79 CancellationSignal cancellationSignal, LayoutResultCallback callback, 80 Bundle metadata) { 81 callback.onLayoutFinished(mDocumentInfo, false); 82 } 83 84 @Override 85 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, 86 CancellationSignal cancellationSignal, WriteResultCallback callback) { 87 mWriteFileAsyncTask = new WriteFileAsyncTask(destination, cancellationSignal, callback); 88 mWriteFileAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 89 (Void[]) null); 90 } 91 92 private final class WriteFileAsyncTask extends AsyncTask<Void, Void, Void> { 93 94 private final ParcelFileDescriptor mDestination; 95 96 private final WriteResultCallback mResultCallback; 97 98 private final CancellationSignal mCancellationSignal; 99 100 public WriteFileAsyncTask(ParcelFileDescriptor destination, 101 CancellationSignal cancellationSignal, WriteResultCallback callback) { 102 mDestination = destination; 103 mResultCallback = callback; 104 mCancellationSignal = cancellationSignal; 105 mCancellationSignal.setOnCancelListener(new OnCancelListener() { 106 @Override 107 public void onCancel() { 108 cancel(true); 109 } 110 }); 111 } 112 113 @Override 114 protected Void doInBackground(Void... params) { 115 InputStream in = null; 116 OutputStream out = new FileOutputStream(mDestination.getFileDescriptor()); 117 final byte[] buffer = new byte[8192]; 118 try { 119 in = new FileInputStream(mFile); 120 while (true) { 121 if (isCancelled()) { 122 break; 123 } 124 final int readByteCount = in.read(buffer); 125 if (readByteCount < 0) { 126 break; 127 } 128 out.write(buffer, 0, readByteCount); 129 } 130 } catch (IOException ioe) { 131 Log.e(LOG_TAG, "Error writing data!", ioe); 132 mResultCallback.onWriteFailed(mContext.getString( 133 R.string.write_fail_reason_cannot_write)); 134 } finally { 135 IoUtils.closeQuietly(in); 136 IoUtils.closeQuietly(out); 137 } 138 return null; 139 } 140 141 @Override 142 protected void onPostExecute(Void result) { 143 mResultCallback.onWriteFinished(new PageRange[] {PageRange.ALL_PAGES}); 144 } 145 146 @Override 147 protected void onCancelled(Void result) { 148 mResultCallback.onWriteFailed(mContext.getString( 149 R.string.write_fail_reason_cancelled)); 150 } 151 } 152} 153 154