Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e509dcb4 authored by Philip P. Moltmann's avatar Philip P. Moltmann Committed by Android (Google) Code Review
Browse files

Merge "Move Xerox recommendation plugin to service." into nyc-dev

parents 30e434a9 63498aff
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -32,12 +32,20 @@
        <item>Hewlett Packard</item>
    </string-array>

    <!-- Samsung plugin -->
    <string-array name="known_print_vendor_info_for_samsung" translatable="false">
        <item>com.sec.app.samsungprintservice</item>
        <item>Samsung Electronics</item>
        <item>Samsung</item>
    </string-array>

    <!-- Xerox plugin -->
    <string-array name="known_print_vendor_info_for_xerox" translatable="false">
        <item>com.xerox.printservice</item>
        <item>Xerox</item>
        <item>Xerox</item>
    </string-array>

    <array name="known_print_plugin_vendors" translatable="false">
        <item>@array/known_print_vendor_info_for_mopria</item>
        <item>@array/known_print_vendor_info_for_hp</item>
+0 −8
Original line number Diff line number Diff line
@@ -42,14 +42,6 @@
        </mdns-names>
    </vendor>

    <vendor>
        <name>@string/plugin_vendor_xerox</name>
        <package>com.xerox.printservice</package>
        <mdns-names>
            <mdns-name>Xerox</mdns-name>
        </mdns-names>
    </vendor>

    <vendor>
        <name>@string/plugin_vendor_epson</name>
        <package>com.epson.mobilephone.android.epsonprintserviceplugin</package>
+9 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugi
import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig;
import com.android.printservice.recommendation.plugin.mopria.MopriaRecommendationPlugin;
import com.android.printservice.recommendation.plugin.samsung.SamsungRecommendationPlugin;
import com.android.printservice.recommendation.plugin.xerox.XeroxPrintServiceRecommendationPlugin;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
@@ -83,6 +84,14 @@ public class RecommendationServiceImpl extends RecommendationService
                    " plugin", e);
        }

        try {
            mPlugins.add(new RemotePrintServicePlugin(
                    new XeroxPrintServiceRecommendationPlugin(this), this, false));
        } catch (Exception e) {
            Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_xerox) +
                    " plugin", e);
        }

        final int numPlugins = mPlugins.size();
        for (int i = 0; i < numPlugins; i++) {
            try {
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.printservice.recommendation.plugin.xerox;

import android.net.nsd.NsdServiceInfo;
import android.text.TextUtils;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;

class MDnsUtils {
    public static final String ATTRIBUTE__TY = "ty";
    public static final String ATTRIBUTE__PRODUCT = "product";
    public static final String ATTRIBUTE__USB_MFG = "usb_MFG";
    public static final String ATTRIBUTE__USB_MDL = "usb_MDL";
    public static final String ATTRIBUTE__MFG = "mfg";
    public static final String EXCLUDE_FUJI = "fuji";
    public static final String PDL_ATTRIBUTE = "pdl";

    public static boolean isVendorPrinter(NsdServiceInfo networkDevice, String[] vendorValues) {

        Map<String, byte[]> attributes = networkDevice.getAttributes();
        String product = getString(attributes.get(ATTRIBUTE__PRODUCT));
        String ty = getString(attributes.get(ATTRIBUTE__TY));
        String usbMfg = getString(attributes.get(ATTRIBUTE__USB_MFG));
        String usbMdl = getString(attributes.get(ATTRIBUTE__USB_MDL));
        String mfg = getString(attributes.get(ATTRIBUTE__MFG));
        return containsVendor(product, vendorValues) || containsVendor(ty, vendorValues) || containsVendor(usbMfg, vendorValues) || containsVendor(mfg, vendorValues) && !(containsString(ty, EXCLUDE_FUJI) || containsString(product, EXCLUDE_FUJI) || containsString(usbMdl, EXCLUDE_FUJI));

    }

    public static String getVendor(NsdServiceInfo networkDevice) {
        String vendor;

        Map<String, byte[]> attributes = networkDevice.getAttributes();
        vendor = getString(attributes.get(ATTRIBUTE__MFG));
        if (!TextUtils.isEmpty(vendor)) return vendor;
        vendor = getString(attributes.get(ATTRIBUTE__USB_MFG));
        if (!TextUtils.isEmpty(vendor)) return vendor;

        return null;
    }

    public static boolean checkPDLSupport(NsdServiceInfo networkDevice, String[] pdlFormats) {
        if (pdlFormats == null) return false;

        String pdls = MDnsUtils.getString(networkDevice.getAttributes().get(PDL_ATTRIBUTE));
        if (pdls != null) {
            for (String pdl : pdlFormats) {
                if (pdls.contains(pdl)) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean containsVendor(String container, String[] vendorValues) {
        if ((container == null) || (vendorValues == null)) return false;
        for (String value : vendorValues) {
            if (containsString(container, value)
                    || containsString(container.toLowerCase(Locale.US), value.toLowerCase(Locale.US))
                    || containsString(container.toUpperCase(Locale.US), value.toUpperCase(Locale.US)))
                return true;
        }
        return false;
    }

    private static String getString(byte[] value) {
        if (value != null) return new String(value, StandardCharsets.UTF_8);
        return null;
    }

    private static boolean containsString(String container, String contained) {
        return (container != null) && (contained != null) && (container.equalsIgnoreCase(contained) || container.contains(contained + " "));
    }
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.printservice.recommendation.plugin.xerox;

import android.net.nsd.NsdServiceInfo;

import java.util.HashMap;

final class PrinterHashMap extends HashMap<String, NsdServiceInfo> {
    public static String getKey(NsdServiceInfo serviceInfo) {
        return serviceInfo.getServiceName();
    }

    public NsdServiceInfo addPrinter(NsdServiceInfo device) {
        return put(getKey(device), device);
    }

    public NsdServiceInfo removePrinter(NsdServiceInfo device) {
        return remove(getKey(device));
    }
}
Loading