PrintJob.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.print;
18
19/**
20 * This class represents a print job from the perspective of
21 * an application.
22 */
23public final class PrintJob {
24
25    private final PrintManager mPrintManager;
26
27    private PrintJobInfo mCachedInfo;
28
29    PrintJob(PrintJobInfo info, PrintManager printManager) {
30        mCachedInfo = info;
31        mPrintManager = printManager;
32    }
33
34    /**
35     * Gets the unique print job id.
36     *
37     * @return The id.
38     */
39    public PrintJobId getId() {
40        return mCachedInfo.getId();
41    }
42
43    /**
44     * Gets the {@link PrintJobInfo} that describes this job.
45     * <p>
46     * <strong>Node:</strong>The returned info object is a snapshot of the
47     * current print job state. Every call to this method returns a fresh
48     * info object that reflects the current print job state.
49     * </p>
50     *
51     * @return The print job info.
52     */
53    public PrintJobInfo getInfo() {
54        if (isInImmutableState()) {
55            return mCachedInfo;
56        }
57        PrintJobInfo info = mPrintManager.getPrintJobInfo(mCachedInfo.getId());
58        if (info != null) {
59            mCachedInfo = info;
60        }
61        return mCachedInfo;
62    }
63
64    /**
65     * Cancels this print job.
66     */
67    public void cancel() {
68        if (!isInImmutableState()) {
69            mPrintManager.cancelPrintJob(mCachedInfo.getId());
70        }
71    }
72
73    private boolean isInImmutableState() {
74        final int state = mCachedInfo.getState();
75        return state == PrintJobInfo.STATE_COMPLETED
76                || state == PrintJobInfo.STATE_CANCELED;
77    }
78
79    @Override
80    public boolean equals(Object obj) {
81        if (this == obj) {
82            return true;
83        }
84        if (obj == null) {
85            return false;
86        }
87        if (getClass() != obj.getClass()) {
88            return false;
89        }
90        PrintJob other = (PrintJob) obj;
91        return mCachedInfo.getId().equals(other.mCachedInfo.getId());
92    }
93
94    @Override
95    public int hashCode() {
96        return mCachedInfo.getId().hashCode();
97    }
98}
99