163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann/*
263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Copyright (C) 2016 The Android Open Source Project
363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * you may not use this file except in compliance with the License.
663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * You may obtain a copy of the License at
763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
1063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Unless required by applicable law or agreed to in writing, software
1163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
1263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * See the License for the specific language governing permissions and
1463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * limitations under the License.
1563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann */
1663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannpackage com.android.printservice.recommendation.plugin.xerox;
1763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
1863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport android.net.nsd.NsdServiceInfo;
1963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport android.text.TextUtils;
2063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
2163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport java.nio.charset.StandardCharsets;
2263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport java.util.Locale;
2363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport java.util.Map;
2463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
2563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannclass MDnsUtils {
2663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String ATTRIBUTE__TY = "ty";
2763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String ATTRIBUTE__PRODUCT = "product";
2863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String ATTRIBUTE__USB_MFG = "usb_MFG";
2963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String ATTRIBUTE__USB_MDL = "usb_MDL";
3063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String ATTRIBUTE__MFG = "mfg";
3163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String EXCLUDE_FUJI = "fuji";
3263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static final String PDL_ATTRIBUTE = "pdl";
3363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
3463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static boolean isVendorPrinter(NsdServiceInfo networkDevice, String[] vendorValues) {
3563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
3663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        Map<String, byte[]> attributes = networkDevice.getAttributes();
3763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String product = getString(attributes.get(ATTRIBUTE__PRODUCT));
3863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String ty = getString(attributes.get(ATTRIBUTE__TY));
3963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String usbMfg = getString(attributes.get(ATTRIBUTE__USB_MFG));
4063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String usbMdl = getString(attributes.get(ATTRIBUTE__USB_MDL));
4163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String mfg = getString(attributes.get(ATTRIBUTE__MFG));
42620c5af97b556031c16f43c5c0410f7a5b0a3ec8Philip P. Moltmann        return (containsVendor(product, vendorValues) || containsVendor(ty, vendorValues) ||
43620c5af97b556031c16f43c5c0410f7a5b0a3ec8Philip P. Moltmann                containsVendor(usbMfg, vendorValues) || containsVendor(mfg, vendorValues)) &&
44620c5af97b556031c16f43c5c0410f7a5b0a3ec8Philip P. Moltmann                !(containsString(ty, EXCLUDE_FUJI) || containsString(product, EXCLUDE_FUJI) ||
45620c5af97b556031c16f43c5c0410f7a5b0a3ec8Philip P. Moltmann                        containsString(usbMdl, EXCLUDE_FUJI));
4663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
4763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
4863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static String getVendor(NsdServiceInfo networkDevice) {
4963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String vendor;
5063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
5163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        Map<String, byte[]> attributes = networkDevice.getAttributes();
5263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        vendor = getString(attributes.get(ATTRIBUTE__MFG));
5363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if (!TextUtils.isEmpty(vendor)) return vendor;
5463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        vendor = getString(attributes.get(ATTRIBUTE__USB_MFG));
5563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if (!TextUtils.isEmpty(vendor)) return vendor;
5663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
5763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return null;
5863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
5963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
6063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public static boolean checkPDLSupport(NsdServiceInfo networkDevice, String[] pdlFormats) {
6163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if (pdlFormats == null) return false;
6263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
6363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        String pdls = MDnsUtils.getString(networkDevice.getAttributes().get(PDL_ATTRIBUTE));
6463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if (pdls != null) {
6563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            for (String pdl : pdlFormats) {
6663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                if (pdls.contains(pdl)) {
6763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                    return true;
6863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                }
6963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            }
7063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        }
7163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return false;
7263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
7363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
7463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private static boolean containsVendor(String container, String[] vendorValues) {
7563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if ((container == null) || (vendorValues == null)) return false;
7663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        for (String value : vendorValues) {
7763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            if (containsString(container, value)
7863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                    || containsString(container.toLowerCase(Locale.US), value.toLowerCase(Locale.US))
7963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                    || containsString(container.toUpperCase(Locale.US), value.toUpperCase(Locale.US)))
8063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann                return true;
8163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        }
8263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return false;
8363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
8463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
8563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private static String getString(byte[] value) {
8663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        if (value != null) return new String(value, StandardCharsets.UTF_8);
8763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return null;
8863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
8963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
9063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private static boolean containsString(String container, String contained) {
9163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return (container != null) && (contained != null) && (container.equalsIgnoreCase(contained) || container.contains(contained + " "));
9263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
9363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann}
94