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