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