1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package com.android.bips.jni;
20 
21 import android.print.PrintAttributes;
22 import android.print.PrinterCapabilitiesInfo;
23 import android.text.TextUtils;
24 
25 import com.android.bips.BuiltInPrintService;
26 import com.android.bips.R;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Objects;
33 
34 public class LocalPrinterCapabilities {
35     public String path;
36     public String name;
37     public String uuid;
38     public String location;
39 
40     public boolean duplex;
41     public boolean borderless;
42     public boolean color;
43 
44     /** Reported MIME types include at least one that the lower layer supports */
45     public boolean isSupported;
46 
47     public String mediaDefault;
48     public int[] supportedMediaTypes;
49     public int[] supportedMediaSizes;
50 
51     /** Bears the underlying native C structure (printer_capabilities_t) or null if not present */
52     public byte[] nativeData;
53 
buildCapabilities(BuiltInPrintService service, PrinterCapabilitiesInfo.Builder builder)54     public void buildCapabilities(BuiltInPrintService service,
55             PrinterCapabilitiesInfo.Builder builder) {
56         builder.setColorModes(
57                 PrintAttributes.COLOR_MODE_MONOCHROME |
58                         (color ? PrintAttributes.COLOR_MODE_COLOR : 0),
59                 (color ? PrintAttributes.COLOR_MODE_COLOR : PrintAttributes.COLOR_MODE_MONOCHROME));
60 
61         MediaSizes mediaSizes = MediaSizes.getInstance(service);
62 
63         String defaultMediaName = mediaDefault;
64         if (TextUtils.isEmpty(defaultMediaName) ||
65                 null == mediaSizes.toMediaSize(defaultMediaName)) {
66             defaultMediaName = MediaSizes.DEFAULT_MEDIA_NAME;
67         }
68 
69         List<String> mediaNames = new ArrayList<>();
70         for (int supportedMediaSize : supportedMediaSizes) {
71             String mediaName = MediaSizes.toMediaName(supportedMediaSize);
72             if (mediaName != null) {
73                 mediaNames.add(mediaName);
74             }
75         }
76 
77         if (mediaNames.isEmpty()) {
78             mediaNames.addAll(MediaSizes.DEFAULT_MEDIA_NAMES);
79         }
80 
81         if (!mediaNames.contains(defaultMediaName)) {
82             defaultMediaName = mediaNames.get(0);
83         }
84 
85         // Add media sizes without duplicates
86         for (String mediaName : new HashSet<>(mediaNames)) {
87             builder.addMediaSize(mediaSizes.toMediaSize(mediaName),
88                     Objects.equals(mediaName, defaultMediaName));
89         }
90 
91         builder.addResolution(new PrintAttributes.Resolution(
92                 BackendConstants.RESOLUTION_300_DPI,
93                 service.getString(R.string.resolution_300_dpi), 300, 300), true);
94 
95         if (duplex) {
96             builder.setDuplexModes(
97                     PrintAttributes.DUPLEX_MODE_NONE | PrintAttributes.DUPLEX_MODE_LONG_EDGE |
98                             PrintAttributes.DUPLEX_MODE_SHORT_EDGE,
99                     PrintAttributes.DUPLEX_MODE_NONE);
100         }
101 
102         if (borderless) {
103             builder.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
104         }
105     }
106 
107     @Override
toString()108     public String toString() {
109         return "LocalPrinterCapabilities{" +
110                 "path=" + path +
111                 " name=" + name +
112                 " uuid=" + uuid +
113                 " location=" + location +
114                 " duplex=" + duplex +
115                 " borderless=" + borderless +
116                 " color=" + color +
117                 " isSupported=" + isSupported +
118                 " mediaDefault=" + mediaDefault +
119                 " supportedMediaTypes=" + Arrays.toString(supportedMediaTypes) +
120                 " supportedMediaSizes=" + Arrays.toString(supportedMediaSizes) +
121                 "}";
122     }
123 }