summaryrefslogtreecommitdiff
path: root/packages/PrintRecommendationService
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
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')
-rw-r--r--packages/PrintRecommendationService/Android.mk29
-rw-r--r--packages/PrintRecommendationService/AndroidManifest.xml40
-rw-r--r--packages/PrintRecommendationService/CleanSpec.mk49
-rw-r--r--packages/PrintRecommendationService/MODULE_LICENSE_APACHE20
-rw-r--r--packages/PrintRecommendationService/NOTICE190
-rw-r--r--packages/PrintRecommendationService/res/values/donottranslate.xml18
-rw-r--r--packages/PrintRecommendationService/res/values/strings.xml30
-rw-r--r--packages/PrintRecommendationService/res/xml/vendorconfigs.xml96
-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
15 files changed, 1544 insertions, 0 deletions
diff --git a/packages/PrintRecommendationService/Android.mk b/packages/PrintRecommendationService/Android.mk
new file mode 100644
index 000000000000..66cb0573aef0
--- /dev/null
+++ b/packages/PrintRecommendationService/Android.mk
@@ -0,0 +1,29 @@
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := PrintRecommendationService
+
+include $(BUILD_PACKAGE)
+
+LOCAL_SDK_VERSION := system_current
+
+include $(call all-makefiles-under, $(LOCAL_PATH))
diff --git a/packages/PrintRecommendationService/AndroidManifest.xml b/packages/PrintRecommendationService/AndroidManifest.xml
new file mode 100644
index 000000000000..0eb218c853ec
--- /dev/null
+++ b/packages/PrintRecommendationService/AndroidManifest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ * 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.
+ */
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.printservice.recommendation">
+
+ <uses-permission android:name="android.permission.INTERNET" />
+
+ <application
+ android:allowClearUserData="false"
+ android:label="@string/app_label"
+ android:allowBackup= "false">
+
+ <service
+ android:name=".RecommendationServiceImpl"
+ android:permission="android.permission.BIND_PRINT_RECOMMENDATION_SERVICE">
+
+ <intent-filter>
+ <action android:name="android.printservice.recommendation.RecommendationService" />
+ </intent-filter>
+ </service>
+
+ </application>
+
+</manifest>
diff --git a/packages/PrintRecommendationService/CleanSpec.mk b/packages/PrintRecommendationService/CleanSpec.mk
new file mode 100644
index 000000000000..c087cb88680f
--- /dev/null
+++ b/packages/PrintRecommendationService/CleanSpec.mk
@@ -0,0 +1,49 @@
+# 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.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list. These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+# $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+# $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list. E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
diff --git a/packages/PrintRecommendationService/MODULE_LICENSE_APACHE2 b/packages/PrintRecommendationService/MODULE_LICENSE_APACHE2
new file mode 100644
index 000000000000..e69de29bb2d1
--- /dev/null
+++ b/packages/PrintRecommendationService/MODULE_LICENSE_APACHE2
diff --git a/packages/PrintRecommendationService/NOTICE b/packages/PrintRecommendationService/NOTICE
new file mode 100644
index 000000000000..c5b1efa7aac7
--- /dev/null
+++ b/packages/PrintRecommendationService/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/packages/PrintRecommendationService/res/values/donottranslate.xml b/packages/PrintRecommendationService/res/values/donottranslate.xml
new file mode 100644
index 000000000000..4cf0eaf4181b
--- /dev/null
+++ b/packages/PrintRecommendationService/res/values/donottranslate.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<resources>
+ <string name="app_label">Print Service Recommendation Service</string>
+</resources>
diff --git a/packages/PrintRecommendationService/res/values/strings.xml b/packages/PrintRecommendationService/res/values/strings.xml
new file mode 100644
index 000000000000..07d0004fa09b
--- /dev/null
+++ b/packages/PrintRecommendationService/res/values/strings.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+ (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.
+-->
+
+<resources>
+ <string name="plugin_vendor_hp">HP</string>
+ <string name="plugin_vendor_lexmark">Lexmark</string>
+ <string name="plugin_vendor_brother">Brother</string>
+ <string name="plugin_vendor_canon">Canon</string>
+ <string name="plugin_vendor_xerox">Xerox</string>
+ <string name="plugin_vendor_samsung">Samsung Electronics</string>
+ <string name="plugin_vendor_epson">Epson</string>
+ <string name="plugin_vendor_konika_minolta">Konika Minolta</string>
+ <string name="plugin_vendor_fuji">Fuji</string>
+</resources>
diff --git a/packages/PrintRecommendationService/res/xml/vendorconfigs.xml b/packages/PrintRecommendationService/res/xml/vendorconfigs.xml
new file mode 100644
index 000000000000..119943cd5f9f
--- /dev/null
+++ b/packages/PrintRecommendationService/res/xml/vendorconfigs.xml
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+ (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.
+ -->
+
+<vendors>
+ <vendor>
+ <name>@string/plugin_vendor_hp</name>
+ <package>com.hp.android.printservice</package>
+ <mdns-names>
+ <mdns-name>HP</mdns-name>
+ <mdns-name>Hewlett-Packard</mdns-name>
+ <mdns-name>Hewlett Packard</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_lexmark</name>
+ <package>com.lexmark.print.plugin</package>
+ <mdns-names>
+ <mdns-name>Lexmark</mdns-name>
+ <mdns-name>Lexmark International</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_brother</name>
+ <package>com.brother.printservice</package>
+ <mdns-names>
+ <mdns-name>Brother</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_canon</name>
+ <package>jp.co.canon.android.printservice.plugin</package>
+ <mdns-names>
+ <mdns-name>Canon</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_xerox</name>
+ <package>com.xerox.printservice</package>
+ <mdns-names>
+ <mdns-name>Xerox</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_samsung</name>
+ <package>com.sec.app.samsungprintservice</package>
+ <mdns-names>
+ <mdns-name>Samsung</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_epson</name>
+ <package>com.epson.mobilephone.android.epsonprintserviceplugin</package>
+ <mdns-names>
+ <mdns-name>Epson</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_konika_minolta</name>
+ <package>com.kmbt.printservice</package>
+ <mdns-names>
+ <mdns-name>kmkmkm</mdns-name>
+ <mdns-name>Konica Minolta</mdns-name>
+ <mdns-name>Minolta</mdns-name>
+ </mdns-names>
+ </vendor>
+
+ <vendor>
+ <name>@string/plugin_vendor_fuji</name>
+ <package>jp.co.fujixerox.prt.PrintUtil.PCL</package>
+ <mdns-names>
+ <mdns-name>FUJI XEROX</mdns-name>
+ </mdns-names>
+ </vendor>
+</vendors>
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);
+ }
+ }
+
+}