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.mdnsFilter; 18 19 import android.content.Context; 20 import android.net.nsd.NsdServiceInfo; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.StringRes; 24 25 import com.android.printservice.recommendation.PrintServicePlugin; 26 import com.android.printservice.recommendation.util.MDNSFilteredDiscovery; 27 import com.android.printservice.recommendation.util.MDNSUtils; 28 29 import java.util.HashSet; 30 import java.util.List; 31 import java.util.Set; 32 33 /** 34 * A plugin listening for mDNS results and only adding the ones that {@link 35 * MDNSUtils#isVendorPrinter match} configured list 36 */ 37 public class MDNSFilterPlugin implements PrintServicePlugin { 38 39 /** The mDNS service types supported */ 40 private static final Set<String> PRINTER_SERVICE_TYPES = new HashSet<String>() {{ 41 add("_ipp._tcp"); 42 }}; 43 44 /** 45 * The printer filter for {@link MDNSFilteredDiscovery} passing only mDNS results 46 * that {@link MDNSUtils#isVendorPrinter match} configured list 47 */ 48 private static class VendorNameFilter implements MDNSFilteredDiscovery.PrinterFilter { 49 /** mDNS names handled by the print service this plugin is for */ 50 private final @NonNull Set<String> mMDNSNames; 51 52 /** 53 * Filter constructor 54 * 55 * @param vendorNames The vendor names to pass 56 */ 57 VendorNameFilter(@NonNull Set<String> vendorNames) { 58 mMDNSNames = new HashSet<>(vendorNames); 59 } 60 61 @Override 62 public boolean matchesCriteria(NsdServiceInfo nsdServiceInfo) { 63 return MDNSUtils.isVendorPrinter(nsdServiceInfo, mMDNSNames); 64 } 65 } 66 67 /** Name of the print service this plugin is for */ 68 private final @StringRes int mName; 69 70 /** Package name of the print service this plugin is for */ 71 private final @NonNull CharSequence mPackageName; 72 73 /** The mDNS filtered discovery */ 74 private final MDNSFilteredDiscovery mMDNSFilteredDiscovery; 75 76 /** 77 * Create new stub that assumes that a print service can be used to print on all mPrinters 78 * matching some mDNS names. 79 * 80 * @param context The context the plugin runs in 81 * @param name The user friendly name of the print service 82 * @param packageName The package name of the print service 83 * @param mDNSNames The mDNS names of the printer. 84 */ 85 public MDNSFilterPlugin(@NonNull Context context, @NonNull String name, 86 @NonNull CharSequence packageName, @NonNull List<String> mDNSNames) { 87 mName = context.getResources().getIdentifier(name, null, 88 "com.android.printservice.recommendation"); 89 mPackageName = packageName; 90 mMDNSFilteredDiscovery = new MDNSFilteredDiscovery(context, PRINTER_SERVICE_TYPES, 91 new VendorNameFilter(new HashSet<>(mDNSNames))); 92 } 93 94 @Override 95 public @NonNull CharSequence getPackageName() { 96 return mPackageName; 97 } 98 99 @Override 100 public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception { 101 mMDNSFilteredDiscovery.start(callback); 102 } 103 104 @Override 105 public @StringRes int getName() { 106 return mName; 107 } 108 109 @Override 110 public void stop() throws Exception { 111 mMDNSFilteredDiscovery.stop(); 112 } 113 } 114