PrintDocument.java revision 2fbd2a7f070f246ddafd9de94efa9a98861e9136
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
17package android.printservice;
18
19import android.os.ParcelFileDescriptor;
20import android.os.RemoteException;
21import android.print.PrintDocumentInfo;
22import android.print.PrintJobId;
23import android.util.Log;
24
25import 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 * <p>
31 * <strong>Note: </strong> All methods of this class must be executed on the
32 * main application thread.
33 * </p>
34 */
35public final class PrintDocument {
36
37    private static final String LOG_TAG = "PrintDocument";
38
39    private final PrintJobId mPrintJobId;
40
41    private final IPrintServiceClient mPrintServiceClient;
42
43    private final PrintDocumentInfo mInfo;
44
45    PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
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() {
58        PrintService.throwIfNotCalledOnMainThread();
59        return mInfo;
60    }
61
62    /**
63     * Gets the data associated with this document.
64     * <p>
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.
68     * </p>
69     *
70     * @return A file descriptor for reading the data.
71     */
72    public ParcelFileDescriptor getData() {
73        PrintService.throwIfNotCalledOnMainThread();
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);
81            return source;
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}
98