blob: f99b185229bc0b261a16f549ba9477c420bfd2a8 [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;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070034import android.text.TextUtils;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070035
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080036import com.android.internal.util.Preconditions;
37
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
40
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070041/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070042 * This class represents the description of a printer. Instances of
43 * this class are created by print services to report to the system
44 * the printers they manage. The information of this class has two
45 * major components, printer properties such as name, id, status,
46 * description and printer capabilities which describe the various
47 * print modes a printer supports such as media sizes, margins, etc.
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080048 * <p>
49 * Once {@link PrinterInfo.Builder#build() built} the objects are immutable.
50 * </p>
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070051 */
52public final class PrinterInfo implements Parcelable {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070053
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080054 /** @hide */
55 @IntDef({
56 STATUS_IDLE, STATUS_BUSY, STATUS_UNAVAILABLE
57 })
58 @Retention(RetentionPolicy.SOURCE)
59 public @interface Status {
60 }
Svetoslav Ganovaec14172013-08-27 00:30:54 -070061 /** Printer status: the printer is idle and ready to print. */
62 public static final int STATUS_IDLE = 1;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070063
Svetoslav Ganovaec14172013-08-27 00:30:54 -070064 /** Printer status: the printer is busy printing. */
65 public static final int STATUS_BUSY = 2;
66
67 /** Printer status: the printer is not available. */
68 public static final int STATUS_UNAVAILABLE = 3;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070069
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080070 private final @NonNull PrinterId mId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070071
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080072 /** Resource inside the printer's services's package to be used as an icon */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080073 private final int mIconResourceId;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080074
75 /** If a custom icon can be loaded for the printer */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080076 private final boolean mHasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080077
78 /** The generation of the icon in the cache. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080079 private final int mCustomPrinterIconGen;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080080
81 /** Intent that launches the activity showing more information about the printer. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080082 private final @Nullable PendingIntent mInfoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080083
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080084 private final @NonNull String mName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070085
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080086 private final @Status int mStatus;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070087
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080088 private final @Nullable String mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070089
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080090 private final @Nullable PrinterCapabilitiesInfo mCapabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070091
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080092 private PrinterInfo(@NonNull PrinterId printerId, @NonNull String name, @Status int status,
93 int iconResourceId, boolean hasCustomPrinterIcon, String description,
94 PendingIntent infoIntent, PrinterCapabilitiesInfo capabilities,
95 int customPrinterIconGen) {
96 mId = printerId;
97 mName = name;
98 mStatus = status;
99 mIconResourceId = iconResourceId;
100 mHasCustomPrinterIcon = hasCustomPrinterIcon;
101 mDescription = description;
102 mInfoIntent = infoIntent;
103 mCapabilities = capabilities;
104 mCustomPrinterIconGen = customPrinterIconGen;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700105 }
106
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700107 /**
108 * Get the globally unique printer id.
109 *
110 * @return The printer id.
111 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800112 public @NonNull PrinterId getId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700113 return mId;
114 }
115
116 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800117 * Get the icon to be used for this printer. If no per printer icon is available, the printer's
118 * service's icon is returned. If the printer has a custom icon this icon might get requested
119 * asynchronously. Once the icon is loaded the discovery sessions will be notified that the
120 * printer changed.
121 *
122 * @param context The context that will be using the icons
123 * @return The icon to be used for the printer or null if no icon could be found.
124 * @hide
125 */
126 @TestApi
127 public @Nullable Drawable loadIcon(@NonNull Context context) {
128 Drawable drawable = null;
129 PackageManager packageManager = context.getPackageManager();
130
131 if (mHasCustomPrinterIcon) {
132 PrintManager printManager = (PrintManager) context
133 .getSystemService(Context.PRINT_SERVICE);
134
135 Icon icon = printManager.getCustomPrinterIcon(mId);
136
137 if (icon != null) {
138 drawable = icon.loadDrawable(context);
139 }
140 }
141
142 if (drawable == null) {
143 try {
144 String packageName = mId.getServiceName().getPackageName();
145 PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
146 ApplicationInfo appInfo = packageInfo.applicationInfo;
147
148 // If no custom icon is available, try the icon from the resources
149 if (mIconResourceId != 0) {
150 drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo);
151 }
152
153 // Fall back to the printer's service's icon if no per printer icon could be found
154 if (drawable == null) {
155 drawable = appInfo.loadIcon(packageManager);
156 }
157 } catch (NameNotFoundException e) {
158 }
159 }
160
161 return drawable;
162 }
163
164 /**
Philip P. Moltmann2e301262016-06-16 12:39:54 -0700165 * Check if the printer has a custom printer icon.
166 *
167 * @return {@code true} iff the printer has a custom printer icon.
168 *
169 * @hide
170 */
171 public boolean getHasCustomPrinterIcon() {
172 return mHasCustomPrinterIcon;
173 }
174
175 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700176 * Get the printer name.
177 *
178 * @return The printer name.
179 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800180 public @NonNull String getName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700181 return mName;
182 }
183
184 /**
185 * Gets the printer status.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700186 *
187 * @return The status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700188 *
189 * @see #STATUS_BUSY
190 * @see #STATUS_IDLE
191 * @see #STATUS_UNAVAILABLE
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700192 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800193 public @Status int getStatus() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700194 return mStatus;
195 }
196
197 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700198 * Gets the printer description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700199 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700200 * @return The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700201 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800202 public @Nullable String getDescription() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700203 return mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700204 }
205
206 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800207 * Get the {@link PendingIntent} that launches the activity showing more information about the
208 * printer.
209 *
210 * @return the {@link PendingIntent} that launches the activity showing more information about
211 * the printer or null if it is not configured
212 * @hide
213 */
214 public @Nullable PendingIntent getInfoIntent() {
215 return mInfoIntent;
216 }
217
218 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700219 * Gets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700220 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700221 * @return The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700222 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800223 public @Nullable PrinterCapabilitiesInfo getCapabilities() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700224 return mCapabilities;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700225 }
226
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800227 /**
228 * Check if printerId is valid.
229 *
230 * @param printerId The printerId that might be valid
231 * @return The valid printerId
232 * @throws IllegalArgumentException if printerId is not valid.
233 */
234 private static @NonNull PrinterId checkPrinterId(PrinterId printerId) {
235 return Preconditions.checkNotNull(printerId, "printerId cannot be null.");
236 }
237
238 /**
239 * Check if status is valid.
240 *
241 * @param status The status that might be valid
242 * @return The valid status
243 * @throws IllegalArgumentException if status is not valid.
244 */
245 private static @Status int checkStatus(int status) {
246 if (!(status == STATUS_IDLE
247 || status == STATUS_BUSY
248 || status == STATUS_UNAVAILABLE)) {
249 throw new IllegalArgumentException("status is invalid.");
250 }
251
252 return status;
253 }
254
255 /**
256 * Check if name is valid.
257 *
258 * @param name The name that might be valid
259 * @return The valid name
260 * @throws IllegalArgumentException if name is not valid.
261 */
262 private static @NonNull String checkName(String name) {
263 return Preconditions.checkStringNotEmpty(name, "name cannot be empty.");
264 }
265
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700266 private PrinterInfo(Parcel parcel) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800267 // mName can be null due to unchecked set in Builder.setName and status can be invalid
268 // due to unchecked set in Builder.setStatus, hence we can only check mId for a valid state
269 mId = checkPrinterId((PrinterId) parcel.readParcelable(null));
270 mName = checkName(parcel.readString());
271 mStatus = checkStatus(parcel.readInt());
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700272 mDescription = parcel.readString();
273 mCapabilities = parcel.readParcelable(null);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800274 mIconResourceId = parcel.readInt();
275 mHasCustomPrinterIcon = parcel.readByte() != 0;
276 mCustomPrinterIconGen = parcel.readInt();
277 mInfoIntent = parcel.readParcelable(null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700278 }
279
280 @Override
281 public int describeContents() {
282 return 0;
283 }
284
285 @Override
286 public void writeToParcel(Parcel parcel, int flags) {
287 parcel.writeParcelable(mId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700288 parcel.writeString(mName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700289 parcel.writeInt(mStatus);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700290 parcel.writeString(mDescription);
291 parcel.writeParcelable(mCapabilities, flags);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800292 parcel.writeInt(mIconResourceId);
293 parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0));
294 parcel.writeInt(mCustomPrinterIconGen);
295 parcel.writeParcelable(mInfoIntent, flags);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700296 }
297
298 @Override
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700299 public int hashCode() {
300 final int prime = 31;
301 int result = 1;
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800302 result = prime * result + mId.hashCode();
303 result = prime * result + mName.hashCode();
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700304 result = prime * result + mStatus;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700305 result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
306 result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800307 result = prime * result + mIconResourceId;
308 result = prime * result + (mHasCustomPrinterIcon ? 1 : 0);
309 result = prime * result + mCustomPrinterIconGen;
310 result = prime * result + ((mInfoIntent != null) ? mInfoIntent.hashCode() : 0);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700311 return result;
312 }
313
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800314 /**
315 * Compare two {@link PrinterInfo printerInfos} in all aspects beside being null and the
316 * {@link #mStatus}.
317 *
318 * @param other the other {@link PrinterInfo}
319 * @return true iff the infos are equivalent
320 * @hide
321 */
322 public boolean equalsIgnoringStatus(PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800323 if (!mId.equals(other.mId)) {
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800324 return false;
325 }
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800326 if (!mName.equals(other.mName)) {
327 return false;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800328 }
329 if (!TextUtils.equals(mDescription, other.mDescription)) {
330 return false;
331 }
332 if (mCapabilities == null) {
333 if (other.mCapabilities != null) {
334 return false;
335 }
336 } else if (!mCapabilities.equals(other.mCapabilities)) {
337 return false;
338 }
339 if (mIconResourceId != other.mIconResourceId) {
340 return false;
341 }
342 if (mHasCustomPrinterIcon != other.mHasCustomPrinterIcon) {
343 return false;
344 }
345 if (mCustomPrinterIconGen != other.mCustomPrinterIconGen) {
346 return false;
347 }
348 if (mInfoIntent == null) {
349 if (other.mInfoIntent != null) {
350 return false;
351 }
352 } else if (!mInfoIntent.equals(other.mInfoIntent)) {
353 return false;
354 }
355 return true;
356 }
357
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700358 @Override
359 public boolean equals(Object obj) {
360 if (this == obj) {
361 return true;
362 }
363 if (obj == null) {
364 return false;
365 }
366 if (getClass() != obj.getClass()) {
367 return false;
368 }
369 PrinterInfo other = (PrinterInfo) obj;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800370 if (!equalsIgnoringStatus(other)) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700371 return false;
372 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700373 if (mStatus != other.mStatus) {
374 return false;
375 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700376 return true;
377 }
378
379 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700380 public String toString() {
381 StringBuilder builder = new StringBuilder();
382 builder.append("PrinterInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700383 builder.append("id=").append(mId);
384 builder.append(", name=").append(mName);
385 builder.append(", status=").append(mStatus);
386 builder.append(", description=").append(mDescription);
387 builder.append(", capabilities=").append(mCapabilities);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800388 builder.append(", iconResId=").append(mIconResourceId);
389 builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon);
390 builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen);
391 builder.append(", infoIntent=").append(mInfoIntent);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700392 builder.append("\"}");
393 return builder.toString();
394 }
395
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700396 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700397 * Builder for creating of a {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700398 */
399 public static final class Builder {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800400 private @NonNull PrinterId mPrinterId;
401 private @NonNull String mName;
402 private @Status int mStatus;
403 private int mIconResourceId;
404 private boolean mHasCustomPrinterIcon;
405 private String mDescription;
406 private PendingIntent mInfoIntent;
407 private PrinterCapabilitiesInfo mCapabilities;
408 private int mCustomPrinterIconGen;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700409
410 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700411 * Constructor.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700412 *
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700413 * @param printerId The printer id. Cannot be null.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700414 * @param name The printer name. Cannot be empty.
415 * @param status The printer status. Must be a valid status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700416 * @throws IllegalArgumentException If the printer id is null, or the
417 * printer name is empty or the status is not a valid one.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700418 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800419 public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800420 mPrinterId = checkPrinterId(printerId);
421 mName = checkName(name);
422 mStatus = checkStatus(status);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700423 }
424
425 /**
426 * Constructor.
427 *
Svetoslav269403b2013-08-14 17:31:04 -0700428 * @param other Other info from which to start building.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700429 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800430 public Builder(@NonNull PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800431 mPrinterId = other.mId;
432 mName = other.mName;
433 mStatus = other.mStatus;
434 mIconResourceId = other.mIconResourceId;
435 mHasCustomPrinterIcon = other.mHasCustomPrinterIcon;
436 mDescription = other.mDescription;
437 mInfoIntent = other.mInfoIntent;
438 mCapabilities = other.mCapabilities;
439 mCustomPrinterIconGen = other.mCustomPrinterIconGen;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700440 }
441
442 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700443 * Sets the printer status.
444 *
445 * @param status The status.
446 * @return This builder.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700447 * @see PrinterInfo#STATUS_IDLE
448 * @see PrinterInfo#STATUS_BUSY
449 * @see PrinterInfo#STATUS_UNAVAILABLE
450 */
Philip P. Moltmannb8c1a02ad2016-01-06 09:34:00 -0800451 public @NonNull Builder setStatus(@Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800452 mStatus = checkStatus(status);
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700453 return this;
454 }
455
456 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800457 * Set a drawable resource as icon for this printer. If no icon is set the printer's
458 * service's icon is used for the printer.
459 *
Philip P. Moltmannbb362062015-12-22 20:08:00 -0800460 * @param iconResourceId The resource ID of the icon.
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800461 * @return This builder.
462 * @see PrinterInfo.Builder#setHasCustomPrinterIcon
463 */
464 public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800465 mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId,
466 "iconResourceId can't be negative");
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800467 return this;
468 }
469
470 /**
471 * Declares that the print service can load a custom per printer's icon. If both
472 * {@link PrinterInfo.Builder#setIconResourceId} and a custom icon are set the resource icon
473 * is shown while the custom icon loads but then the custom icon is used. If
474 * {@link PrinterInfo.Builder#setIconResourceId} is not set the printer's service's icon is
475 * shown while loading.
476 * <p>
477 * The icon is requested asynchronously and only when needed via
478 * {@link android.printservice.PrinterDiscoverySession#onRequestCustomPrinterIcon}.
479 * </p>
480 *
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700481 * @param hasCustomPrinterIcon If the printer has a custom icon or not.
482 *
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800483 * @return This builder.
484 */
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700485 public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) {
486 mHasCustomPrinterIcon = hasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800487 return this;
488 }
489
490 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700491 * Sets the <strong>localized</strong> printer name which
492 * is shown to the user
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700493 *
494 * @param name The name.
495 * @return This builder.
496 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800497 public @NonNull Builder setName(@NonNull String name) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800498 mName = checkName(name);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700499 return this;
500 }
501
502 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700503 * Sets the <strong>localized</strong> printer description
504 * which is shown to the user
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700505 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700506 * @param description The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700507 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700508 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800509 public @NonNull Builder setDescription(@NonNull String description) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800510 mDescription = description;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700511 return this;
512 }
513
514 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800515 * Sets the {@link PendingIntent} that launches an activity showing more information about
516 * the printer.
517 *
518 * @param infoIntent The {@link PendingIntent intent}.
519 * @return This builder.
520 */
521 public @NonNull Builder setInfoIntent(@NonNull PendingIntent infoIntent) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800522 mInfoIntent = infoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800523 return this;
524 }
525
526 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700527 * Sets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700528 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700529 * @param capabilities The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700530 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700531 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800532 public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800533 mCapabilities = capabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700534 return this;
535 }
536
537 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700538 * Creates a new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700539 *
540 * @return A new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700541 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800542 public @NonNull PrinterInfo build() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800543 return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId,
544 mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities,
545 mCustomPrinterIconGen);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700546 }
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800547
548 /**
549 * Increments the generation number of the custom printer icon. As the {@link PrinterInfo}
550 * does not match the previous one anymore, users of the {@link PrinterInfo} will reload the
551 * icon if needed.
552 *
553 * @return This builder.
554 * @hide
555 */
556 public @NonNull Builder incCustomPrinterIconGen() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800557 mCustomPrinterIconGen++;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800558 return this;
559 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700560 }
561
562 public static final Parcelable.Creator<PrinterInfo> CREATOR =
563 new Parcelable.Creator<PrinterInfo>() {
564 @Override
565 public PrinterInfo createFromParcel(Parcel parcel) {
566 return new PrinterInfo(parcel);
567 }
568
569 @Override
570 public PrinterInfo[] newArray(int size) {
571 return new PrinterInfo[size];
572 }
573 };
574}