blob: 0121ae13e32f889bb5baf0aadec47fd618dab060 [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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
Svetoslav Ganova0027152013-06-25 14:59:53 -070021import android.os.ParcelFileDescriptor;
22import android.os.RemoteException;
23import android.print.PrintDocumentInfo;
Svetoslav2fbd2a72013-09-16 17:53:51 -070024import android.print.PrintJobId;
Svetoslav Ganova0027152013-06-25 14:59:53 -070025import android.util.Log;
26
Svetoslav Ganova0027152013-06-25 14:59:53 -070027import 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 Ganovd26d4892013-08-28 14:37:54 -070032 * <p>
33 * <strong>Note: </strong> All methods of this class must be executed on the
34 * main application thread.
35 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -070036 */
37public final class PrintDocument {
38
39 private static final String LOG_TAG = "PrintDocument";
40
Svetoslav2fbd2a72013-09-16 17:53:51 -070041 private final PrintJobId mPrintJobId;
Svetoslav Ganova0027152013-06-25 14:59:53 -070042
43 private final IPrintServiceClient mPrintServiceClient;
44
45 private final PrintDocumentInfo mInfo;
46
Svetoslav2fbd2a72013-09-16 17:53:51 -070047 PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
Svetoslav Ganova0027152013-06-25 14:59:53 -070048 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. Moltmannc43639c2015-12-18 13:58:40 -080059 public @NonNull PrintDocumentInfo getInfo() {
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070060 PrintService.throwIfNotCalledOnMainThread();
Svetoslav Ganova0027152013-06-25 14:59:53 -070061 return mInfo;
62 }
63
64 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070065 * Gets the data associated with this document.
Svetoslav Ganova0027152013-06-25 14:59:53 -070066 * <p>
Svetoslav Ganov798bed62013-08-11 12:29:39 -070067 * <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 Ganova0027152013-06-25 14:59:53 -070070 * </p>
71 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -070072 * @return A file descriptor for reading the data.
Svetoslav Ganova0027152013-06-25 14:59:53 -070073 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080074 public @Nullable ParcelFileDescriptor getData() {
Svetoslav Ganovd26d4892013-08-28 14:37:54 -070075 PrintService.throwIfNotCalledOnMainThread();
Svetoslav Ganova0027152013-06-25 14:59:53 -070076 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 Ganovd26d4892013-08-28 14:37:54 -070083 return source;
Svetoslav Ganova0027152013-06-25 14:59:53 -070084 } 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}