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; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 22 | import android.print.PrintJobId; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 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. |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 30 | * <p> |
| 31 | * <strong>Note: </strong> All methods of this class must be executed on the |
| 32 | * main application thread. |
| 33 | * </p> |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 34 | */ |
| 35 | public final class PrintDocument { |
| 36 | |
| 37 | private static final String LOG_TAG = "PrintDocument"; |
| 38 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 39 | private final PrintJobId mPrintJobId; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 40 | |
| 41 | private final IPrintServiceClient mPrintServiceClient; |
| 42 | |
| 43 | private final PrintDocumentInfo mInfo; |
| 44 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 45 | PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient, |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 46 | PrintDocumentInfo info) { |
| 47 | mPrintJobId = printJobId; |
| 48 | mPrintServiceClient = printServiceClient; |
| 49 | mInfo = info; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets the {@link PrintDocumentInfo} that describes this document. |
| 54 | * |
| 55 | * @return The document info. |
| 56 | */ |
| 57 | public PrintDocumentInfo getInfo() { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 58 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 59 | return mInfo; |
| 60 | } |
| 61 | |
| 62 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 63 | * Gets the data associated with this document. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 64 | * <p> |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 65 | * <strong>Note: </strong> It is a responsibility of the client to open a |
| 66 | * stream to the returned file descriptor, fully read the data, and close |
| 67 | * the file descriptor. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 68 | * </p> |
| 69 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 70 | * @return A file descriptor for reading the data. |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 71 | */ |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 72 | public ParcelFileDescriptor getData() { |
| 73 | PrintService.throwIfNotCalledOnMainThread(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 74 | ParcelFileDescriptor source = null; |
| 75 | ParcelFileDescriptor sink = null; |
| 76 | try { |
| 77 | ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 78 | source = fds[0]; |
| 79 | sink = fds[1]; |
| 80 | mPrintServiceClient.writePrintJobData(sink, mPrintJobId); |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 81 | return source; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 82 | } catch (IOException ioe) { |
| 83 | Log.e(LOG_TAG, "Error calling getting print job data!", ioe); |
| 84 | } catch (RemoteException re) { |
| 85 | Log.e(LOG_TAG, "Error calling getting print job data!", re); |
| 86 | } finally { |
| 87 | if (sink != null) { |
| 88 | try { |
| 89 | sink.close(); |
| 90 | } catch (IOException ioe) { |
| 91 | /* ignore */ |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | } |