Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 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 | |
| 17 | package android.printservice; |
| 18 | |
| 19 | import android.os.ParcelFileDescriptor; |
| 20 | import android.os.RemoteException; |
| 21 | import android.print.PrintDocumentInfo; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.io.FileDescriptor; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | /** |
| 28 | * This class represents a printed document from the perspective of a print |
| 29 | * service. It exposes APIs to query the document and obtain its data. |
| 30 | */ |
| 31 | public final class PrintDocument { |
| 32 | |
| 33 | private static final String LOG_TAG = "PrintDocument"; |
| 34 | |
| 35 | private final int mPrintJobId; |
| 36 | |
| 37 | private final IPrintServiceClient mPrintServiceClient; |
| 38 | |
| 39 | private final PrintDocumentInfo mInfo; |
| 40 | |
| 41 | PrintDocument(int printJobId, IPrintServiceClient printServiceClient, |
| 42 | PrintDocumentInfo info) { |
| 43 | mPrintJobId = printJobId; |
| 44 | mPrintServiceClient = printServiceClient; |
| 45 | mInfo = info; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Gets the {@link PrintDocumentInfo} that describes this document. |
| 50 | * |
| 51 | * @return The document info. |
| 52 | */ |
| 53 | public PrintDocumentInfo getInfo() { |
| 54 | return mInfo; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets the data associated with this document. It is a responsibility of the |
| 59 | * client to open a stream to the returned file descriptor and fully read the |
| 60 | * data. |
| 61 | * <p> |
| 62 | * <strong>Note:</strong> It is your responsibility to close the file descriptor. |
| 63 | * </p> |
| 64 | * |
| 65 | * @return A file descriptor for reading the data or <code>null</code>. |
| 66 | */ |
| 67 | public FileDescriptor getData() { |
| 68 | ParcelFileDescriptor source = null; |
| 69 | ParcelFileDescriptor sink = null; |
| 70 | try { |
| 71 | ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 72 | source = fds[0]; |
| 73 | sink = fds[1]; |
| 74 | mPrintServiceClient.writePrintJobData(sink, mPrintJobId); |
| 75 | return source.getFileDescriptor(); |
| 76 | } catch (IOException ioe) { |
| 77 | Log.e(LOG_TAG, "Error calling getting print job data!", ioe); |
| 78 | } catch (RemoteException re) { |
| 79 | Log.e(LOG_TAG, "Error calling getting print job data!", re); |
| 80 | } finally { |
| 81 | if (sink != null) { |
| 82 | try { |
| 83 | sink.close(); |
| 84 | } catch (IOException ioe) { |
| 85 | /* ignore */ |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | } |