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
17package com.android.printservice.recommendation.plugin.mdnsFilter;
18
19import android.content.Context;
20import android.net.nsd.NsdServiceInfo;
21import android.annotation.NonNull;
22import android.annotation.StringRes;
23
24import com.android.printservice.recommendation.PrintServicePlugin;
25import com.android.printservice.recommendation.util.MDNSFilteredDiscovery;
26import com.android.printservice.recommendation.util.MDNSUtils;
27
28import java.util.HashSet;
29import java.util.List;
30import java.util.Set;
31
32/**
33 * A plugin listening for mDNS results and only adding the ones that {@link
34 * MDNSUtils#isVendorPrinter match} configured list
35 */
36public class MDNSFilterPlugin implements PrintServicePlugin {
37
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    }
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
72    /** The mDNS filtered discovery */
73    private final MDNSFilteredDiscovery mMDNSFilteredDiscovery;
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) {
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)));
91    }
92
93    @Override
94    public @NonNull CharSequence getPackageName() {
95        return mPackageName;
96    }
97
98    @Override
99    public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
100        mMDNSFilteredDiscovery.start(callback);
101    }
102
103    @Override
104    public @StringRes int getName() {
105        return mName;
106    }
107
108    @Override
109    public void stop() throws Exception {
110        mMDNSFilteredDiscovery.stop();
111    }
112}
113