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.annotation.NonNull;
20import android.annotation.Nullable;
21import android.os.ParcelFileDescriptor;
22import android.os.RemoteException;
23import android.print.PrintDocumentInfo;
24import android.print.PrintJobId;
25import android.util.Log;
26
27import 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.
32 * <p>
33 * <strong>Note: </strong> All methods of this class must be executed on the
34 * main application thread.
35 * </p>
36 */
37public final class PrintDocument {
38
39    private static final String LOG_TAG = "PrintDocument";
40
41    private final PrintJobId mPrintJobId;
42
43    private final IPrintServiceClient mPrintServiceClient;
44
45    private final PrintDocumentInfo mInfo;
46
47    PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
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     */
59    public @NonNull PrintDocumentInfo getInfo() {
60        PrintService.throwIfNotCalledOnMainThread();
61        return mInfo;
62    }
63
64    /**
65     * Gets the data associated with this document.
66     * <p>
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.
70     * </p>
71     *
72     * @return A file descriptor for reading the data.
73     */
74    public @Nullable ParcelFileDescriptor getData() {
75        PrintService.throwIfNotCalledOnMainThread();
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);
83            return source;
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}
100