blob: d60a25f54a22bc13e481d52def5b05261cbfcf01 [file] [log] [blame]
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -07001/*
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
17package com.android.printservice.recommendation.plugin.mdnsFilter;
18
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070019import android.content.Context;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070020import android.net.nsd.NsdServiceInfo;
Sergey Yakovlev989c44b2016-11-18 18:59:47 +030021import android.annotation.NonNull;
22import android.annotation.StringRes;
23
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070024import com.android.printservice.recommendation.PrintServicePlugin;
Sergey Yakovlev989c44b2016-11-18 18:59:47 +030025import com.android.printservice.recommendation.util.MDNSFilteredDiscovery;
26import com.android.printservice.recommendation.util.MDNSUtils;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070027
28import java.util.HashSet;
29import java.util.List;
Sergey Yakovlev989c44b2016-11-18 18:59:47 +030030import java.util.Set;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070031
32/**
33 * A plugin listening for mDNS results and only adding the ones that {@link
34 * MDNSUtils#isVendorPrinter match} configured list
35 */
Sergey Yakovlev989c44b2016-11-18 18:59:47 +030036public class MDNSFilterPlugin implements PrintServicePlugin {
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070037
Sergey Yakovlev989c44b2016-11-18 18:59:47 +030038 /** 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. Moltmann9dcb86a2016-03-14 14:31:12 -070065
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 Yakovlev989c44b2016-11-18 18:59:47 +030072 /** The mDNS filtered discovery */
73 private final MDNSFilteredDiscovery mMDNSFilteredDiscovery;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070074
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 Yakovlev989c44b2016-11-18 18:59:47 +030086 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. Moltmann9dcb86a2016-03-14 14:31:12 -070091 }
92
93 @Override
94 public @NonNull CharSequence getPackageName() {
95 return mPackageName;
96 }
97
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070098 @Override
99 public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
Sergey Yakovlev989c44b2016-11-18 18:59:47 +0300100 mMDNSFilteredDiscovery.start(callback);
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700101 }
102
103 @Override
104 public @StringRes int getName() {
105 return mName;
106 }
107
108 @Override
109 public void stop() throws Exception {
Sergey Yakovlev989c44b2016-11-18 18:59:47 +0300110 mMDNSFilteredDiscovery.stop();
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -0700111 }
112}