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; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 35 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 36 | import com.android.internal.util.Preconditions; |
| 37 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 38 | import java.lang.annotation.Retention; |
| 39 | import java.lang.annotation.RetentionPolicy; |
| 40 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 41 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 42 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 48 | * <p> |
| 49 | * Once {@link PrinterInfo.Builder#build() built} the objects are immutable. |
| 50 | * </p> |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 51 | */ |
| 52 | public final class PrinterInfo implements Parcelable { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 53 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 54 | /** @hide */ |
| 55 | @IntDef({ |
| 56 | STATUS_IDLE, STATUS_BUSY, STATUS_UNAVAILABLE |
| 57 | }) |
| 58 | @Retention(RetentionPolicy.SOURCE) |
| 59 | public @interface Status { |
| 60 | } |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 61 | /** Printer status: the printer is idle and ready to print. */ |
| 62 | public static final int STATUS_IDLE = 1; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 63 | |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 64 | /** 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 69 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 70 | private final @NonNull PrinterId mId; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 71 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 72 | /** 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] | 73 | private final int mIconResourceId; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 74 | |
| 75 | /** If a custom icon can be loaded for the printer */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 76 | private final boolean mHasCustomPrinterIcon; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 77 | |
| 78 | /** The generation of the icon in the cache. */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 79 | private final int mCustomPrinterIconGen; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 80 | |
| 81 | /** Intent that launches the activity showing more information about the printer. */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 82 | private final @Nullable PendingIntent mInfoIntent; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 83 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 84 | private final @NonNull String mName; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 85 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 86 | private final @Status int mStatus; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 87 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 88 | private final @Nullable String mDescription; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 89 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 90 | private final @Nullable PrinterCapabilitiesInfo mCapabilities; |
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 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 Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Get the globally unique printer id. |
| 109 | * |
| 110 | * @return The printer id. |
| 111 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 112 | public @NonNull PrinterId getId() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 113 | return mId; |
| 114 | } |
| 115 | |
| 116 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 117 | * 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. Moltmann | 2e30126 | 2016-06-16 12:39:54 -0700 | [diff] [blame] | 165 | * 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 176 | * Get the printer name. |
| 177 | * |
| 178 | * @return The printer name. |
| 179 | */ |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 180 | public @NonNull String getName() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 181 | return mName; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Gets the printer status. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 186 | * |
| 187 | * @return The status. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 188 | * |
| 189 | * @see #STATUS_BUSY |
| 190 | * @see #STATUS_IDLE |
| 191 | * @see #STATUS_UNAVAILABLE |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 192 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 193 | public @Status int getStatus() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 194 | return mStatus; |
| 195 | } |
| 196 | |
| 197 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 198 | * Gets the printer description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 199 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 200 | * @return The description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 201 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 202 | public @Nullable String getDescription() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 203 | return mDescription; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 207 | * 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 219 | * Gets the printer capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 220 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 221 | * @return The capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 222 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 223 | public @Nullable PrinterCapabilitiesInfo getCapabilities() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 224 | return mCapabilities; |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 227 | /** |
| 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 266 | private PrinterInfo(Parcel parcel) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 267 | // 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 272 | mDescription = parcel.readString(); |
| 273 | mCapabilities = parcel.readParcelable(null); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 274 | mIconResourceId = parcel.readInt(); |
| 275 | mHasCustomPrinterIcon = parcel.readByte() != 0; |
| 276 | mCustomPrinterIconGen = parcel.readInt(); |
| 277 | mInfoIntent = parcel.readParcelable(null); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 278 | } |
| 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 288 | parcel.writeString(mName); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 289 | parcel.writeInt(mStatus); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 290 | parcel.writeString(mDescription); |
| 291 | parcel.writeParcelable(mCapabilities, flags); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 292 | parcel.writeInt(mIconResourceId); |
| 293 | parcel.writeByte((byte) (mHasCustomPrinterIcon ? 1 : 0)); |
| 294 | parcel.writeInt(mCustomPrinterIconGen); |
| 295 | parcel.writeParcelable(mInfoIntent, flags); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 299 | public int hashCode() { |
| 300 | final int prime = 31; |
| 301 | int result = 1; |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 302 | result = prime * result + mId.hashCode(); |
| 303 | result = prime * result + mName.hashCode(); |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 304 | result = prime * result + mStatus; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 305 | result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0); |
| 306 | result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 307 | 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 Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 311 | return result; |
| 312 | } |
| 313 | |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 314 | /** |
| 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 323 | if (!mId.equals(other.mId)) { |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 324 | return false; |
| 325 | } |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 326 | if (!mName.equals(other.mName)) { |
| 327 | return false; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 328 | } |
| 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 Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 358 | @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. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 370 | if (!equalsIgnoringStatus(other)) { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 371 | return false; |
| 372 | } |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 373 | if (mStatus != other.mStatus) { |
| 374 | return false; |
| 375 | } |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 376 | return true; |
| 377 | } |
| 378 | |
| 379 | @Override |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 380 | public String toString() { |
| 381 | StringBuilder builder = new StringBuilder(); |
| 382 | builder.append("PrinterInfo{"); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 383 | 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. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 388 | builder.append(", iconResId=").append(mIconResourceId); |
| 389 | builder.append(", hasCustomPrinterIcon=").append(mHasCustomPrinterIcon); |
| 390 | builder.append(", customPrinterIconGen=").append(mCustomPrinterIconGen); |
| 391 | builder.append(", infoIntent=").append(mInfoIntent); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 392 | builder.append("\"}"); |
| 393 | return builder.toString(); |
| 394 | } |
| 395 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 396 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 397 | * Builder for creating of a {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 398 | */ |
| 399 | public static final class Builder { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 400 | 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 409 | |
| 410 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 411 | * Constructor. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 412 | * |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 413 | * @param printerId The printer id. Cannot be null. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 414 | * @param name The printer name. Cannot be empty. |
| 415 | * @param status The printer status. Must be a valid status. |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 416 | * @throws IllegalArgumentException If the printer id is null, or the |
| 417 | * printer name is empty or the status is not a valid one. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 418 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 419 | public Builder(@NonNull PrinterId printerId, @NonNull String name, @Status int status) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 420 | mPrinterId = checkPrinterId(printerId); |
| 421 | mName = checkName(name); |
| 422 | mStatus = checkStatus(status); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Constructor. |
| 427 | * |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 428 | * @param other Other info from which to start building. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 429 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 430 | public Builder(@NonNull PrinterInfo other) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 431 | 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 Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 443 | * Sets the printer status. |
| 444 | * |
| 445 | * @param status The status. |
| 446 | * @return This builder. |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 447 | * @see PrinterInfo#STATUS_IDLE |
| 448 | * @see PrinterInfo#STATUS_BUSY |
| 449 | * @see PrinterInfo#STATUS_UNAVAILABLE |
| 450 | */ |
Philip P. Moltmann | b8c1a02ad | 2016-01-06 09:34:00 -0800 | [diff] [blame] | 451 | public @NonNull Builder setStatus(@Status int status) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 452 | mStatus = checkStatus(status); |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 453 | return this; |
| 454 | } |
| 455 | |
| 456 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 457 | * 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. Moltmann | bb36206 | 2015-12-22 20:08:00 -0800 | [diff] [blame] | 460 | * @param iconResourceId The resource ID of the icon. |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 461 | * @return This builder. |
| 462 | * @see PrinterInfo.Builder#setHasCustomPrinterIcon |
| 463 | */ |
| 464 | public @NonNull Builder setIconResourceId(@DrawableRes int iconResourceId) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 465 | mIconResourceId = Preconditions.checkArgumentNonnegative(iconResourceId, |
| 466 | "iconResourceId can't be negative"); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 467 | 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. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 481 | * @param hasCustomPrinterIcon If the printer has a custom icon or not. |
| 482 | * |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 483 | * @return This builder. |
| 484 | */ |
Philip P. Moltmann | d74d1e5 | 2016-03-17 16:37:47 -0700 | [diff] [blame] | 485 | public @NonNull Builder setHasCustomPrinterIcon(boolean hasCustomPrinterIcon) { |
| 486 | mHasCustomPrinterIcon = hasCustomPrinterIcon; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 487 | return this; |
| 488 | } |
| 489 | |
| 490 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 491 | * Sets the <strong>localized</strong> printer name which |
| 492 | * is shown to the user |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 493 | * |
| 494 | * @param name The name. |
| 495 | * @return This builder. |
| 496 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 497 | public @NonNull Builder setName(@NonNull String name) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 498 | mName = checkName(name); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 499 | return this; |
| 500 | } |
| 501 | |
| 502 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 503 | * Sets the <strong>localized</strong> printer description |
| 504 | * which is shown to the user |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 505 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 506 | * @param description The description. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 507 | * @return This builder. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 508 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 509 | public @NonNull Builder setDescription(@NonNull String description) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 510 | mDescription = description; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 511 | return this; |
| 512 | } |
| 513 | |
| 514 | /** |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 515 | * 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 522 | mInfoIntent = infoIntent; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 523 | return this; |
| 524 | } |
| 525 | |
| 526 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 527 | * Sets the printer capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 528 | * |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 529 | * @param capabilities The capabilities. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 530 | * @return This builder. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 531 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 532 | public @NonNull Builder setCapabilities(@NonNull PrinterCapabilitiesInfo capabilities) { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 533 | mCapabilities = capabilities; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 534 | return this; |
| 535 | } |
| 536 | |
| 537 | /** |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 538 | * Creates a new {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 539 | * |
| 540 | * @return A new {@link PrinterInfo}. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 541 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 542 | public @NonNull PrinterInfo build() { |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 543 | return new PrinterInfo(mPrinterId, mName, mStatus, mIconResourceId, |
| 544 | mHasCustomPrinterIcon, mDescription, mInfoIntent, mCapabilities, |
| 545 | mCustomPrinterIconGen); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 546 | } |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 547 | |
| 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. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 557 | mCustomPrinterIconGen++; |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 558 | return this; |
| 559 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 560 | } |
| 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 | } |