Sergey Yakovlev | 989c44b | 2016-11-18 18:59:47 +0300 | [diff] [blame] | 1 | /* |
| 2 | * (c) Copyright 2016 Samsung Electronics |
| 3 | * (c) Copyright 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package com.android.printservice.recommendation.plugin.samsung; |
| 18 | |
| 19 | import android.net.nsd.NsdServiceInfo; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.text.TextUtils; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import com.android.printservice.recommendation.util.MDNSFilteredDiscovery; |
| 25 | import com.android.printservice.recommendation.util.MDNSUtils; |
| 26 | |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Locale; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
| 33 | * Printer filter for Samsung printer models supported by the print service plugin |
| 34 | */ |
| 35 | class PrinterFilterSamsung implements MDNSFilteredDiscovery.PrinterFilter { |
| 36 | private static final String TAG = "PrinterFilterSamsung"; |
| 37 | |
| 38 | static final Set<String> SAMSUNG_MDNS_SERVICES = new HashSet<String>() {{ |
| 39 | add("_pdl-datastream._tcp"); |
| 40 | }}; |
| 41 | |
| 42 | private static final String[] NOT_SUPPORTED_MODELS = new String[]{ |
| 43 | "SCX-5x15", |
| 44 | "SF-555P", |
| 45 | "CF-555P", |
| 46 | "SCX-4x16", |
| 47 | "SCX-4214F", |
| 48 | "CLP-500", |
| 49 | "CJX-", |
| 50 | "MJC-" |
| 51 | }; |
| 52 | private static final String ATTR_USB_MFG = "usb_MFG"; |
| 53 | private static final String ATTR_MFG = "mfg"; |
| 54 | private static final String ATTR_USB_MDL = "usb_MDL"; |
| 55 | private static final String ATTR_MDL = "mdl"; |
| 56 | private static final String ATTR_PRODUCT = "product"; |
| 57 | private static final String ATTR_TY = "ty"; |
| 58 | |
| 59 | private static Set<String> SAMUNG_VENDOR_SET = new HashSet<String>() {{ |
| 60 | add("samsung"); |
| 61 | }}; |
| 62 | |
| 63 | @Override |
| 64 | public boolean matchesCriteria(NsdServiceInfo nsdServiceInfo) { |
| 65 | if (!MDNSUtils.isSupportedServiceType(nsdServiceInfo, SAMSUNG_MDNS_SERVICES)) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (!MDNSUtils.isVendorPrinter(nsdServiceInfo, SAMUNG_VENDOR_SET)) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | String modelName = getSamsungModelName(nsdServiceInfo); |
| 74 | if (modelName != null && isSupportedSamsungModel(modelName)) { |
| 75 | Log.d(TAG, "Samsung printer found: " + nsdServiceInfo.getServiceName()); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | private boolean isSupportedSamsungModel(String model) { |
| 82 | if (!TextUtils.isEmpty(model)) { |
| 83 | String modelToUpper = model.toUpperCase(Locale.US); |
| 84 | for (String unSupportedPrinter : NOT_SUPPORTED_MODELS) { |
| 85 | if (modelToUpper.contains(unSupportedPrinter)) { |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | private String getSamsungModelName(@NonNull NsdServiceInfo resolvedDevice) { |
| 94 | Map<String,byte[]> attributes = resolvedDevice.getAttributes(); |
| 95 | String usb_mfg = MDNSUtils.getString(attributes.get(ATTR_USB_MFG)); |
| 96 | if (TextUtils.isEmpty(usb_mfg)) { |
| 97 | usb_mfg = MDNSUtils.getString(attributes.get(ATTR_MFG)); |
| 98 | } |
| 99 | |
| 100 | String usb_mdl = MDNSUtils.getString(attributes.get(ATTR_USB_MDL)); |
| 101 | if (TextUtils.isEmpty(usb_mdl)) { |
| 102 | usb_mdl = MDNSUtils.getString(attributes.get(ATTR_MDL)); |
| 103 | } |
| 104 | |
| 105 | String modelName; |
| 106 | if (!TextUtils.isEmpty(usb_mfg) && !TextUtils.isEmpty(usb_mdl)) { |
| 107 | modelName = usb_mfg.trim() + " " + usb_mdl.trim(); |
| 108 | } else { |
| 109 | modelName = MDNSUtils.getString(attributes.get(ATTR_PRODUCT)); |
| 110 | if (TextUtils.isEmpty(modelName)) { |
| 111 | modelName = MDNSUtils.getString(attributes.get(ATTR_TY)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return modelName; |
| 116 | } |
| 117 | } |