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