PrintDocumentInfo.java revision 88d199130d44c6bacb383a7757e782cf97483c68
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 19import android.os.Parcel; 20import android.os.Parcelable; 21 22/** 23 * This class encapsulates information about a printed document. 24 */ 25public final class PrintDocumentInfo implements Parcelable { 26 27 /** 28 * Constant for unknown page count (default). 29 */ 30 public static final int PAGE_COUNT_UNKNOWN = -1; 31 32 /** 33 * Content type: unknown (default). 34 */ 35 public static final int CONTENT_TYPE_UNKNOWN = -1; 36 37 /** 38 * Content type: document. 39 */ 40 public static final int CONTENT_TYPE_DOCUMENT = 0; 41 42 /** 43 * Content type: photo. 44 */ 45 public static final int CONTENT_TYPE_PHOTO = 1; 46 47 private int mPageCount; 48 private int mContentType; 49 50 /** 51 * Creates a new instance. 52 */ 53 private PrintDocumentInfo() { 54 mPageCount = PAGE_COUNT_UNKNOWN; 55 mContentType = CONTENT_TYPE_UNKNOWN; 56 } 57 58 /** 59 * Creates a new instance. 60 * 61 * @param Prototype from which to clone. 62 */ 63 private PrintDocumentInfo(PrintDocumentInfo prototype) { 64 mPageCount = prototype.mPageCount; 65 mContentType = prototype.mContentType; 66 } 67 68 /** 69 * Creates a new instance. 70 * 71 * @param parcel Data from which to initialize. 72 */ 73 private PrintDocumentInfo(Parcel parcel) { 74 mPageCount = parcel.readInt(); 75 mContentType = parcel.readInt(); 76 } 77 78 /** 79 * Gets the total number of pages. 80 * 81 * @return The number of pages. 82 * 83 * @see #PAGE_COUNT_UNKNOWN 84 */ 85 public int getPageCount() { 86 return mPageCount; 87 } 88 89 /** 90 * Gets the content type. 91 * 92 * @return The content type. 93 * 94 * @see #CONTENT_TYPE_UNKNOWN 95 * @see #CONTENT_TYPE_DOCUMENT 96 * @see #CONTENT_TYPE_PHOTO 97 */ 98 public int getContentType() { 99 return mContentType; 100 } 101 102 @Override 103 public int describeContents() { 104 return 0; 105 } 106 107 @Override 108 public void writeToParcel(Parcel parcel, int flags) { 109 parcel.writeInt(mPageCount); 110 parcel.writeInt(mContentType); 111 } 112 113 @Override 114 public String toString() { 115 StringBuilder builder = new StringBuilder(); 116 builder.append("PrintDocumentInfo{"); 117 builder.append("pageCount: ").append(mPageCount); 118 builder.append(", contentType: ").append(mContentType); 119 builder.append("}"); 120 return builder.toString(); 121 } 122 123 /** 124 * Builder for creating an {@link PrintDocumentInfo}. 125 */ 126 public static final class Builder { 127 private final PrintDocumentInfo mPrototype = new PrintDocumentInfo(); 128 129 /** 130 * Sets the total number of pages. 131 * 132 * @param pageCount The number of pages. Must be greater than 133 * or equal to zero or {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}. 134 */ 135 public Builder setPageCount(int pageCount) { 136 if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) { 137 throw new IllegalArgumentException("pageCount" 138 + " must be greater than or euqal to zero or" 139 + " DocumentInfo#PAGE_COUNT_UNKNOWN"); 140 } 141 mPrototype.mPageCount = pageCount; 142 return this; 143 } 144 145 /** 146 * Sets the content type. 147 * 148 * @param type The content type. 149 * 150 * @see #CONTENT_TYPE_UNKNOWN 151 * @see #CONTENT_TYPE_DOCUMENT 152 * @see #CONTENT_TYPE_PHOTO 153 */ 154 public Builder setContentType(int type) { 155 mPrototype.mContentType = type; 156 return this; 157 } 158 159 /** 160 * Creates a new {@link PrintDocumentInfo} instance. 161 * 162 * @return The new instance. 163 */ 164 public PrintDocumentInfo create() { 165 return new PrintDocumentInfo(mPrototype); 166 } 167 } 168 169 public static final Parcelable.Creator<PrintDocumentInfo> CREATOR = 170 new Creator<PrintDocumentInfo>() { 171 @Override 172 public PrintDocumentInfo createFromParcel(Parcel parcel) { 173 return new PrintDocumentInfo(parcel); 174 } 175 176 @Override 177 public PrintDocumentInfo[] newArray(int size) { 178 return new PrintDocumentInfo[size]; 179 } 180 }; 181} 182