• 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     /** Reported MIME types include at least one that the lower layer supports */
46     public boolean isSupported;
47 
48     public String mediaDefault;
49     public int[] supportedMediaTypes;
50     public int[] supportedMediaSizes;
51 
52     public InetAddress inetAddress;
53 
54     /** Bears the underlying native C structure (printer_capabilities_t) or null if not present */
55     public byte[] nativeData;
56 
buildCapabilities(BuiltInPrintService service, PrinterCapabilitiesInfo.Builder builder)57     public void buildCapabilities(BuiltInPrintService service,
58             PrinterCapabilitiesInfo.Builder builder) {
59         builder.setColorModes(
60                 PrintAttributes.COLOR_MODE_MONOCHROME
61                         | (color ? PrintAttributes.COLOR_MODE_COLOR : 0),
62                 (color ? PrintAttributes.COLOR_MODE_COLOR : PrintAttributes.COLOR_MODE_MONOCHROME));
63 
64         MediaSizes mediaSizes = MediaSizes.getInstance(service);
65 
66         String defaultMediaName = mediaDefault;
67         if (TextUtils.isEmpty(defaultMediaName)
68                 || null == mediaSizes.toMediaSize(defaultMediaName)) {
69             defaultMediaName = MediaSizes.DEFAULT_MEDIA_NAME;
70         }
71 
72         List<String> mediaNames = new ArrayList<>();
73         for (int supportedMediaSize : supportedMediaSizes) {
74             String mediaName = MediaSizes.toMediaName(supportedMediaSize);
75             if (mediaName != null) {
76                 mediaNames.add(mediaName);
77             }
78         }
79 
80         if (mediaNames.isEmpty()) {
81             mediaNames.addAll(MediaSizes.DEFAULT_MEDIA_NAMES);
82         }
83 
84         if (!mediaNames.contains(defaultMediaName)) {
85             defaultMediaName = mediaNames.get(0);
86         }
87 
88         // Add media sizes without duplicates
89         for (String mediaName : new HashSet<>(mediaNames)) {
90             builder.addMediaSize(mediaSizes.toMediaSize(mediaName),
91                     Objects.equals(mediaName, defaultMediaName));
92         }
93 
94         builder.addResolution(new PrintAttributes.Resolution(
95                 BackendConstants.RESOLUTION_300_DPI,
96                 service.getString(R.string.resolution_300_dpi), 300, 300), true);
97 
98         if (duplex) {
99             builder.setDuplexModes(
100                     PrintAttributes.DUPLEX_MODE_NONE | PrintAttributes.DUPLEX_MODE_LONG_EDGE
101                             | PrintAttributes.DUPLEX_MODE_SHORT_EDGE,
102                     PrintAttributes.DUPLEX_MODE_NONE);
103         }
104 
105         if (borderless) {
106             builder.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
107         }
108     }
109 
110     @Override
toString()111     public String toString() {
112         return "LocalPrinterCapabilities{"
113                 + "path=" + path
114                 + " name=" + name
115                 + " uuid=" + uuid
116                 + " location=" + location
117                 + " duplex=" + duplex
118                 + " borderless=" + borderless
119                 + " color=" + color
120                 + " isSupported=" + isSupported
121                 + " mediaDefault=" + mediaDefault
122                 + " supportedMediaTypes=" + Arrays.toString(supportedMediaTypes)
123                 + " supportedMediaSizes=" + Arrays.toString(supportedMediaSizes)
124                 + " inetAddress=" + inetAddress
125                 + "}";
126     }
127 }
128