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 17 package com.android.printservice.recommendation.plugin.hp; 18 19 import android.net.nsd.NsdServiceInfo; 20 import android.text.TextUtils; 21 22 import java.nio.charset.StandardCharsets; 23 import java.util.Locale; 24 import java.util.Map; 25 26 public class MDnsUtils { 27 public static final String ATTRIBUTE__TY = "ty"; 28 public static final String ATTRIBUTE__PRODUCT = "product"; 29 public static final String ATTRIBUTE__USB_MFG = "usb_MFG"; 30 public static final String ATTRIBUTE__MFG = "mfg"; 31 32 public static String getString(byte[] value) { 33 if (value != null) return new String(value,StandardCharsets.UTF_8); 34 return null; 35 } 36 37 public static boolean isVendorPrinter(NsdServiceInfo networkDevice, String[] vendorValues) { 38 39 Map<String,byte[]> attributes = networkDevice.getAttributes(); 40 String product = getString(attributes.get(ATTRIBUTE__PRODUCT)); 41 String ty = getString(attributes.get(ATTRIBUTE__TY)); 42 String usbMfg = getString(attributes.get(ATTRIBUTE__USB_MFG)); 43 String mfg = getString(attributes.get(ATTRIBUTE__MFG)); 44 return containsVendor(product, vendorValues) || containsVendor(ty, vendorValues) || containsVendor(usbMfg, vendorValues) || containsVendor(mfg, vendorValues); 45 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 private static boolean containsVendor(String container, String[] vendorValues) { 61 if ((container == null) || (vendorValues == null)) return false; 62 for (String value : vendorValues) { 63 if (containsString(container, value) 64 || containsString(container.toLowerCase(Locale.US), value.toLowerCase(Locale.US)) 65 || containsString(container.toUpperCase(Locale.US), value.toUpperCase(Locale.US))) 66 return true; 67 } 68 return false; 69 } 70 71 private static boolean containsString(String container, String contained) { 72 return (container != null) && (contained != null) && (container.equalsIgnoreCase(contained) || container.contains(contained + " ")); 73 } 74 } 75