Debian Codegraph @

core/java/com/android/internal/print/DumpUtils.java

raw symbols SCIP
1 /*
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
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 * Utilities for dumping print related proto buffer
46 */
47 public class DumpUtils {
48 /**
49 * Write a {@link PrinterId} to a proto.
50 *
51 * @param proto The proto to write to
52 * @param idName Clear text name of the proto-id
53 * @param id The proto-id of the component name
54 * @param printerId The printer id to write
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 * Write a {@link PrinterCapabilitiesInfo} to a proto.
67 *
68 * @param proto The proto to write to
69 * @param idName Clear text name of the proto-id
70 * @param id The proto-id of the component name
71 * @param cap The capabilities to write
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 * Write a {@link PrinterInfo} to a proto.
119 *
120 * @param context The context used to resolve resources
121 * @param proto The proto to write to
122 * @param idName Clear text name of the proto-id
123 * @param id The proto-id of the component name
124 * @param info The printer info to write
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 * Write a {@link PrintAttributes.MediaSize} to a proto.
146 *
147 * @param context The context used to resolve resources
148 * @param proto The proto to write to
149 * @param idName Clear text name of the proto-id
150 * @param id The proto-id of the component name
151 * @param mediaSize The media size to write
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 * Write a {@link PrintAttributes.Resolution} to a proto.
165 *
166 * @param proto The proto to write to
167 * @param idName Clear text name of the proto-id
168 * @param id The proto-id of the component name
169 * @param res The resolution to write
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 * Write a {@link PrintAttributes.Margins} to a proto.
183 *
184 * @param proto The proto to write to
185 * @param idName Clear text name of the proto-id
186 * @param id The proto-id of the component name
187 * @param margins The margins to write
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 * Write a {@link PrintAttributes} to a proto.
201 *
202 * @param context The context used to resolve resources
203 * @param proto The proto to write to
204 * @param idName Clear text name of the proto-id
205 * @param id The proto-id of the component name
206 * @param attributes The attributes to write
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 * Write a {@link PrintDocumentInfo} to a proto.
236 *
237 * @param proto The proto to write to
238 * @param idName Clear text name of the proto-id
239 * @param id The proto-id of the component name
240 * @param info The info to write
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 * Write a {@link PageRange} to a proto.
259 *
260 * @param proto The proto to write to
261 * @param idName Clear text name of the proto-id
262 * @param id The proto-id of the component name
263 * @param range The range to write
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 * Write a {@link PrintJobInfo} to a proto.
275 *
276 * @param context The context used to resolve resources
277 * @param proto The proto to write to
278 * @param idName Clear text name of the proto-id
279 * @param id The proto-id of the component name
280 * @param printJobInfo The print job info to write
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 }