blob: 4d5ccc0e803efbd402415593b303e10b470b559a [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
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
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080019import android.annotation.DrawableRes;
Philip P. Moltmannbb362062015-12-22 20:08:00 -080020import android.annotation.IntDef;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080021import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.annotation.TestApi;
24import android.app.PendingIntent;
25import android.content.Context;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.PackageManager.NameNotFoundException;
30import android.graphics.drawable.Drawable;
31import android.graphics.drawable.Icon;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070032import android.os.Parcel;
33import android.os.Parcelable;
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070034import android.service.print.PrinterInfoProto;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070035import android.text.TextUtils;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070036
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080037import com.android.internal.util.Preconditions;
38
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080039import java.lang.annotation.Retention;
40import java.lang.annotation.RetentionPolicy;
41
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070042/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070043 * This class represents the description of a printer. Instances of
44 * this class are created by print services to report to the system
45 * the printers they manage. The information of this class has two
46 * major components, printer properties such as name, id, status,
47 * description and printer capabilities which describe the various
48 * print modes a printer supports such as media sizes, margins, etc.
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080049 * <p>
50 * Once {@link PrinterInfo.Builder#build() built} the objects are immutable.
51 * </p>
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070052 */
53public final class PrinterInfo implements Parcelable {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070054
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080055 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070056 @IntDef(prefix = { "STATUS_" }, value = {
57 STATUS_IDLE,
58 STATUS_BUSY,
59 STATUS_UNAVAILABLE
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080060 })
61 @Retention(RetentionPolicy.SOURCE)
62 public @interface Status {
63 }
Jeff Sharkeyce8db992017-12-13 20:05:05 -070064
Svetoslav Ganovaec14172013-08-27 00:30:54 -070065 /** Printer status: the printer is idle and ready to print. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070066 public static final int STATUS_IDLE = PrinterInfoProto.STATUS_IDLE;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070067
Svetoslav Ganovaec14172013-08-27 00:30:54 -070068 /** Printer status: the printer is busy printing. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070069 public static final int STATUS_BUSY = PrinterInfoProto.STATUS_BUSY;
Svetoslav Ganovaec14172013-08-27 00:30:54 -070070
71 /** Printer status: the printer is not available. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070072 public static final int STATUS_UNAVAILABLE = PrinterInfoProto.STATUS_UNAVAILABLE;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070073
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080074 private final @NonNull PrinterId mId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070075
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080076 /** Resource inside the printer's services's package to be used as an icon */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080077 private final int mIconResourceId;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080078
79 /** If a custom icon can be loaded for the printer */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080080 private final boolean mHasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080081
82 /** The generation of the icon in the cache. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080083 private final int mCustomPrinterIconGen;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080084
85 /** Intent that launches the activity showing more information about the printer. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080086 private final @Nullable PendingIntent mInfoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080087
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080088 private final @NonNull String mName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070089
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080090 private final @Status int mStatus;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070091
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080092 private final @Nullable String mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070093
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080094 private final @Nullable PrinterCapabilitiesInfo mCapabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070095
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080096 private PrinterInfo(@NonNull PrinterId printerId, @NonNull String name, @Status int status,
97 int iconResourceId, boolean hasCustomPrinterIcon, String description,
98 PendingIntent infoIntent, PrinterCapabilitiesInfo capabilities,
99 int customPrinterIconGen) {
100 mId = printerId;
101 mName = name;
102 mStatus = status;
103 mIconResourceId = iconResourceId;
104 mHasCustomPrinterIcon = hasCustomPrinterIcon;
105 mDescription = description;
106 mInfoIntent = infoIntent;
107 mCapabilities = capabilities;
108 mCustomPrinterIconGen = customPrinterIconGen;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700109 }
110
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700111 /**
112 * Get the globally unique printer id.
113 *
114 * @return The printer id.
115 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800116 public @NonNull PrinterId getId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700117 return mId;
118 }
119
120 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800121 * Get the icon to be used for this printer. If no per printer icon is available, the printer's
122 * service's icon is returned. If the printer has a custom icon this icon might get requested
123 * asynchronously. Once the icon is loaded the discovery sessions will be notified that the
124 * printer changed.
125 *
126 * @param context The context that will be using the icons
127 * @return The icon to be used for the printer or null if no icon could be found.
128 * @hide
129 */
130 @TestApi
131 public @Nullable Drawable loadIcon(@NonNull Context context) {
132 Drawable drawable = null;
133 PackageManager packageManager = context.getPackageManager();
134
135 if (mHasCustomPrinterIcon) {
136 PrintManager printManager = (PrintManager) context
137 .getSystemService(Context.PRINT_SERVICE);
138
139 Icon icon = printManager.getCustomPrinterIcon(mId);
140
141 if (icon != null) {
142 drawable = icon.loadDrawable(context);
143 }
144 }
145
146 if (drawable == null) {
147 try {
148 String packageName = mId.getServiceName().getPackageName();
149 PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
150 ApplicationInfo appInfo = packageInfo.applicationInfo;
151
152 // If no custom icon is available, try the icon from the resources
153 if (mIconResourceId != 0) {
154 drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo);
155 }
156
157 // Fall back to the printer's service's icon if no per printer icon could be found
158 if (drawable == null) {
159 drawable = appInfo.loadIcon(packageManager);
160 }
161 } catch (NameNotFoundException e) {
162 }
163 }
164
165 return drawable;
166 }
167
168 /**
Philip P. Moltmann2e301262016-06-16 12:39:54 -0700169 * Check if the printer has a custom printer icon.
170 *
171 * @return {@code true} iff the printer has a custom printer icon.
172 *
173 * @hide
174 */
175 public boolean getHasCustomPrinterIcon() {
176 return mHasCustomPrinterIcon;
177 }
178
179 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700180 * Get the printer name.
181 *
182 * @return The printer name.
183 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800184 public @NonNull String getName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700185 return mName;
186 }
187
188 /**
189 * Gets the printer status.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700190 *
191 * @return The status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700192 *
193 * @see #STATUS_BUSY
194 * @see #STATUS_IDLE
195 * @see #STATUS_UNAVAILABLE
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700196 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800197 public @Status int getStatus() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700198 return mStatus;
199 }
200
201 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700202 * Gets the printer description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700203 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700204 * @return The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700205 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800206 public @Nullable String getDescription() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700207 return mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700208 }
209
210 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800211 * Get the {@link PendingIntent} that launches the activity showing more information about the
212 * printer.
213 *
214 * @return the {@link PendingIntent} that launches the activity showing more information about
215 * the printer or null if it is not configured
216 * @hide
217 */
218 public @Nullable PendingIntent getInfoIntent() {
219 return mInfoIntent;
220 }
221
222 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700223 * Gets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700224 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700225 * @return The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700226 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800227 public @Nullable PrinterCapabilitiesInfo getCapabilities() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700228 return mCapabilities;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700229 }
230
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800231 /**
232 * Check if printerId is valid.
233 *
234 * @param printerId The printerId that might be valid
235 * @return The valid printerId
236 * @throws IllegalArgumentException if printerId is not valid.
237 */
238 private static @NonNull PrinterId checkPrinterId(PrinterId printerId) {
239 return Preconditions.checkNotNull(printerId, "printerId cannot be null.");
240 }
241
242 /**
243 * Check if status is valid.
244 *
245 * @param status The status that might be valid
246 * @return The valid status
247 * @throws IllegalArgumentException if status is not valid.
248 */
249 private static @Status int checkStatus(int status) {
250 if (!(status == STATUS_IDLE
251 || status == STATUS_BUSY
252 || status == STATUS_UNAVAILABLE)) {
253 throw new IllegalArgumentException("status is invalid.");
254 }
255
256 return status;
257 }
258
259 /**
260 * Check if name is valid.
261 *
262 * @param name The name that might be valid
263 * @return The valid name
264 * @throws IllegalArgumentException if name is not valid.
265 */
266 private static @NonNull String checkName(String name) {
267 return Preconditions.checkStringNotEmpty(name, "name cannot be empty.");
268 }
269
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700270 private PrinterInfo(Parcel parcel) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800271 // mName can be null due to unchecked set in Builder.setName and status can be invalid
272 // due to unchecked set in Builder.setStatus, hence we can only check mId for a valid state
273 mId = checkPrinterId((PrinterId) parcel.readParcelable(null));
274 mName = checkName(parcel.readString());
275 mStatus = checkStatus(parcel.readInt());
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700276 mDescription = parcel.readString();
277 mCapabilities = parcel.readParcelable(null);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800278 mIconResourceId = parcel.readInt();
279 mHasCustomPrinterIcon = parcel.readByte() != 0;
280 mCustomPrinterIconGen = parcel.readInt();
281 mInfoIntent = parcel.readParcelable(null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700282 }
283
284 @Override
285 public int describeContents() {
286 return 0;
287 }
288
289 @Override
290 public void writeToParcel(Parcel parcel, int flags) {
291 parcel.writeParcelable(mId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700292 parcel.writeString(mName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700293 parcel.writeInt(mStatus);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700294 parcel.writeString(mDescription);
295 parcel.writeParcelable(mCapabilities, flags);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800296 parcel.writeInt(mIconResourceId);
297 parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0));
298 parcel.writeInt(mCustomPrinterIconGen);
299 parcel.writeParcelable(mInfoIntent, flags);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700300 }
301
302 @Override
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700303 public int hashCode() {
304 final int prime = 31;
305 int result = 1;
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800306 result = prime * result + mId.hashCode();
307 result = prime * result + mName.hashCode();
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700308 result = prime * result + mStatus;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700309 result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
310 result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800311 result = prime * result + mIconResourceId;
312 result = prime * result + (mHasCustomPrinterIcon ? 1 : 0);
313 result = prime * result + mCustomPrinterIconGen;
314 result = prime * result + ((mInfoIntent != null) ? mInfoIntent.hashCode() : 0);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700315 return result;
316 }
317
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800318 /**
319 * Compare two {@link PrinterInfo printerInfos} in all aspects beside being null and the
320 * {@link #mStatus}.
321 *
322 * @param other the other {@link PrinterInfo}
323 * @return true iff the infos are equivalent
324 * @hide
325 */
326 public boolean equalsIgnoringStatus(PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800327 if (!mId.equals(other.mId)) {
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800328 return false;
329 }
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800330 if (!mName.equals(other.mName)) {
331 return false;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800332 }
333 if (!TextUtils.equals(mDescription, other.mDescription)) {
334 return false;
335 }
336 if (mCapabilities == null) {
337 if (other.mCapabilities != null) {
338 return false;
339 }
340 } else if (!mCapabilities.equals(other.mCapabilities)) {
341 return false;
342 }
343 if (mIconResourceId != other.mIconResourceId) {
344 return false;
345 }
346 if (mHasCustomPrinterIcon != other.mHasCustomPrinterIcon) {
347 return false;
348 }
349 if (mCustomPrinterIconGen != other.mCustomPrinterIconGen) {
350 return false;
351 }
352 if (mInfoIntent == null) {
353 if (other.mInfoIntent != null) {
354 return false;
355 }
356 } else if (!mInfoIntent.equals(other.mInfoIntent)) {
357 return false;
358 }
359 return true;
360 }
361
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700362 @Override
363 public boolean equals(Object obj) {
364 if (this == obj) {
365 return true;
366 }
367 if (obj == null) {
368 return false;
369 }
370 if (getClass() != obj.getClass()) {
371 return false;
372 }
373 PrinterInfo other = (PrinterInfo) obj;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800374 if (!equalsIgnoringStatus(other)) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700375 return false;
376 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700377 if (mStatus != other.mStatus) {
378 return false;
379 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700380 return true;
381 }
382
383 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700384 public String toString() {
385 StringBuilder builder = new StringBuilder();
386 builder.append("PrinterInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700387 builder.append("id=").append(mId);
388 builder.append(", name=").append(mName);
389 builder.append(", status=").append(mStatus);
390 builder.append(", description=").append(mDescription);
391 builder.append(", capabilities=").append(mCapabilities);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800392 builder.append(", iconResId=").append(mIconResourceId);
393 builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon);
394 builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen);
395 builder.append(", infoIntent=").append(mInfoIntent);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700396 builder.append("\"}");
397 return builder.toString();
398 }
399
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700400 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700401 * Builder for creating of a {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700402 */
403 public static final class Builder {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800404 private @NonNull PrinterId mPrinterId;
405 private @NonNull String mName;
406 private @Status int mStatus;
407 private int mIconResourceId;
408 private boolean mHasCustomPrinterIcon;
409 private String mDescription;
410 private PendingIntent mInfoIntent;
411 private PrinterCapabilitiesInfo mCapabilities;
412 private int mCustomPrinterIconGen;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700413
414 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700415 * Constructor.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700416 *
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700417 * @param printerId The printer id. Cannot be null.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700418 * @param name The printer name. Cannot be empty.
419 * @param status The printer status. Must be a valid status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700420 * @throws IllegalArgumentException If the printer id is null, or the
421 * printer name is empty or the status is not a valid one.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700422 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800423 public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800424 mPrinterId = checkPrinterId(printerId);
425 mName = checkName(name);
426 mStatus = checkStatus(status);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700427 }
428
429 /**
430 * Constructor.
431 *
Svetoslav269403b2013-08-14 17:31:04 -0700432 * @param other Other info from which to start building.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700433 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800434 public Builder(@NonNull PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800435 mPrinterId = other.mId;
436 mName = other.mName;
437 mStatus = other.mStatus;
438 mIconResourceId = other.mIconResourceId;
439 mHasCustomPrinterIcon = other.mHasCustomPrinterIcon;
440 mDescription = other.mDescription;
441 mInfoIntent = other.mInfoIntent;
442 mCapabilities = other.mCapabilities;
443 mCustomPrinterIconGen = other.mCustomPrinterIconGen;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700444 }
445
446 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700447 * Sets the printer status.
448 *
449 * @param status The status.
450 * @return This builder.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700451 * @see PrinterInfo#STATUS_IDLE
452 * @see PrinterInfo#STATUS_BUSY
453 * @see PrinterInfo#STATUS_UNAVAILABLE
454 */
Philip P. Moltmannb8c1a02ad2016-01-06 09:34:00 -0800455 public @NonNull Builder setStatus(@Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800456 mStatus = checkStatus(status);
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700457 return this;
458 }
459
460 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800461 * Set a drawable resource as icon for this printer. If no icon is set the printer's
462 * service's icon is used for the printer.
463 *
Philip P. Moltmannbb362062015-12-22 20:08:00 -0800464 * @param iconResourceId The resource ID of the icon.
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800465 * @return This builder.
466 * @see PrinterInfo.Builder#setHasCustomPrinterIcon
467 */
468 public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800469 mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId,
470 "iconResourceId can't be negative");
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800471 return this;
472 }
473
474 /**
475 * Declares that the print service can load a custom per printer's icon. If both
476 * {@link PrinterInfo.Builder#setIconResourceId} and a custom icon are set the resource icon
477 * is shown while the custom icon loads but then the custom icon is used. If
478 * {@link PrinterInfo.Builder#setIconResourceId} is not set the printer's service's icon is
479 * shown while loading.
480 * <p>
481 * The icon is requested asynchronously and only when needed via
482 * {@link android.printservice.PrinterDiscoverySession#onRequestCustomPrinterIcon}.
483 * </p>
484 *
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700485 * @param hasCustomPrinterIcon If the printer has a custom icon or not.
486 *
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800487 * @return This builder.
488 */
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700489 public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) {
490 mHasCustomPrinterIcon = hasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800491 return this;
492 }
493
494 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700495 * Sets the <strong>localized</strong> printer name which
496 * is shown to the user
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700497 *
498 * @param name The name.
499 * @return This builder.
500 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800501 public @NonNull Builder setName(@NonNull String name) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800502 mName = checkName(name);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700503 return this;
504 }
505
506 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700507 * Sets the <strong>localized</strong> printer description
508 * which is shown to the user
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700509 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700510 * @param description The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700511 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700512 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800513 public @NonNull Builder setDescription(@NonNull String description) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800514 mDescription = description;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700515 return this;
516 }
517
518 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800519 * Sets the {@link PendingIntent} that launches an activity showing more information about
520 * the printer.
521 *
522 * @param infoIntent The {@link PendingIntent intent}.
523 * @return This builder.
524 */
525 public @NonNull Builder setInfoIntent(@NonNull PendingIntent infoIntent) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800526 mInfoIntent = infoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800527 return this;
528 }
529
530 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700531 * Sets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700532 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700533 * @param capabilities The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700534 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700535 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800536 public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800537 mCapabilities = capabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700538 return this;
539 }
540
541 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700542 * Creates a new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700543 *
544 * @return A new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700545 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800546 public @NonNull PrinterInfo build() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800547 return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId,
548 mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities,
549 mCustomPrinterIconGen);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700550 }
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800551
552 /**
553 * Increments the generation number of the custom printer icon. As the {@link PrinterInfo}
554 * does not match the previous one anymore, users of the {@link PrinterInfo} will reload the
555 * icon if needed.
556 *
557 * @return This builder.
558 * @hide
559 */
560 public @NonNull Builder incCustomPrinterIconGen() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800561 mCustomPrinterIconGen++;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800562 return this;
563 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700564 }
565
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700566 public static final @android.annotation.NonNull Parcelable.Creator<PrinterInfo> CREATOR =
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700567 new Parcelable.Creator<PrinterInfo>() {
568 @Override
569 public PrinterInfo createFromParcel(Parcel parcel) {
570 return new PrinterInfo(parcel);
571 }
572
573 @Override
574 public PrinterInfo[] newArray(int size) {
575 return new PrinterInfo[size];
576 }
577 };
578}