• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.net.InetAddress;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Objects;
34 
35 public class LocalPrinterCapabilities {
36     public String path;
37     public String name;
38     public String uuid;
39     public String location;
40 
41     public boolean duplex;
42     public boolean borderless;
43     public boolean color;
44 
45     public int printerTopMargin;
46     public int printerBottomMargin;
47     public int printerLeftMargin;
48     public int printerRightMargin;
49 
50     /** Reported MIME types include at least one that the lower layer supports */
51     public boolean isSupported;
52 
53     public String mediaDefault;
54     public int[] supportedMediaTypes;
55     public int[] supportedMediaSizes;
56 
57     public InetAddress inetAddress;
58 
59     /** Bears the underlying native C structure (printer_capabilities_t) or null if not present */
60     public byte[] nativeData;
61 
62     /** Public key of certificate for this printer, if known */
63     public byte[] certificate;
64 
65     public int[] mediaReadySizes;
66     public String mopriaCertified;
67     public String[] markerNames;
68     public String[] markerTypes;
69     public String[] markerColors;
70     public int[] markerHighLevel;
71     public int[] markerLowLevel;
72     public int[] markerLevel;
73     public String[] mPrinterIconUris;
74 
buildCapabilities(BuiltInPrintService service, PrinterCapabilitiesInfo.Builder builder)75     public void buildCapabilities(BuiltInPrintService service,
76             PrinterCapabilitiesInfo.Builder builder) {
77         builder.setColorModes(
78                 PrintAttributes.COLOR_MODE_MONOCHROME
79                         | (color ? PrintAttributes.COLOR_MODE_COLOR : 0),
80                 (color ? PrintAttributes.COLOR_MODE_COLOR : PrintAttributes.COLOR_MODE_MONOCHROME));
81 
82         MediaSizes mediaSizes = MediaSizes.getInstance(service);
83 
84         String defaultMediaName = mediaDefault;
85         if (TextUtils.isEmpty(defaultMediaName)
86                 || null == mediaSizes.toMediaSize(defaultMediaName)) {
87             defaultMediaName = MediaSizes.DEFAULT_MEDIA_NAME;
88         }
89 
90         List<String> mediaNames = new ArrayList<>();
91         for (int supportedMediaSize : supportedMediaSizes) {
92             String mediaName = MediaSizes.toMediaName(supportedMediaSize);
93             if (mediaName != null) {
94                 mediaNames.add(mediaName);
95             }
96         }
97 
98         if (mediaNames.isEmpty()) {
99             mediaNames.addAll(MediaSizes.DEFAULT_MEDIA_NAMES);
100         }
101 
102         if (!mediaNames.contains(defaultMediaName)) {
103             defaultMediaName = mediaNames.get(0);
104         }
105 
106         // Add media sizes without duplicates
107         for (String mediaName : new HashSet<>(mediaNames)) {
108             builder.addMediaSize(mediaSizes.toMediaSize(mediaName),
109                     Objects.equals(mediaName, defaultMediaName));
110         }
111 
112         builder.addResolution(new PrintAttributes.Resolution(
113                 BackendConstants.RESOLUTION_300_DPI,
114                 service.getString(R.string.resolution_300_dpi), 300, 300), true);
115 
116         if (duplex) {
117             builder.setDuplexModes(
118                     PrintAttributes.DUPLEX_MODE_NONE | PrintAttributes.DUPLEX_MODE_LONG_EDGE
119                             | PrintAttributes.DUPLEX_MODE_SHORT_EDGE,
120                     PrintAttributes.DUPLEX_MODE_NONE);
121         }
122 
123         if (borderless) {
124             builder.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
125         }
126     }
127 
128     @Override
toString()129     public String toString() {
130         return "LocalPrinterCapabilities{"
131                 + "path=" + path
132                 + " name=" + name
133                 + " uuid=" + uuid
134                 + " location=" + location
135                 + " duplex=" + duplex
136                 + " borderless=" + borderless
137                 + " color=" + color
138                 + " printerTopMargin=" + printerTopMargin
139                 + " printerBottomMargin=" + printerBottomMargin
140                 + " printerLeftMargin=" + printerLeftMargin
141                 + " printerRightMargin=" + printerRightMargin
142                 + " isSupported=" + isSupported
143                 + " mediaDefault=" + mediaDefault
144                 + " supportedMediaTypes=" + Arrays.toString(supportedMediaTypes)
145                 + " supportedMediaSizes=" + Arrays.toString(supportedMediaSizes)
146                 + " inetAddress=" + inetAddress
147                 + " certificate=" + (certificate != null)
148                 + "}";
149     }
150 }
151