| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.print; |
| 18 |
|
| 19 |
import static com.android.internal.util.dump.DumpUtils.writeComponentName; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.content.Context; |
| 23 |
import android.print.PageRange; |
| 24 |
import android.print.PrintAttributes; |
| 25 |
import android.print.PrintDocumentInfo; |
| 26 |
import android.print.PrintJobId; |
| 27 |
import android.print.PrintJobInfo; |
| 28 |
import android.print.PrinterCapabilitiesInfo; |
| 29 |
import android.print.PrinterId; |
| 30 |
import android.print.PrinterInfo; |
| 31 |
import android.service.print.MarginsProto; |
| 32 |
import android.service.print.MediaSizeProto; |
| 33 |
import android.service.print.PageRangeProto; |
| 34 |
import android.service.print.PrintAttributesProto; |
| 35 |
import android.service.print.PrintDocumentInfoProto; |
| 36 |
import android.service.print.PrintJobInfoProto; |
| 37 |
import android.service.print.PrinterCapabilitiesProto; |
| 38 |
import android.service.print.PrinterIdProto; |
| 39 |
import android.service.print.PrinterInfoProto; |
| 40 |
import android.service.print.ResolutionProto; |
| 41 |
|
| 42 |
import com.android.internal.util.dump.DualDumpOutputStream; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class DumpUtils { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static void writePrinterId(@NonNull DualDumpOutputStream proto, String idName, long id, |
| 57 |
@NonNull PrinterId printerId) { |
| 58 |
long token = proto.start(idName, id); |
| 59 |
writeComponentName(proto, "service_name", PrinterIdProto.SERVICE_NAME, |
| 60 |
printerId.getServiceName()); |
| 61 |
proto.write("local_id", PrinterIdProto.LOCAL_ID, printerId.getLocalId()); |
| 62 |
proto.end(token); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static void writePrinterCapabilities(@NonNull Context context, |
| 74 |
@NonNull DualDumpOutputStream proto, String idName, long id, |
| 75 |
@NonNull PrinterCapabilitiesInfo cap) { |
| 76 |
long token = proto.start(idName, id); |
| 77 |
writeMargins(proto, "min_margins", PrinterCapabilitiesProto.MIN_MARGINS, |
| 78 |
cap.getMinMargins()); |
| 79 |
|
| 80 |
int numMediaSizes = cap.getMediaSizes().size(); |
| 81 |
for (int i = 0; i < numMediaSizes; i++) { |
| 82 |
writeMediaSize(context, proto, "media_sizes", PrinterCapabilitiesProto.MEDIA_SIZES, |
| 83 |
cap.getMediaSizes().get(i)); |
| 84 |
} |
| 85 |
|
| 86 |
int numResolutions = cap.getResolutions().size(); |
| 87 |
for (int i = 0; i < numResolutions; i++) { |
| 88 |
writeResolution(proto, "resolutions", PrinterCapabilitiesProto.RESOLUTIONS, |
| 89 |
cap.getResolutions().get(i)); |
| 90 |
} |
| 91 |
|
| 92 |
if ((cap.getColorModes() & PrintAttributes.COLOR_MODE_MONOCHROME) != 0) { |
| 93 |
proto.write("color_modes", PrinterCapabilitiesProto.COLOR_MODES, |
| 94 |
PrintAttributesProto.COLOR_MODE_MONOCHROME); |
| 95 |
} |
| 96 |
if ((cap.getColorModes() & PrintAttributes.COLOR_MODE_COLOR) != 0) { |
| 97 |
proto.write("color_modes", PrinterCapabilitiesProto.COLOR_MODES, |
| 98 |
PrintAttributesProto.COLOR_MODE_COLOR); |
| 99 |
} |
| 100 |
|
| 101 |
if ((cap.getDuplexModes() & PrintAttributes.DUPLEX_MODE_NONE) != 0) { |
| 102 |
proto.write("duplex_modes", PrinterCapabilitiesProto.DUPLEX_MODES, |
| 103 |
PrintAttributesProto.DUPLEX_MODE_NONE); |
| 104 |
} |
| 105 |
if ((cap.getDuplexModes() & PrintAttributes.DUPLEX_MODE_LONG_EDGE) != 0) { |
| 106 |
proto.write("duplex_modes", PrinterCapabilitiesProto.DUPLEX_MODES, |
| 107 |
PrintAttributesProto.DUPLEX_MODE_LONG_EDGE); |
| 108 |
} |
| 109 |
if ((cap.getDuplexModes() & PrintAttributes.DUPLEX_MODE_SHORT_EDGE) != 0) { |
| 110 |
proto.write("duplex_modes", PrinterCapabilitiesProto.DUPLEX_MODES, |
| 111 |
PrintAttributesProto.DUPLEX_MODE_SHORT_EDGE); |
| 112 |
} |
| 113 |
|
| 114 |
proto.end(token); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static void writePrinterInfo(@NonNull Context context, |
| 127 |
@NonNull DualDumpOutputStream proto, String idName, long id, |
| 128 |
@NonNull PrinterInfo info) { |
| 129 |
long token = proto.start(idName, id); |
| 130 |
writePrinterId(proto, "id", PrinterInfoProto.ID, info.getId()); |
| 131 |
proto.write("name", PrinterInfoProto.NAME, info.getName()); |
| 132 |
proto.write("status", PrinterInfoProto.STATUS, info.getStatus()); |
| 133 |
proto.write("description", PrinterInfoProto.DESCRIPTION, info.getDescription()); |
| 134 |
|
| 135 |
PrinterCapabilitiesInfo cap = info.getCapabilities(); |
| 136 |
if (cap != null) { |
| 137 |
writePrinterCapabilities(context, proto, "capabilities", PrinterInfoProto.CAPABILITIES, |
| 138 |
cap); |
| 139 |
} |
| 140 |
|
| 141 |
proto.end(token); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static void writeMediaSize(@NonNull Context context, @NonNull DualDumpOutputStream proto, |
| 154 |
String idName, long id, @NonNull PrintAttributes.MediaSize mediaSize) { |
| 155 |
long token = proto.start(idName, id); |
| 156 |
proto.write("id", MediaSizeProto.ID, mediaSize.getId()); |
| 157 |
proto.write("label", MediaSizeProto.LABEL, mediaSize.getLabel(context.getPackageManager())); |
| 158 |
proto.write("height_mils", MediaSizeProto.HEIGHT_MILS, mediaSize.getHeightMils()); |
| 159 |
proto.write("width_mils", MediaSizeProto.WIDTH_MILS, mediaSize.getWidthMils()); |
| 160 |
proto.end(token); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static void writeResolution(@NonNull DualDumpOutputStream proto, String idName, long id, |
| 172 |
@NonNull PrintAttributes.Resolution res) { |
| 173 |
long token = proto.start(idName, id); |
| 174 |
proto.write("id", ResolutionProto.ID, res.getId()); |
| 175 |
proto.write("label", ResolutionProto.LABEL, res.getLabel()); |
| 176 |
proto.write("horizontal_DPI", ResolutionProto.HORIZONTAL_DPI, res.getHorizontalDpi()); |
| 177 |
proto.write("veritical_DPI", ResolutionProto.VERTICAL_DPI, res.getVerticalDpi()); |
| 178 |
proto.end(token); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static void writeMargins(@NonNull DualDumpOutputStream proto, String idName, long id, |
| 190 |
@NonNull PrintAttributes.Margins margins) { |
| 191 |
long token = proto.start(idName, id); |
| 192 |
proto.write("top_mils", MarginsProto.TOP_MILS, margins.getTopMils()); |
| 193 |
proto.write("left_mils", MarginsProto.LEFT_MILS, margins.getLeftMils()); |
| 194 |
proto.write("right_mils", MarginsProto.RIGHT_MILS, margins.getRightMils()); |
| 195 |
proto.write("bottom_mils", MarginsProto.BOTTOM_MILS, margins.getBottomMils()); |
| 196 |
proto.end(token); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static void writePrintAttributes(@NonNull Context context, |
| 209 |
@NonNull DualDumpOutputStream proto, String idName, long id, |
| 210 |
@NonNull PrintAttributes attributes) { |
| 211 |
long token = proto.start(idName, id); |
| 212 |
|
| 213 |
PrintAttributes.MediaSize mediaSize = attributes.getMediaSize(); |
| 214 |
if (mediaSize != null) { |
| 215 |
writeMediaSize(context, proto, "media_size", PrintAttributesProto.MEDIA_SIZE, mediaSize); |
| 216 |
proto.write("is_portrait", PrintAttributesProto.IS_PORTRAIT, attributes.isPortrait()); |
| 217 |
} |
| 218 |
|
| 219 |
PrintAttributes.Resolution res = attributes.getResolution(); |
| 220 |
if (res != null) { |
| 221 |
writeResolution(proto, "resolution", PrintAttributesProto.RESOLUTION, res); |
| 222 |
} |
| 223 |
|
| 224 |
PrintAttributes.Margins minMargins = attributes.getMinMargins(); |
| 225 |
if (minMargins != null) { |
| 226 |
writeMargins(proto, "min_margings", PrintAttributesProto.MIN_MARGINS, minMargins); |
| 227 |
} |
| 228 |
|
| 229 |
proto.write("color_mode", PrintAttributesProto.COLOR_MODE, attributes.getColorMode()); |
| 230 |
proto.write("duplex_mode", PrintAttributesProto.DUPLEX_MODE, attributes.getDuplexMode()); |
| 231 |
proto.end(token); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static void writePrintDocumentInfo(@NonNull DualDumpOutputStream proto, String idName, |
| 243 |
long id, @NonNull PrintDocumentInfo info) { |
| 244 |
long token = proto.start(idName, id); |
| 245 |
proto.write("name", PrintDocumentInfoProto.NAME, info.getName()); |
| 246 |
|
| 247 |
int pageCount = info.getPageCount(); |
| 248 |
if (pageCount != PrintDocumentInfo.PAGE_COUNT_UNKNOWN) { |
| 249 |
proto.write("page_count", PrintDocumentInfoProto.PAGE_COUNT, pageCount); |
| 250 |
} |
| 251 |
|
| 252 |
proto.write("content_type", PrintDocumentInfoProto.CONTENT_TYPE, info.getContentType()); |
| 253 |
proto.write("data_size", PrintDocumentInfoProto.DATA_SIZE, info.getDataSize()); |
| 254 |
proto.end(token); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public static void writePageRange(@NonNull DualDumpOutputStream proto, String idName, long id, |
| 266 |
@NonNull PageRange range) { |
| 267 |
long token = proto.start(idName, id); |
| 268 |
proto.write("start", PageRangeProto.START, range.getStart()); |
| 269 |
proto.write("end", PageRangeProto.END, range.getEnd()); |
| 270 |
proto.end(token); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public static void writePrintJobInfo(@NonNull Context context, |
| 283 |
@NonNull DualDumpOutputStream proto, String idName, long id, |
| 284 |
@NonNull PrintJobInfo printJobInfo) { |
| 285 |
long token = proto.start(idName, id); |
| 286 |
proto.write("label", PrintJobInfoProto.LABEL, printJobInfo.getLabel()); |
| 287 |
|
| 288 |
PrintJobId printJobId = printJobInfo.getId(); |
| 289 |
if (printJobId != null) { |
| 290 |
proto.write("print_job_id", PrintJobInfoProto.PRINT_JOB_ID, |
| 291 |
printJobId.flattenToString()); |
| 292 |
} |
| 293 |
|
| 294 |
int state = printJobInfo.getState(); |
| 295 |
if (state >= PrintJobInfoProto.STATE_CREATED && state <= PrintJobInfoProto.STATE_CANCELED) { |
| 296 |
proto.write("state", PrintJobInfoProto.STATE, state); |
| 297 |
} else { |
| 298 |
proto.write("state", PrintJobInfoProto.STATE, PrintJobInfoProto.STATE_UNKNOWN); |
| 299 |
} |
| 300 |
|
| 301 |
PrinterId printer = printJobInfo.getPrinterId(); |
| 302 |
if (printer != null) { |
| 303 |
writePrinterId(proto, "printer", PrintJobInfoProto.PRINTER, printer); |
| 304 |
} |
| 305 |
|
| 306 |
String tag = printJobInfo.getTag(); |
| 307 |
if (tag != null) { |
| 308 |
proto.write("tag", PrintJobInfoProto.TAG, tag); |
| 309 |
} |
| 310 |
|
| 311 |
proto.write("creation_time", PrintJobInfoProto.CREATION_TIME, |
| 312 |
printJobInfo.getCreationTime()); |
| 313 |
|
| 314 |
PrintAttributes attributes = printJobInfo.getAttributes(); |
| 315 |
if (attributes != null) { |
| 316 |
writePrintAttributes(context, proto, "attributes", PrintJobInfoProto.ATTRIBUTES, |
| 317 |
attributes); |
| 318 |
} |
| 319 |
|
| 320 |
PrintDocumentInfo docInfo = printJobInfo.getDocumentInfo(); |
| 321 |
if (docInfo != null) { |
| 322 |
writePrintDocumentInfo(proto, "document_info", PrintJobInfoProto.DOCUMENT_INFO, |
| 323 |
docInfo); |
| 324 |
} |
| 325 |
|
| 326 |
proto.write("is_canceling", PrintJobInfoProto.IS_CANCELING, printJobInfo.isCancelling()); |
| 327 |
|
| 328 |
PageRange[] pages = printJobInfo.getPages(); |
| 329 |
if (pages != null) { |
| 330 |
for (int i = 0; i < pages.length; i++) { |
| 331 |
writePageRange(proto, "pages", PrintJobInfoProto.PAGES, pages[i]); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
proto.write("has_advanced_options", PrintJobInfoProto.HAS_ADVANCED_OPTIONS, |
| 336 |
printJobInfo.getAdvancedOptions() != null); |
| 337 |
proto.write("progress", PrintJobInfoProto.PROGRESS, printJobInfo.getProgress()); |
| 338 |
|
| 339 |
CharSequence status = printJobInfo.getStatus(context.getPackageManager()); |
| 340 |
if (status != null) { |
| 341 |
proto.write("status", PrintJobInfoProto.STATUS, status.toString()); |
| 342 |
} |
| 343 |
|
| 344 |
proto.end(token); |
| 345 |
} |
| 346 |
} |