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