blob: 88feab7fc2ca9355c150ee6c75064b942e4d5265 [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 */
56 @IntDef({
57 STATUS_IDLE, STATUS_BUSY, STATUS_UNAVAILABLE
58 })
59 @Retention(RetentionPolicy.SOURCE)
60 public @interface Status {
61 }
Svetoslav Ganovaec14172013-08-27 00:30:54 -070062 /** Printer status: the printer is idle and ready to print. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070063 public static final int STATUS_IDLE = PrinterInfoProto.STATUS_IDLE;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070064
Svetoslav Ganovaec14172013-08-27 00:30:54 -070065 /** Printer status: the printer is busy printing. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070066 public static final int STATUS_BUSY = PrinterInfoProto.STATUS_BUSY;
Svetoslav Ganovaec14172013-08-27 00:30:54 -070067
68 /** Printer status: the printer is not available. */
Philip P. Moltmannc0a128d2017-06-19 10:55:09 -070069 public static final int STATUS_UNAVAILABLE = PrinterInfoProto.STATUS_UNAVAILABLE;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070070
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080071 private final @NonNull PrinterId mId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070072
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080073 /** Resource inside the printer's services's package to be used as an icon */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080074 private final int mIconResourceId;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080075
76 /** If a custom icon can be loaded for the printer */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080077 private final boolean mHasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080078
79 /** The generation of the icon in the cache. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080080 private final int mCustomPrinterIconGen;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080081
82 /** Intent that launches the activity showing more information about the printer. */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080083 private final @Nullable PendingIntent mInfoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -080084
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080085 private final @NonNull String mName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070086
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080087 private final @Status int mStatus;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070088
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080089 private final @Nullable String mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070090
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080091 private final @Nullable PrinterCapabilitiesInfo mCapabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070092
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -080093 private PrinterInfo(@NonNull PrinterId printerId, @NonNull String name, @Status int status,
94 int iconResourceId, boolean hasCustomPrinterIcon, String description,
95 PendingIntent infoIntent, PrinterCapabilitiesInfo capabilities,
96 int customPrinterIconGen) {
97 mId = printerId;
98 mName = name;
99 mStatus = status;
100 mIconResourceId = iconResourceId;
101 mHasCustomPrinterIcon = hasCustomPrinterIcon;
102 mDescription = description;
103 mInfoIntent = infoIntent;
104 mCapabilities = capabilities;
105 mCustomPrinterIconGen = customPrinterIconGen;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700106 }
107
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700108 /**
109 * Get the globally unique printer id.
110 *
111 * @return The printer id.
112 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800113 public @NonNull PrinterId getId() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700114 return mId;
115 }
116
117 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800118 * Get the icon to be used for this printer. If no per printer icon is available, the printer's
119 * service's icon is returned. If the printer has a custom icon this icon might get requested
120 * asynchronously. Once the icon is loaded the discovery sessions will be notified that the
121 * printer changed.
122 *
123 * @param context The context that will be using the icons
124 * @return The icon to be used for the printer or null if no icon could be found.
125 * @hide
126 */
127 @TestApi
128 public @Nullable Drawable loadIcon(@NonNull Context context) {
129 Drawable drawable = null;
130 PackageManager packageManager = context.getPackageManager();
131
132 if (mHasCustomPrinterIcon) {
133 PrintManager printManager = (PrintManager) context
134 .getSystemService(Context.PRINT_SERVICE);
135
136 Icon icon = printManager.getCustomPrinterIcon(mId);
137
138 if (icon != null) {
139 drawable = icon.loadDrawable(context);
140 }
141 }
142
143 if (drawable == null) {
144 try {
145 String packageName = mId.getServiceName().getPackageName();
146 PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
147 ApplicationInfo appInfo = packageInfo.applicationInfo;
148
149 // If no custom icon is available, try the icon from the resources
150 if (mIconResourceId != 0) {
151 drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo);
152 }
153
154 // Fall back to the printer's service's icon if no per printer icon could be found
155 if (drawable == null) {
156 drawable = appInfo.loadIcon(packageManager);
157 }
158 } catch (NameNotFoundException e) {
159 }
160 }
161
162 return drawable;
163 }
164
165 /**
Philip P. Moltmann2e301262016-06-16 12:39:54 -0700166 * Check if the printer has a custom printer icon.
167 *
168 * @return {@code true} iff the printer has a custom printer icon.
169 *
170 * @hide
171 */
172 public boolean getHasCustomPrinterIcon() {
173 return mHasCustomPrinterIcon;
174 }
175
176 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700177 * Get the printer name.
178 *
179 * @return The printer name.
180 */
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800181 public @NonNull String getName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700182 return mName;
183 }
184
185 /**
186 * Gets the printer status.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700187 *
188 * @return The status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700189 *
190 * @see #STATUS_BUSY
191 * @see #STATUS_IDLE
192 * @see #STATUS_UNAVAILABLE
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700193 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800194 public @Status int getStatus() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700195 return mStatus;
196 }
197
198 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700199 * Gets the printer description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700200 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700201 * @return The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700202 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800203 public @Nullable String getDescription() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700204 return mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700205 }
206
207 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800208 * Get the {@link PendingIntent} that launches the activity showing more information about the
209 * printer.
210 *
211 * @return the {@link PendingIntent} that launches the activity showing more information about
212 * the printer or null if it is not configured
213 * @hide
214 */
215 public @Nullable PendingIntent getInfoIntent() {
216 return mInfoIntent;
217 }
218
219 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700220 * Gets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700221 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700222 * @return The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700223 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800224 public @Nullable PrinterCapabilitiesInfo getCapabilities() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700225 return mCapabilities;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700226 }
227
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800228 /**
229 * Check if printerId is valid.
230 *
231 * @param printerId The printerId that might be valid
232 * @return The valid printerId
233 * @throws IllegalArgumentException if printerId is not valid.
234 */
235 private static @NonNull PrinterId checkPrinterId(PrinterId printerId) {
236 return Preconditions.checkNotNull(printerId, "printerId cannot be null.");
237 }
238
239 /**
240 * Check if status is valid.
241 *
242 * @param status The status that might be valid
243 * @return The valid status
244 * @throws IllegalArgumentException if status is not valid.
245 */
246 private static @Status int checkStatus(int status) {
247 if (!(status == STATUS_IDLE
248 || status == STATUS_BUSY
249 || status == STATUS_UNAVAILABLE)) {
250 throw new IllegalArgumentException("status is invalid.");
251 }
252
253 return status;
254 }
255
256 /**
257 * Check if name is valid.
258 *
259 * @param name The name that might be valid
260 * @return The valid name
261 * @throws IllegalArgumentException if name is not valid.
262 */
263 private static @NonNull String checkName(String name) {
264 return Preconditions.checkStringNotEmpty(name, "name cannot be empty.");
265 }
266
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700267 private PrinterInfo(Parcel parcel) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800268 // mName can be null due to unchecked set in Builder.setName and status can be invalid
269 // due to unchecked set in Builder.setStatus, hence we can only check mId for a valid state
270 mId = checkPrinterId((PrinterId) parcel.readParcelable(null));
271 mName = checkName(parcel.readString());
272 mStatus = checkStatus(parcel.readInt());
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700273 mDescription = parcel.readString();
274 mCapabilities = parcel.readParcelable(null);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800275 mIconResourceId = parcel.readInt();
276 mHasCustomPrinterIcon = parcel.readByte() != 0;
277 mCustomPrinterIconGen = parcel.readInt();
278 mInfoIntent = parcel.readParcelable(null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700279 }
280
281 @Override
282 public int describeContents() {
283 return 0;
284 }
285
286 @Override
287 public void writeToParcel(Parcel parcel, int flags) {
288 parcel.writeParcelable(mId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700289 parcel.writeString(mName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700290 parcel.writeInt(mStatus);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700291 parcel.writeString(mDescription);
292 parcel.writeParcelable(mCapabilities, flags);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800293 parcel.writeInt(mIconResourceId);
294 parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0));
295 parcel.writeInt(mCustomPrinterIconGen);
296 parcel.writeParcelable(mInfoIntent, flags);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700297 }
298
299 @Override
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700300 public int hashCode() {
301 final int prime = 31;
302 int result = 1;
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800303 result = prime * result + mId.hashCode();
304 result = prime * result + mName.hashCode();
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700305 result = prime * result + mStatus;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700306 result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
307 result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800308 result = prime * result + mIconResourceId;
309 result = prime * result + (mHasCustomPrinterIcon ? 1 : 0);
310 result = prime * result + mCustomPrinterIconGen;
311 result = prime * result + ((mInfoIntent != null) ? mInfoIntent.hashCode() : 0);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700312 return result;
313 }
314
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800315 /**
316 * Compare two {@link PrinterInfo printerInfos} in all aspects beside being null and the
317 * {@link #mStatus}.
318 *
319 * @param other the other {@link PrinterInfo}
320 * @return true iff the infos are equivalent
321 * @hide
322 */
323 public boolean equalsIgnoringStatus(PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800324 if (!mId.equals(other.mId)) {
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800325 return false;
326 }
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800327 if (!mName.equals(other.mName)) {
328 return false;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800329 }
330 if (!TextUtils.equals(mDescription, other.mDescription)) {
331 return false;
332 }
333 if (mCapabilities == null) {
334 if (other.mCapabilities != null) {
335 return false;
336 }
337 } else if (!mCapabilities.equals(other.mCapabilities)) {
338 return false;
339 }
340 if (mIconResourceId != other.mIconResourceId) {
341 return false;
342 }
343 if (mHasCustomPrinterIcon != other.mHasCustomPrinterIcon) {
344 return false;
345 }
346 if (mCustomPrinterIconGen != other.mCustomPrinterIconGen) {
347 return false;
348 }
349 if (mInfoIntent == null) {
350 if (other.mInfoIntent != null) {
351 return false;
352 }
353 } else if (!mInfoIntent.equals(other.mInfoIntent)) {
354 return false;
355 }
356 return true;
357 }
358
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700359 @Override
360 public boolean equals(Object obj) {
361 if (this == obj) {
362 return true;
363 }
364 if (obj == null) {
365 return false;
366 }
367 if (getClass() != obj.getClass()) {
368 return false;
369 }
370 PrinterInfo other = (PrinterInfo) obj;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800371 if (!equalsIgnoringStatus(other)) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700372 return false;
373 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700374 if (mStatus != other.mStatus) {
375 return false;
376 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700377 return true;
378 }
379
380 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700381 public String toString() {
382 StringBuilder builder = new StringBuilder();
383 builder.append("PrinterInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700384 builder.append("id=").append(mId);
385 builder.append(", name=").append(mName);
386 builder.append(", status=").append(mStatus);
387 builder.append(", description=").append(mDescription);
388 builder.append(", capabilities=").append(mCapabilities);
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800389 builder.append(", iconResId=").append(mIconResourceId);
390 builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon);
391 builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen);
392 builder.append(", infoIntent=").append(mInfoIntent);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700393 builder.append("\"}");
394 return builder.toString();
395 }
396
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700397 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700398 * Builder for creating of a {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700399 */
400 public static final class Builder {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800401 private @NonNull PrinterId mPrinterId;
402 private @NonNull String mName;
403 private @Status int mStatus;
404 private int mIconResourceId;
405 private boolean mHasCustomPrinterIcon;
406 private String mDescription;
407 private PendingIntent mInfoIntent;
408 private PrinterCapabilitiesInfo mCapabilities;
409 private int mCustomPrinterIconGen;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700410
411 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700412 * Constructor.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700413 *
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700414 * @param printerId The printer id. Cannot be null.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700415 * @param name The printer name. Cannot be empty.
416 * @param status The printer status. Must be a valid status.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700417 * @throws IllegalArgumentException If the printer id is null, or the
418 * printer name is empty or the status is not a valid one.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700419 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800420 public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800421 mPrinterId = checkPrinterId(printerId);
422 mName = checkName(name);
423 mStatus = checkStatus(status);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700424 }
425
426 /**
427 * Constructor.
428 *
Svetoslav269403b2013-08-14 17:31:04 -0700429 * @param other Other info from which to start building.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700430 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800431 public Builder(@NonNull PrinterInfo other) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800432 mPrinterId = other.mId;
433 mName = other.mName;
434 mStatus = other.mStatus;
435 mIconResourceId = other.mIconResourceId;
436 mHasCustomPrinterIcon = other.mHasCustomPrinterIcon;
437 mDescription = other.mDescription;
438 mInfoIntent = other.mInfoIntent;
439 mCapabilities = other.mCapabilities;
440 mCustomPrinterIconGen = other.mCustomPrinterIconGen;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700441 }
442
443 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700444 * Sets the printer status.
445 *
446 * @param status The status.
447 * @return This builder.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700448 * @see PrinterInfo#STATUS_IDLE
449 * @see PrinterInfo#STATUS_BUSY
450 * @see PrinterInfo#STATUS_UNAVAILABLE
451 */
Philip P. Moltmannb8c1a02ad2016-01-06 09:34:00 -0800452 public @NonNull Builder setStatus(@Status int status) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800453 mStatus = checkStatus(status);
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700454 return this;
455 }
456
457 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800458 * Set a drawable resource as icon for this printer. If no icon is set the printer's
459 * service's icon is used for the printer.
460 *
Philip P. Moltmannbb362062015-12-22 20:08:00 -0800461 * @param iconResourceId The resource ID of the icon.
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800462 * @return This builder.
463 * @see PrinterInfo.Builder#setHasCustomPrinterIcon
464 */
465 public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800466 mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId,
467 "iconResourceId can't be negative");
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800468 return this;
469 }
470
471 /**
472 * Declares that the print service can load a custom per printer's icon. If both
473 * {@link PrinterInfo.Builder#setIconResourceId} and a custom icon are set the resource icon
474 * is shown while the custom icon loads but then the custom icon is used. If
475 * {@link PrinterInfo.Builder#setIconResourceId} is not set the printer's service's icon is
476 * shown while loading.
477 * <p>
478 * The icon is requested asynchronously and only when needed via
479 * {@link android.printservice.PrinterDiscoverySession#onRequestCustomPrinterIcon}.
480 * </p>
481 *
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700482 * @param hasCustomPrinterIcon If the printer has a custom icon or not.
483 *
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800484 * @return This builder.
485 */
Philip P. Moltmannd74d1e52016-03-17 16:37:47 -0700486 public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) {
487 mHasCustomPrinterIcon = hasCustomPrinterIcon;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800488 return this;
489 }
490
491 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700492 * Sets the <strong>localized</strong> printer name which
493 * is shown to the user
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700494 *
495 * @param name The name.
496 * @return This builder.
497 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800498 public @NonNull Builder setName(@NonNull String name) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800499 mName = checkName(name);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700500 return this;
501 }
502
503 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700504 * Sets the <strong>localized</strong> printer description
505 * which is shown to the user
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700506 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700507 * @param description The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700508 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700509 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800510 public @NonNull Builder setDescription(@NonNull String description) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800511 mDescription = description;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700512 return this;
513 }
514
515 /**
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800516 * Sets the {@link PendingIntent} that launches an activity showing more information about
517 * the printer.
518 *
519 * @param infoIntent The {@link PendingIntent intent}.
520 * @return This builder.
521 */
522 public @NonNull Builder setInfoIntent(@NonNull PendingIntent infoIntent) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800523 mInfoIntent = infoIntent;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800524 return this;
525 }
526
527 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700528 * Sets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700529 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700530 * @param capabilities The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700531 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700532 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800533 public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800534 mCapabilities = capabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700535 return this;
536 }
537
538 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700539 * Creates a new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700540 *
541 * @return A new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700542 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800543 public @NonNull PrinterInfo build() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800544 return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId,
545 mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities,
546 mCustomPrinterIconGen);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700547 }
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800548
549 /**
550 * Increments the generation number of the custom printer icon. As the {@link PrinterInfo}
551 * does not match the previous one anymore, users of the {@link PrinterInfo} will reload the
552 * icon if needed.
553 *
554 * @return This builder.
555 * @hide
556 */
557 public @NonNull Builder incCustomPrinterIconGen() {
Philip P. Moltmannc2ad2262016-01-13 09:17:15 -0800558 mCustomPrinterIconGen++;
Philip P. Moltmannbb9f6862015-12-01 14:44:24 -0800559 return this;
560 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700561 }
562
563 public static final Parcelable.Creator<PrinterInfo> CREATOR =
564 new Parcelable.Creator<PrinterInfo>() {
565 @Override
566 public PrinterInfo createFromParcel(Parcel parcel) {
567 return new PrinterInfo(parcel);
568 }
569
570 @Override
571 public PrinterInfo[] newArray(int size) {
572 return new PrinterInfo[size];
573 }
574 };
575}