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
19c43639c3067dda5df189fb3cbf14f256c17e677dPhilip P. Moltmannimport android.annotation.NonNull;
20c43639c3067dda5df189fb3cbf14f256c17e677dPhilip P. Moltmannimport android.annotation.Nullable;
21a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.os.ParcelFileDescriptor;
22a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.os.RemoteException;
23a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.print.PrintDocumentInfo;
242fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslavimport android.print.PrintJobId;
25a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport android.util.Log;
26a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
27a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovimport java.io.IOException;
28a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
29a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov/**
30a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * This class represents a printed document from the perspective of a print
31a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov * service. It exposes APIs to query the document and obtain its data.
32d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * <p>
33d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * <strong>Note: </strong> All methods of this class must be executed on the
34d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * main application thread.
35d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov * </p>
36a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov */
37a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganovpublic final class PrintDocument {
38a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
39a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private static final String LOG_TAG = "PrintDocument";
40a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
412fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslav    private final PrintJobId mPrintJobId;
42a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
43a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private final IPrintServiceClient mPrintServiceClient;
44a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
45a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    private final PrintDocumentInfo mInfo;
46a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
472fbd2a7f070f246ddafd9de94efa9a98861e9136Svetoslav    PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
48a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            PrintDocumentInfo info) {
49a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mPrintJobId = printJobId;
50a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mPrintServiceClient = printServiceClient;
51a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        mInfo = info;
52a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
53a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
54a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    /**
55a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * Gets the {@link PrintDocumentInfo} that describes this document.
56a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     *
57a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * @return The document info.
58a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     */
59c43639c3067dda5df189fb3cbf14f256c17e677dPhilip P. Moltmann    public @NonNull PrintDocumentInfo getInfo() {
60d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov        PrintService.throwIfNotCalledOnMainThread();
61a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        return mInfo;
62a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
63a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov
64a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    /**
65798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * Gets the data associated with this document.
66a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * <p>
67798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * <strong>Note: </strong> It is a responsibility of the client to open a
68798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * stream to the returned file descriptor, fully read the data, and close
69798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * the file descriptor.
70a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     * </p>
71a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     *
72798bed6cc7d273e72b0253288605db9cd2b57740Svetoslav Ganov     * @return A file descriptor for reading the data.
73a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov     */
74c43639c3067dda5df189fb3cbf14f256c17e677dPhilip P. Moltmann    public @Nullable ParcelFileDescriptor getData() {
75d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov        PrintService.throwIfNotCalledOnMainThread();
76a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        ParcelFileDescriptor source = null;
77a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        ParcelFileDescriptor sink = null;
78a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        try {
79a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
80a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            source = fds[0];
81a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            sink = fds[1];
82a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            mPrintServiceClient.writePrintJobData(sink, mPrintJobId);
83d26d4898fcc9b78f4b66118895c375384098205eSvetoslav Ganov            return source;
84a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } catch (IOException ioe) {
85a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            Log.e(LOG_TAG, "Error calling getting print job data!", ioe);
86a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } catch (RemoteException re) {
87a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            Log.e(LOG_TAG, "Error calling getting print job data!", re);
88a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        } finally {
89a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            if (sink != null) {
90a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                try {
91a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                    sink.close();
92a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                } catch (IOException ioe) {
93a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                    /* ignore */
94a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov                }
95a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov            }
96a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        }
97a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov        return null;
98a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov    }
99a00271533f639c8ed36429c663889ac9f654bc72Svetoslav Ganov}
100