summaryrefslogtreecommitdiff
path: root/packages/PrintRecommendationService/src
diff options
context:
space:
mode:
authorPhilip P. Moltmann <moltmann@google.com>2016-04-04 14:02:57 -0700
committerPhilip P. Moltmann <moltmann@google.com>2016-04-19 14:31:04 -0700
commitb87c08da82d50b1358f068a3ae44068022c7af2e (patch)
treef8addffe3f8eed3a78daa4a0d7160f8323f52285 /packages/PrintRecommendationService/src
parent9c211a339689a2e54da3315ccdbf22add472c76a (diff)
Expose additional fields needed by PrintRecommendationService GTS test
and move files into properly named directory. Fixes: 28025769, 28214466 Change-Id: I14737515fc12525a1685a1a222f21913755ac988
Diffstat (limited to 'packages/PrintRecommendationService/src')
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java75
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java110
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java152
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java199
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java325
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java98
-rw-r--r--packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java133
7 files changed, 1092 insertions, 0 deletions
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java
new file mode 100644
index 000000000000..d604ef8a49ea
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/PrintServicePlugin.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation;
+
+import android.annotation.IntRange;
+import android.annotation.NonNull;
+import android.annotation.StringRes;
+
+/**
+ * Interface to be implemented by each print service plugin.
+ * <p/>
+ * A print service plugin is a minimal version of a real {@link android.printservice.PrintService
+ * print service}. You cannot print using the plugin. The only functionality in the plugin is to
+ * report the number of printers that the real service would discover.
+ */
+public interface PrintServicePlugin {
+ /**
+ * Call back used by the print service plugins.
+ */
+ interface PrinterDiscoveryCallback {
+ /**
+ * Announce that something changed and the UI for this plugin should be updated.
+ *
+ * @param numDiscoveredPrinters The number of printers discovered.
+ */
+ void onChanged(@IntRange(from = 0) int numDiscoveredPrinters);
+ }
+
+ /**
+ * Get the name (a string reference) of the {@link android.printservice.PrintService print
+ * service} with the {@link #getPackageName specified package name}. This is read once, hence
+ * returning different data at different times is not allowed.
+ *
+ * @return The name of the print service as a string reference. The localization is handled
+ * outside of the plugin.
+ */
+ @StringRes int getName();
+
+ /**
+ * The package name of the full print service.
+ *
+ * @return The package name
+ */
+ @NonNull CharSequence getPackageName();
+
+ /**
+ * Start the discovery plugin.
+ *
+ * @param callback Callbacks used by this plugin.
+ *
+ * @throws Exception If anything went wrong when starting the plugin
+ */
+ void start(@NonNull PrinterDiscoveryCallback callback) throws Exception;
+
+ /**
+ * Stop the plugin. This can only return once the plugin is completely finished and cleaned up.
+ *
+ * @throws Exception If anything went wrong while stopping plugin
+ */
+ void stop() throws Exception;
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java
new file mode 100644
index 000000000000..9f6dad8f2e2a
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RecommendationServiceImpl.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation;
+
+import android.content.res.Configuration;
+import android.printservice.recommendation.RecommendationInfo;
+import android.printservice.recommendation.RecommendationService;
+import android.printservice.PrintService;
+import android.util.Log;
+import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin;
+import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+/**
+ * Service that recommends {@link PrintService print services} that might be a good idea to install.
+ */
+public class RecommendationServiceImpl extends RecommendationService
+ implements RemotePrintServicePlugin.OnChangedListener {
+ private static final String LOG_TAG = "PrintServiceRecService";
+
+ /** All registered plugins */
+ private ArrayList<RemotePrintServicePlugin> mPlugins;
+
+ @Override
+ public void onConnected() {
+ mPlugins = new ArrayList<>();
+
+ try {
+ for (VendorConfig config : VendorConfig.getAllConfigs(this)) {
+ try {
+ mPlugins.add(new RemotePrintServicePlugin(new MDNSFilterPlugin(this,
+ config.name, config.packageName, config.mDNSNames), this, false));
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Could not initiate simple MDNS plugin for " +
+ config.packageName, e);
+ }
+ }
+ } catch (IOException | XmlPullParserException e) {
+ new RuntimeException("Could not parse vendorconfig", e);
+ }
+
+ final int numPlugins = mPlugins.size();
+ for (int i = 0; i < numPlugins; i++) {
+ try {
+ mPlugins.get(i).start();
+ } catch (RemotePrintServicePlugin.PluginException e) {
+ Log.e(LOG_TAG, "Could not start plugin", e);
+ }
+ }
+ }
+
+ @Override
+ public void onDisconnected() {
+ final int numPlugins = mPlugins.size();
+ for (int i = 0; i < numPlugins; i++) {
+ try {
+ mPlugins.get(i).stop();
+ } catch (RemotePrintServicePlugin.PluginException e) {
+ Log.e(LOG_TAG, "Could not stop plugin", e);
+ }
+ }
+ }
+
+ @Override
+ public void onConfigurationChanged(Configuration newConfig) {
+ // Need to update plugin names as they might be localized
+ onChanged();
+ }
+
+ @Override
+ public void onChanged() {
+ ArrayList<RecommendationInfo> recommendations = new ArrayList<>();
+
+ final int numPlugins = mPlugins.size();
+ for (int i = 0; i < numPlugins; i++) {
+ RemotePrintServicePlugin plugin = mPlugins.get(i);
+
+ try {
+ int numPrinters = plugin.getNumPrinters();
+
+ if (numPrinters > 0) {
+ recommendations.add(new RecommendationInfo(plugin.packageName,
+ getString(plugin.name), numPrinters,
+ plugin.recommendsMultiVendorService));
+ }
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Could not read state of plugin for " + plugin.packageName, e);
+ }
+ }
+
+ updateRecommendations(recommendations);
+ }
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java
new file mode 100644
index 000000000000..dbd164946dfb
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/RemotePrintServicePlugin.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation;
+
+import android.annotation.IntRange;
+import android.annotation.NonNull;
+import android.annotation.StringRes;
+import com.android.internal.util.Preconditions;
+
+/**
+ * Wrapper for a {@link PrintServicePlugin}, isolating issues with the plugin as good as possible
+ * from the {@link RecommendationServiceImpl service}.
+ */
+class RemotePrintServicePlugin implements PrintServicePlugin.PrinterDiscoveryCallback {
+ /** Lock for this object */
+ private final Object mLock = new Object();
+
+ /** The name of the print service. */
+ public final @StringRes int name;
+
+ /** If the print service if for more than a single vendor */
+ public final boolean recommendsMultiVendorService;
+
+ /** The package name of the full print service */
+ public final @NonNull CharSequence packageName;
+
+ /** Wrapped plugin */
+ private final @NonNull PrintServicePlugin mPlugin;
+
+ /** The number of printers discovered by the plugin */
+ private @IntRange(from = 0) int mNumPrinters;
+
+ /** If the plugin is started by not yet stopped */
+ private boolean isRunning;
+
+ /** Listener for changes to {@link #mNumPrinters}. */
+ private @NonNull OnChangedListener mListener;
+
+ /**
+ * Create a new remote for a {@link PrintServicePlugin plugin}.
+ *
+ * @param plugin The plugin to be wrapped
+ * @param listener The listener to be notified about changes in this plugin
+ * @param recommendsMultiVendorService If the plugin detects printers of more than a single
+ * vendor
+ *
+ * @throws PluginException If the plugin has issues while caching basic stub properties
+ */
+ public RemotePrintServicePlugin(@NonNull PrintServicePlugin plugin,
+ @NonNull OnChangedListener listener, boolean recommendsMultiVendorService)
+ throws PluginException {
+ mListener = listener;
+ mPlugin = plugin;
+ this.recommendsMultiVendorService = recommendsMultiVendorService;
+
+ // We handle any throwable to isolate our self from bugs in the plugin code.
+ // Cache simple properties to avoid having to deal with exceptions later in the code.
+ try {
+ name = Preconditions.checkArgumentPositive(mPlugin.getName(), "name");
+ packageName = Preconditions.checkStringNotEmpty(mPlugin.getPackageName(),
+ "packageName");
+ } catch (Throwable e) {
+ throw new PluginException(mPlugin, "Cannot cache simple properties ", e);
+ }
+
+ isRunning = false;
+ }
+
+ /**
+ * Start the plugin. From now on there might be callbacks to the registered listener.
+ */
+ public void start()
+ throws PluginException {
+ // We handle any throwable to isolate our self from bugs in the stub code
+ try {
+ synchronized (mLock) {
+ isRunning = true;
+ mPlugin.start(this);
+ }
+ } catch (Throwable e) {
+ throw new PluginException(mPlugin, "Cannot start", e);
+ }
+ }
+
+ /**
+ * Stop the plugin. From this call on there will not be any more callbacks.
+ */
+ public void stop() throws PluginException {
+ // We handle any throwable to isolate our self from bugs in the stub code
+ try {
+ synchronized (mLock) {
+ mPlugin.stop();
+ isRunning = false;
+ }
+ } catch (Throwable e) {
+ throw new PluginException(mPlugin, "Cannot stop", e);
+ }
+ }
+
+ /**
+ * Get the current number of printers reported by the stub.
+ *
+ * @return The number of printers reported by the stub.
+ */
+ public @IntRange(from = 0) int getNumPrinters() {
+ return mNumPrinters;
+ }
+
+ @Override
+ public void onChanged(@IntRange(from = 0) int numDiscoveredPrinters) {
+ synchronized (mLock) {
+ Preconditions.checkState(isRunning);
+
+ mNumPrinters = Preconditions.checkArgumentNonnegative(numDiscoveredPrinters,
+ "numDiscoveredPrinters");
+
+ if (mNumPrinters > 0) {
+ mListener.onChanged();
+ }
+ }
+ }
+
+ /**
+ * Listener to listen for changes to {@link #getNumPrinters}
+ */
+ public interface OnChangedListener {
+ void onChanged();
+ }
+
+ /**
+ * Exception thrown if the stub has any issues.
+ */
+ public class PluginException extends Exception {
+ private PluginException(PrintServicePlugin plugin, String message, Throwable e) {
+ super(plugin + ": " + message, e);
+ }
+ }
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java
new file mode 100644
index 000000000000..26300b1e37b9
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/MDNSFilterPlugin.java
@@ -0,0 +1,199 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation.plugin.mdnsFilter;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.StringRes;
+import android.content.Context;
+import android.net.nsd.NsdManager;
+import android.net.nsd.NsdServiceInfo;
+import android.util.Log;
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.util.Preconditions;
+import com.android.printservice.recommendation.PrintServicePlugin;
+import com.android.printservice.recommendation.util.MDNSUtils;
+import com.android.printservice.recommendation.util.NsdResolveQueue;
+
+import java.util.HashSet;
+import java.util.List;
+
+/**
+ * A plugin listening for mDNS results and only adding the ones that {@link
+ * MDNSUtils#isVendorPrinter match} configured list
+ */
+public class MDNSFilterPlugin implements PrintServicePlugin, NsdManager.DiscoveryListener {
+ private static final String LOG_TAG = "MDNSFilterPlugin";
+
+ private static final String PRINTER_SERVICE_TYPE = "_ipp._tcp";
+
+ /** Name of the print service this plugin is for */
+ private final @StringRes int mName;
+
+ /** Package name of the print service this plugin is for */
+ private final @NonNull CharSequence mPackageName;
+
+ /** mDNS names handled by the print service this plugin is for */
+ private final @NonNull HashSet<String> mMDNSNames;
+
+ /** Printer identifiers of the mPrinters found. */
+ @GuardedBy("mLock")
+ private final @NonNull HashSet<String> mPrinters;
+
+ /** Context of the user of this plugin */
+ private final @NonNull Context mContext;
+
+ /**
+ * Call back to report the number of mPrinters found.
+ *
+ * We assume that {@link #start} and {@link #stop} are never called in parallel, hence it is
+ * safe to not synchronize access to this field.
+ */
+ private @Nullable PrinterDiscoveryCallback mCallback;
+
+ /** Queue used to resolve nsd infos */
+ private final @NonNull NsdResolveQueue mResolveQueue;
+
+ /**
+ * Create new stub that assumes that a print service can be used to print on all mPrinters
+ * matching some mDNS names.
+ *
+ * @param context The context the plugin runs in
+ * @param name The user friendly name of the print service
+ * @param packageName The package name of the print service
+ * @param mDNSNames The mDNS names of the printer.
+ */
+ public MDNSFilterPlugin(@NonNull Context context, @NonNull String name,
+ @NonNull CharSequence packageName, @NonNull List<String> mDNSNames) {
+ mContext = Preconditions.checkNotNull(context, "context");
+ mName = mContext.getResources().getIdentifier(Preconditions.checkStringNotEmpty(name,
+ "name"), null, mContext.getPackageName());
+ mPackageName = Preconditions.checkStringNotEmpty(packageName);
+ mMDNSNames = new HashSet<>(Preconditions
+ .checkCollectionNotEmpty(Preconditions.checkCollectionElementsNotNull(mDNSNames,
+ "mDNSNames"), "mDNSNames"));
+
+ mResolveQueue = NsdResolveQueue.getInstance();
+ mPrinters = new HashSet<>();
+ }
+
+ @Override
+ public @NonNull CharSequence getPackageName() {
+ return mPackageName;
+ }
+
+ /**
+ * @return The NDS manager
+ */
+ private NsdManager getNDSManager() {
+ return (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
+ }
+
+ @Override
+ public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
+ mCallback = callback;
+
+ getNDSManager().discoverServices(PRINTER_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD,
+ this);
+ }
+
+ @Override
+ public @StringRes int getName() {
+ return mName;
+ }
+
+ @Override
+ public void stop() throws Exception {
+ mCallback.onChanged(0);
+ mCallback = null;
+
+ getNDSManager().stopServiceDiscovery(this);
+ }
+
+ @Override
+ public void onStartDiscoveryFailed(String serviceType, int errorCode) {
+ Log.w(LOG_TAG, "Failed to start network discovery for type " + serviceType + ": "
+ + errorCode);
+ }
+
+ @Override
+ public void onStopDiscoveryFailed(String serviceType, int errorCode) {
+ Log.w(LOG_TAG, "Failed to stop network discovery for type " + serviceType + ": "
+ + errorCode);
+ }
+
+ @Override
+ public void onDiscoveryStarted(String serviceType) {
+ // empty
+ }
+
+ @Override
+ public void onDiscoveryStopped(String serviceType) {
+ mPrinters.clear();
+ }
+
+ @Override
+ public void onServiceFound(NsdServiceInfo serviceInfo) {
+ mResolveQueue.resolve(getNDSManager(), serviceInfo,
+ new NsdManager.ResolveListener() {
+ @Override
+ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
+ Log.w(LOG_TAG, "Service found: could not resolve " + serviceInfo + ": " +
+ errorCode);
+ }
+
+ @Override
+ public void onServiceResolved(NsdServiceInfo serviceInfo) {
+ if (MDNSUtils.isVendorPrinter(serviceInfo, mMDNSNames)) {
+ if (mCallback != null) {
+ boolean added = mPrinters.add(serviceInfo.getHost().getHostAddress());
+
+ if (added) {
+ mCallback.onChanged(mPrinters.size());
+ }
+ }
+ }
+ }
+ });
+ }
+
+ @Override
+ public void onServiceLost(NsdServiceInfo serviceInfo) {
+ mResolveQueue.resolve(getNDSManager(), serviceInfo,
+ new NsdManager.ResolveListener() {
+ @Override
+ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
+ Log.w(LOG_TAG, "Service lost: Could not resolve " + serviceInfo + ": "
+ + errorCode);
+ }
+
+ @Override
+ public void onServiceResolved(NsdServiceInfo serviceInfo) {
+ if (MDNSUtils.isVendorPrinter(serviceInfo, mMDNSNames)) {
+ if (mCallback != null) {
+ boolean removed = mPrinters
+ .remove(serviceInfo.getHost().getHostAddress());
+
+ if (removed) {
+ mCallback.onChanged(mPrinters.size());
+ }
+ }
+ }
+ }
+ });
+ }
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java
new file mode 100644
index 000000000000..57d5c710f6bd
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/plugin/mdnsFilter/VendorConfig.java
@@ -0,0 +1,325 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation.plugin.mdnsFilter;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.content.res.XmlResourceParser;
+import android.util.ArrayMap;
+import com.android.internal.annotations.Immutable;
+import com.android.internal.util.Preconditions;
+import com.android.printservice.recommendation.R;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Vendor configuration as read from {@link R.xml#vendorconfigs vendorconfigs.xml}. Configuration
+ * can be read via {@link #getConfig(Context, String)}.
+ */
+@Immutable
+public class VendorConfig {
+ /** Lock for {@link #sConfigs} */
+ private static final Object sLock = new Object();
+
+ /** Strings used as XML tags */
+ private static final String VENDORS_TAG = "vendors";
+ private static final String VENDOR_TAG = "vendor";
+ private static final String NAME_TAG = "name";
+ private static final String PACKAGE_TAG = "package";
+ private static final String MDNSNAMES_TAG = "mdns-names";
+ private static final String MDNSNAME_TAG = "mdns-name";
+
+ /** Map from vendor name to config. Initialized on first {@link #getConfig use}. */
+ private static @Nullable ArrayMap<String, VendorConfig> sConfigs;
+
+ /** Localized vendor name */
+ public final @NonNull String name;
+
+ /** Package name containing the print service for this vendor */
+ public final @NonNull String packageName;
+
+ /** mDNS names used by this vendor */
+ public final @NonNull List<String> mDNSNames;
+
+ /**
+ * Create an immutable configuration.
+ */
+ private VendorConfig(@NonNull String name, @NonNull String packageName,
+ @NonNull List<String> mDNSNames) {
+ this.name = Preconditions.checkStringNotEmpty(name);
+ this.packageName = Preconditions.checkStringNotEmpty(packageName);
+ this.mDNSNames = Preconditions.checkCollectionElementsNotNull(mDNSNames, "mDNSName");
+ }
+
+ /**
+ * Get the configuration for a vendor.
+ *
+ * @param context Calling context
+ * @param name The name of the config to read
+ *
+ * @return the config for the vendor or null if not found
+ *
+ * @throws IOException
+ * @throws XmlPullParserException
+ */
+ public static @Nullable VendorConfig getConfig(@NonNull Context context, @NonNull String name)
+ throws IOException, XmlPullParserException {
+ synchronized (sLock) {
+ if (sConfigs == null) {
+ sConfigs = readVendorConfigs(context);
+ }
+
+ return sConfigs.get(name);
+ }
+ }
+
+ /**
+ * Get all known vendor configurations.
+ *
+ * @param context Calling context
+ *
+ * @return The known configurations
+ *
+ * @throws IOException
+ * @throws XmlPullParserException
+ */
+ public static @NonNull Collection<VendorConfig> getAllConfigs(@NonNull Context context)
+ throws IOException, XmlPullParserException {
+ synchronized (sLock) {
+ if (sConfigs == null) {
+ sConfigs = readVendorConfigs(context);
+ }
+
+ return sConfigs.values();
+ }
+ }
+
+ /**
+ * Read the text from a XML tag.
+ *
+ * @param parser XML parser to read from
+ *
+ * @return The text or "" if no text was found
+ *
+ * @throws IOException
+ * @throws XmlPullParserException
+ */
+ private static @NonNull String readText(XmlPullParser parser)
+ throws IOException, XmlPullParserException {
+ String result = "";
+
+ if (parser.next() == XmlPullParser.TEXT) {
+ result = parser.getText();
+ parser.nextTag();
+ }
+
+ return result;
+ }
+
+ /**
+ * Read a tag with a text content from the parser.
+ *
+ * @param parser XML parser to read from
+ * @param tagName The name of the tag to read
+ *
+ * @return The text content of the tag
+ *
+ * @throws IOException
+ * @throws XmlPullParserException
+ */
+ private static @NonNull String readSimpleTag(@NonNull Context context,
+ @NonNull XmlPullParser parser, @NonNull String tagName, boolean resolveReferences)
+ throws IOException, XmlPullParserException {
+ parser.require(XmlPullParser.START_TAG, null, tagName);
+ String text = readText(parser);
+ parser.require(XmlPullParser.END_TAG, null, tagName);
+
+ if (resolveReferences && text.startsWith("@")) {
+ return context.getResources().getString(
+ context.getResources().getIdentifier(text, null, context.getPackageName()));
+ } else {
+ return text;
+ }
+ }
+
+ /**
+ * Read content of a list of tags.
+ *
+ * @param parser XML parser to read from
+ * @param tagName The name of the list tag
+ * @param subTagName The name of the list-element tags
+ * @param tagReader The {@link TagReader reader} to use to read the tag content
+ * @param <T> The type of the parsed tag content
+ *
+ * @return A list of {@link T}
+ *
+ * @throws XmlPullParserException
+ * @throws IOException
+ */
+ private static @NonNull <T> ArrayList<T> readTagList(@NonNull XmlPullParser parser,
+ @NonNull String tagName, @NonNull String subTagName, @NonNull TagReader<T> tagReader)
+ throws XmlPullParserException, IOException {
+ ArrayList<T> entries = new ArrayList<>();
+
+ parser.require(XmlPullParser.START_TAG, null, tagName);
+ while (parser.next() != XmlPullParser.END_TAG) {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ continue;
+ }
+
+ if (parser.getName().equals(subTagName)) {
+ entries.add(tagReader.readTag(parser, subTagName));
+ } else {
+ throw new XmlPullParserException(
+ "Unexpected subtag of " + tagName + ": " + parser.getName());
+ }
+ }
+
+ return entries;
+ }
+
+ /**
+ * Read the vendor configuration file.
+ *
+ * @param context The content issuing the read
+ *
+ * @return An map pointing from vendor name to config
+ *
+ * @throws IOException
+ * @throws XmlPullParserException
+ */
+ private static @NonNull ArrayMap<String, VendorConfig> readVendorConfigs(
+ @NonNull final Context context) throws IOException, XmlPullParserException {
+ try (XmlResourceParser parser = context.getResources().getXml(R.xml.vendorconfigs)) {
+ // Skip header
+ int parsingEvent;
+ do {
+ parsingEvent = parser.next();
+ } while (parsingEvent != XmlResourceParser.START_TAG);
+
+ ArrayList<VendorConfig> configs = readTagList(parser, VENDORS_TAG, VENDOR_TAG,
+ new TagReader<VendorConfig>() {
+ public VendorConfig readTag(XmlPullParser parser, String tagName)
+ throws XmlPullParserException, IOException {
+ return readVendorConfig(context, parser, tagName);
+ }
+ });
+
+ ArrayMap<String, VendorConfig> configMap = new ArrayMap<>(configs.size());
+ final int numConfigs = configs.size();
+ for (int i = 0; i < numConfigs; i++) {
+ VendorConfig config = configs.get(i);
+
+ configMap.put(config.name, config);
+ }
+
+ return configMap;
+ }
+ }
+
+ /**
+ * Read a single vendor configuration.
+ *
+ * @param parser XML parser to read from
+ * @param tagName The vendor tag
+ * @param context Calling context
+ *
+ * @return A config
+ *
+ * @throws XmlPullParserException
+ * @throws IOException
+ */
+ private static VendorConfig readVendorConfig(@NonNull final Context context,
+ @NonNull XmlPullParser parser, @NonNull String tagName) throws XmlPullParserException,
+ IOException {
+ parser.require(XmlPullParser.START_TAG, null, tagName);
+
+ String name = null;
+ String packageName = null;
+ List<String> mDNSNames = null;
+
+ while (parser.next() != XmlPullParser.END_TAG) {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ continue;
+ }
+
+ String subTagName = parser.getName();
+
+ switch (subTagName) {
+ case NAME_TAG:
+ name = readSimpleTag(context, parser, NAME_TAG, false);
+ break;
+ case PACKAGE_TAG:
+ packageName = readSimpleTag(context, parser, PACKAGE_TAG, true);
+ break;
+ case MDNSNAMES_TAG:
+ mDNSNames = readTagList(parser, MDNSNAMES_TAG, MDNSNAME_TAG,
+ new TagReader<String>() {
+ public String readTag(XmlPullParser parser, String tagName)
+ throws XmlPullParserException, IOException {
+ return readSimpleTag(context, parser, tagName, true);
+ }
+ }
+ );
+ break;
+ default:
+ throw new XmlPullParserException("Unexpected subtag of " + tagName + ": "
+ + subTagName);
+
+ }
+ }
+
+ if (name == null) {
+ throw new XmlPullParserException("name is required");
+ }
+
+ if (packageName == null) {
+ throw new XmlPullParserException("package is required");
+ }
+
+ if (mDNSNames == null) {
+ mDNSNames = Collections.emptyList();
+ }
+
+ // A vendor config should be immutable
+ mDNSNames = Collections.unmodifiableList(mDNSNames);
+
+ return new VendorConfig(name, packageName, mDNSNames);
+ }
+
+ @Override
+ public String toString() {
+ return name + " -> " + packageName + ", " + mDNSNames;
+ }
+
+ /**
+ * Used a a "function pointer" when reading a tag in {@link #readTagList(XmlPullParser, String,
+ * String, TagReader)}.
+ *
+ * @param <T> The type of content to read
+ */
+ private interface TagReader<T> {
+ T readTag(XmlPullParser parser, String tagName) throws XmlPullParserException, IOException;
+ }
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java
new file mode 100644
index 000000000000..0541c3565dba
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/MDNSUtils.java
@@ -0,0 +1,98 @@
+/*
+ * (c) Copyright 2016 Mopria Alliance, Inc.
+ * (c) Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation.util;
+
+import android.annotation.NonNull;
+import android.net.nsd.NsdServiceInfo;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Utils for dealing with mDNS attributes
+ */
+public class MDNSUtils {
+ public static final String ATTRIBUTE_TY = "ty";
+ public static final String ATTRIBUTE_PRODUCT = "product";
+ public static final String ATTRIBUTE_USB_MFG = "usb_mfg";
+ public static final String ATTRIBUTE_MFG = "mfg";
+
+ /**
+ * Check if the service has any of a set of vendor names.
+ *
+ * @param serviceInfo The service
+ * @param vendorNames The vendors
+ *
+ * @return true iff the has any of the set of vendor names
+ */
+ public static boolean isVendorPrinter(@NonNull NsdServiceInfo serviceInfo,
+ @NonNull Set<String> vendorNames) {
+ for (Map.Entry<String, byte[]> entry : serviceInfo.getAttributes().entrySet()) {
+ // keys are case insensitive
+ String key = entry.getKey().toLowerCase();
+
+ switch (key) {
+ case ATTRIBUTE_TY:
+ case ATTRIBUTE_PRODUCT:
+ case ATTRIBUTE_USB_MFG:
+ case ATTRIBUTE_MFG:
+ if (entry.getValue() != null) {
+ if (containsVendor(new String(entry.getValue(), StandardCharsets.UTF_8),
+ vendorNames)) {
+ return true;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Check if the attribute matches any of the vendor names, ignoring capitalization.
+ *
+ * @param attr The attribute
+ * @param vendorNames The vendor names
+ *
+ * @return true iff the attribute matches any of the vendor names
+ */
+ private static boolean containsVendor(@NonNull String attr, @NonNull Set<String> vendorNames) {
+ for (String name : vendorNames) {
+ if (containsString(attr.toLowerCase(), name.toLowerCase())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check if a string in another string.
+ *
+ * @param container The string that contains the string
+ * @param contained The string that is contained
+ *
+ * @return true if the string is contained in the other
+ */
+ private static boolean containsString(@NonNull String container, @NonNull String contained) {
+ return container.equalsIgnoreCase(contained) || container.contains(contained + " ");
+ }
+}
diff --git a/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java
new file mode 100644
index 000000000000..fad50f6a404b
--- /dev/null
+++ b/packages/PrintRecommendationService/src/com/android/printservice/recommendation/util/NsdResolveQueue.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.printservice.recommendation.util;
+
+import android.annotation.NonNull;
+import android.net.nsd.NsdManager;
+import android.net.nsd.NsdServiceInfo;
+import com.android.internal.annotations.GuardedBy;
+
+import java.util.LinkedList;
+
+/**
+ * Nsd resolve requests for the same info cancel each other. Hence this class synchronizes the
+ * resolutions to hide this effect.
+ */
+public class NsdResolveQueue {
+ /** Lock for {@link #sInstance} */
+ private static final Object sLock = new Object();
+
+ /** Instance of this singleton */
+ @GuardedBy("sLock")
+ private static NsdResolveQueue sInstance;
+
+ /** Lock for {@link #mResolveRequests} */
+ private final Object mLock = new Object();
+
+ /** Current set of registered service info resolve attempts */
+ @GuardedBy("mLock")
+ private final LinkedList<NsdResolveRequest> mResolveRequests = new LinkedList<>();
+
+ public static NsdResolveQueue getInstance() {
+ synchronized (sLock) {
+ if (sInstance == null) {
+ sInstance = new NsdResolveQueue();
+ }
+
+ return sInstance;
+ }
+ }
+
+ /**
+ * Container for a request to resolve a serviceInfo.
+ */
+ private static class NsdResolveRequest {
+ final @NonNull NsdManager nsdManager;
+ final @NonNull NsdServiceInfo serviceInfo;
+ final @NonNull NsdManager.ResolveListener listener;
+
+ private NsdResolveRequest(@NonNull NsdManager nsdManager,
+ @NonNull NsdServiceInfo serviceInfo, @NonNull NsdManager.ResolveListener listener) {
+ this.nsdManager = nsdManager;
+ this.serviceInfo = serviceInfo;
+ this.listener = listener;
+ }
+ }
+
+ /**
+ * Resolve a serviceInfo or queue the request if there is a request currently in flight.
+ *
+ * @param nsdManager The nsd manager to use
+ * @param serviceInfo The service info to resolve
+ * @param listener The listener to call back once the info is resolved.
+ */
+ public void resolve(@NonNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo,
+ @NonNull NsdManager.ResolveListener listener) {
+ synchronized (mLock) {
+ mResolveRequests.addLast(new NsdResolveRequest(nsdManager, serviceInfo,
+ new ListenerWrapper(listener)));
+
+ if (mResolveRequests.size() == 1) {
+ resolveNextRequest();
+ }
+ }
+ }
+
+ /**
+ * Wrapper for a {@link NsdManager.ResolveListener}. Calls the listener and then
+ * {@link #resolveNextRequest()}.
+ */
+ private class ListenerWrapper implements NsdManager.ResolveListener {
+ private final @NonNull NsdManager.ResolveListener mListener;
+
+ private ListenerWrapper(@NonNull NsdManager.ResolveListener listener) {
+ mListener = listener;
+ }
+
+ @Override
+ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
+ mListener.onResolveFailed(serviceInfo, errorCode);
+
+ synchronized (mLock) {
+ mResolveRequests.pop();
+ resolveNextRequest();
+ }
+ }
+
+ @Override
+ public void onServiceResolved(NsdServiceInfo serviceInfo) {
+ mListener.onServiceResolved(serviceInfo);
+
+ synchronized (mLock) {
+ mResolveRequests.pop();
+ resolveNextRequest();
+ }
+ }
+ }
+
+ /**
+ * Resolve the next request if there is one.
+ */
+ private void resolveNextRequest() {
+ if (!mResolveRequests.isEmpty()) {
+ NsdResolveRequest request = mResolveRequests.getFirst();
+
+ request.nsdManager.resolveService(request.serviceInfo, request.listener);
+ }
+ }
+
+}