blob: e43f2a8363765ee426813d189a080c950d64628e [file] [log] [blame]
Svetoslav Ganova0027152013-06-25 14:59:53 -07001/*
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.printservice;
18
19import android.os.ParcelFileDescriptor;
20import android.os.RemoteException;
21import android.print.PrintDocumentInfo;
Svetoslav2fbd2a72013-09-16 17:53:51 -070022import android.print.PrintJobId;
Svetoslav Ganova0027152013-06-25 14:59:53 -070023import android.util.Log;
24
Svetoslav Ganova0027152013-06-25 14:59:53 -070025import 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 Ganovd26d4892013-08-28 14:37:54 -070030 * <p>
31 * <strong>Note: </strong> All methods of this class must be executed on the
32 * main application thread.
33 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -070034 */
35public final class PrintDocument {
36
37 private static final String LOG_TAG = "PrintDocument";
38
Svetoslav2fbd2a72013-09-16 17:53:51 -070039 private final PrintJobId mPrintJobId;
Svetoslav Ganova0027152013-06-25 14:59:53 -070040
41 private final IPrintServiceClient mPrintServiceClient;
42
43 private final PrintDocumentInfo mInfo;
44
Svetoslav2fbd2a72013-09-16 17:53:51 -070045 PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
Svetoslav Ganova0027152013-06-25 14:59:53 -070046 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 Ganovd26d4892013-08-28 14:37:54 -070058 PrintService.throwIfNotCalledOnMainThread();
Svetoslav Ganova0027152013-06-25 14:59:53 -070059 return mInfo;
60 }
61
62 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070063 * Gets the data associated with this document.
Svetoslav Ganova0027152013-06-25 14:59:53 -070064 * <p>
Svetoslav Ganov798bed62013-08-11 12:29:39 -070065 * <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 Ganova0027152013-06-25 14:59:53 -070068 * </p>
69 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -070070 * @return A file descriptor for reading the data.
Svetoslav Ganova0027152013-06-25 14:59:53 -070071 */
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070072 public ParcelFileDescriptor getData() {
73 PrintService.throwIfNotCalledOnMainThread();
Svetoslav Ganova0027152013-06-25 14:59:53 -070074 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 Ganovd26d4892013-08-28 14:37:54 -070081 return source;
Svetoslav Ganova0027152013-06-25 14:59:53 -070082 } 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}