Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 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 | |
| 17 | package android.print; |
| 18 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 19 | import android.annotation.DrawableRes; |
Philip P. Moltmann | bb36206 | 2015-12-22 20:08:00 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.TestApi; |
| 24 | import android.app.PendingIntent; |
| 25 | import android.content.Context; |
| 26 | import android.content.pm.ApplicationInfo; |
| 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.PackageManager.NameNotFoundException; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.graphics.drawable.Icon; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 32 | import android.os.Parcel; |
| 33 | import android.os.Parcelable; |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 34 | import android.service.print.PrinterInfoProto; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 36 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 37 | import com.android.internal.util.Preconditions; |
| 38 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
| 41 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 42 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 43 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 49 | * <p> |
| 50 | * Once {@link PrinterInfo.Builder#build() built} the objects are immutable. |
| 51 | * </p> |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 52 | */ |
| 53 | public final class PrinterInfo implements Parcelable { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 54 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 55 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 56 | @IntDef(prefix = { "STATUS_" }, value = { |
| 57 | STATUS_IDLE, |
| 58 | STATUS_BUSY, |
| 59 | STATUS_UNAVAILABLE |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 60 | }) |
| 61 | @Retention(RetentionPolicy.SOURCE) |
| 62 | public @interface Status { |
| 63 | } |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 64 | |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 65 | /** Printer status: the printer is idle and ready to print. */ |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 66 | public static final int STATUS_IDLE = PrinterInfoProto.STATUS_IDLE; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 67 | |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 68 | /** Printer status: the printer is busy printing. */ |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 69 | public static final int STATUS_BUSY = PrinterInfoProto.STATUS_BUSY; |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 70 | |
| 71 | /** Printer status: the printer is not available. */ |
Philip P. Moltmann | c0a128d | 2017-06-19 10:55:09 -0700 | [diff] [blame] | 72 | public static final int STATUS_UNAVAILABLE = PrinterInfoProto.STATUS_UNAVAILABLE; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 73 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 74 | private final @NonNull PrinterId mId; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 75 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 76 | /** Resource inside the printer's services's package to be used as an icon */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 77 | private final int mIconResourceId; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 78 | |
| 79 | /** If a custom icon can be loaded for the printer */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 80 | private final boolean mHasCustomPrinterIcon; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 81 | |
| 82 | /** The generation of the icon in the cache. */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 83 | private final int mCustomPrinterIconGen; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 84 | |
| 85 | /** Intent that launches the activity showing more information about the printer. */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 86 | private final @Nullable PendingIntent mInfoIntent; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 87 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 88 | private final @NonNull String mName; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 89 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 90 | private final @Status int mStatus; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 91 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 92 | private final @Nullable String mDescription; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 93 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 94 | private final @Nullable PrinterCapabilitiesInfo mCapabilities; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 95 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 96 | 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 Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Get the globally unique printer id. |
| 113 | * |
| 114 | * @return The printer id. |
| 115 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 116 | public @NonNull PrinterId getId() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 117 | return mId; |
| 118 | } |
| 119 | |
| 120 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 121 | * 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. Moltmann | 2e30126 | 2016-06-16 12:39:54 -0700 | [diff] [blame] | 169 | * 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 180 | * Get the printer name. |
| 181 | * |
| 182 | * @return The printer name. |
| 183 | */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 184 | public @NonNull String getName() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 185 | return mName; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Gets the printer status. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 190 | * |
| 191 | * @return The status. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 192 | * |
| 193 | * @see #STATUS_BUSY |
| 194 | * @see #STATUS_IDLE |
| 195 | * @see #STATUS_UNAVAILABLE |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 196 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 197 | public @Status int getStatus() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 198 | return mStatus; |
| 199 | } |
| 200 | |
| 201 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 202 | * Gets the printer description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 203 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 204 | * @return The description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 205 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 206 | public @Nullable String getDescription() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 207 | return mDescription; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 211 | * 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 223 | * Gets the printer capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 224 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 225 | * @return The capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 226 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 227 | public @Nullable PrinterCapabilitiesInfo getCapabilities() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 228 | return mCapabilities; |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 231 | /** |
| 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 270 | private PrinterInfo(Parcel parcel) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 271 | // 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 276 | mDescription = parcel.readString(); |
| 277 | mCapabilities = parcel.readParcelable(null); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 278 | mIconResourceId = parcel.readInt(); |
| 279 | mHasCustomPrinterIcon = parcel.readByte() != 0; |
| 280 | mCustomPrinterIconGen = parcel.readInt(); |
| 281 | mInfoIntent = parcel.readParcelable(null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 282 | } |
| 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 292 | parcel.writeString(mName); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 293 | parcel.writeInt(mStatus); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 294 | parcel.writeString(mDescription); |
| 295 | parcel.writeParcelable(mCapabilities, flags); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 296 | parcel.writeInt(mIconResourceId); |
| 297 | parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0)); |
| 298 | parcel.writeInt(mCustomPrinterIconGen); |
| 299 | parcel.writeParcelable(mInfoIntent, flags); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 303 | public int hashCode() { |
| 304 | final int prime = 31; |
| 305 | int result = 1; |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 306 | result = prime * result + mId.hashCode(); |
| 307 | result = prime * result + mName.hashCode(); |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 308 | result = prime * result + mStatus; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 309 | result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0); |
| 310 | result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 311 | 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 Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 315 | return result; |
| 316 | } |
| 317 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 318 | /** |
| 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 327 | if (!mId.equals(other.mId)) { |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 328 | return false; |
| 329 | } |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 330 | if (!mName.equals(other.mName)) { |
| 331 | return false; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 332 | } |
| 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 Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 362 | @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. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 374 | if (!equalsIgnoringStatus(other)) { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 377 | if (mStatus != other.mStatus) { |
| 378 | return false; |
| 379 | } |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
| 383 | @Override |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 384 | public String toString() { |
| 385 | StringBuilder builder = new StringBuilder(); |
| 386 | builder.append("PrinterInfo{"); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 387 | 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. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 392 | builder.append(", iconResId=").append(mIconResourceId); |
| 393 | builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon); |
| 394 | builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen); |
| 395 | builder.append(", infoIntent=").append(mInfoIntent); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 396 | builder.append("\"}"); |
| 397 | return builder.toString(); |
| 398 | } |
| 399 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 400 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 401 | * Builder for creating of a {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 402 | */ |
| 403 | public static final class Builder { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 404 | 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 413 | |
| 414 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 415 | * Constructor. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 416 | * |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 417 | * @param printerId The printer id. Cannot be null. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 418 | * @param name The printer name. Cannot be empty. |
| 419 | * @param status The printer status. Must be a valid status. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 420 | * @throws IllegalArgumentException If the printer id is null, or the |
| 421 | * printer name is empty or the status is not a valid one. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 422 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 423 | public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 424 | mPrinterId = checkPrinterId(printerId); |
| 425 | mName = checkName(name); |
| 426 | mStatus = checkStatus(status); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Constructor. |
| 431 | * |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 432 | * @param other Other info from which to start building. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 433 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 434 | public Builder(@NonNull PrinterInfo other) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 435 | 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 447 | * Sets the printer status. |
| 448 | * |
| 449 | * @param status The status. |
| 450 | * @return This builder. |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 451 | * @see PrinterInfo#STATUS_IDLE |
| 452 | * @see PrinterInfo#STATUS_BUSY |
| 453 | * @see PrinterInfo#STATUS_UNAVAILABLE |
| 454 | */ |
Philip P. Moltmann | b8c1a02ad | 2016-01-06 09:34:00 -0800 | [diff] [blame] | 455 | public @NonNull Builder setStatus(@Status int status) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 456 | mStatus = checkStatus(status); |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 457 | return this; |
| 458 | } |
| 459 | |
| 460 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 461 | * 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. Moltmann | bb36206 | 2015-12-22 20:08:00 -0800 | [diff] [blame] | 464 | * @param iconResourceId The resource ID of the icon. |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 465 | * @return This builder. |
| 466 | * @see PrinterInfo.Builder#setHasCustomPrinterIcon |
| 467 | */ |
| 468 | public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 469 | mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId, |
| 470 | "iconResourceId can't be negative"); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 471 | 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. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 485 | * @param hasCustomPrinterIcon If the printer has a custom icon or not. |
| 486 | * |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 487 | * @return This builder. |
| 488 | */ |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 489 | public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) { |
| 490 | mHasCustomPrinterIcon = hasCustomPrinterIcon; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 491 | return this; |
| 492 | } |
| 493 | |
| 494 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 495 | * Sets the <strong>localized</strong> printer name which |
| 496 | * is shown to the user |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 497 | * |
| 498 | * @param name The name. |
| 499 | * @return This builder. |
| 500 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 501 | public @NonNull Builder setName(@NonNull String name) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 502 | mName = checkName(name); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 503 | return this; |
| 504 | } |
| 505 | |
| 506 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 507 | * Sets the <strong>localized</strong> printer description |
| 508 | * which is shown to the user |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 509 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 510 | * @param description The description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 511 | * @return This builder. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 512 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 513 | public @NonNull Builder setDescription(@NonNull String description) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 514 | mDescription = description; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 515 | return this; |
| 516 | } |
| 517 | |
| 518 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 519 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 526 | mInfoIntent = infoIntent; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 527 | return this; |
| 528 | } |
| 529 | |
| 530 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 531 | * Sets the printer capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 532 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 533 | * @param capabilities The capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 534 | * @return This builder. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 535 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 536 | public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 537 | mCapabilities = capabilities; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 538 | return this; |
| 539 | } |
| 540 | |
| 541 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 542 | * Creates a new {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 543 | * |
| 544 | * @return A new {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 545 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 546 | public @NonNull PrinterInfo build() { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 547 | return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId, |
| 548 | mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities, |
| 549 | mCustomPrinterIconGen); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 550 | } |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 551 | |
| 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 561 | mCustomPrinterIconGen++; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 562 | return this; |
| 563 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 566 | public static final @android.annotation.NonNull Parcelable.Creator<PrinterInfo> CREATOR = |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 567 | 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 | } |