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 57 /** Public key of certificate for this printer, if known */ 58 public byte[] certificate; 59 buildCapabilities(BuiltInPrintService service, PrinterCapabilitiesInfo.Builder builder)60 public void buildCapabilities(BuiltInPrintService service, 61 PrinterCapabilitiesInfo.Builder builder) { 62 builder.setColorModes( 63 PrintAttributes.COLOR_MODE_MONOCHROME 64 | (color ? PrintAttributes.COLOR_MODE_COLOR : 0), 65 (color ? PrintAttributes.COLOR_MODE_COLOR : PrintAttributes.COLOR_MODE_MONOCHROME)); 66 67 MediaSizes mediaSizes = MediaSizes.getInstance(service); 68 69 String defaultMediaName = mediaDefault; 70 if (TextUtils.isEmpty(defaultMediaName) 71 || null == mediaSizes.toMediaSize(defaultMediaName)) { 72 defaultMediaName = MediaSizes.DEFAULT_MEDIA_NAME; 73 } 74 75 List<String> mediaNames = new ArrayList<>(); 76 for (int supportedMediaSize : supportedMediaSizes) { 77 String mediaName = MediaSizes.toMediaName(supportedMediaSize); 78 if (mediaName != null) { 79 mediaNames.add(mediaName); 80 } 81 } 82 83 if (mediaNames.isEmpty()) { 84 mediaNames.addAll(MediaSizes.DEFAULT_MEDIA_NAMES); 85 } 86 87 if (!mediaNames.contains(defaultMediaName)) { 88 defaultMediaName = mediaNames.get(0); 89 } 90 91 // Add media sizes without duplicates 92 for (String mediaName : new HashSet<>(mediaNames)) { 93 builder.addMediaSize(mediaSizes.toMediaSize(mediaName), 94 Objects.equals(mediaName, defaultMediaName)); 95 } 96 97 builder.addResolution(new PrintAttributes.Resolution( 98 BackendConstants.RESOLUTION_300_DPI, 99 service.getString(R.string.resolution_300_dpi), 300, 300), true); 100 101 if (duplex) { 102 builder.setDuplexModes( 103 PrintAttributes.DUPLEX_MODE_NONE | PrintAttributes.DUPLEX_MODE_LONG_EDGE 104 | PrintAttributes.DUPLEX_MODE_SHORT_EDGE, 105 PrintAttributes.DUPLEX_MODE_NONE); 106 } 107 108 if (borderless) { 109 builder.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)); 110 } 111 } 112 113 @Override toString()114 public String toString() { 115 return "LocalPrinterCapabilities{" 116 + "path=" + path 117 + " name=" + name 118 + " uuid=" + uuid 119 + " location=" + location 120 + " duplex=" + duplex 121 + " borderless=" + borderless 122 + " color=" + color 123 + " isSupported=" + isSupported 124 + " mediaDefault=" + mediaDefault 125 + " supportedMediaTypes=" + Arrays.toString(supportedMediaTypes) 126 + " supportedMediaSizes=" + Arrays.toString(supportedMediaSizes) 127 + " inetAddress=" + inetAddress 128 + " certificate=" + (certificate != null) 129 + "}"; 130 } 131 } 132