1a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov/*
2a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * Copyright (C) 2013 The Android Open Source Project
3a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov *
4a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License");
5a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * you may not use this file except in compliance with the License.
6a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * You may obtain a copy of the License at
7a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov *
8a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov *      http://www.apache.org/licenses/LICENSE-2.0
9a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov *
10a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software
11a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS,
12a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * See the License for the specific language governing permissions and
14a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * limitations under the License.
15a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov */
16a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
17a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovpackage android.printservice;
18a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
19a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.os.ParcelFileDescriptor;
20a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.os.RemoteException;
21a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.print.PrintDocumentInfo;
222fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslavimport android.print.PrintJobId;
23a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.util.Log;
24a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
25a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport java.io.IOException;
26a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
27a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov/**
28a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * This class represents a printed document from the perspective of a print
29a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * service. It exposes APIs to query the document and obtain its data.
30d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * <p>
31d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * <strong>Note: </strong> All methods of this class must be executed on the
32d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * main application thread.
33d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * </p>
34a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov */
35a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovpublic final class PrintDocument {
36a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
37a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private static final String LOG_TAG = "PrintDocument";
38a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
392fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslav    private final PrintJobId mPrintJobId;
40a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
41a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private final IPrintServiceClient mPrintServiceClient;
42a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
43a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private final PrintDocumentInfo mInfo;
44a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
452fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslav    PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
46a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            PrintDocumentInfo info) {
47a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mPrintJobId = printJobId;
48a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mPrintServiceClient = printServiceClient;
49a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mInfo = info;
50a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
51a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
52a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    /**
53a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * Gets the {@link PrintDocumentInfo} that describes this document.
54a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     *
55a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * @return The document info.
56a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     */
57a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    public PrintDocumentInfo getInfo() {
58d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov        PrintService.throwIfNotCalledOnMainThread();
59a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        return mInfo;
60a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
61a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
62a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    /**
63798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * Gets the data associated with this document.
64a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * <p>
65798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * <strong>Note: </strong> It is a responsibility of the client to open a
66798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * stream to the returned file descriptor, fully read the data, and close
67798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * the file descriptor.
68a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * </p>
69a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     *
70798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * @return A file descriptor for reading the data.
71a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     */
72d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov    public ParcelFileDescriptor getData() {
73d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov        PrintService.throwIfNotCalledOnMainThread();
74a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        ParcelFileDescriptor source = null;
75a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        ParcelFileDescriptor sink = null;
76a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        try {
77a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
78a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            source = fds[0];
79a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            sink = fds[1];
80a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            mPrintServiceClient.writePrintJobData(sink, mPrintJobId);
81d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov            return source;
82a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } catch (IOException ioe) {
83a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            Log.e(LOG_TAG, "Error calling getting print job data!", ioe);
84a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } catch (RemoteException re) {
85a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            Log.e(LOG_TAG, "Error calling getting print job data!", re);
86a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } finally {
87a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            if (sink != null) {
88a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                try {
89a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                    sink.close();
90a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                } catch (IOException ioe) {
91a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                    /* ignore */
92a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                }
93a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            }
94a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        }
95a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        return null;
96a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
97a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov}
98