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

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

Merge "Add Samsung print recommendation plugin." into nyc-dev

parents 56f2f917 a26b775d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -32,8 +32,15 @@
        <item>Hewlett Packard</item>
    </string-array>

    <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>

    <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>
        <item>@array/known_print_vendor_info_for_samsung</item>
    </array>
</resources>
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@
    <string name="plugin_vendor_brother">Brother</string>
    <string name="plugin_vendor_canon">Canon</string>
    <string name="plugin_vendor_xerox">Xerox</string>
    <string name="plugin_vendor_samsung">Samsung Electronics</string>
    <string name="plugin_vendor_samsung">Samsung</string>
    <string name="plugin_vendor_epson">Epson</string>
    <string name="plugin_vendor_konica_minolta">Konica Minolta</string>
    <string name="plugin_vendor_fuji">Fuji</string>
+0 −8
Original line number Diff line number Diff line
@@ -50,14 +50,6 @@
        </mdns-names>
    </vendor>

    <vendor>
        <name>@string/plugin_vendor_samsung</name>
        <package>com.sec.app.samsungprintservice</package>
        <mdns-names>
            <mdns-name>Samsung</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
@@ -25,6 +25,7 @@ import com.android.printservice.recommendation.plugin.hp.HPRecommendationPlugin;
import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin;
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 org.xmlpull.v1.XmlPullParserException;

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

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

        final int numPlugins = mPlugins.size();
        for (int i = 0; i < numPlugins; i++) {
            try {
+74 −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.samsung;

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

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

public 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__MFG = "mfg";

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

    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 mfg = getString(attributes.get(ATTRIBUTE__MFG));
        return containsVendor(product, vendorValues) || containsVendor(ty, vendorValues) || containsVendor(usbMfg, vendorValues) || containsVendor(mfg, vendorValues);

    }

    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;
    }

    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 boolean containsString(String container, String contained) {
        return (container != null) && (contained != null) && (container.equalsIgnoreCase(contained) || container.contains(contained + " "));
    }
}
Loading