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