Home | History | Annotate | Download | only in xerox
      1 /*
      2  * Copyright (C) 2016 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 package com.android.printservice.recommendation.plugin.xerox;
     17 
     18 import android.net.nsd.NsdServiceInfo;
     19 import android.text.TextUtils;
     20 
     21 import java.nio.charset.StandardCharsets;
     22 import java.util.Locale;
     23 import java.util.Map;
     24 
     25 class MDnsUtils {
     26     public static final String ATTRIBUTE__TY = "ty";
     27     public static final String ATTRIBUTE__PRODUCT = "product";
     28     public static final String ATTRIBUTE__USB_MFG = "usb_MFG";
     29     public static final String ATTRIBUTE__USB_MDL = "usb_MDL";
     30     public static final String ATTRIBUTE__MFG = "mfg";
     31     public static final String EXCLUDE_FUJI = "fuji";
     32     public static final String PDL_ATTRIBUTE = "pdl";
     33 
     34     public static boolean isVendorPrinter(NsdServiceInfo networkDevice, String[] vendorValues) {
     35 
     36         Map<String, byte[]> attributes = networkDevice.getAttributes();
     37         String product = getString(attributes.get(ATTRIBUTE__PRODUCT));
     38         String ty = getString(attributes.get(ATTRIBUTE__TY));
     39         String usbMfg = getString(attributes.get(ATTRIBUTE__USB_MFG));
     40         String usbMdl = getString(attributes.get(ATTRIBUTE__USB_MDL));
     41         String mfg = getString(attributes.get(ATTRIBUTE__MFG));
     42         return (containsVendor(product, vendorValues) || containsVendor(ty, vendorValues) ||
     43                 containsVendor(usbMfg, vendorValues) || containsVendor(mfg, vendorValues)) &&
     44                 !(containsString(ty, EXCLUDE_FUJI) || containsString(product, EXCLUDE_FUJI) ||
     45                         containsString(usbMdl, EXCLUDE_FUJI));
     46     }
     47 
     48     public static String getVendor(NsdServiceInfo networkDevice) {
     49         String vendor;
     50 
     51         Map<String, byte[]> attributes = networkDevice.getAttributes();
     52         vendor = getString(attributes.get(ATTRIBUTE__MFG));
     53         if (!TextUtils.isEmpty(vendor)) return vendor;
     54         vendor = getString(attributes.get(ATTRIBUTE__USB_MFG));
     55         if (!TextUtils.isEmpty(vendor)) return vendor;
     56 
     57         return null;
     58     }
     59 
     60     public static boolean checkPDLSupport(NsdServiceInfo networkDevice, String[] pdlFormats) {
     61         if (pdlFormats == null) return false;
     62 
     63         String pdls = MDnsUtils.getString(networkDevice.getAttributes().get(PDL_ATTRIBUTE));
     64         if (pdls != null) {
     65             for (String pdl : pdlFormats) {
     66                 if (pdls.contains(pdl)) {
     67                     return true;
     68                 }
     69             }
     70         }
     71         return false;
     72     }
     73 
     74     private static boolean containsVendor(String container, String[] vendorValues) {
     75         if ((container == null) || (vendorValues == null)) return false;
     76         for (String value : vendorValues) {
     77             if (containsString(container, value)
     78                     || containsString(container.toLowerCase(Locale.US), value.toLowerCase(Locale.US))
     79                     || containsString(container.toUpperCase(Locale.US), value.toUpperCase(Locale.US)))
     80                 return true;
     81         }
     82         return false;
     83     }
     84 
     85     private static String getString(byte[] value) {
     86         if (value != null) return new String(value, StandardCharsets.UTF_8);
     87         return null;
     88     }
     89 
     90     private static boolean containsString(String container, String contained) {
     91         return (container != null) && (contained != null) && (container.equalsIgnoreCase(contained) || container.contains(contained + " "));
     92     }
     93 }
     94