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